Path: blob/master/modules/post/multi/gather/dns_bruteforce.rb
19500 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 DNS Forward Lookup Bruteforce',12'Description' => %q{13Brute force subdomains and hostnames via wordlist.14},15'License' => MSF_LICENSE,16'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>'],17'Platform' => %w[bsd linux osx solaris win],18'SessionTypes' => [ 'meterpreter', 'shell' ],19'Notes' => {20'Stability' => [CRASH_SAFE],21'SideEffects' => [],22'Reliability' => []23}24)25)26register_options(27[28OptString.new('DOMAIN', [true, 'Domain to do a forward lookup bruteforce against.']),29OptPath.new('NAMELIST', [30true, 'List of hostnames or subdomains to use.',31::File.join(Msf::Config.data_directory, 'wordlists', 'namelist.txt')32])33]34)35end3637# Run Method for when run command is issued38def run39domain = datastore['DOMAIN']40hostlst = datastore['NAMELIST']41a = []4243print_status("Performing DNS Forward Lookup Bruteforce for Domain #{domain}")4445name_list = []46if ::File.exist?(hostlst)47::File.open(hostlst).each do |n|48name_list << n49end50end5152case session.platform53when 'windows'54cmd = 'nslookup'55when 'solaris'56cmd = '/usr/sbin/host '57else58cmd = '/usr/bin/host '59end6061while !name_list.nil? && !name_list.empty?621.upto session.max_threads do63a << framework.threads.spawn("Module(#{refname})", false, name_list.shift) do |n|64next if n.nil?6566vprint_status("Trying #{n.strip}.#{domain}")67r = cmd_exec(cmd, "#{n.strip}.#{domain}")6869case session.platform70when 'windows'71proccess_win(r, "#{n.strip}.#{domain}")72else73process_nix(r, "#{n.strip}.#{domain}")74end75end76a.map(&:join)77end78end79end8081# Process the data returned by nslookup82def proccess_win(data, ns_opt)83if data =~ /Name/84# Remove unnecessary data and get the section with the addresses85returned_data = data.split(/Name:/)[1]86# check each element of the array to see if they are IP87returned_data.gsub(/\r\n\t |\r\n|Aliases:|Addresses:/, ' ').split(' ').each do |e|88if Rex::Socket.dotted_ip?(e)89print_good("#{ns_opt} #{e}")90report_host(host: e, name: ns_opt.strip)91end92end93end94end9596# Process the data returned by the host command97def process_nix(data, ns_opt)98data.each_line do |line|99dns_data = line.scan(/(\S*) has address (\S*)$/)100next if dns_data.empty?101102dns_data.each do |e|103print_good("#{ns_opt} #{e[1]}")104report_host(host: e[1], name: ns_opt.strip)105end106end107end108end109110111