forked from phishfort/phishfort-lists
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_new_file.py
286 lines (256 loc) · 9.17 KB
/
process_new_file.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
import requests
import json
import os
from shutil import copyfile
import argparse
import sys
#from settings import urlscanapikey
import time
from pprint import pformat
from tldextract import tldextract
from datetime import datetime
def urlscan_screenshot(url):
#First, let's see if it's been indexed already.
domain = tldextract.extract(url).fqdn
print("\t\t[-] Looking up domain screenshot {}".format(domain))
if lookup_screenshot(url):
print ("\t\t[-] Already screenshotted...")
return
else:
print ("\t\t[-] No screenshot yet, sending to urlscan...")
urlscanio = "https://urlscan.io/api/v1/scan/"
key = urlscanapikey
headers = {
"Content-Type" : "application/json",
"API-Key" : key
}
data = {
"url" : url,
"public" : "on"
}
try:
r = requests.post(urlscanio, headers=headers, json=data)
save_screenshot(url, r.json()['result'])
except Exception as e:
print (e)
pass
time.sleep(2)
def fetch_screenshots():
if os.path.exists("screenshots/mapping.json"):
f = open("screenshots/mapping.json", 'r')
current = f.read()
f.close()
current = json.loads(current)
return current
else:
return False
def lookup_screenshot(url):
domain = tldextract.extract(url).fqdn
if os.path.exists("screenshots/mapping.json"):
f = open("screenshots/mapping.json", 'r')
current = f.read()
f.close()
current = json.loads(current)
return current.get(domain, False)
else:
return False
def save_screenshot(url, urlscanurl):
domain = tldextract.extract(url).fqdn
if os.path.exists("screenshots/mapping.json"):
f = open("screenshots/mapping.json", 'r')
current = f.read()
f.close()
current = json.loads(current)
current[domain] = urlscanurl
f = open("screenshots/mapping.json", 'w')
current = f.write(json.dumps(current))
f.close()
else:
current = {domain : urlscanurl}
f = open("screenshots/mapping.json", 'w')
current = f.write(json.dumps(current))
f.close()
def ensure_that_domain_file_exists_and_is_valid_json():
'''
Make sure that we pass EAL nicely formatted data and that the file exists.
'''
file_exists = os.path.exists("blacklists/domains.json")
if not file_exists:
create_file = open("blacklists/domains.json", 'w')
create_file.close()
try:
domains = open("blacklists/domains.json", 'r')
contents = domains.read()
domains.close()
json_contents = json.loads(contents)
return json_contents
except:
#Create a backup of the current domains file, then place and empty
#json file there.
print ('''
The domains file was not json compliant, making a backup and then
replacing the domain file with empty json array.
''')
copyfile("blacklists/domains.json", "blacklists/domains.json.bak")
create_file = open("blacklists/domains.json", 'w')
create_file.write("[]\n")
create_file.close()
return False
def preprocess_domain(domain):
'''
Tidy up the domain for processing.
:param domain: domain to clean
:return: cleaned up domain
'''
#Lets ensure that it's in an nice string format for idna
try:
domain = domain.strip("*.").strip(".").strip().lower()
if domain.startswith("www."):
domain = domain[4:]
domain = domain.encode("idna").decode("utf-8")
except Exception as e:
print (e)
print ("[x] Error converting domain {} to idna, skipping adding...".format(domain))
return False
return domain
def push_changes_to_eal():
os.system("git add blacklists/domains.json ")
os.system("git add screenshots/mapping.json ")
os.system("git commit -m \"updated blacklist\"")
os.system("git push")
def prepare_pull_metamask(phishfort_blacklist):
blacklist = []
whitelist = get_existing_whitelist()
try:
r3 = requests.get("https://raw.githubusercontent.com/MetaMask/eth-phishing-detect/master/src/config.json")
blacklist = blacklist + r3.json()['blacklist']
except Exception as e:
print (e)
print ("Error parsing metamask blacklist")
return False
final = []
description = ""
screenshots = fetch_screenshots()
for entry in phishfort_blacklist:
if entry in blacklist or entry in whitelist:
continue
final.append(entry)
print(entry)
description+="{}: {} added on {}\n".format(entry, screenshots.get(entry, "*not available*"), str(datetime.now()))
if len(final) > 0:
final.sort()
print(final)
input()
f = open("metamask_pull.txt", 'w')
f.write(pformat(final))#.replace("'", '"'))
f.write("\n**********\n")
f.write(description)
f.close()
print("[+] Wrote metamask pr to metamask_pull.txt")
def get_existing_blacklists():
'''
Fetch the existing EAL blacklist and ensure that we do not duplicate entries.
'''
blacklist = []
try:
r = requests.get("https://api.infura.io/v2/blacklist")
blacklist = r.json()['blacklist']
print(r.json())
input()
except:
print ("[x] Error fetching blacklist, please ensure that you are able to reach the Infura endpoint.")
return False
try:
r2 = requests.get("https://api.cryptoscamdb.org/v1/blacklist")
print (r2.json().keys())
blacklist = blacklist + r2.json()['result']
print(r2.json())
input()
except Exception as e:
print(e)
print ("[x] Error fetching blacklist, please ensure that you are able to reach the Etherscam endpoint.")
return False
try:
r3 = requests.get("https://raw.githubusercontent.com/MetaMask/eth-phishing-detect/master/src/config.json")
blacklist = blacklist + r3.json()['blacklist']
print(r3.json())
input()
except Exception as e:
print (e)
print ("Error parsing metamask blacklist")
return False
return set(blacklist)
def extend_json_array_file(filename, contents):
f = open(filename, 'r')
old_contents = f.read()
f.close()
array = set(json.loads(old_contents) + list(contents))
f = open(filename, 'w')
f.write(json.dumps(list(array), indent=4, sort_keys=True))
f.close()
def extend_json_dict_file(filename, contents):
try:
f = open(filename, 'r')
old_contents = f.read()
f.close()
except:
old_contents = "{}"
combined_dict = {**json.loads(old_contents), **contents}
f = open(filename, 'w')
f.write(json.dumps(combined_dict,indent=4, sort_keys=True))
f.close()
def get_existing_whitelist():
r = requests.get("https://api.cryptoscamdb.org/v1/whitelist")
whitelist = r.json()['result']
r3 = requests.get("https://raw.githubusercontent.com/MetaMask/eth-phishing-detect/master/src/config.json")
whitelist = whitelist + r3.json()['whitelist']
return whitelist
def load_file():
ensure_that_domain_file_exists_and_is_valid_json()
f = open(args.blacklist_file, 'r')
contents = f.readlines()
f.close()
if len(contents) == 0:
print ("[x] Error - the blacklist file provided is empty")
sys.exit()
blacklist = get_existing_blacklists()
print(blacklist)
print ("[+] Currently {} entries in EAL blacklist...".format(len(blacklist)))
if not blacklist:
sys.exit()
#New entries set
new_entries = set()
#Let's store an internal record of when we reported this. We can also used
#this date to clean out old domains from the blacklist.
internal_record = {}
#Unfiltered....
unfiltered_blacklist = set()
whitelist = set(get_existing_whitelist())
#Loop through each of the entries
for entry in contents:
clean_entry = preprocess_domain(entry)
if not clean_entry:
continue
if entry in whitelist:
print ("\t[-] Found {} but it is whitelisted.")
continue
unfiltered_blacklist.add(clean_entry)
if entry in blacklist:
print ("\t[-] Found {} but it is already present in the blacklist.")
continue
print ("\t[+] Added {}".format(clean_entry))
new_entries.add(clean_entry)
#urlscan_screenshot(clean_entry)
internal_record[clean_entry] = str(time.time())
print ("[+] Found {} new entries...".format(len(new_entries)))
extend_json_array_file("blacklists/domains.json", new_entries)
extend_json_dict_file("internal_domain_tracking.json", internal_record)
ensure_that_domain_file_exists_and_is_valid_json()
prepare_pull_metamask(unfiltered_blacklist)
parser = argparse.ArgumentParser(description='Tool for storing and commiting blacklist to git.')
parser.add_argument('--blacklist-file', metavar='N',type=str,nargs="?", const="blacklist", required=True,
help='The new blacklist file to add')
args = parser.parse_args()
if __name__ == "__main__":
load_file()
push_changes_to_eal()