33# Current source: https://github.com/rapid7/metasploit-framework
44##
55
6+ require 'rex/post/meterpreter/extensions/stdapi/constants'
7+
68class MetasploitModule < Msf ::Post
79 include Msf ::Post ::File
810 include Msf ::Post ::Windows ::Accounts
911 include Msf ::Post ::Windows ::Registry
12+ include Msf ::Post ::DNS ::ResolveHost
1013
1114 def initialize ( info = { } )
1215 super (
@@ -58,36 +61,19 @@ def run
5861
5962 # Takes the host name and makes use of nslookup to resolve the IP
6063 #
61- # @param [String] host Hostname
64+ # @param [Object] hostname
65+ # @param [Object] family
6266 # @return [String] ip The resolved IP
63- def resolve_host ( host )
64- vprint_status ( "Looking up IP for #{ host } " )
65- return host if Rex ::Socket . dotted_ip? ( host )
66-
67- ip = [ ]
68- data = cmd_exec ( "nslookup #{ host } " )
69- if data =~ /Name/
70- # Remove unnecessary data and get the section with the addresses
71- returned_data = data . split ( /Name:/ ) [ 1 ]
72- # check each element of the array to see if they are IP
73- returned_data . gsub ( /\r \n \t |\r \n |Aliases:|Addresses:|Address:/ , ' ' ) . split ( ' ' ) . each do |e |
74- if Rex ::Socket . dotted_ip? ( e )
75- ip << e
76- end
77- end
78- end
79-
80- if ip . blank?
81- 'Not resolvable'
82- else
83- ip . join ( ', ' )
84- end
67+ def gethost ( hostname , family )
68+ ## get IP for host
69+ vprint_status ( "Looking up IP for #{ hostname } " )
70+ resolve_host ( hostname , family )
8571 end
8672
8773 def get_domain_computers
8874 computer_list = [ ]
8975 divisor = "-------------------------------------------------------------------------------\r \n "
90- net_view_response = cmd_exec ( ' net view' )
76+ net_view_response = cmd_exec ( "cmd.exe" , "/c net view" )
9177 unless net_view_response . include? ( divisor )
9278 print_error ( "The net view command failed with: #{ net_view_response } " )
9379 return [ ]
@@ -104,6 +90,7 @@ def get_domain_computers
10490 end
10591
10692 def list_computers ( domain , hosts )
93+ meterpreter_dns_resolving_errors = [ ]
10794 tbl = Rex ::Text ::Table . new (
10895 'Header' => 'List of identified Hosts.' ,
10996 'Indent' => 1 ,
@@ -115,12 +102,28 @@ def list_computers(domain, hosts)
115102 ]
116103 )
117104 hosts . each do |hostname |
118- hostip = resolve_host ( hostname )
119- tbl << [ domain , hostname , hostip ]
105+ hostipv4 = gethost ( hostname , AF_INET )
106+ hostipv6 = gethost ( hostname , AF_INET6 )
107+
108+ if hostipv4 [ :ips ] . empty?
109+ meterpreter_dns_resolving_errors << "IPV4: #{ hostname } could not be resolved"
110+ else
111+ tbl << [ domain , hostname , hostipv4 [ :ips ] . join ( ',' ) ]
112+ end
113+
114+ if hostipv6 [ :ips ] . empty?
115+ meterpreter_dns_resolving_errors << "IPV6: #{ hostname } could not be resolved" if hostipv6 [ :ips ] . empty?
116+ else
117+ tbl << [ domain , hostname , hostipv6 [ :ips ] . join ( ',' ) ] unless hostipv6 [ :ips ] . nil?
118+ end
120119 end
121120
122121 print_line ( "\n #{ tbl } \n " )
123122
123+ meterpreter_dns_resolving_errors . each do | error |
124+ print_warning ( error )
125+ end
126+
124127 report_note (
125128 host : session ,
126129 type : 'domain.hosts' ,
0 commit comments