Path: blob/master/modules/post/windows/recon/computer_browser_discovery.rb
19500 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Auxiliary::Report78def initialize(info = {})9super(10update_info(11info,12'Name' => 'Windows Recon Computer Browser Discovery',13'Description' => %q{14This module uses railgun to discover hostnames and IPs on the network.15LTYPE should be set to one of the following values: WK (all workstations), SVR (all servers),16SQL (all SQL servers), DC (all Domain Controllers), DCBKUP (all Domain Backup Servers),17NOVELL (all Novell servers), PRINTSVR (all Print Que servers), MASTERBROWSER (all Master Browsers),18WINDOWS (all Windows hosts), or UNIX (all Unix hosts).19},20'License' => MSF_LICENSE,21'Author' => [ 'mubix' ],22'Platform' => [ 'win' ],23'SessionTypes' => [ 'meterpreter' ],24'Compat' => {25'Meterpreter' => {26'Commands' => %w[27stdapi_net_resolve_host28stdapi_railgun_api29stdapi_railgun_memread30]31}32},33'Notes' => {34'Stability' => [CRASH_SAFE],35'SideEffects' => [],36'Reliability' => []37}38)39)4041register_options(42[43OptString.new('LTYPE', [true, 'Account informations (type info for known types)', 'WK']), # Enum would be a better choice44OptString.new('DOMAIN', [false, 'Domain to perform lookups on, default is current domain', nil]),45OptBool.new('SAVEHOSTS', [true, 'Save Discovered Hosts to the Database', false])46]47)48end4950def parse_netserverenum(startmem, count)51base = 052sys_list = []53mem = client.railgun.memread(startmem, 24 * count)5455count.times do |_i|56x = {}57x[:platform_id] = mem[(base + 0), 4].unpack('V*')[0]58cnameptr = mem[(base + 4), 4].unpack('V*')[0]59x[:major_ver] = mem[(base + 8), 4].unpack('V*')[0]60x[:minor_ver] = mem[(base + 12), 4].unpack('V*')[0]61x[:type] = mem[(base + 16), 4].unpack('V*')[0]62commentptr = mem[(base + 20), 4].unpack('V*')[0]6364x[:cname] = client.railgun.memread(cnameptr, 27).split("\0\0")[0].split("\0").join65x[:comment] = client.railgun.memread(commentptr, 255).split("\0\0")[0].split("\0").join66sys_list << x67base += 2468vprint_status("Identified: #{x[:cname]} - #{x[:comment]}")69end70return sys_list71end7273def run74client = session7576# Default = SV_TYPE_NT77# Servers = SV_TYPE_ALL78# Workstations = SV_TYPE_WORKSTATION79# Domain Controllers = SV_TYPE_DOMAINCTRL80# Novell Server = SV_TYPE_NOVELL81# Terminal Servers = SV_TYPE_TERMINALSERVER82# SQL Servers = SV_TYPE_SQLSERVER83lookuptype = 18485case datastore['LTYPE']86when 'WK' then lookuptype = '1'.hex87when 'SVR' then lookuptype = '2'.hex88when 'SQL' then lookuptype = '4'.hex89when 'DC' then lookuptype = '8'.hex90when 'DCBKUP' then lookuptype = '10'.hex91when 'TIME' then lookuptype = '20'.hex92when 'NOVELL' then lookuptype = '80'.hex93when 'PRINTSVR' then lookuptype = '200'.hex94when 'MASTERBROWSER' then lookuptype = '40000'.hex95when 'WINDOWS' then lookuptype = '400000'.hex96when 'UNIX' then lookuptype = '800'.hex97when 'LOCAL' then lookuptype = '40000000'.hex98end99100result = client.railgun.netapi32.NetServerEnum(nil, 101, 4, -1, 4, 4, lookuptype, datastore['DOMAIN'], 0)101102if result['totalentries'] == 0103print_error('No systems found of that type')104return105end106print_good("Found #{result['totalentries']} systems.")107108netview = parse_netserverenum(result['bufptr'], result['totalentries'])109110## get IP for host111begin112netview.each do |x|113vprint_status("Looking up IP for #{x[:cname]}")114print '.'115result = client.net.resolve.resolve_host(x[:cname])116if result[:ip].nil? || result[:ip].blank?117print_error("There was an error resolving the IP for #{x[:cname]}")118next119else120x[:ip] = result[:ip]121end122end123rescue StandardError => e124print_error(e)125print_status('Windows 2000 and prior does not support getaddrinfo')126end127128netview = netview.sort_by { |h| h[:type] }129130results = Rex::Text::Table.new(131'Header' => 'Netdiscovery Results',132'Indent' => 2,133'Columns' => ['TYPE', 'IP', 'COMPUTER NAME', 'VERSION', 'COMMENT']134)135136netview.each do |x|137results << [x[:type], x[:ip], x[:cname], "#{x[:major_ver]}.#{x[:minor_ver]}", x[:comment]]138report_host(host: x[:ip]) if datastore['SAVEHOSTS'] && !x[:ip].empty?139end140print_status(results.to_s)141store_loot('discovered.hosts', 'text/plain', session, results.to_s, 'discovered_hosts.txt', 'Computer Browser Discovered Hosts')142143print_status('If none of the IP addresses show up you are running this from a Win2k or older system')144print_status("If a host doesn't have an IP it either timed out or only has an IPv6 address assinged to it")145end146end147148149