PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/auxiliary/gather/enum_dns.rb

https://github.com/Jonono2/metasploit-framework
Ruby | 544 lines | 499 code | 25 blank | 20 comment | 60 complexity | 479f8ff8aeec400e3df49d4ffa66c7bd MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, GPL-3.0, LGPL-2.1, GPL-2.0
  1. ##
  2. # This module requires Metasploit: http//metasploit.com/download
  3. # Current source: https://github.com/rapid7/metasploit-framework
  4. ##
  5. require 'msf/core'
  6. require "net/dns/resolver"
  7. class Metasploit3 < Msf::Auxiliary
  8. include Msf::Auxiliary::Report
  9. def initialize(info = {})
  10. super(update_info(info,
  11. 'Name' => 'DNS Record Scanner and Enumerator ',
  12. 'Description' => %q{
  13. This module can be used to gather information about a domain from a
  14. given DNS server by performing various DNS queries such as zone
  15. transfers, reverse lookups, SRV record bruteforcing, and other techniques.
  16. },
  17. 'Author' => [ 'Carlos Perez <carlos_perez[at]darkoperator.com>' ],
  18. 'License' => MSF_LICENSE,
  19. 'References' =>
  20. [
  21. ['CVE', '1999-0532'],
  22. ['OSVDB', '492'],
  23. ]
  24. ))
  25. register_options(
  26. [
  27. OptString.new('DOMAIN', [ true, "The target domain name"]),
  28. OptBool.new('ENUM_AXFR', [ true, 'Initiate a zone transfer against each NS record', true]),
  29. OptBool.new('ENUM_TLD', [ true, 'Perform a TLD expansion by replacing the TLD with the IANA TLD list', false]),
  30. OptBool.new('ENUM_STD', [ true, 'Enumerate standard record types (A,MX,NS,TXT and SOA)', true]),
  31. OptBool.new('ENUM_BRT', [ true, 'Brute force subdomains and hostnames via the supplied wordlist', false]),
  32. OptBool.new('ENUM_IP6', [ true, 'Brute force hosts with IPv6 AAAA records',false]),
  33. OptBool.new('ENUM_RVL', [ true, 'Reverse lookup a range of IP addresses', false]),
  34. OptBool.new('ENUM_SRV', [ true, 'Enumerate the most common SRV records', true]),
  35. OptPath.new('WORDLIST', [ false, "Wordlist for domain name bruteforcing", ::File.join(Msf::Config.data_directory, "wordlists", "namelist.txt")]),
  36. OptAddress.new('NS', [ false, "Specify the nameserver to use for queries (default is system DNS)" ]),
  37. OptAddressRange.new('IPRANGE', [false, "The target address range or CIDR identifier"]),
  38. OptBool.new('STOP_WLDCRD', [ true, 'Stops bruteforce enumeration if wildcard resolution is detected', false])
  39. ], self.class)
  40. register_advanced_options(
  41. [
  42. OptInt.new('RETRY', [ false, "Number of times to try to resolve a record if no response is received", 2]),
  43. OptInt.new('RETRY_INTERVAL', [ false, "Number of seconds to wait before doing a retry", 2]),
  44. OptBool.new('TCP_DNS', [false, "Run queries over TCP", false]),
  45. ], self.class)
  46. end
  47. #---------------------------------------------------------------------------------
  48. def switchdns(target)
  49. if not datastore['NS'].nil?
  50. print_status("Using DNS Server: #{datastore['NS']}")
  51. @res.nameserver=(datastore['NS'])
  52. @nsinuse = datastore['NS']
  53. else
  54. querysoa = @res.query(target, "SOA")
  55. if (querysoa)
  56. (querysoa.answer.select { |i| i.class == Net::DNS::RR::SOA}).each do |rr|
  57. query1soa = @res.search(rr.mname)
  58. if (query1soa and query1soa.answer[0])
  59. print_status("Setting DNS Server to #{target} NS: #{query1soa.answer[0].address}")
  60. @res.nameserver=(query1soa.answer[0].address)
  61. @nsinuse = query1soa.answer[0].address
  62. end
  63. end
  64. end
  65. end
  66. end
  67. #---------------------------------------------------------------------------------
  68. def wildcard(target)
  69. rendsub = rand(10000).to_s
  70. query = @res.query("#{rendsub}.#{target}", "A")
  71. if query.answer.length != 0
  72. print_status("This domain has wildcards enabled!!")
  73. query.answer.each do |rr|
  74. print_status("Wildcard IP for #{rendsub}.#{target} is: #{rr.address.to_s}") if rr.class != Net::DNS::RR::CNAME
  75. end
  76. return true
  77. else
  78. return false
  79. end
  80. end
  81. #---------------------------------------------------------------------------------
  82. def genrcd(target)
  83. print_status("Retrieving general DNS records")
  84. query = @res.search(target)
  85. if (query)
  86. query.answer.each do |rr|
  87. next unless rr.class == Net::DNS::RR::A
  88. print_status("Domain: #{target} IP address: #{rr.address} Record: A ")
  89. report_note(:host => @nsinuse.to_s,
  90. :proto => 'udp',
  91. :sname => 'dns',
  92. :port => 53 ,
  93. :type => 'dns.enum',
  94. :update => :unique_data,
  95. :data => "#{rr.address.to_s},#{target},A")
  96. end
  97. end
  98. query = @res.query(target, "SOA")
  99. if (query)
  100. (query.answer.select { |i| i.class == Net::DNS::RR::SOA}).each do |rr|
  101. query1 = @res.search(rr.mname)
  102. if (query1)
  103. query1.answer.each do |ip|
  104. print_status("Start of Authority: #{rr.mname} IP address: #{ip.address} Record: SOA")
  105. report_note(:host => @nsinuse.to_s,
  106. :proto => 'udp',
  107. :sname => 'dns',
  108. :port => 53 ,
  109. :type => 'dns.enum',
  110. :update => :unique_data,
  111. :data => "#{ip.address.to_s},#{rr.mname},SOA")
  112. end
  113. end
  114. end
  115. end
  116. query = @res.query(target, "NS")
  117. if (query)
  118. (query.answer.select { |i| i.class == Net::DNS::RR::NS}).each do |rr|
  119. query1 = @res.search(rr.nsdname)
  120. if (query1)
  121. query1.answer.each do |ip|
  122. next unless ip.class == Net::DNS::RR::A
  123. print_status("Name Server: #{rr.nsdname} IP address: #{ip.address} Record: NS")
  124. report_note(:host => @nsinuse.to_s,
  125. :proto => 'udp',
  126. :sname => 'dns',
  127. :port => 53 ,
  128. :type => 'dns.enum',
  129. :update => :unique_data,
  130. :data => "#{ip.address.to_s},#{rr.nsdname},NS")
  131. end
  132. end
  133. end
  134. end
  135. query = @res.query(target, "MX")
  136. if (query)
  137. (query.answer.select { |i| i.class == Net::DNS::RR::MX}).each do |rr|
  138. print_status("Name: #{rr.exchange} Preference: #{rr.preference} Record: MX")
  139. report_note(:host => @nsinuse.to_s,
  140. :proto => 'udp',
  141. :sname => 'dns',
  142. :port => 53 ,
  143. :type => 'dns.enum',
  144. :update => :unique_data,
  145. :data => "#{rr.exchange},MX")
  146. end
  147. end
  148. query = @res.query(target, "TXT")
  149. if (query)
  150. query.answer.each do |rr|
  151. print_status(rr.inspect)
  152. print_status("Text: #{rr.inspect}")
  153. report_note(:host => @nsinuse.to_s,
  154. :proto => 'udp',
  155. :sname => 'dns',
  156. :port => 53 ,
  157. :type => 'dns.enum',
  158. :update => :unique_data,
  159. :data => rr.inspect)
  160. end
  161. end
  162. end
  163. #---------------------------------------------------------------------------------
  164. def tldexpnd(targetdom,nssrv)
  165. target = targetdom.scan(/(\S*)[.]\w*\z/).join
  166. target.chomp!
  167. if not nssrv.nil?
  168. @res.nameserver=(nssrv)
  169. @nsinuse = nssrv
  170. end
  171. i, a = 0, []
  172. tlds = [
  173. "com", "org", "net", "edu", "mil", "gov", "uk", "af", "al", "dz",
  174. "as", "ad", "ao", "ai", "aq", "ag", "ar", "am", "aw", "ac","au",
  175. "at", "az", "bs", "bh", "bd", "bb", "by", "be", "bz", "bj", "bm",
  176. "bt", "bo", "ba", "bw", "bv", "br", "io", "bn", "bg", "bf", "bi",
  177. "kh", "cm", "ca", "cv", "ky", "cf", "td", "cl", "cn", "cx", "cc",
  178. "co", "km", "cd", "cg", "ck", "cr", "ci", "hr", "cu", "cy", "cz",
  179. "dk", "dj", "dm", "do", "tp", "ec", "eg", "sv", "gq", "er", "ee",
  180. "et", "fk", "fo", "fj", "fi", "fr", "gf", "pf", "tf", "ga", "gm",
  181. "ge", "de", "gh", "gi", "gr", "gl", "gd", "gp", "gu", "gt", "gg",
  182. "gn", "gw", "gy", "ht", "hm", "va", "hn", "hk", "hu", "is", "in",
  183. "id", "ir", "iq", "ie", "im", "il", "it", "jm", "jp", "je", "jo",
  184. "kz", "ke", "ki", "kp", "kr", "kw", "kg", "la", "lv", "lb", "ls",
  185. "lr", "ly", "li", "lt", "lu", "mo", "mk", "mg", "mw", "my", "mv",
  186. "ml", "mt", "mh", "mq", "mr", "mu", "yt", "mx", "fm", "md", "mc",
  187. "mn", "ms", "ma", "mz", "mm", "na", "nr", "np", "nl", "an", "nc",
  188. "nz", "ni", "ne", "ng", "nu", "nf", "mp", "no", "om", "pk", "pw",
  189. "pa", "pg", "py", "pe", "ph", "pn", "pl", "pt", "pr", "qa", "re",
  190. "ro", "ru", "rw", "kn", "lc", "vc", "ws", "sm", "st", "sa", "sn",
  191. "sc", "sl", "sg", "sk", "si", "sb", "so", "za", "gz", "es", "lk",
  192. "sh", "pm", "sd", "sr", "sj", "sz", "se", "ch", "sy", "tw", "tj",
  193. "tz", "th", "tg", "tk", "to", "tt", "tn", "tr", "tm", "tc", "tv",
  194. "ug", "ua", "ae", "gb", "us", "um", "uy", "uz", "vu", "ve", "vn",
  195. "vg", "vi", "wf", "eh", "ye", "yu", "za", "zr", "zm", "zw", "int",
  196. "gs", "info", "biz", "su", "name", "coop", "aero" ]
  197. print_status("Performing Top Level Domain expansion using #{tlds.size} TLDs")
  198. tlds.each do |tld|
  199. query1 = @res.search("#{target}.#{tld}")
  200. if (query1)
  201. query1.answer.each do |rr|
  202. print_status("Domain: #{target}.#{tld} Name: #{rr.name} IP address: #{rr.address} Record: A ") if rr.class == Net::DNS::RR::A
  203. report_note(:host => @nsinuse.to_s,
  204. :proto => 'udp',
  205. :sname => 'dns',
  206. :port => 53,
  207. :type => 'dns.enum',
  208. :update => :unique_data,
  209. :data => "#{rr.address.to_s},#{target}.#{tld},A") if rr.class == Net::DNS::RR::A
  210. end
  211. end
  212. end
  213. end
  214. #-------------------------------------------------------------------------------
  215. def dnsbrute(target, wordlist, nssrv)
  216. print_status("Running bruteforce against domain #{target}")
  217. arr = []
  218. i, a = 0, []
  219. ::File.open(wordlist, "rb").each_line do |line|
  220. if not nssrv.nil?
  221. @res.nameserver=(nssrv)
  222. @nsinuse = nssrv
  223. end
  224. query1 = @res.search("#{line.chomp}.#{target}")
  225. if (query1)
  226. query1.answer.each do |rr|
  227. if rr.class == Net::DNS::RR::A
  228. print_status("Hostname: #{line.chomp}.#{target} IP address: #{rr.address.to_s}")
  229. report_note(:host => @nsinuse.to_s,
  230. :proto => 'udp',
  231. :sname => 'dns',
  232. :port => 53 ,
  233. :type => 'dns.enum',
  234. :update => :unique_data,
  235. :data => "#{rr.address.to_s},#{line.chomp}.#{target},A")
  236. next unless rr.class == Net::DNS::RR::CNAME
  237. end
  238. end
  239. end
  240. end
  241. end
  242. #-------------------------------------------------------------------------------
  243. def bruteipv6(target, wordlist, nssrv)
  244. print_status("Bruteforcing IPv6 addresses against domain #{target}")
  245. arr = []
  246. i, a = 0, []
  247. arr = IO.readlines(wordlist)
  248. if not nssrv.nil?
  249. @res.nameserver=(nssrv)
  250. @nsinuse = nssrv
  251. end
  252. arr.each do |line|
  253. query1 = @res.search("#{line.chomp}.#{target}", "AAAA")
  254. if (query1)
  255. query1.answer.each do |rr|
  256. if rr.class == Net::DNS::RR::AAAA
  257. print_status("Hostname: #{line.chomp}.#{target} IPv6 Address: #{rr.address.to_s}")
  258. report_note(:host => @nsinuse.to_s,
  259. :proto => 'udp',
  260. :sname => 'dns',
  261. :port => 53 ,
  262. :type => 'dns.enum',
  263. :update => :unique_data,
  264. :data => "#{rr.address.to_s},#{line.chomp}.#{target},AAAA")
  265. next unless rr.class == Net::DNS::RR::CNAME
  266. end
  267. end
  268. end
  269. end
  270. end
  271. #-------------------------------------------------------------------------------
  272. def reverselkp(iprange,nssrv)
  273. print_status("Running reverse lookup against IP range #{iprange}")
  274. if not nssrv.nil?
  275. @res.nameserver = (nssrv)
  276. @nsinuse = nssrv
  277. end
  278. ar = Rex::Socket::RangeWalker.new(iprange)
  279. tl = []
  280. while (true)
  281. # Spawn threads for each host
  282. while (tl.length < @threadnum)
  283. ip = ar.next_ip
  284. break if not ip
  285. tl << framework.threads.spawn("Module(#{self.refname})-#{ip}", false, ip.dup) do |tip|
  286. begin
  287. query = @res.query(tip)
  288. query.each_ptr do |addresstp|
  289. print_status("Hostname: #{addresstp} IP address: #{tip.to_s}")
  290. report_note(:host => @nsinuse.to_s,
  291. :proto => 'udp',
  292. :sname => 'dns',
  293. :port => 53 ,
  294. :type => 'dns.enum',
  295. :update => :unique_data,
  296. :data => "#{addresstp},#{tip},A")
  297. end
  298. rescue ::Interrupt
  299. raise $!
  300. rescue ::Rex::ConnectionError
  301. rescue ::Exception => e
  302. print_error("Error: #{tip}: #{e.message}")
  303. elog("Error running against host #{tip}: #{e.message}\n#{e.backtrace.join("\n")}")
  304. end
  305. end
  306. end
  307. # Exit once we run out of hosts
  308. if(tl.length == 0)
  309. break
  310. end
  311. tl.first.join
  312. tl.delete_if { |t| not t.alive? }
  313. end
  314. end
  315. #-------------------------------------------------------------------------------
  316. #SRV Record Enumeration
  317. def srvqry(dom,nssrv)
  318. print_status("Enumerating SRV records for #{dom}")
  319. i, a = 0, []
  320. #Most common SRV Records
  321. srvrcd = [
  322. "_gc._tcp.","_kerberos._tcp.", "_kerberos._udp.","_ldap._tcp","_test._tcp.",
  323. "_sips._tcp.","_sip._udp.","_sip._tcp.","_aix._tcp.","_aix._tcp.","_finger._tcp.",
  324. "_ftp._tcp.","_http._tcp.","_nntp._tcp.","_telnet._tcp.","_whois._tcp.","_h323cs._tcp.",
  325. "_h323cs._udp.","_h323be._tcp.","_h323be._udp.","_h323ls._tcp.","_h323ls._udp.",
  326. "_sipinternal._tcp.","_sipinternaltls._tcp.","_sip._tls.","_sipfederationtls._tcp.",
  327. "_jabber._tcp.","_xmpp-server._tcp.","_xmpp-client._tcp.","_imap._tcp.","_certificates._tcp.",
  328. "_crls._tcp.","_pgpkeys._tcp.","_pgprevokations._tcp.","_cmp._tcp.","_svcp._tcp.","_crl._tcp.",
  329. "_ocsp._tcp.","_PKIXREP._tcp.","_smtp._tcp.","_hkp._tcp.","_hkps._tcp.","_jabber._udp.",
  330. "_xmpp-server._udp.","_xmpp-client._udp.","_jabber-client._tcp.","_jabber-client._udp."]
  331. srvrcd.each do |srvt|
  332. trg = "#{srvt}#{dom}"
  333. query = @res.query(trg , Net::DNS::SRV)
  334. if query
  335. query.answer.each do |srv|
  336. print_status("SRV Record: #{trg} Host: #{srv.host} Port: #{srv.port} Priority: #{srv.priority}") if srv.type != "CNAME"
  337. end
  338. end
  339. end
  340. end
  341. #-------------------------------------------------------------------------------
  342. #For Performing Zone Transfers
  343. def axfr(target, nssrv)
  344. print_status("Performing zone transfer against all nameservers in #{target}")
  345. if not nssrv.nil?
  346. @res.nameserver=(nssrv)
  347. @nsinuse = nssrv
  348. end
  349. @res.tcp_timeout=15
  350. query = @res.query(target, "NS")
  351. if (query.answer.length != 0)
  352. (query.answer.select { |i| i.class == Net::DNS::RR::NS}).each do |nsrcd|
  353. print_status("Testing nameserver: #{nsrcd.nsdname}")
  354. nssrvquery = @res.query(nsrcd.nsdname, "A")
  355. if nssrvquery.answer.length == 0
  356. nssrvip = Rex::Socket.gethostbyname(nsrcd.nsdname)[3].bytes.reduce {|a,b| [a,b].join(".")}
  357. else
  358. nssrvip = nssrvquery.answer[0].address.to_s
  359. end
  360. begin
  361. @res.nameserver=(nssrvip)
  362. @nsinuse = nssrvip
  363. zone = []
  364. zone = @res.axfr(target)
  365. if zone.length != 0
  366. print_status("Zone transfer successful")
  367. report_note(:host => nssrvip,
  368. :proto => 'udp',
  369. :sname => 'dns',
  370. :port => 53 ,
  371. :type => 'dns.enum',
  372. :update => :unique_data,
  373. :data => "Zone transfer successful")
  374. #Prints each record according to its type
  375. zone.each do |response|
  376. response.answer.each do |rr|
  377. begin
  378. case rr.type
  379. when "A"
  380. print_status("Name: #{rr.name} IP address: #{rr.address} Record: A ")
  381. report_note(:host => nssrvip,
  382. :proto => 'udp',
  383. :sname => 'dns',
  384. :port => 53 ,
  385. :type => 'dns.enum',
  386. :update => :unique_data,
  387. :data => "#{rr.address.to_s},#{rr.name},A")
  388. when "SOA"
  389. print_status("Name: #{rr.mname} Record: SOA")
  390. report_note(:host => nssrvip,
  391. :proto => 'udp',
  392. :sname => 'dns',
  393. :port => 53 ,
  394. :type => 'dns.enum',
  395. :update => :unique_data,
  396. :data => "#{rr.name},SOA")
  397. when "MX"
  398. print_status("Name: #{rr.exchange} Preference: #{rr.preference} Record: MX")
  399. report_note(:host => nssrvip,
  400. :proto => 'udp',
  401. :sname => 'dns',
  402. :port => 53 ,
  403. :type => 'dns.enum',
  404. :update => :unique_data,
  405. :data => "#{rr.exchange},MX")
  406. when "CNAME"
  407. print_status("Name: #{rr.cname} Record: CNAME")
  408. report_note(:host => nssrvip,
  409. :proto => 'udp',
  410. :sname => 'dns',
  411. :port => 53 ,
  412. :type => 'dns.enum',
  413. :update => :unique_data,
  414. :data => "#{rr.cname},CNAME")
  415. when "HINFO"
  416. print_status("CPU: #{rr.cpu} OS: #{rr.os} Record: HINFO")
  417. report_note(:host => nssrvip,
  418. :proto => 'udp',
  419. :sname => 'dns',
  420. :port => 53 ,
  421. :type => 'dns.enum',
  422. :update => :unique_data,
  423. :data => "CPU:#{rr.cpu},OS:#{rr.os},HINFO")
  424. when "AAAA"
  425. print_status("IPv6 Address: #{rr.address} Record: AAAA")
  426. report_note(:host => nssrvip,
  427. :proto => 'udp',
  428. :sname => 'dns',
  429. :port => 53 ,
  430. :type => 'dns.enum',
  431. :update => :unique_data,
  432. :data => "#{rr.address.to_s}, AAAA")
  433. when "NS"
  434. print_status("Name: #{rr.nsdname} Record: NS")
  435. report_note(:host => nssrvip,
  436. :proto => 'udp',
  437. :sname => 'dns',
  438. :port => 53 ,
  439. :type => 'dns.enum',
  440. :update => :unique_data,
  441. :data => "#{rr.nsdname},NS")
  442. when "TXT"
  443. print_status("Text: #{rr.inspect}")
  444. report_note(:host => nssrvip,
  445. :proto => 'udp',
  446. :sname => 'dns',
  447. :port => 53 ,
  448. :type => 'dns.enum',
  449. :update => :unique_data,
  450. :data => rr.inspect)
  451. when "SRV"
  452. print_status("Host: #{rr.host} Port: #{rr.port} Priority: #{rr.priority} Record: SRV")
  453. report_note(:host => nssrvip,
  454. :proto => 'udp',
  455. :sname => 'dns',
  456. :port => 53 ,
  457. :type => 'dns.enum',
  458. :update => :unique_data,
  459. :data => "#{rr.host},#{rr.port},#{rr.priority},SRV")
  460. end
  461. rescue ActiveRecord::RecordInvalid
  462. #Do nothing. Probably tried to store :host => 127.0.0.1
  463. end
  464. end
  465. end
  466. else
  467. print_error("Zone transfer failed (length was zero)")
  468. end
  469. rescue Exception => e
  470. print_error("Error executing zone transfer: #{e.message}")
  471. elog("Error executing zone transfer: #{e.message}\n#{e.backtrace.join("\n")}")
  472. end
  473. end
  474. else
  475. print_error("Could not resolve domain #{target}")
  476. end
  477. end
  478. def run
  479. @res = Net::DNS::Resolver.new()
  480. if datastore['TCP_DNS']
  481. vprint_status("Using DNS/TCP")
  482. @res.use_tcp = true
  483. end
  484. @res.retry = datastore['RETRY'].to_i
  485. @res.retry_interval = datastore['RETRY_INTERVAL'].to_i
  486. @threadnum = datastore['THREADS'].to_i
  487. wldcrd = wildcard(datastore['DOMAIN'])
  488. switchdns(datastore['DOMAIN'])
  489. if(datastore['ENUM_STD'])
  490. genrcd(datastore['DOMAIN'])
  491. end
  492. if(datastore['ENUM_TLD'])
  493. tldexpnd(datastore['DOMAIN'],datastore['NS'])
  494. end
  495. if(datastore['ENUM_BRT'])
  496. if wldcrd and datastore['STOP_WLDCRD']
  497. print_error("Wildcard record found!")
  498. else
  499. dnsbrute(datastore['DOMAIN'],datastore['WORDLIST'],datastore['NS'])
  500. end
  501. end
  502. if(datastore['ENUM_IP6'])
  503. if wldcrd and datastore['STOP_WLDCRD']
  504. print_status("Wildcard Record Found!")
  505. else
  506. bruteipv6(datastore['DOMAIN'],datastore['WORDLIST'],datastore['NS'])
  507. end
  508. end
  509. if(datastore['ENUM_AXFR'])
  510. axfr(datastore['DOMAIN'],datastore['NS'])
  511. end
  512. if(datastore['ENUM_SRV'])
  513. srvqry(datastore['DOMAIN'],datastore['NS'])
  514. end
  515. if(datastore['ENUM_RVL'] and datastore['IPRANGE'] and not datastore['IPRANGE'].empty?)
  516. reverselkp(datastore['IPRANGE'],datastore['NS'])
  517. end
  518. end
  519. end