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/modules/post/windows/gather/enum_computers.rb
Views: 11655
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post6include Msf::Post::File7include Msf::Post::Windows::Accounts8include Msf::Post::Windows::Registry910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Windows Gather Enumerate Computers',15'Description' => %q{16This module will enumerate computers included in the primary Active Directory domain.17},18'License' => MSF_LICENSE,19'Author' => [ 'Joshua Abraham <jabra[at]rapid7.com>'],20'Platform' => [ 'win'],21'SessionTypes' => %w[meterpreter powershell shell],22'Notes' => {23'Stability' => [CRASH_SAFE],24'Reliability' => [],25'SideEffects' => []26},27'Compat' => {28'Meterpreter' => {29'Commands' => %w[30stdapi_net_resolve_host31]32}33}34)35)36end3738def run39hostname = sysinfo.nil? ? cmd_exec('hostname') : sysinfo['Computer']40print_status("Running module against #{hostname} (#{session.session_host})")4142domain = get_domain_name4344fail_with(Failure::Unknown, 'Could not retrieve domain name. Is the host part of a domain?') unless domain4546netbios_domain_name = domain.split('.').first.upcase4748hostname_list = get_domain_computers4950if hostname_list.empty?51print_error('No computers found')52return53end5455list_computers(netbios_domain_name, hostname_list)56end5758# Takes the host name and makes use of nslookup to resolve the IP59#60# @param [String] host Hostname61# @return [String] ip The resolved IP62def resolve_host(host)63vprint_status("Looking up IP for #{host}")64return host if Rex::Socket.dotted_ip?(host)6566ip = []67data = cmd_exec("nslookup #{host}")68if data =~ /Name/69# Remove unnecessary data and get the section with the addresses70returned_data = data.split(/Name:/)[1]71# check each element of the array to see if they are IP72returned_data.gsub(/\r\n\t |\r\n|Aliases:|Addresses:|Address:/, ' ').split(' ').each do |e|73if Rex::Socket.dotted_ip?(e)74ip << e75end76end77end7879if ip.blank?80'Not resolvable'81else82ip.join(', ')83end84end8586def get_domain_computers87computer_list = []88divisor = "-------------------------------------------------------------------------------\r\n"89net_view_response = cmd_exec('net view')90unless net_view_response.include?(divisor)91print_error("The net view command failed with: #{net_view_response}")92return []93end9495raw_list = net_view_response.split(divisor)[1]96raw_list.sub!(/The command completed successfully\./, '')97raw_list.gsub!(/\\\\/, '')98raw_list.split(' ').each do |m|99computer_list << m100end101102computer_list103end104105def list_computers(domain, hosts)106tbl = Rex::Text::Table.new(107'Header' => 'List of identified Hosts.',108'Indent' => 1,109'Columns' =>110[111'Domain',112'Hostname',113'IPs',114]115)116hosts.each do |hostname|117hostip = resolve_host(hostname)118tbl << [domain, hostname, hostip]119end120121print_line("\n#{tbl}\n")122123report_note(124host: session,125type: 'domain.hosts',126data: tbl.to_csv127)128end129end130131132