forked from rbsec/dnscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnscan.py
executable file
·340 lines (302 loc) · 11.1 KB
/
dnscan.py
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python
#
# dnscan copyright (C) 2013-2014 rbsec
# Licensed under GPLv3, see LICENSE for details
#
from __future__ import print_function
import os
import re
import sys
import threading
import time
try: # Ugly hack because Python3 decided to rename Queue to queue
import Queue
except ImportError:
import queue as Queue
try:
import argparse
except:
print("FATAL: Module argparse missing (python-argparse)")
sys.exit(1)
try:
import dns.query
import dns.resolver
import dns.zone
except:
print("FATAL: Module dnspython missing (python-dnspython)")
sys.exit(1)
# Usage: dnscan.py -d <domain name>
class scanner(threading.Thread):
def __init__(self, queue):
global wildcard
threading.Thread.__init__(self)
self.queue = queue
def get_name(self, domain):
global wildcard
try:
if sys.stdout.isatty(): # Don't spam output if redirected
sys.stdout.write(domain + " \r")
sys.stdout.flush()
res = lookup(domain, recordtype)
if args.tld and res:
nameservers = sorted(list(res))
ns0 = str(nameservers[0])[:-1] # First nameserver
print(domain + " - " + col.brown + ns0 + col.end)
if args.tld:
if res:
print(domain + " - " + res)
return
for rdata in res:
if wildcard:
if rdata.address == wildcard:
return
if args.domain_first:
print(domain + " - " + col.brown + rdata.address + col.end)
else:
print(rdata.address + " - " + col.brown + domain + col.end)
if outfile:
if args.domain_first:
print(domain + " - " + rdata.address, file=outfile)
else:
print(rdata.address + " - " + domain, file=outfile)
if domain != target and args.recurse: # Don't scan root domain twice
add_target(domain) # Recursively scan subdomains
except:
pass
def run(self):
while True:
try:
domain = self.queue.get(timeout=1)
except:
return
self.get_name(domain)
self.queue.task_done()
class output:
def status(self, message):
print(col.blue + "[*] " + col.end + message)
if outfile:
print("[*] " + message, file=outfile)
def good(self, message):
print(col.green + "[+] " + col.end + message)
if outfile:
print("[+] " + message, file=outfile)
def verbose(self, message):
if args.verbose:
print(col.brown + "[v] " + col.end + message)
if outfile:
print("[v] " + message, file=outfile)
def warn(self, message):
print(col.red + "[-] " + col.end + message)
if outfile:
print("[-] " + message, file=outfile)
def fatal(self, message):
print("\n" + col.red + "FATAL: " + message + col.end)
if outfile:
print("FATAL " + message, file=outfile)
class col:
if sys.stdout.isatty():
green = '\033[32m'
blue = '\033[94m'
red = '\033[31m'
brown = '\033[33m'
end = '\033[0m'
else: # Colours mess up redirected output, disable them
green = ""
blue = ""
red = ""
brown = ""
end = ""
def lookup(domain, recordtype):
try:
res = resolver.query(domain, recordtype)
return res
except:
return
def get_wildcard(target):
# Use current unix time as a test subdomain
epochtime = str(int(time.time()))
# Prepend a letter to work around incompetent companies like CableOne
# and their stupid attempts at DNS hijacking
res = lookup("a" + epochtime + "." + target, recordtype)
if res:
out.good(col.red + "Wildcard" + col.end + " domain found - " + col.brown + res[0].address + col.end)
return res[0].address
else:
out.verbose("No wildcard domain found")
def get_nameservers(target):
try:
ns = resolver.query(target, 'NS')
return ns
except:
return
def get_v6(target):
out.verbose("Getting IPv6 (AAAA) records")
try:
res = lookup(target, "AAAA")
if res:
out.good("IPv6 (AAAA) records found. Try running dnscan with the "+ col.green + "-6 " + col.end + "option.")
for v6 in res:
print(str(v6) + "\n")
if outfile:
print(v6, file=outfile)
except:
return
def get_txt(target):
out.verbose("Getting TXT records")
try:
res = lookup(target, "TXT")
if res:
out.good("TXT records found")
for txt in res:
print(txt)
if outfile:
print(txt, file=outfile)
print("")
except:
return
def get_mx(target):
out.verbose("Getting MX records")
try:
res = lookup(target, "MX")
except:
return
# Return if we don't get any MX records back
if not res:
return
out.good("MX records found, added to target list")
for mx in res:
print(mx.to_text())
if outfile:
print(mx.to_text(), file=outfile)
mxsub = re.search("([a-z0-9\.\-]+)\."+target, mx.to_text(), re.IGNORECASE)
try:
if mxsub.group(1) and mxsub.group(1) not in wordlist:
queue.put(mxsub.group(1) + "." + target)
except AttributeError:
pass
print("")
def zone_transfer(domain, ns):
out.verbose("Trying zone transfer against " + str(ns))
try:
zone = dns.zone.from_xfr(dns.query.xfr(str(ns), domain, relativize=False),
relativize=False)
out.good("Zone transfer sucessful using nameserver " + col.brown + str(ns) + col.end)
names = list(zone.nodes.keys())
names.sort()
for n in names:
print(zone[n].to_text(n)) # Print raw zone
if outfile:
print(zone[n].to_text(n), file=outfile)
sys.exit(0)
except Exception:
pass
def add_target(domain):
for word in wordlist:
queue.put(word + "." + domain)
def add_tlds(domain):
for tld in wordlist:
queue.put(domain + "." + tld)
def get_args():
global args
parser = argparse.ArgumentParser('dnscan.py', formatter_class=lambda prog:argparse.HelpFormatter(prog,max_help_position=40))
parser.add_argument('-d', '--domain', help='Target domain', dest='domain', required=True)
parser.add_argument('-w', '--wordlist', help='Wordlist', dest='wordlist', required=False)
parser.add_argument('-t', '--threads', help='Number of threads', dest='threads', required=False, type=int, default=8)
parser.add_argument('-6', '--ipv6', help='Scan for AAAA records', action="store_true", dest='ipv6', required=False, default=False)
parser.add_argument('-z', '--zonetransfer', action="store_true", default=False, help='Only perform zone transfers', dest='zonetransfer', required=False)
parser.add_argument('-r', '--recursive', action="store_true", default=False, help="Recursively scan subdomains", dest='recurse', required=False)
parser.add_argument('-T', '--tld', action="store_true", default=False, help="Scan for TLDs", dest='tld', required=False)
parser.add_argument('-o', '--output', help="Write output to a file", dest='output_filename', required=False)
parser.add_argument('-D', '--domain-first', action="store_true", default=False, help='Output domain first, rather than IP address', dest='domain_first', required=False)
parser.add_argument('-v', '--verbose', action="store_true", default=False, help='Verbose mode', dest='verbose', required=False)
args = parser.parse_args()
def setup():
global target, wordlist, queue, resolver, recordtype, outfile
target = args.domain
if args.tld and not args.wordlist:
args.wordlist = os.path.join(os.path.dirname(os.path.realpath(__file__)), "tlds.txt")
else:
if not args.wordlist: # Try to use default wordlist if non specified
args.wordlist = os.path.join(os.path.dirname(os.path.realpath(__file__)), "subdomains.txt")
try:
wordlist = open(args.wordlist).read().splitlines()
except:
out.fatal("Could not open wordlist " + args.wordlist)
sys.exit(1)
# Open file handle for output
try:
outfile = open(args.output_filename, "w")
except TypeError:
outfile = None
except IOError:
out.fatal("Could not open output file: " + args.output_filename)
sys.exit(1)
# Number of threads should be between 1 and 32
if args.threads < 1:
args.threads = 1
elif args.threads > 32:
args.threads = 32
queue = Queue.Queue()
resolver = dns.resolver.Resolver()
resolver.timeout = 1
# Record type
if args.ipv6:
recordtype = 'AAAA'
elif args.tld:
recordtype = 'NS'
else:
recordtype = 'A'
if __name__ == "__main__":
global wildcard
out = output()
get_args()
setup()
if args.tld:
if "." in target:
out.warn("Warning: TLD scanning works best with just the domain root")
out.good("TLD Scan")
add_tlds(target)
else:
queue.put(target) # Add actual domain as well as subdomains
nameservers = get_nameservers(target)
out.good("Getting nameservers")
targetns = [] # NS servers for target
try: # Subdomains often don't have NS recoards..
for ns in nameservers:
ns = str(ns)[:-1] # Removed trailing dot
res = lookup(ns, "A")
for rdata in res:
targetns.append(rdata.address)
print(rdata.address + " - " + col.brown + ns + col.end)
if outfile:
print(rdata.address + " - " + ns, file=outfile)
zone_transfer(target, ns)
except SystemExit:
sys.exit(0)
except:
out.warn("Getting nameservers failed")
# resolver.nameservers = targetns # Use target's NS servers for lokups
# Missing results using domain's NS - removed for now
out.warn("Zone transfer failed\n")
if args.zonetransfer:
sys.exit(0)
get_v6(target)
get_txt(target)
get_mx(target)
wildcard = get_wildcard(target)
out.status("Scanning " + target + " for " + recordtype + " records")
add_target(target)
for i in range(args.threads):
t = scanner(queue)
t.setDaemon(True)
t.start()
try:
for i in range(args.threads):
t.join(1024) # Timeout needed or threads ignore exceptions
except KeyboardInterrupt:
out.fatal("Caught KeyboardInterrupt, quitting...")
if outfile:
outfile.close()
sys.exit(1)
if outfile:
outfile.close()