-
Notifications
You must be signed in to change notification settings - Fork 2
/
testDNSxfer.py.old
executable file
·139 lines (114 loc) · 4.14 KB
/
testDNSxfer.py.old
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
#!/usr/bin/env python
'''
# Info: Query a supplied dns zone (or just a name without tld/gtld) for it's NS records and
test if it's offering transfers out to the world.
# Required: dnspython:
sh-3.2$ pip install dnspython
# Copyright (C) 2012
# Author: Beau Taub <[email protected]>
# testDNSxfer is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
# testDNSxfer is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with testDNSxfer; see the file COPYING. If not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
'''
import dns.query, dns.zone, dns.resolver
import sys, socket, re, string
socket.setdefaulttimeout(10)
'''
Get NS records
'''
def getNameservers(zonename):
try:
nameservers = dns.resolver.query(zonename, 'NS')
return nameservers
'''
Catch exceptions
'''
except dns.resolver.NoNameservers:
print("\nDomain:", zonename, "has no ns records, sorry ;(")
return ""
except dns.resolver.NXDOMAIN:
print("\nNon-existent domain %s\n" % zonename)
return ""
except dns.exception.Timeout:
print("\nTimeout\n")
return ""
except dns.resolver.NoAnswer:
print("\nproblem getting NS record\n")
return ""
'''
Check for and get required zone argument. Clean up the domain string if it contains anything other than
the domain name
'''
def getArgs():
if len(sys.argv) != 2:
print('\n Usage: \n\n %s fqdn\n' % sys.argv[0])
print('\n Purpose: \n\n Test a domain for open zone transfer\n')
sys.exit(1)
else:
zonename=re.sub('https://|http://|www.','',sys.argv[1])
zonename=zonename.split('/')
print("ZONENAME: %s" %zonename[0])
return zonename[0]
'''
Try to transfer the zone
'''
def getZoneXfer(zonename):
for nameserver in getNameservers(zonename):
try:
print('\nQuerying nameserver %s for DNS zone %s\nResult:\r' % (nameserver,zonename))
tryxfer = dns.zone.from_xfr(dns.query.xfr(str(nameserver), zonename))
names = tryxfer.nodes.keys()
names.sort()
f = open('/tmp/%s.%s.txt' %(zonename,str(nameserver)),'w')
for n in names:
print(tryxfer[n].to_text(n))
f.write(tryxfer[n].to_text(n)+'\n')
f.write('\nZone transferred from: ' + str(nameserver) + '\n')
f.close()
except dns.zone.NoNS:
print("\nDomain: %s exists, but has no ns records, sorry ;( " %zonename)
except dns.resolver.NXDOMAIN:
print("\nDomain:", zonename, "unresponsive, try again\n")
except dns.exception.FormError:
print("\nXfer refused, good work dns admin\n")
except EOFError:
print("\nEOFError\n")
except KeyboardInterrupt:
print("\nUser cancelled\n")
except KeyError as e:
print("KeyError %s" % e)
except socket.error:
print("\nFailed: connection refused\n")
zonename = getArgs()
'''
If arg has no tld, let's append a few
'''
if string.find(zonename,'.') < 0:
for tld in ('net','com','org','us','gov','info','co.uk','co.nz','it','pl','co.in','ly'):
print("TLD %s" % tld)
getZoneXfer("%s.%s"%(zonename,tld))
#getZoneXfer(zonename + '.' + tld)
sys.exit(0)
'''
Get nameserver records
'''
try:
ns = getNameservers(zonename)
if ns:
print("Number of NS records: %s" % len(ns))
getZoneXfer(zonename)
except dns.resolver.NXDOMAIN:
print("AAA Invalid name %s" % zonename)
sys.exit(1)
except dns.exception.Timeout:
print("\nAAATimeout while attempting to contact DNS server")
sys.exit(1)