-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathzoneMatcher.py
174 lines (142 loc) · 5.99 KB
/
zoneMatcher.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
# this file reads large zone fiels and extract its set of NS records
import re
import json
import argparse
import logging
import sys
from domutils import getparent
def evalNSSet(cyclic, nsset):
ret = False
for ns in nsset:
parentNS = getparent(ns)
if parentNS[-1] != ".":
parentNS = parentNS + "."
if parentNS in cyclic:
ret = True
return ret
def parseZone(cyclic, zonefile, extension):
bugged = dict()
ns_regex = re.compile('(?:[a-zA-Z\.0-9_-]+\s+\d+\s+)(IN\s+NS\s+)(?:[a-zA-Z\.0-9_-])')
if extension[-1] != ".":
extension = extension + "."
if extension[0] != ".":
extension = "." + extension
counter = 0
with open(zonefile) as f:
nsset = set()
foundZone = False
tempDomain = ''
for line in f:
#line = line.lower()
#sp = line.split('\t')
sp = line.split()
counter = counter + 1
if float(counter) % 100_000 == 0:
print(f"reading line {counter} of zone file")
if ns_regex.match(line):
#if ' ns ' in line and 'rrsig' not in line and 'dnskey' not in line and 'nsec3' not in line:
if sp[0] != '':
if not foundZone:
tempDomain = sp[0]
print('at not foundZone. Tempodomain: {}'.format(tempDomain))
if len(tempDomain.split(".")) == 1:
tempDomain = tempDomain + extension
foundZone = True
tempNS = sp[-1].rstrip()
if tempNS[-1] != ".":
tempNS = tempNS + extension
nsset.add(tempNS)
print('nsset: {}'.format(nsset))
else:
'''
thne it is a new zone, need to do two things:
1. calc if it is bugged
2. create new zone
'''
print('at else not foundZone')
result = evalNSSet(cyclic, nsset)
print('result: {}'.format(result))
if result:
bugged[tempDomain] = list(nsset)
# reset
foundZone = False
nsset = set()
tempDomain = sp[0]
if len(tempDomain.split(".")) == 1:
tempDomain = tempDomain + extension
foundZone = True
tempNS = sp[-1].rstrip()
if tempNS[-1] != ".":
tempNS = tempNS + extension
nsset.add(tempNS)
else:
if foundZone:
tempNS = sp[-1].rstrip()
if tempNS[-1] != ".":
tempNS = tempNS + extension
nsset.add(tempNS)
else:
# new zone
tempDomain = sp[0]
if len(tempDomain.split(".")) == 1:
tempDomain = tempDomain + extension
foundZone = True
nsset.add(sp[-1].rstrip())
return bugged
def getCyclic(infile):
ret = set()
deps = dict()
try:
with open(infile, 'r') as f:
deps = json.load(f)
except:
# if it's multiple dics in one json file
with open(infile, 'r') as f:
for line in f:
tempD = json.loads(line)
for k, v in tempD.items():
deps[k] = v
for key, value in deps.items():
if 'fullDep' in key:
for ns1, ns2 in value.items():
ret.add(ns1)
ret.add(ns2)
return ret
def zone_matcher(cyclic_domain_file=None, zonefile=None, zoneorigin=None, output_file=None):
print("step 8: read cyclic domains")
cyclic=''
try:
cyclic = getCyclic(cyclic_domain_file)
except FileNotFoundError:
logging.info("ERROR: no cyclic domain file")
if cyclic=='':
logging.info('ERROR: no cyclic domain file ')
sys.exit(cyclic_domain_file + " does not exist; exiting ")
else:
print("step 8a: read zone file and find them")
troubledDomains = parseZone(cyclic, zonefile, zoneorigin)
print('troubleddomains: {}'.format(troubledDomains))
print("step 8b: writing it to json")
print(cyclic)
print(troubledDomains)
if len(troubledDomains)>0:
with open(output_file, 'w') as fp:
json.dump(troubledDomains, fp)
print(f"\nThere are {len(troubledDomains)} domains that have at least one cyclic dependent NS")
print('done')
else:
logging.info("ERROR: could not match domain names to NS records; please check zoneMatcher.py")
sys.exit('ERROR: could not match domain names to NS records; please check zoneMatcher.py')
if __name__ == '__main__':
# Setup logging if called from command line
logging.basicConfig(filename='zone-matcher.log',
level=logging.INFO, format="%(asctime)s zone_matcher: %(levelname)s %(message)s")
# Read the command line arguments
argparser = argparse.ArgumentParser(description="Determines how many domains are affected by cyclic dependency")
argparser.add_argument('full_cycle_file', type=str, help="File with the list of full cycles")
argparser.add_argument('zonefile', type=str, help="Zone file to analyze")
argparser.add_argument('zonename', type=str, help="Zone origin")
argparser.add_argument('output', type=str, help="File to save the output")
args = argparser.parse_args()
zone_matcher(cyclic_domain_file=args.full_cycle_file, zonefile=args.zonefile,
zoneorigin=args.zonename, output_file=args.output)