-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
233 lines (204 loc) · 9.9 KB
/
main.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
import subprocess
import re
import urllib.request
import csv
import traceback
import multiprocessing
from collections import deque
# Function to write data to a CSV file
def write_to_csv(queue):
# Open the CSV file in write mode
with open('domains_database.csv', 'w', newline='') as dmarc_csv:
writer = csv.writer(dmarc_csv)
# Write the header row
writer.writerow([
'Domain', 'Type', 'Agency', 'Organization', 'City', 'State', 'Security Contact Email',
'v', 'p', 'sp', 'pct', 'adkim', 'aspf', 'fo', 'ruf', 'rua', 'rf', 'ri'
])
# Continuously get items from the queue and write them to the CSV file
while True:
item = queue.get()
if item is None:
break
writer.writerow(item)
# Function to check if a domain exists in the last 10 lines of the CSV
def duplicate_check(domain, csv_filename):
with open(csv_filename, 'r', newline='') as dmarc_csv:
csv_reader = csv.reader(dmarc_csv)
last_10_lines = deque(csv_reader, maxlen=10)
for row in last_10_lines:
if row and row[0] == domain:
return True
return False
# Main function for processing domain information
def main_proc(queue, domaininfo_decoded):
try:
dmarc_sort = dict()
domaininfo_split = re.split(",", domaininfo_decoded)
dmarc_sort["Domain"] = domaininfo_split[0]
# Check if the current line is the header row, if not, process the data
if dmarc_sort["Domain"] != "Domain Name":
if not duplicate_check(dmarc_sort["Domain"], 'domains_database.csv'):
try:
dmarc_sort["Type"] = domaininfo_split[1]
except:
dmarc_sort["Type"] = "null"
try:
dmarc_sort["Agency"] = domaininfo_split[2]
except:
dmarc_sort["Agency"] = "null"
try:
dmarc_sort["Organization"] = domaininfo_split[3]
except:
dmarc_sort["Organization"] = "null"
try:
dmarc_sort["City"] = domaininfo_split[4]
except:
dmarc_sort["City"] = "null"
try:
dmarc_sort["State"] = domaininfo_split[5]
except:
dmarc_sort["State"] = "null"
try:
dmarc_sort["Security Contact Email"] = domaininfo_split[6].replace("\n", "")
except:
dmarc_sort["Security Contact Email"] = "null"
else:
print(f"Duplicate domain: {dmarc_sort['Domain']}")
else:
return ("Line is header")
# Perform nslookup to retrieve DMARC record information
nslookup_result = subprocess.run(f"nslookup -type=txt _dmarc.{dmarc_sort['Domain']}",
capture_output=True).stdout.decode()
# Extract relevant DMARC record values using regular expressions
try:
dmarc_raw = re.findall('"(.*?)"', nslookup_result)[0] + " "
except:
dmarc_raw = "v=null"
# Process and store the DMARC record values
try:
dmarc_sort["v"] = re.findall('v=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["v"] = "null"
if dmarc_sort["v"] != "null":
try:
dmarc_sort["p"] = re.findall('p=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["p"] = "error"
try:
dmarc_sort["sp"] = re.findall('sp=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["sp"] = dmarc_sort["p"]
try:
dmarc_sort["pct"] = re.findall('pct=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["pct"] = "100"
try:
dmarc_sort["adkim"] = re.findall('adkim=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["adkim"] = "r"
try:
dmarc_sort["aspf"] = re.findall('aspf=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["aspf"] = "r"
try:
dmarc_sort["fo"] = re.findall('fo=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["fo"] = "0"
try:
dmarc_sort["ruf"] = re.findall('ruf=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["ruf"] = "none"
try:
dmarc_sort["rua"] = re.findall('rua=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["rua"] = "none"
try:
dmarc_sort["rf"] = re.findall('rf=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["rf"] = "afrf"
try:
dmarc_sort["ri"] = re.findall('ri=(.*?)[; \n]', dmarc_raw)[0]
except:
dmarc_sort["ri"] = "86400"
# Put the values into the queue for writing to the CSV file
queue.put(dmarc_sort.values())
# Print the processed data
print(dmarc_sort)
# Handle any exceptions that occur during processing
except Exception as e:
print(f"Error on line {domaininfo_decoded}")
print(e)
traceback.print_exc()
# Function to scrape military domains and process their information
def mil_domain_get(url, queue):
print(url)
# Read the webpage content
mil_webpage = urllib.request.urlopen(url).read()
# Extract the relevant HTML sections containing domain information
mil_html = re.findall(r'<div class="DGOVListLink.*?"><a(.*?)</a>', str(mil_webpage))
# Process each HTML section to retrieve domain information
for i in mil_html:
try:
# Extract the URL, domain, and department information
mil_url = re.findall(r'href="(.*?)"', i)[0]
mil_domain = re.findall(r'(?:https?://)?.*?([a-z0-9\-]+\.[a-z0-9\-]+)(?:$|/)', mil_url)[0]
mil_department = re.findall(r'">(.*?);', i + ";")[0]
# Construct a line of data for the military domain
mil_line = f"{mil_domain},Military,Department of Defense,{mil_department},null,null,null"
# Invoke the main processing function for the military domain
pool.apply(main_proc, args=(queue, mil_line))
# Handle any exceptions that occur during processing
except:
print(f"Issue formatting line {i}")
traceback.print_exc()
if __name__ == '__main__':
# Create a multiprocessing manager and a queue for inter-process communication
manager = multiprocessing.Manager()
queue = manager.Queue()
# Start a separate process for writing data to the CSV file
writer_process = multiprocessing.Process(target=write_to_csv, args=(queue,))
writer_process.start()
# Create a multiprocessing pool for concurrent processing
pool = multiprocessing.Pool()
# Process each line from the URL containing domain information
for line in urllib.request.urlopen("https://raw.githubusercontent.com/cisagov/dotgov-data/main/current-full.csv"):
domaininfo_raw = line.decode('utf-8')
pool.apply(main_proc, args=(queue, domaininfo_raw))
# Scrape and process domain information for military domains
mil_scrapedomains = [
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=B",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=C",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=D",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=E",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=F",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=G",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=H",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=I",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=K",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=L",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=M",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=N",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=O",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=P",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=Q",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=R",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=S",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=T",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=U",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=V",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=W",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=X",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=Y",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=Z",
"https://www.defense.gov/Resources/Military-Departments/A-Z-List/?page=0-9"
]
for i in mil_scrapedomains:
mil_domain_get(i, queue)
# Add a None value to the queue to signal the end of processing
queue.put(None)
# Close the multiprocessing pool and wait for all processes to finish
pool.close()
pool.join()
writer_process.join()