From 6c382c8eb79a7cc55fcde33d50486c19ade68fde Mon Sep 17 00:00:00 2001 From: Spencer McIntyre Date: Wed, 9 Oct 2013 16:52:53 -0400 Subject: [PATCH] Return nil on error, and move the module to post/multi. --- .../extensions/stdapi/net/resolve.rb | 4 +- modules/post/multi/gather/resolve_hosts.rb | 67 +++++++++++++++++++ modules/post/windows/gather/resolve_hosts.rb | 10 ++- 3 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 modules/post/multi/gather/resolve_hosts.rb diff --git a/lib/rex/post/meterpreter/extensions/stdapi/net/resolve.rb b/lib/rex/post/meterpreter/extensions/stdapi/net/resolve.rb index 832c607b0c..0a212fa22f 100644 --- a/lib/rex/post/meterpreter/extensions/stdapi/net/resolve.rb +++ b/lib/rex/post/meterpreter/extensions/stdapi/net/resolve.rb @@ -48,7 +48,7 @@ class Resolve def resolve_hosts(hostnames, family=AF_INET) request = Packet.create_request('stdapi_net_resolve_hosts') request.add_tlv(TLV_TYPE_ADDR_TYPE, family) - + hostnames.each do |hostname| request.add_tlv(TLV_TYPE_HOST_NAME, hostname) end @@ -84,7 +84,7 @@ class Resolve end if raw.empty? - ip = "" + ip = nil else if type == AF_INET ip = Rex::Socket.addr_ntoa(raw[0..3]) diff --git a/modules/post/multi/gather/resolve_hosts.rb b/modules/post/multi/gather/resolve_hosts.rb new file mode 100644 index 0000000000..076acee24f --- /dev/null +++ b/modules/post/multi/gather/resolve_hosts.rb @@ -0,0 +1,67 @@ +# +# This file is part of the Metasploit Framework and may be subject to +# redistribution and commercial restrictions. Please see the Metasploit +# web site for more information on licensing and terms of use. +# http://metasploit.com/ +## + +require 'msf/core' +require 'rex' + +class Metasploit3 < Msf::Post + + def initialize(info={}) + super( update_info( info, + 'Name' => 'Windows Resolve Hosts', + 'Description' => %q{ + Resolves hostnames to either IPv4 or IPv6 addresses from the perspective of the remote host. + }, + 'License' => MSF_LICENSE, + 'Author' => [ 'Ben Campbell ' ], + 'Platform' => %w{ win python }, + 'SessionTypes' => [ 'meterpreter' ] + )) + + register_options([ + OptString.new('HOSTNAMES', [true, 'Comma seperated list of hostnames to resolve.']), + OptEnum.new('AI_FAMILY', [true, 'Address Family', 'IPv4', ['IPv4', 'IPv6'] ]) + ], self.class) + end + + def run + hosts = datastore['HOSTNAMES'].split(',') + + if datastore['AI_FAMILY'] == 'IPv4' + family = AF_INET + else + family = AF_INET6 + end + + # Clear whitespace + hosts.collect{|x| x.strip!} + + print_status("Attempting to resolve '#{hosts.join(', ')}' on #{sysinfo['Computer']}") if not sysinfo.nil? + + response = client.net.resolve.resolve_hosts(hosts, family) + + table = Rex::Ui::Text::Table.new( + 'Indent' => 0, + 'SortIndex' => -1, + 'Columns' => + [ + 'Hostname', + 'IP', + ] + ) + + response.each do |result| + if result[:ip].nil? + table << [result[:hostname], '[Failed To Resolve]'] + else + table << [result[:hostname], result[:ip]] + end + end + + table.print + end +end diff --git a/modules/post/windows/gather/resolve_hosts.rb b/modules/post/windows/gather/resolve_hosts.rb index 55c2cda5e4..40a557fabd 100644 --- a/modules/post/windows/gather/resolve_hosts.rb +++ b/modules/post/windows/gather/resolve_hosts.rb @@ -10,6 +10,10 @@ require 'rex' class Metasploit3 < Msf::Post + require 'msf/core/module/deprecated' + include Msf::Module::Deprecated + deprecated Date.new(2013, 12, 9), 'post/multi/gather/resolve_hosts' + def initialize(info={}) super( update_info( info, 'Name' => 'Windows Resolve Hosts', @@ -55,7 +59,11 @@ class Metasploit3 < Msf::Post ) response.each do |result| - table << [result[:hostname], result[:ip]] + if result[:ip].nil? + table << [result[:hostname], '[Failed To Resolve]'] + else + table << [result[:hostname], result[:ip]] + end end table.print