Path: blob/master/modules/post/multi/gather/resolve_hosts.rb
19851 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Post67def initialize(info = {})8super(9update_info(10info,11'Name' => 'Multi Gather Resolve Hosts',12'Description' => %q{13Resolves hostnames to either IPv4 or IPv6 addresses from the perspective of the remote host.14},15'License' => MSF_LICENSE,16'Author' => [ 'Ben Campbell' ],17'Platform' => %w[win python],18'SessionTypes' => [ 'meterpreter' ],19'Compat' => {20'Meterpreter' => {21'Commands' => %w[22stdapi_net_resolve_hosts23]24}25},26'Notes' => {27'Stability' => [CRASH_SAFE],28'SideEffects' => [],29'Reliability' => []30}31)32)3334register_options([35OptString.new('HOSTNAMES', [false, 'Comma separated list of hostnames to resolve.']),36OptPath.new('HOSTFILE', [false, 'Line separated file with hostnames to resolve.']),37OptEnum.new('AI_FAMILY', [true, 'Address Family', 'IPv4', ['IPv4', 'IPv6'] ]),38OptBool.new('DATABASE', [false, 'Report found hosts to DB', true])39])40end4142def run43hosts = []44if datastore['HOSTNAMES']45hostnames = datastore['HOSTNAMES'].split(',')46hostnames.each do |hostname|47hostname.strip!48hosts << hostname unless hostname.empty?49end50end5152if datastore['HOSTFILE']53::File.open(datastore['HOSTFILE'], 'rb').each_line do |hostname|54hostname.strip!55hosts << hostname unless hostname.empty?56end57end5859if hosts.empty?60fail_with(Failure::BadConfig, 'No hostnames to resolve.')61end6263hosts.uniq!6465if datastore['AI_FAMILY'] == 'IPv4'66family = AF_INET67else68family = AF_INET669end7071print_status("Attempting to resolve '#{hosts.join(', ')}' on #{sysinfo['Computer']}") if sysinfo7273response = client.net.resolve.resolve_hosts(hosts, family)7475table = Rex::Text::Table.new(76'Indent' => 0,77'SortIndex' => -1,78'Columns' =>79[80'Hostname',81'IP',82]83)8485response.each do |result|86if result[:ip].nil?87table << [result[:hostname], '[Failed To Resolve]']88next89end9091if datastore['DATABASE']92report_host(93host: result[:ip],94name: result[:hostname]95)96end9798table << [result[:hostname], result[:ip]]99end100101table.print102end103end104105106