metasploit-framework/modules/auxiliary/gather/dns_reverse_lookup.rb

134 lines
4.1 KiB
Ruby
Raw Normal View History

##
2013-10-15 19:52:12 +00:00
# This module requires Metasploit: http//metasploit.com/download
# Current source: https://github.com/rapid7/metasploit-framework
##
require 'msf/core'
require "net/dns/resolver"
require 'rex'
class Metasploit3 < Msf::Auxiliary
2013-08-30 21:28:54 +00:00
include Msf::Auxiliary::Report
2013-08-30 21:28:54 +00:00
def initialize(info = {})
super(update_info(info,
'Name' => 'DNS Reverse Lookup Enumeration',
'Description' => %q{
2013-08-30 21:28:54 +00:00
This module performs DNS reverse lookup against a given IP range in order to
retrieve valid addresses and names.
},
'Author' =>
[
'Carlos Perez <carlos_perez[at]darkoperator.com>', # Base code
'Thanat0s <thanspam[at]trollprod[dot]org>' # Output, Throttling & Db notes add
],
'License' => BSD_LICENSE
2013-08-30 21:28:54 +00:00
))
2013-08-30 21:28:54 +00:00
register_options(
[
OptAddressRange.new('RANGE', [true, 'IP range to perform reverse lookup against.']),
OptAddress.new('NS', [ false, "Specify the nameserver to use for queries, otherwise use the system DNS." ]),
OptString.new('OUT_FILE', [ false, "Specify a CSV output file" ])
2013-08-30 21:28:54 +00:00
], self.class)
2013-08-30 21:28:54 +00:00
register_advanced_options(
[
OptInt.new('RETRY', [ false, "Number of tries to resolve a record if no response is received.", 2]),
OptInt.new('RETRY_INTERVAL', [ false, "Number of seconds to wait before doing a retry.", 2]),
OptInt.new('THREADS', [ true, "The number of concurrent threads.", 1]),
OptInt.new('THROTTLE', [ false, "Specify the resolution throttle in query per sec. 0 means unthrottled",0 ])
2013-08-30 21:28:54 +00:00
], self.class)
end
2013-08-30 21:28:54 +00:00
def run
@res = Net::DNS::Resolver.new()
2013-02-11 15:31:13 +00:00
2013-08-30 21:28:54 +00:00
if datastore['RETRY']
@res.retry = datastore['RETRY'].to_i
end
2013-02-11 15:31:13 +00:00
2013-08-30 21:28:54 +00:00
if datastore['RETRY_INTERVAL']
@res.retry_interval = datastore['RETRY_INTERVAL'].to_i
end
2013-02-11 15:31:13 +00:00
2013-08-30 21:28:54 +00:00
@threadnum = datastore['THREADS'].to_i
switchdns() unless datastore['NS'].nil?
reverselkp(datastore['RANGE'])
end
2013-08-30 21:28:54 +00:00
def reverselkp(iprange)
print_status("Running reverse lookup against IP range #{iprange}")
ar = Rex::Socket::RangeWalker.new(iprange)
tl = []
2014-04-14 19:11:04 +00:00
# Basic Throttling
sleep_time = 0.0
if (datastore['THROTTLE'] != 0)
2014-04-23 09:38:23 +00:00
sleep_time = (1.0/datastore['THROTTLE'])*datastore['THREADS']
2014-04-14 19:11:04 +00:00
print_status("Throttle set to #{datastore['THROTTLE']} queries per seconds")
end
# Output..
if datastore['OUT_FILE']
print_status("Scan result saved in #{datastore['OUT_FILE']}")
open(datastore['OUT_FILE'], 'w') do |f|
f.puts "; IP, Host"
end
end
2013-08-30 21:28:54 +00:00
while (true)
# Spawn threads for each host
2014-04-12 15:43:39 +00:00
hosts = Hash.new
2013-08-30 21:28:54 +00:00
while (tl.length <= @threadnum)
ip = ar.next_ip
break if not ip
tl << framework.threads.spawn("Module(#{self.refname})-#{ip}", false, ip.dup) do |tip|
begin
2014-04-14 19:11:04 +00:00
Rex.sleep(sleep_time)
2013-08-30 21:28:54 +00:00
query = @res.query(tip)
query.each_ptr do |addresstp|
2014-04-14 19:11:04 +00:00
print_status("Host Name: #{addresstp}, IP Address: #{tip.to_s}")
if datastore['OUT_FILE']
open(datastore['OUT_FILE'], 'a') do |f|
f.puts "#{tip.to_s},#{addresstp}"
end
end
2013-08-30 21:28:54 +00:00
report_host(
:host => tip.to_s,
:name => addresstp
)
2014-04-14 19:11:04 +00:00
if !hosts[tip]
2014-04-12 15:43:39 +00:00
hosts[tip] = Array.new
end
hosts[tip].push addresstp
end
2014-04-14 19:11:04 +00:00
unless hosts[tip].nil? or hosts[tip].empty?
report_note(
:host => tip.to_s,
:type => "RDNS_Record",
:data => hosts[tip]
)
2013-08-30 21:28:54 +00:00
end
rescue ::Interrupt
raise $!
rescue ::Rex::ConnectionError
rescue ::Exception => e
print_error("Error: #{tip}: #{e.message}")
end
end
end
# Exit once we run out of hosts
if(tl.length == 0)
break
end
tl.first.join
tl.delete_if { |t| not t.alive? }
end
end
2013-08-30 21:28:54 +00:00
def switchdns()
print_status("Using DNS server: #{datastore['NS']}")
@res.nameserver=(datastore['NS'])
@nsinuse = datastore['NS']
end
2013-02-09 00:52:49 +00:00
end