Files
metasploit-gs/scripts/meterpreter/multicommand.rb
T

112 lines
2.9 KiB
Ruby
Raw Normal View History

##
# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.
2018-02-13 05:30:05 -06:00
# If you'd like to improve this script, please try to port it as a post
# module instead. Thank you.
##
2009-06-20 22:21:17 +00:00
#Meterpreter script for running multiple commands on Windows 2003, Windows Vista
# and Windows XP and Windows 2008 targets.
#Provided by Carlos Perez at carlos_perez[at]darkoperator[dot]com
#Verion: 0.1
################## Variable Declarations ##################
session = client
wininfo = client.sys.config.sysinfo
# Setting Arguments
@@exec_opts = Rex::Parser::Arguments.new(
2013-09-30 13:47:53 -05:00
"-h" => [ false,"Help menu." ],
2017-08-06 17:52:07 -05:00
"-c" => [ true,"Commands to execute. The command must be enclosed in double quotes and separated by a comma."],
2013-09-30 13:47:53 -05:00
"-f" => [ true,"File where to saved output of command."],
2017-08-06 17:52:07 -05:00
"-r" => [ true,"Text file with list of commands, one per line."]
2009-06-20 22:21:17 +00:00
)
#Setting Argument variables
2011-06-05 13:10:49 +00:00
commands = []
2010-08-13 11:38:08 +00:00
script = nil
2009-06-20 22:21:17 +00:00
outfile = nil
help = 0
################## Function Declarations ##################
# Function for running a list of commands stored in a array, returs string
def list_exec(session,cmdlst)
2013-09-30 13:47:53 -05:00
print_status("Running Command List ...")
tmpout = ""
cmdout = ""
r=''
session.response_timeout=120
cmdlst.each do |cmd|
next if cmd.strip.length < 1
next if cmd[0,1] == "#"
begin
print_status "\trunning command #{cmd}"
tmpout = "\n"
tmpout << "*****************************************\n"
tmpout << " Output of #{cmd}\n"
tmpout << "*****************************************\n"
r = session.sys.process.execute(cmd, nil, {'Hidden' => true, 'Channelized' => true})
while(d = r.channel.read)
tmpout << d
break if d == ""
end
cmdout << tmpout
r.channel.close
#r.close
rescue ::Exception => e
print_status("Error Running Command #{cmd}: #{e.class} #{e}")
end
end
cmdout
2009-06-20 22:21:17 +00:00
end
# Function for writing results of other functions to a file
def filewrt(file2wrt, data2wrt)
2013-09-30 13:47:53 -05:00
output = ::File.open(file2wrt, "a")
data2wrt.each_line do |d|
output.puts(d)
end
output.close
2009-06-20 22:21:17 +00:00
end
def usage
2013-09-30 13:47:53 -05:00
print_line("Windows Multi Command Execution Meterpreter Script ")
print_line(@@exec_opts.usage)
raise Rex::Script::Completed
2010-04-24 14:59:41 +00:00
2009-06-20 22:21:17 +00:00
end
2010-12-14 19:28:44 +00:00
2009-06-20 22:21:17 +00:00
################## Main ##################
@@exec_opts.parse(args) { |opt, idx, val|
2013-09-30 13:47:53 -05:00
case opt
2009-06-20 22:21:17 +00:00
2017-08-06 17:52:07 -05:00
when "-c"
2013-09-30 13:47:53 -05:00
commands = val.split(",")
2017-08-06 17:52:07 -05:00
when "-r"
2013-09-30 13:47:53 -05:00
script = val
2016-04-20 08:11:34 -04:00
if not ::File.exist?(script)
2013-09-30 13:47:53 -05:00
raise "Command List File does not exists!"
else
::File.open(script, "r").each_line do |line|
commands << line.chomp
end
end
when "-f"
outfile = val
when "-h"
help = 1
end
2009-06-20 22:21:17 +00:00
}
2010-05-03 17:13:09 +00:00
if args.length == 0 or help == 1
2013-09-30 13:47:53 -05:00
usage
2010-08-13 11:38:08 +00:00
elsif commands or script
2013-09-30 13:47:53 -05:00
if outfile
filewrt(outfile, list_exec(session,commands))
else
list_exec(session,commands).each_line do |l|
print_status(l.chomp)
end
end
raise Rex::Script::Completed
2009-06-20 22:21:17 +00:00
else
2013-09-30 13:47:53 -05:00
usage
2009-06-20 22:21:17 +00:00
end