-
Notifications
You must be signed in to change notification settings - Fork 8
/
phishing.py
executable file
·311 lines (234 loc) · 9.54 KB
/
phishing.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
#!/usr/bin/env python3
# This file is part of Phishing Army.
#
# Phishing Army was made with ♥ by Andrea Draghetti
#
# This file may be licensed under the terms of of the
# GNU General Public License Version 3 (the ``GPL'').
#
import os
import sys
import gzip
import json
import Config
import logging
import requests
import tldextract
from datetime import datetime
timeout_connection = 120
header_desktop = {"User-Agent": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
"Accept-Language": "it,en-US;q=0.7,en;q=0.3"}
header_phishtank = {"User-Agent": "phishtank/phishingarmy",
"Accept-Language": "it,en-US;q=0.7,en;q=0.3"}
tldcache = tldextract.TLDExtract()
block_list = []
block_list_extended = []
# LOG initialization
logging.basicConfig(filename="phishing.log",
format="%(asctime)s - %(funcName)10s():%(lineno)s - %(levelname)s - %(message)s",
level=logging.INFO)
def parse_domain(url):
url = url.lower()
registered_domain = tldcache(url).registered_domain
sub_domain = tldcache(url).subdomain
# Remove punycode domain
registered_domain = registered_domain.encode("idna").decode("utf-8")
sub_domain = sub_domain.encode("idna").decode("utf-8")
if sub_domain:
full_domain = sub_domain + "." + registered_domain
else:
full_domain = registered_domain
if registered_domain and registered_domain not in white_list:
block_list.append(full_domain)
block_list_extended.append(full_domain)
if sub_domain == "www":
block_list_extended.append(registered_domain)
# This integration has been suspended to avoid false positives.
#
# if full_domain != registered_domain:
# block_list_extended.append(registered_domain)
# Download data from phishtank.com
def phishtank():
url_download = "https://data.phishtank.com/data/" + Config.phishtanktoken + "/online-valid.json.gz"
try:
r = requests.get(url_download, headers=header_phishtank, timeout=timeout_connection)
if r.status_code == 200:
with open("./list/online-valid.json.gz", "wb") as f:
f.write(r.content)
f = gzip.open("./list/online-valid.json.gz", "rb")
file_content = f.read()
f.close()
data = json.loads(file_content)
if data:
for each in data:
url = each["url"]
if url:
url = url.strip()
parse_domain(url)
except Exception as e:
logging.error(e, exc_info=True)
raise
# Download data from Urlscan.io
def urlscanio():
url_download = ["https://urlscan.io/api/v1/search/?q=task.tags:%22sinking-yachts%22",
"https://urlscan.io/api/v1/search/?q=task.tags:%22%23phishing%22"]
for url in url_download:
try:
r = requests.get(url, headers=header_desktop, timeout=timeout_connection)
if r.status_code == 200:
data = json.loads(r.text)
if data:
for each in data["results"]:
url = each["task"]["url"].lower()
if url:
url = url.strip()
parse_domain(url)
except Exception as e:
logging.error(e, exc_info=True)
raise
# Download data from OpenPhishing.com
def openphish():
url_download = "https://openphish.com/feed.txt"
try:
r = requests.get(url_download, headers=header_desktop, timeout=timeout_connection)
if r.status_code == 200:
for line in r.iter_lines(decode_unicode=True):
if line:
url = line.strip()
parse_domain(url)
except Exception as e:
logging.error(e, exc_info=True)
raise
# Download data from PhishFindR
def phishfindr():
url_list = [
"https://raw.githubusercontent.com/mitchellkrogza/Phishing.Database/master/phishing-links-ACTIVE-NOW.txt",
"https://raw.githubusercontent.com/mitchellkrogza/Phishing.Database/master/phishing-links-ACTIVE-TODAY.txt",
"https://raw.githubusercontent.com/mitchellkrogza/Phishing.Database/master/phishing-links-NEW-last-hour.txt",
"https://raw.githubusercontent.com/mitchellkrogza/Phishing.Database/master/phishing-links-NEW-today.txt",
"https://raw.githubusercontent.com/mitchellkrogza/Phishing.Database/master/phishing-links-ACTIVE.txt"]
for url_download in url_list:
try:
r = requests.get(url_download, headers=header_desktop, timeout=timeout_connection)
if r.status_code == 200:
for line in r.iter_lines(decode_unicode=True):
if line:
url = line.strip()
parse_domain(url)
except Exception as e:
logging.error(e, exc_info=True)
raise
# Download data from Cert.pl
def certpl():
url_download = "https://hole.cert.pl/domains/domains.txt"
try:
r = requests.get(url_download, headers=header_desktop, timeout=timeout_connection)
if r.status_code == 200:
for line in r.iter_lines(decode_unicode=True):
if line:
url = line.strip()
parse_domain(url)
except Exception as e:
logging.error(e, exc_info=True)
raise
# Download data from Phishunt.io
def phishuntio():
url_download = "https://phishunt.io/feed.txt"
try:
r = requests.get(url_download, headers=header_desktop, timeout=timeout_connection)
if r.status_code == 200:
for line in r.iter_lines(decode_unicode=True):
if line:
url = line.strip()
parse_domain(url)
except Exception as e:
logging.error(e, exc_info=True)
raise
# Load WhiteList
def whitelist():
# Set Global the Whitelist
global white_list
# List of whitelisted domain
white_list = []
# Load Gobal Whitelist
try:
f = open("./list/global_whitelist.txt", "r")
for line in f:
if line:
line = line.strip()
line = line.lower()
if not line.startswith("#"):
white_list.append(line)
f.close()
except Exception as e:
logging.error(e, exc_info=True)
pass
# Load Personal Whitelist
try:
f = open("./list/personal_whitelist.txt", "r")
for line in f:
if line:
line = line.strip()
line = line.lower()
if not line.startswith("#"):
white_list.append(line)
f.close()
except Exception as e:
logging.error(e, exc_info=True)
pass
def main():
# Whitelist loading
whitelist()
logging.info("Loading %s domain in white_list" % len(white_list))
# PhishTank loading
phishtank()
# OpenPhish loading
openphish()
# PhishFindR loading
# phishfindr()
# Cert.pl loading
certpl()
# Phishunt.io loading
phishuntio()
# Urlscan.io loading
urlscanio()
# Eliminate duplicates and sort the generated lists
block_list_sorted = sorted(set(block_list))
block_list_extended_sorted = sorted(set(block_list_extended))
logging.info("Generated the Blocklist containing %s domains" % len(block_list_sorted))
logging.info("Generated the Extended Blocklist containing %s domains" % len(block_list_extended_sorted))
banner = "# \n" \
"# Phishing Army | The Blocklist to filter Phishing \n" \
"# \n" \
"# Last Update: %s\n" \
"# \n" \
"# Project website: https://phishing.army \n" \
"# Support the project with a donation:: https://www.buymeacoffee.com/andreadraghetti \n" \
"# \n" \
"# This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. \n" \
"# ======================================================================================================\n" % datetime.utcnow().strftime(
"%a, %d %b %Y %H:%M:%S UTC")
banner_extended = "# \n" \
"# Phishing Army | The Blocklist to filter Phishing \n" \
"# \n" \
"# Last Update: %s\n" \
"# \n" \
"# This is the extended version, also contains domains without subdomains.\n" \
"# \n" \
"# Project website: https://phishing.army \n" \
"# Support the project with a donation:: https://www.buymeacoffee.com/andreadraghetti \n" \
"# \n" \
"# This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. \n" \
"# ======================================================================================================\n" % datetime.utcnow().strftime(
"%a, %d %b %Y %H:%M:%S UTC")
# Write the blocklist files
with open(Config.outputdirectory + "phishing_army_blocklist.txt", "w") as f:
f.write("%s\n" % banner)
for item in block_list_sorted:
f.write("%s\n" % item)
with open(Config.outputdirectory + "phishing_army_blocklist_extended.txt", "w") as f:
f.write("%s\n" % banner_extended)
for item in block_list_extended_sorted:
f.write("%s\n" % item)
if __name__ == "__main__":
main()