-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollect_kits.py
352 lines (307 loc) · 12.7 KB
/
collect_kits.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
#!/usr/bin/env python3
import hashlib
import logging
import requests
import socket
import json
import os
from datetime import datetime
from urllib.parse import urlparse, urljoin
from queue import Queue
from bs4 import BeautifulSoup
from multiprocessing import Pool
'''
collect_kits was derived from Jordan Wright <[email protected]>'s https://github.com/duo-labs/phish-collect. This file is a modified version of his
collector.py
'''
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO,
handlers=[logging.FileHandler("collector.log"), logging.StreamHandler()])
logging.getLogger("requests").setLevel(logging.WARNING)
logging.getLogger("urllib3").setLevel(logging.WARNING)
BLACKLIST = []
class Collector(object):
''' A class that handles collecting phishing sites '''
def __init__(self):
''' Creates a new instance of the collector'''
self.session = requests.Session()
self.session.verify = False
self.session.headers.update({
'User-Agent':
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.98 Safari/537.36'
})
self.config = {
'kit_directory': 'kits',
'html_directory': 'html',
'visits_directory': 'visits',
'max_links_per_directory': 100
}
try:
with open('wordlist.txt', 'r') as fout:
self.wordlist = fout.read().split('\n')
except:
self.wordlist = []
def collect(self, url):
''' Collects the data associated with a phishkit '''
try:
with open('kits_processed', 'r') as kits_json:
kits = json.load(kits_json)
except:
logging.error('Error loading urls json')
kits = []
try:
with open('urls_processed', 'r') as urls_json:
urls_processed = json.load(urls_json)
except:
logging.error('Error loading urls json')
urls_processed = []
try:
parts = urlparse(url)
if parts.netloc in BLACKLIST:
raise Exception('Sample URL is blacklisted from analysis.')
for u in urls_processed:
if url == u['url']:
logging.info('URL already processed: {}'.format(url))
return False
url_ex = {}
url_ex['url'] = url
url_ex['status'], url_ex['html'] = self.collect_html(url, parts.netloc)
url_ex['ip'] = self.lookup_ip(url)
url_ex['url_sha1'] = hashlib.sha1(url.encode("utf-8")).hexdigest()
kits += self.collect_kits(url_ex)
urls_processed.append(url_ex)
with open('kits_processed', 'w') as kout:
json.dump(kits, kout)
with open('urls_processed', 'w') as uout:
json.dump(urls_processed, uout)
return True
except Exception as e:
logging.exception('Error in collect')
# Give a reasonable error status
return True
def lookup_ip(self, url):
'''
Returns the IP address the URL resolves to.
'''
try:
parts = urlparse(url)
return socket.gethostbyname(parts.netloc)
except Exception:
return None
def download_kit(self, url, url_ex):
'''
Attempts to fetch a file at the current URL
'''
kit = None
try:
response = self.session.get(url, stream=True, verify=False, timeout=5)
if not response.ok:
logging.info('Invalid response for zip URL: {} : {}'.format(url, str(response.status_code)))
return kit
# Shoutout to everyone who doesn't know how to status code
if 'text/html' in response.headers.get('Content-Type'):
return kit
filename = url.split('/')[-1]
filepath = '{}/{}-{}'.format(self.config['kit_directory'], url_ex['url_sha1'], filename)
filesize = 0
kit_hash = hashlib.sha1()
first = True
with open(filepath, 'wb') as kit_file:
for chunk in response.iter_content(chunk_size=1024):
if chunk:
if first and not chunk.startswith(b'PK'):
logging.info('Invalid zip file, aborting download')
return kit
first = False
kit_hash.update(chunk)
kit_file.write(chunk)
filesize += len(chunk)
logging.info('Found kit for {}'.format(url))
kit = {
'url': url,
'source_url': url_ex['url'],
'source_url_hash': url_ex['url_sha1'],
'source_url_status': url_ex['status'],
'source_url_html_hash': url_ex['html'],
'source_url_hash': url_ex['url_sha1'],
'source_url_ip': url_ex['ip'],
'filepath': filepath,
'filename': filename,
'filesize': filesize,
'hash': kit_hash.hexdigest()}
except Exception as e:
logging.error('error for {} : {}'.format(url, e))
return kit
def indexing_enabled(self, url):
'''
Fetches the requested URL and determined if indexing is enabled.
If it is, we return the links found
'''
links = []
response = self.session.get(url, verify=False, timeout=5)
if not response.ok:
return links
soup = BeautifulSoup(response.text, 'html.parser')
if 'Index of' not in response.text:
return links
# Get all the links
for a in soup.find_all('a'):
if 'Parent Directory' in a.text:
continue
href = a['href']
if href and href[0] == '?':
continue
# TODO: Normalize this url to support only relative urls
links.append(urljoin(url, href))
return links
def collect_kits(self, url_ex):
'''
Crawls the site looking for open directories or zip files
left available to the public, and looks for visits files
'''
url = url_ex['url']
queue = Queue()
parts = urlparse(url)
paths = parts.path.split('/')[1:]
url_hash = url_ex['url_sha1']
kit_urls = []
crawled = []
kits = []
#fix paths
if paths == ['']:
paths = []
#add subdomains, domain to wordlist
thewordlist = list(self.wordlist)
thewordlist.append(parts.netloc)
for sub in parts.netloc.split('.'):
if len(sub) < 4: #exlude www, com, org, ner, etc
continue
thewordlist.append(sub)
#enumerate wordlist and add to crawled queue against root domain
for w in thewordlist:
phish_url = '{}://{}/{}/'.format(parts.scheme, parts.netloc, w)
crawled.append(phish_url)
logging.info('Added url to crawled: {}'.format(phish_url))
# Add the initial paths to our queue, including filename without extension
logging.info('collect_kits found the following paths: {}'.format(paths))
for i in range(0, len(paths)):
pathing, fext = os.path.splitext('/'.join(paths[:len(paths) - i]))
phish_url = '{}://{}/{}/'.format(parts.scheme, parts.netloc, pathing)
queue.put(phish_url)
crawled.append(phish_url)
logging.info('Added url to queue and crawled: {}'.format(phish_url))
# Try to get the ZIP by looking for open directories - if we find other sub-
# directories in an open index, add those to the queue.
while not queue.empty():
phish_url = queue.get()
logging.info('Checking for open directory at: {}'.format(phish_url))
links = self.indexing_enabled(phish_url)
if not links:
continue
directory_links = 0
for link in links:
if link in crawled:
continue
if link.endswith('.zip'):
kit = self.download_kit(link, url_ex)
if kit:
kits.append(kit)
kit_urls.append(link)
continue
if link[-1] == '/':
# Short circuit if this directory is huge - won't stop us from finding
# a kit if it's in the same directory
directory_links += 1
if directory_links > self.config['max_links_per_directory']:
continue
logging.info('Adding URL to Queue: {}'.format(link))
queue.put(link)
crawled.append(link)
for phish_url in crawled:
#check for visitors/visits file
names = ['res', 'results', 'vu', 'visits', 'visitors', 'rezults', 'result']
for name in names:
visit_url = '{}{}.txt'.format(phish_url, name)
visit_status, visit_hash = self.collect_html(visit_url, parts.netloc, True)
if visit_hash:
logging.info('Found Visits file: {} status: {} hash: {}'.format(visit_url, visit_status, visit_hash))
# Remove the trailing slash and add .zip
phish_url = '{}.zip'.format(phish_url[:-1])
if phish_url in kit_urls:
logging.info(
'Skipping URL since the kit was already downloaded: {}'.
format(phish_url))
continue
logging.info('Fetching kit by zip {}'.format(phish_url))
kit = self.download_kit(phish_url, url_ex)
if kit:
kits.append(kit)
return kits
def collect_html(self, url, dom, visits=False):
'''
Fetches the HTML of a phishing page
'''
logging.info('Fetching {}'.format(url))
status_code = None
content_hash = None
content = None
try:
response = self.session.get(url, verify=False, timeout=3)
if response.ok:
content = response.text
content_hash = hashlib.sha1(content.encode("utf-8")).hexdigest()
status_code = response.status_code
else:
logging.info('Unsuccessful response for sample: {} : {}'.format(url, content))
except Exception:
logging.error('Invalid response for sample: {}'.format(url))
try:
if content:
if visits:
extension = 'txt'
if 'DOCTYPE' in content:
return None, None
logging.info('Writing visits {}'.format(url))
directory = self.config['visits_directory']
else:
extension = 'html'
logging.info('Writing html {}'.format(url))
directory = self.config['html_directory']
filepath = '{}/{}_{}.{}'.format(directory, dom, content_hash, extension)
with open(filepath, 'w') as html_f:
html_f.write(content)
except Exception:
logging.error('Unsuccessful writing html: {}'.format(url))
return status_code, content_hash
def process_sample(url):
c = Collector()
try:
logging.info('Processing sample: {}'.format(url))
c.collect(url)
except Exception as e:
logging.error('Error processing sample: {}: {}'.format(url.encode('utf-8'), e))
def main():
logging.info('---------------------------------------')
logging.info('Report for timestamp: {}'.format(datetime.now()))
logging.info('---------------------------------------')
pool = Pool(8)
urls = []
try:
payload = {'q': 'task.source:phishtank OR task.source:openphish OR task.source:certstream-suspicious', 'size': 1000}
r = requests.get(url='https://urlscan.io/api/v1/search/', params=payload, allow_redirects=True, timeout=(5, 12))
data = r.json()
except requests.exceptions.ConnectTimeout as e:
logging.error("Error while connecting to urlscan.io: {}".format(e))
except Exception as e:
logging.error("Urlscan connection error: {}".format(e))
for item in data.get('results', []):
u = item.get('page', {}).get('url')
if u:
process_sample(u)
urls.append(u)
#pool.map(process_sample, urls)
#pool.close()
#pool.join()
if __name__ == '__main__':
main()