-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathNewDomainSimilarity.py
139 lines (127 loc) · 5.66 KB
/
NewDomainSimilarity.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
# Script to import newly registered domains, then perform specified similarity tests against a supplied
# list of terms to identify related items, such as typo-squats and domain similars.
# Requires a wordlist as mandatory input, with one item per line, to use as basis for matching.
# Released under GNU GPLv3
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
__author__ = 'Joe Slowik, Dragos Inc.'
import math, requests, os, zipfile, io, datetime, difflib, editdistance, argparse
def argumentParser():
parser = argparse.ArgumentParser()
parser.add_argument('wordlist', action='store', help='Word List file to use for similarity matches')
parser.add_argument('outputDirectory', action='store', help='Location for output, default is CWD')
parser.add_argument('-t', '--type', choices=['s','e','j'], default='s', help='''Pick similarity calculation type:s
for difflib similarity, e for edit
distance, J for Jaccard''')
return parser.parse_args()
class Domain:
def __init__(self,score,domain):
self.score = score
self.domain = domain
def __repr__(self):
return repr((self.score,self.domain))
def jaccardTest(newDomain,listItem):
intersection_cardinality = len(set.intersection(*[set(newDomain),set(listItem)]))
union_cardinality = len(set.union(*[set(newDomain),set(listItem)]))
return intersection_cardinality/float(union_cardinality)
#Using example from http://dataconomy.com/2015/04/implementing-the-five-most-popular-similarity-measures-in-python/
def calculatePreviousDay():
today = datetime.datetime.utcnow().date()
yesterday = today - datetime.timedelta(days=1)
yesterdayDate = yesterday.strftime("%Y-%m-%d")
return yesterdayDate
def retrieveDomainList():
#Retrieve list of new domains
date = calculatePreviousDay()
domainlist = []
headers = { 'User-Agent': 'Threat Intelligence Research'}
dateValue = date + '.zip'
dateB64 = base64.b64encode(dateValue.encode('utf-8')).decode('utf-8')
#format: https://whoisds.com//whois-database/newly-registered-domains/YYYY-MM-DD.zip/nrd
url = 'https://whoisds.com/whois-database/newly-registered-domains/' + dateB64 + '/nrd'
#print(url)
try:
response = requests.get(url)
#print(str(response.content))
try:
with zipfile.ZipFile(io.BytesIO(response.content)) as zipresponse:
#print('opened zip')
for zipinfo in zipresponse.infolist():
#print('get list of subfiles')
with zipresponse.open(zipinfo) as thefile:
#print('open subfile')
for line in thefile:
#print(str(line))
item = line.decode('ascii')
domainlist.append(str(item).rstrip('\r\n'))
except:
print('Error in processing Zip')
except:
print("Error in retrieving response.")
return domainlist
def scoringFunction(args, dictionary, domains):
scoredList = []
for domain in domains:
item = domain.split('.')[0]
#print(item)
tempVal = 0.0
for record in dictionary:
#print(record)
if args != '':
if args == 's':
seqmatch = difflib.SequenceMatcher(None,item,record)
score = seqmatch.ratio()
#print(str(score))
elif args == 'e':
score = 100 - editdistance.eval(item,record)
#print(str(score))
elif args == 'j':
score = jaccardTest(item,record)
#print(str(score))
#print("Score: " + str(score))
if score > tempVal:
tempVal = score
if tempVal < 0.5:
pass
else:
domainRecord = Domain(tempVal,domain)
#print(str(domainRecord))
scoredList.append(domainRecord)
return scoredList
def openFileReturnAsList(fileLocation):
dictionaryList = []
try:
with open(fileLocation) as file:
for line in file:
#print(line)
dictionaryList.append(line.strip())
except:
print("Error opening file location: " + fileLocation)
return dictionaryList
if __name__ == '__main__':
parser = argumentParser()
#print('Selected function: ' + parser.type)
dictionaryList = openFileReturnAsList(parser.wordlist)
#print("Dictionary list loaded")
domainList = retrieveDomainList()
#print("Domain list retrieved")
scorelist = scoringFunction(parser.type, dictionaryList, domainList)
#print("Scoring function complete")
if parser.type == 's':
type = "similarity"
elif parser.type == 'e':
type = "editdistance"
elif parser.type == "j":
type = "jaccard"
sortScore = sorted(scorelist, key=lambda Domain: Domain.score, reverse=True)
fileName = parser.outputDirectory + 'newDomains_' + calculatePreviousDay() + '_' + type + '.txt'
with open(fileName,'w') as file:
for item in sortScore:
file.write(str(item)+'\n')