-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_core.py
423 lines (309 loc) · 13.1 KB
/
module_core.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import os
import sys
import json
import requests
import logging
import subprocess
import shlex
from util import sqlite_util
from util import ioc_extract, ioc_extract_expander
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(name)s:%(levelname)s:%(message)s')
log = logging.getLogger('module_core')
# Setup MISPERROR object
misperrors = {
'error':'Error'
}
# Setup MODULEINFO object
moduleinfo = {
'version': '0.1',
'author': 'CMU Capstone Project - Mattias Rosner, Reginald Savoy, Ryan Chalk, Sachit Malik, Zain Imran',
'description': '<TODO>',
'module-type': ['expansion']
}
# Setup MISPATTRIBUTES object
mispattributes = {
'input': ['action', 'url', 'recursive', 'md5', 'sha1', 'sha256', 'ipv4', 'malware_name'],
'output': ['text'],
}
# Setup MODULECONFIG object
moduleconfig = [
'custom_API'
]
def introspection():
"""
Introspection function which returns the input value required by the module, and the output format of the module
"""
return mispattributes
def version():
"""
Returns the metadata about the module (see moduleinfo above)
"""
moduleinfo['config'] = moduleconfig
return moduleinfo
def invoke_web_crawler(url):
"""
Invokes the webcrawler that scrapes the URLs and fetches the blog posts
Input:
None
Output:
returncode: integer
0: successful
1: otherwise
Raises Exception:
No
NOTE:
The dependencies of this submodule should be specified in either requirements.txt
or explicit instructions need to be given during the plugin installation.
"""
rc = 0
try:
path_cwd = os.path.abspath('.')
path_spider = os.path.join(path_cwd, 'web-crawler', 'infosecspider','spiders','scraper.py')
if not url:
command = f'scrapy runspider {path_spider}'
else:
command = f'scrapy runspider {path_spider} -a url={url}'
input_args = shlex.split(command)
# !NOTE!: Do not pass any input parameters to the below string, will result in code execution
try:
log.info('[+] Running the webcrawler module...')
completed_proc_instance = subprocess.run(input_args, check=True)
log.info('[+] Successfully scraped data from the URLs...')
except subprocess.CalledProcessError as e:
log.error('[-] Failed to run the web crawler.')
log.error(f'[--] Return code: {e.returncode}')
rc = 1
except:
rc = 1
return rc
def invoke_ioc_extract(output_path, time_duration):
"""
Invokes the IOC extractor submodule
Input:
The path containing the scraped data objects
Time duration to extract the IoCs from (within X hours from current time)
Output:
iocs_dict <dict>: Extracted IoCs
Raises Exception:
Yes/No
NOTE:
The dependencies of this submodule should be specified in either requirements.txt
or explicit instructions need to be given during the plugin installation.
"""
try:
iocs_dict = ioc_extract.initiate_ioc_extraction_main(path_outputs=output_path, view_scraping_within_last_hours=time_duration)
# Check if it is an empty dict or a None type object
if not iocs_dict:
# If so, set it as an empty dict object
iocs_dict = {}
except:
log.error('[-] Failed to run ioc extraction submodule on the scraped data...')
iocs_dict = {}
return iocs_dict
def invoke_web_scraper_recursive(ioc_extract_dict, article, ioc_category):
"""
Recursively calls the web scraper module
Searches the web for the found IoCs
Extracts the IoCs from the newly found articles
Input:
Extracted IoCs <dict>:
Dict of extracted IoCs from the initial scraped articles/reports
Article <str> :
Article to consider when recursively searching the web (only IoCs scraped from this article/report will be searched)
IoC Category <str>:
Category of IoC to consider (Ex: MD5, SHA1 etc.)
Output:
Enriched IoCs <dict>
Raises Exception on error:
No
"""
rc_rec = 0
try:
rc_rec = ioc_extract_expander.recursive_ioc_extractor_from_article_name_and_ioc__over_google_searches(dictionary=ioc_extract_dict,
article_lookup=article,
ioc=ioc_category,
num_google_results=10,
search_speed=3)
except:
rc_rec = 1
return rc_rec
def invoke_store_iocs_in_db(extracted_iocs, database):
"""
Stores the extracted IOCs into the local database
Input:
extracted_iocs: extracted IOCs in JSON/DICT format from ioc_extract submodule
database: path to the database
default: if no path is provided, defaults to local_ioc.db in the current directory
Output:
returncode: integer
0: successful
1: otherwise
Raises Exception:
No
"""
rc = 0
try:
if database:
rc = sqlite_util.store_in_local_ioc_db(extracted_iocs, database)
else:
rc = sqlite_util.store_in_local_ioc_db(extracted_iocs)
except:
rc = 1
return rc
def invoke_retrieve_from_local_ioc_db(input_dict, database):
"""
Given an input dict containing ioc_type and ioc_value to search in the database,
Finds and returns all the matching entries for each ioc_type and ioc_value so specified.
Input:
input_dict <dict>
Example:
{
'md5' : md5_value,
'sha256' : sha256_value,
'ipv4': ip_addr,
'malware_name': malwarename
}
Output:
response_dict <dict>
Example:
{
'md5:md5_value' : [[retreived_instance_1], [retreived_instance_2], ..., [retreived_instance_k]]
...
'error' : ['error_str_1', 'error_str_2', .... ,] # In case of any errors for each searched value
}
Raises Exception:
No
"""
response_dict = {}
response_dict['error'] = []
try:
for _ioc_type, _ioc_val in input_dict.items():
output_list, error = sqlite_util.retrieve_from_local_ioc_db(_ioc_type, _ioc_val, database)
if error != 'SUCCESS':
response_dict['error'].append(error)
continue
key = _ioc_type + ":" + _ioc_val
response_dict[key] = output_list
except:
response_dict['error'].append('Failed to retrieve the IoCs from Local DB...')
return response_dict
def handler(q=False):
"""
[*] CORE API Request Handler Function
"""
if q is False:
return False
_request = json.loads(q)
# input_param_list = ['action', 'url', 'recursive', 'md5', 'sha1', 'sha256', 'ipv4', 'malware_name']
valid_actions = ['scrape', 'get_from_db']
valid_iocs = ['md5', 'sha1', 'sha256', 'ipv4', 'malware_name']
valid_recursive_flags = [0, 1]
# Fetch the action so requested, if not a valid action, return the MISPERROR object with error value set
try:
action_requested = _request['action']
if action_requested not in valid_actions:
misperrors['error'] = 'Invalid action specified. Please select one from: scrape, get_from_db'
return misperrors['error']
except KeyError:
misperrors['error'] = 'No action specified. Please select one from: scrape, get_from_db'
return misperrors['error']
# If the user has requested for scraping, proceed as below
if action_requested == 'scrape':
# Identify the URL so supplied
try:
url = _request['url']
# If none, default to hardcoded sources
except KeyError:
log.info('[-] No URL was specified...')
log.info('[*] Defaulting to harcoded data sources...')
url = ''
# Identify if the recursive flag has been set (1)
try:
recursive = _request['recursive']
if recursive not in valid_recursive_flags:
log.info('[-] Invalid recursive value specified. Defaulting to 0.')
recursive = 0
# If not, set the flag as (0), default behavior would not involve recursion
except KeyError:
log.info('[-] No recursive value specified. Defaulting to 0.')
recursive = 0
# Scrape the URLs
rc_wc = invoke_web_crawler(url)
if rc_wc:
misperrors['error'] = 'Unable to scrape the URLs'
return misperrors['error']
# Extract IOCs
try:
extracted_iocs_dict_initial = invoke_ioc_extract('output/', 1)
except:
log.error('[-] Failed to retrieve IOCs from the scraped data...')
misperrors['error'] = 'Unable to retrieve IOCs from crawled webpages'
return misperrors['error']
global_response_dict = extracted_iocs_dict_initial
# Store extracted IOCs in the database
if extracted_iocs_dict_initial:
rc_db = invoke_store_iocs_in_db(extracted_iocs_dict_initial, database='local_ioc.db')
if rc_db:
log.error('[-] Failed to store the IOCs in the database...')
# Even if it fails to store, we would still like to alert the user
global_response_dict['error'] = 'Failed to store the retreived IOCs in the database, '
# Check if we need to recursively fetch the IoCs
if recursive:
# PLACEHOLDER
article = ''
# PLACEHOLDER
ioc_category = ''
# PLACEHOLDER, Run the recursive webcrawler
rc_rec = invoke_web_scraper_recursive(extracted_iocs_dict_initial, article, ioc_category)
# If not successful, return the response object
if rc_rec:
log.error('[-] Failed to run the recursive submodule...')
try:
global_response_dict['error'] += 'Failed to run the recursive submodule... '
except:
global_response_dict['error'] = 'Failed to run the recursive submodule... '
response = {'results': [{'types': mispattributes['output'], 'values': global_response_dict}]}
return response
# Try to fetch the IoCs
try:
extracted_iocs_dict = invoke_ioc_extract('outputs/', 1)
except:
log.error('[-] Failed to retrieve IOCs from the recursively scraped data...')
try:
global_response_dict['error'] += 'Failed to extract IoCs in the recursive submodule, '
except:
global_response_dict['error'] = 'Failed to extract IoCs in the recursive submodule, '
response = {'results': [{'types': mispattributes['output'], 'values': global_response_dict}]}
return response
# If successful and non-empty, store them in the DB
if extracted_iocs_dict:
# Only set the global dict to the newly extracted_iocs_dict if non-empty
# Else return the original (w/o recursion) dict
global_response_dict = extracted_iocs_dict
rc_db = invoke_store_iocs_in_db(extracted_iocs_dict, database='local_ioc.db')
if rc_db:
log.error('[-] Failed to store the IOCs in the database...')
# Even if it fails to store, we would still like to alert the user
global_response_dict['error'] = 'Failed to store the retreived IOCs in the database, '
# Setup the return object, that would be returned back to the user
response = {'results': [{'types': mispattributes['output'], 'values': global_response_dict}]}
elif action_requested == 'get_from_db':
log.info(f'[+] Action requested: {action_requested}')
input_ioc_dict = {}
for _valid_ioc in valid_iocs:
try:
_ioc_val = _request[_valid_ioc]
if _ioc_val:
input_ioc_dict[_valid_ioc] = _ioc_val
except:
continue
if not input_ioc_dict:
log.error('[-] No IoC type or no IoC value specified...')
misperrors['error'] = 'No IoC type or no IoC value specified...'
return misperrors
log.info('[+] Retreiving information from the database...')
response_dict = invoke_retrieve_from_local_ioc_db(input_ioc_dict, 'local_ioc.db')
log.info('[+] Sending response to the user...')
response = {'results': [{'types': mispattributes['output'], 'values': response_dict}]}
return response