-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScan MX Records.py
More file actions
133 lines (121 loc) · 4.98 KB
/
Copy pathScan MX Records.py
File metadata and controls
133 lines (121 loc) · 4.98 KB
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
#!/bin/env python3
# Scan a list of domains supplied by an input file named domainlist.txt and check for MX, SPF, DMARC, and DKIM records; note any domains that are missing any one of these records
# Prerequisite: dnspython, install it with pip install dnspython
# Hide deprecation warnings
import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning)
import dns.resolver
import os
import sys
sourcefile='domainlist.txt'
# Open the file containing the list of domains
# Check for sourcefile and error out if it is missing or empty with a message stating the file is needed
if not os.path.isfile(sourcefile):
print(f'{sourcefile} is missing')
sys.exit(1)
elif os.stat(sourcefile).st_size == 0:
print(f'{sourcefile} is empty')
sys.exit(1)
with open(sourcefile) as f:
domains = f.read().splitlines()
# Create a list of domains that are missing any one of the records
missing_records = []
# Loop through the list of domains
with open('domainreport.txt', 'w') as report:
for domain in domains:
# Check for MX records
try:
mx_records = dns.resolver.query(domain, 'MX')
except dns.resolver.NXDOMAIN:
print(f'{domain} has no MX records', file=report)
print(f'{domain} has no MX records')
continue
except dns.resolver.NoAnswer:
print(f'{domain} has no MX records', file=report)
print(f'{domain} has no MX records')
missing_records.append(domain)
continue
except dns.resolver.NoNameservers:
print(f'{domain} has no nameservers', file=report)
print(f'{domain} has no nameservers')
continue
except dns.exception.Timeout:
print(f'{domain} has timed out', file=report)
print(f'{domain} has timed out')
continue
else:
print(f'{domain} has MX records', file=report)
print(f'{domain} has MX records')
# Check for SPF records
try:
spf_records = dns.resolver.query(domain, 'TXT')
except dns.resolver.NXDOMAIN:
print(f'{domain} has no SPF records', file=report)
print(f'{domain} has no SPF records')
continue
except dns.resolver.NoAnswer:
print(f'{domain} has no SPF records', file=report)
print(f'{domain} has no SPF records')
missing_records.append(domain)
continue
except dns.resolver.NoNameservers:
print(f'{domain} has no nameservers', file=report)
print(f'{domain} has no nameservers')
continue
except dns.exception.Timeout:
print(f'{domain} has timed out', file=report)
print(f'{domain} has timed out')
continue
else:
print(f'{domain} has SPF records', file=report)
print(f'{domain} has SPF records')
# Check for DMARC records
try:
dmarc_records = dns.resolver.query('_dmarc.' + domain, 'TXT')
except dns.resolver.NXDOMAIN:
print(f'{domain} has no DMARC records', file=report)
print(f'{domain} has no DMARC records')
continue
except dns.resolver.NoAnswer:
print(f'{domain} has no DMARC records', file=report)
print(f'{domain} has no DMARC records')
missing_records.append(domain)
continue
except dns.resolver.NoNameservers:
print(f'{domain} has no nameservers', file=report)
print(f'{domain} has no nameservers')
continue
except dns.exception.Timeout:
print(f'{domain} has timed out', file=report)
print(f'{domain} has timed out')
continue
else:
print(f'{domain} has DMARC records', file=report)
print(f'{domain} has DMARC records')
# Check for DKIM records
dkim_found = False
selectors = ['s1', 's2', 'k1', 'k2', 'selector1', 'selector2']
for selector in selectors:
try:
dkim_records = dns.resolver.query(f'{selector}._domainkey.{domain}', 'TXT')
except dns.resolver.NXDOMAIN:
continue
except dns.resolver.NoAnswer:
continue
except dns.resolver.NoNameservers:
continue
except dns.exception.Timeout:
continue
else:
dkim_found = True
print(f'{domain} has DKIM records for {selector}', file=report)
print(f'{domain} has DKIM records for {selector}')
if not dkim_found:
print(f'{domain} has no DKIM records', file=report)
print(f'{domain} has no DKIM records')
missing_records.append(domain)
continue
# Print the list of domains that are missing any one of the records
print(f'\nDomains missing records:\n{missing_records}', file=report)
print(f'\nDomains missing records:\n{missing_records}')
# Exit the program