-
Notifications
You must be signed in to change notification settings - Fork 16
/
crazyParser.py
executable file
·354 lines (301 loc) · 13.4 KB
/
crazyParser.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/python
'''
This script uses urlcrazy and dnstwist to identify possible typosquatted
domains. The output is compared against an existing list of typosquatted
domains generated by an initial review. If any new domains are identified,
the results are mailed off for review and blocking in your web proxy.
Dependencies:
mydomains.csv
This file contains a list of domains you wish to monitor.
knowndomains.csv
This file contains domains already identified from previous
runs. The file contains a header "Domain,Reason" and a list of
domains, 1 per line. The reason will either be Squatter or
Valid Site if the domain belongs to a legitimate site.
Note: Reason is not used by crazyParser. It is for your
reference only.
urlcrazy: installed at /usr/bin/urlcrazy. If this installed in an
alternate location, the value of urlcrazyPath will need to be
updated to reflect its location.
dnstwist: installed at /opt/dnstwist/dnstwist.py. If this installed in an
alternate location, the value of dnstwistPath will need to be
updated to reflect its location.
crazyParser.py - by @hardwaterhacker - http://hardwatersec.blogspot.com
mike@hardwatersecurity.com
'''
__author__ = 'Mike Saunders - @hardwaterhacker'
__version__ = '20151001'
__email__ = 'mike@hardwatersecurity.com'
import argparse
import os
import sys
import subprocess
import csv
import smtplib
import tempfile
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import atexit
urlcrazyPath = '/usr/bin/urlcrazy' # update if your installation differs
dnstwistPath = '/opt/dnstwist/dnstwist.py' # update if your installation differs
# set up global defaults
tempFiles = [] # define temporary files array
def checkPerms(docRoot, resultsFile):
# Test if we have execute permissions to docRoot
if not os.access(docRoot, os.X_OK):
print "Destination directory " + docRoot + " not accessible."
print "Please check permissions. Exiting..."
sys.exit()
else:
pass
# Test if we have write permissions to docRoot
try:
permtest = tempfile.TemporaryFile('w+b', bufsize=-1, dir=docRoot)
except OSError:
print "Unable to write to desired directory: " + docRoot + "."
print "Please check permissions. Exiting..."
sys.exit()
def checkDepends(myDomains, knownDomains, docRoot, resultsFile, urlcrazy, dnstwist):
# Test if mydomains.csv exists
if not os.access(myDomains, os.F_OK) or not os.access(knownDomains, os.F_OK):
print "Required configuration files - mydomains.csv or knowndomains.csv - not found."
print "Please verify configuration. Exiting..."
sys.exit()
else:
pass
# Test if docRoot is actually a directory
if not os.path.isdir(docRoot):
print "Argument: -d " + docRoot + " is not a directory."
print "Please review arguments. Exiting..."
sys.exit()
else:
pass
# Ensure resultsFile isn't actually a directory
if os.path.exists(resultsFile) and not os.path.isfile(resultsFile):
#if not os.path.isfile(resultsFile):
print "Argument: -o " + resultsFile + " should be a regular file but is something else."
print "Please review arguments. Exiting..."
sys.exit()
else:
pass
# Test if urlcrazy exists
if urlcrazy:
if not os.access(urlcrazyPath, os.F_OK):
print "URLCrazy specified as " + urlcrazyPath + " but was not found."
print "Please check urlcrazyPath in crazyParser.py. Exiting..."
sys.exit()
# Test if dnstwist exists
if dnstwist:
if not os.access(dnstwistPath, os.F_OK):
print "DNStwist specified as " + dnstwistPath + "but was not found."
print "Please check urlcrazyPath in crazyParser.py. Exiting..."
sys.exit()
def doCrazy(docRoot, resultsFile, myDomains, urlcrazy, dnstwist):
# cleanup old results file
try:
os.remove(resultsFile)
except OSError:
pass
with open(myDomains, 'rbU') as domains:
reader = csv.reader(domains)
for domain in domains:
domain = domain.rstrip()
# Run urlcrazy if enabled
if urlcrazy:
ucoutfile = tempfile.NamedTemporaryFile('w', bufsize=-1, suffix='.uctmp', prefix=domain + '.', dir=docRoot, delete=False)
ucargs=[urlcrazyPath, '-f', 'csv', '-o', ucoutfile.name, domain]
try:
with open(os.devnull, 'w') as devnull:
subprocess.call(ucargs, stdout=devnull, close_fds=True, shell=False)
tempFiles.append(ucoutfile.name)
except:
# An error occurred running urlcrazy
print "Unexpected error running urlcrazy:", sys.exc_info()[0]
pass
# Run dnstwist if enabled
dtargs=[dnstwistPath, '-r', '-c', domain]
if dnstwist:
dtoutfile = tempfile.NamedTemporaryFile('w', bufsize=-1, suffix='.dttmp', prefix=domain + '.', dir=docRoot, delete=False)
try:
with open(dtoutfile.name, 'wb') as dtout:
output=subprocess.check_output(dtargs, shell=False)
dtout.write(output)
tempFiles.append(dtoutfile.name)
except:
# An error occurred running dnstwist
print "Unexpected error running dnstwist:", sys.exc_info()[0]
pass
def parseOutput(docRoot, knownDomains, resultsFile, urlcrazy, dnstwist):
# set up domains dictionary
domains = []
# compare known domains to discovered domains
knowndom = []
with open (knownDomains, 'rbU') as domfile:
reader = csv.DictReader(domfile)
for row in reader:
knowndom.append(row['Domain'])
if urlcrazy:
# Parse each urlcrazy temp file in tempFiles list
for file in tempFiles:
if file.endswith(".uctmp"):
with open (file, 'rbU') as csvfile:
reader = csv.DictReader(row.replace('\0', '') for row in csvfile)
for row in reader:
if len(row) != 0:
if row['CC-A'] != "?":
if row['Typo'] in knowndom:
pass
else:
domains.append(row['Typo'])
if dnstwist:
# Parse each dnstwist temp file in tempFiles list
for file in tempFiles:
if file.endswith(".dttmp"):
with open (file, 'rbU') as csvfile:
reader = csv.reader(csvfile)
next(reader) # Due to recent change in dnstwist, skip header line
next(reader) # skip second line, contains original domain
for row in reader:
if row[1] in knowndom:
pass
else:
domains.append(row[1])
# dedupe domains list
domains = dedup(domains)
# write out results
# this file will only contain the header if there are no new results
with open(resultsFile, 'wb') as outfile:
fieldnames = ['Domain']
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
for row in domains:
writer.writerow({'Domain': row})
outfile.close()
def sendMail(resultsFile):
'''
sendMail sends the results of urlcrazy scans,
including diffs to your selected address
using a given address.
Specify your sending account username in mail_user.
Specify your account password in mail_pwd.
Configure for your mail server by modifying the
mailServer = line.
This assumes your mail server supports starttls.
Future versions will allow you to specify whether
or not to use starttls. To suppress starttls,
remove the line mailServer.starttls().
'''
mail_user = "mail_sender_account"
mail_pwd = "your_pass_here"
mail_recip = ["recipient_address_1", "recipient_address_2"]
def mail(to, subject, text, resultsFile, numResults):
msg = MIMEMultipart()
msg['From'] = mail_user
msg['To'] = ", ".join(to)
msg['Subject'] = subject
msg.attach(MIMEText(text))
# Attach the attachment if there are new results
# numResults is the number of rows in the results file
# This is always at least 1 due to the header row
if numResults >= 2:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(resultsFile, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(resultsFile))
msg.attach(part)
else:
pass
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(mail_user, mail_pwd)
mailServer.sendmail(mail_user, to, msg.as_string())
mailServer.close()
# this counts the number of line in the results file
# if it is 1, there were no results
numResults = sum(1 for line in open(resultsFile))
if numResults == 1:
mail(mail_recip,
"Daily DNS typosquatting recon report", # subject line
"There were no new results in today's scan", # your message here
resultsFile, numResults)
else:
mail(mail_recip,
"Daily DNS typosquatting recon report", # subject line
"The results from today's DNS typosquatting scan are attached", # your message here
resultsFile, numResults)
def doCleanup(docRoot):
# Delete all temporary .tmp files created by urlcrazy and dnstwist
for f in tempFiles:
try:
os.remove(f)
except OSError:
print "Error removing temporary file: " + f
pass
def dedup(domainslist, idfun=None): # code from http://www.peterbe.com/plog/uniqifiers-benchmark
if idfun is None:
def idfun(x): return x
seen = {}
result = []
for item in domainslist:
marker = idfun(item)
if marker in seen: continue
seen[marker] = 1
result.append(item)
return result
def main():
# Set up parser for command line arguments
parser = argparse.ArgumentParser(prog='crazyParser.py', description='crazyParser - a tool to detect new typosquatted domain registrations by using the output from dnstwist and/or urlcrazy', add_help=True)
parser.add_argument('-c', '--config', help='Directory location for required config files', default=os.getcwd(), required=False)
parser.add_argument('-o', '--output', help='Save results to file, defaults to results.csv', default='results.csv', required=False)
parser.add_argument('-d', '--directory', help='Directory for saving output, defaults to current directory', default=os.getcwd(), required=False)
parser.add_argument('-m', '--email', help='Email results upon completion, defaults to False', action="store_true", default=False, required=False)
parser.add_argument('--dnstwist', help='Use dnstwist for domain discovery, defaults to False', action="store_true", default=False, required=False)
parser.add_argument('--urlcrazy', help='Use urlcray for domain discovery, defaults to False', action="store_true", default=False, required=False)
if len(sys.argv)==1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
if args.config != os.getcwd():
if os.path.isdir(args.config):
configDir = args.config
else:
print "ERROR! Specified configuration directory " + args.config + " does not exist!"
print "Exiting..."
sys.exit()
else:
configDir = args.config
if args.directory != os.getcwd():
if os.path.isdir(args.directory):
docRoot = args.directory
else:
print "ERROR! Specified output directory " + args.directory + " does not exist!"
print "Exiting..."
sys.exit()
else:
docRoot = args.directory
# set up global files
resultsFile = os.path.join(docRoot, args.output)
myDomains = os.path.join(configDir,'mydomains.csv')
knownDomains = os.path.join(configDir,'knowndomains.csv')
# Check to make sure we have the necessary permissions
checkPerms(docRoot, resultsFile)
# Check dependencies
checkDepends(myDomains, knownDomains, docRoot, resultsFile, args.urlcrazy, args.dnstwist)
# Clean up output files at exit
atexit.register(doCleanup, docRoot)
# Execute discovery
doCrazy(docRoot, resultsFile, myDomains, args.urlcrazy, args.dnstwist)
# parse output
parseOutput(docRoot, knownDomains, resultsFile, args.urlcrazy, args.dnstwist)
# send results if -m/--email is true
if args.email == True:
sendMail(resultsFile)
else:
pass
if __name__ == "__main__":
main()