-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzonetransfer.py
84 lines (73 loc) · 2.58 KB
/
zonetransfer.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
#!/usr/bin/env python3.8
import dns.query, dns.zone, dns.resolver
import sys, socket, re, string
from aws_hostedzone_create import createzone
socket.setdefaulttimeout(10)
'''
Get NS records
'''
def getNameservers(zonename):
#Zonetransfer DNS server for your account
nameservers = ["54.197.245.255"]
try:
#nameservers = dns.resolver.query(zonename, 'NS')
return nameservers
except dns.resolver.NoNameservers:
print("\nDomain:", zonename, "has no ns records, sorry ;(")
return ""
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]
def getZoneXfer(zonename):
for nameserver in getNameservers(zonename):
try:
print('\nQuerying nameserver %s for DNS zone %s\r' % (nameserver,zonename))
tryxfer = dns.zone.from_xfr(dns.query.xfr(str(nameserver), zonename))
names = tryxfer.nodes.keys()
#names.sort()
sorted(names)
f = open('zonefile/%s.txt' %(zonename),'w')
for n in names:
#print(tryxfer[n].to_text(n))
f.write(tryxfer[n].to_text(n)+'\n')
#
f.close()
print('\nZone transferred from: ' + nameserver + '\n')
print ('\n Creating AWS Zone')
createzone(zonename)
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()
'''
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)