-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDNSBruteForcer.rb
317 lines (282 loc) · 9.3 KB
/
DNSBruteForcer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
require 'net/dns'
require 'open-uri'
require 'json'
require 'whois'
class DNSBruteForcer
attr_accessor :dictionary, :domain, :geodetails, :threads, :whois
attr_reader :dnsips, :foundhosts
###################
def initialize()
@dnsserver = Net::DNS::Resolver.new(:searchlist=>[],:domain=>[],:udp_timeout=>15)
@dnsips = @dnsserver.nameservers
@dictionary = nil
@domain = nil
@threads = 5
@nsrecords = [] # each record will have the form {:hostname => name, :ip => ip, :type => CNAME/A}
@geodetails = false
@whois = false
@foundhosts = []
@w = Whois::Client.new
end
###################
def setNameServers(nameservers)
@dnsserver = Net::DNS::Resolver.new(:nameservers => nameservers, :searchlist=>[],:domain=>[],:udp_timeout=>15)
@dnsips = @dnsserver.nameservers
end
###################
def getWhoisInfo(host)
begin
r = @w.lookup(host)
return r.to_s
rescue Exception => e
$stderr.puts "There was an error requesting whois info for #{host}"
return ""
end
end
###################
def getAuthDNSServers(domain)
soaips = []
begin
authDNSs = @dnsserver.query(domain,Net::DNS::SOA)
authDNSs.answer.each{|record|
# Get the IP of this authdns and set it as our new DNS resolver
if record.class == Net::DNS::RR::SOA
soadns = record.mname
# Get the IP of the SOA mname and set it as our new dns resolver
@dnsserver.query(soadns,Net::DNS::A).answer.each { |arecord|
soaips << arecord.address.to_s
}
return soaips
else # Is not a SOA response (What could it be?)
return nil
end
}
rescue Net::DNS::Resolver::NoResponseError => terror
puts "Error: #{terror.message}"
return nil
end
return nil
end
###################
def getAllDNSServer(domain)
dnsips = []
begin
dnss = @dnsserver.query(domain,Net::DNS::NS)
dnss.answer.each{|record|
# Get the IP of this authdns and set it as our new DNS resolver
if record.class == Net::DNS::RR::NS
dns = record.nsdname
# Get the IP of the SOA mname and set it as our new dns resolver
@dnsserver.query(dns,Net::DNS::A).answer.each { |arecord|
dnsips << arecord.address.to_s
}
end
}
return dnsips
rescue Net::DNS::Resolver::NoResponseError => terror
puts "Error: #{terror.message}"
return nil
end
return nil
end
###################
def getAllSOAServer(domain)
soaips = []
begin
dnss = @dnsserver.query(domain,Net::DNS::SOA)
dnss.answer.each{|record|
# Get the IP of this authdns and set it as our new DNS resolver
if record.class == Net::DNS::RR::SOA
soaname = record.mname
# Get the IP of the SOA mname and set it as our new dns resolver
soasearchresponse = @dnsserver.query(soaname,Net::DNS::A)
soasearchresponse.answer.each { |arecord|
soaips << arecord.address.to_s
}
# For emergencies when the soa IP is not found we'll ask Google DNS
if soaips.size == 0
googledns = Net::DNS::Resolver.new(:nameservers => ["8.8.8.8"], :searchlist=>[],:domain=>[],:udp_timeout=>15)
soasearchresponse = googledns.query(soaname,Net::DNS::A)
soasearchresponse.answer.each { |arecord|
soaips << arecord.address.to_s
}
end
end
}
return soaips
rescue Net::DNS::Resolver::NoResponseError => terror
puts "Error: #{terror.message}"
return nil
end
return nil
end
###################
def transferZone(domain)
# Trying transfer zone for all NS of the domain
zone = {
:a => [],
:ns => [],
:cname => [],
:soa => [],
:ptr => [],
:mx => [],
:txt => [],
:others => []
}
nsservers = self.getAllDNSServer(domain)
if nsservers.nil?
nsservers.each{|dnsip|
domain_dns = Net::DNS::Resolver.new(:nameservers => dnsip, :searchlist=>[],:domain=>[],:udp_timeout=>15)
transferresponse = domain_dns.axfr(domain)
if transferresponse.header.rCode == Net::DNS::Header::RCode::NOERROR
# Zone transfer was possible!
transferresponse.answer.each{ |record|
case record.class
when Net::DNS::RR::A
zone[:a] << record
when Net::DNS::RR::NS
zone[:ns] << record
when Net::DNS::RR::CNAME
zone[:cname] << record
when Net::DNS::RR::SOA
zone[:soa] << record
when Net::DNS::RR::PTR
zone[:ptr] << record
when Net::DNS::RR::MX
zone[:mx] << record
when Net::DNS::RR::TXT
zone[:txt] << record
else
zone[:others] << record
end
}
return zone
else
# Zone transfer was refused or any other error
return nil
end
}
else
return nil
end
end
###################
def getGeoDetails(ip)
# If this is an IP checkit
if /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/.match(ip.to_s)
# Check if we already have this geographic information in our results
@foundhosts.each{|h|
if h[:ip].to_s == ip.to_s and !h[:geo].nil?
puts "Geographic data of '#{ip}' has been previously requested to freegeoip.net. Returning local geo data."
return h[:geo]
end
}
# If this IP was not already requested, request to internet server
response = open("http://freegeoip.net/json/#{ip.to_s}")
if !response.nil?
geojson = response.readlines()
j = JSON.parse(geojson[0])
return j
end
end
return nil
end
###################
def splitDictionary()
nlines = %x(wc -l < #{@dictionary}).chomp.to_i
words_for_threads = []
File.open(@dictionary,"r").each.with_index {|subdomain,i|
if words_for_threads[i.to_i%@threads.to_i].nil?
words_for_threads[i.to_i%@threads.to_i] = []
end
words_for_threads[i%@threads.to_i] << subdomain
}
return words_for_threads
end
###################
def bruteforceSubdomainsWithDNS(dns,domain)
foundhosts = []
geo = nil
targetdns = Net::DNS::Resolver.new(:nameservers => dns, :searchlist=>[],:domain=>[],:udp_timeout=>15)
# Split the dictionary in parts for the threads
words_for_threads = splitDictionary()
words_for_threads.each{|words|
t = Thread.new {
# each thread ask for a subset of the dictionary
words.each { |subdomain|
targeth = "#{subdomain.chomp}.#{domain}"
begin
response = targetdns.query(targeth)
# Type of the response is tp.answer[0].class
# Address of the response is tp.answer[0].address
# Hostname tp.answer[0].name
if response.header.rCode.type == "NoError"
response.answer.each {|record|
whois = ""
addr = ""
if record.type == "A"
addr = record.address
else
addr = record.cname
end
if (@whois)
puts "Retrieving whois information of '#{targeth}'"
whois = getWhoisInfo(targeth)
end
if (@geodetails)
puts "Retrieving geographic information of '#{addr}'"
geo = getGeoDetails(addr)
end
@foundhosts << {:name => targeth, :ip => addr, :type => record.type, :geo => geo, :whois => whois}
}
end
rescue Net::DNS::Resolver::NoResponseError
$stderr.puts "DNS server '#{dns}' did not respond to our query..."
end
}
}
t.abort_on_exception = true #¿?
t.join
}
return @foundhosts
end
###################
def bruteforceSubdomains(domain,alldns=false)
@foundhosts = []
if @dictionary.nil?
return nil
else
nsservers = self.getAllDNSServer(domain)
if !nsservers.nil? and nsservers.size > 0
if alldns
nsservers.each{|dnsip|
bruteforceSubdomainsWithDNS(dnsip,domain) # .each {|record| @foundhosts << record }
}
else # Ask only to the first DNS
dnsip = nsservers[0]
# @foundhosts =
bruteforceSubdomainsWithDNS(dnsip,domain)
end
else
# We could not find nameservers for this domain
# This is probably a shared hosting and pointing to a SOA.
# Just ask the SOA
soaserver = getAllSOAServer(domain)
if !soaserver.nil? and soaserver.size > 0
if alldns
soaserver.each{|soaip|
bruteforceSubdomainsWithDNS(soaip,domain) # .each { |record| @foundhosts << record }
}
else # Ask only to the first DNS
soaip = soaserver[0]
# @foundhosts =
bruteforceSubdomainsWithDNS(soaip,domain)
end
else
return nil
end
end
end
return @foundhosts
end
end