forked from MISP/misp-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapiosintds.py
More file actions
354 lines (289 loc) · 23.4 KB
/
apiosintds.py
File metadata and controls
354 lines (289 loc) · 23.4 KB
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
import json
import logging
import sys
import os
from apiosintDS import apiosintDS
log = logging.getLogger('apiosintDS')
log.setLevel(logging.DEBUG)
apiodbg = logging.StreamHandler(sys.stdout)
apiodbg.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
apiodbg.setFormatter(formatter)
log.addHandler(apiodbg)
misperrors = {'error': 'Error'}
mispattributes = {'input': ["domain", "domain|ip", "hostname", "ip-dst", "ip-src", "ip-dst|port", "ip-src|port", "url",
"md5", "sha1", "sha256", "filename|md5", "filename|sha1", "filename|sha256"],
'output': ["domain", "ip-dst", "url", "comment", "md5", "sha1", "sha256", "link", "text"]
}
moduleinfo = {'version': '0.2', 'author': 'Davide Baglieri aka davidonzo',
'description': 'On demand query API for OSINT.digitalside.it project.',
'module-type': ['expansion', 'hover']}
moduleconfig = ['STIX2_details', 'import_related', 'cache', 'cache_directory', 'cache_timeout_h', 'local_directory']
def handler(q=False):
if q is False:
return False
request = json.loads(q)
tosubmit = []
if request.get('domain'):
tosubmit.append(request['domain'])
elif request.get('domain|ip'):
tosubmit.append(request['domain|ip'].split('|')[0])
tosubmit.append(request['domain|ip'].split('|')[1])
elif request.get('hostname'):
tosubmit.append(request['hostname'])
elif request.get('ip-dst'):
tosubmit.append(request['ip-dst'])
elif request.get('ip-src'):
tosubmit.append(request['ip-src'])
elif request.get('ip-dst|port'):
tosubmit.append(request['ip-dst|port'].split('|')[0])
elif request.get('ip-src|port'):
tosubmit.append(request['ip-src|port'].split('|')[0])
elif request.get('url'):
tosubmit.append(request['url'])
elif request.get('md5'):
tosubmit.append(request['md5'])
elif request.get('sha1'):
tosubmit.append(request['sha1'])
elif request.get('sha256'):
tosubmit.append(request['sha256'])
elif request.get('filename|md5'):
tosubmit.append(request['filename|md5'].split('|')[1])
elif request.get('filename|sha1'):
tosubmit.append(request['filename|sha1'].split('|')[1])
elif request.get('filename|sha256'):
tosubmit.append(request['filename|sha256'].split('|')[1])
else:
return False
persistent = 0
if request.get('persistent'):
persistent = request["persistent"]
submitcache = False
submitcache_directory = False
submitcache_timeout = False
submit_stix = False
import_related = False
sumbit_localdirectory = False
r = {"results": []}
if request.get('config'):
if request['config'].get('cache') and request['config']['cache'].lower() == "yes":
submitcache = True
if request['config'].get('import_related') and request['config']['import_related'].lower() == "yes":
import_related = True
if request['config'].get('STIX2_details') and request['config']['STIX2_details'].lower() == "yes":
submit_stix = True
if request['config'].get('cache_timeout_h') and len(request['config']['cache_timeout_h']) > 0:
submitcache_timeout = int(request['config'].get('cache_timeout_h'))
localdirectory = request['config'].get('local_directory')
if localdirectory and len(localdirectory) > 0:
if os.access(localdirectory, os.R_OK):
sumbit_localdirectory = localdirectory
WarningMSG = "Local directory OK! Ignoring cache configuration..."
log.debug(str(WarningMSG))
submitcache = False
sumbitcache_titmeout = False
submitcache_directory = False
else:
ErrorMSG = "Unable to read local 'Threat-Intel' directory ("+localdirectory+"). Please, check your configuration and retry."
log.debug(str(ErrorMSG))
misperrors['error'] = ErrorMSG
return misperrors
if submitcache:
cache_directory = request['config'].get('cache_directory')
if cache_directory and len(cache_directory) > 0:
if os.access(cache_directory, os.W_OK):
submitcache_directory = cache_directory
else:
ErrorMSG = "Cache directory is not writable. Please fix it before."
log.debug(str(ErrorMSG))
misperrors['error'] = ErrorMSG
return misperrors
else:
ErrorMSG = "Value for Plugin.Enrichment_apiosintds_cache_directory is empty but cache option is enabled as recommended. Please set a writable cache directory in plugin settings."
log.debug(str(ErrorMSG))
misperrors['error'] = ErrorMSG
return misperrors
else:
if sumbit_localdirectory == False:
log.debug("Cache option is set to " + str(submitcache) + ". You are not using the internal cache system and this is NOT recommended!")
log.debug("Please, consider to turn on the cache setting it to 'Yes' and specifing a writable directory for the cache directory option.")
try:
response = apiosintDS.request(entities=tosubmit, stix=submit_stix, cache=submitcache, cachedirectory=submitcache_directory, cachetimeout=submitcache_timeout, verbose=True, localdirectory=sumbit_localdirectory)
r["results"] += apiosintParserHover(persistent, response, import_related, submit_stix)
except ValueError as e:
log.debug(str(e))
misperrors['error'] = str(e)
return r
def apiosintParserHover(ispersistent, response, import_related, stix):
apiosinttype = ['hash', 'ip', 'url', 'domain']
line = "##############################################"
linedot = "--------------------------------------------------------------------"
linedotty = "-------------------"
ret = []
retHover = []
if isinstance(response, dict):
for key in response:
if key in apiosinttype:
for item in response[key]["items"]:
if item["response"]:
comment = "IoC '"+item["item"] + "' found in OSINT.DigitaiSide.it repository. List file: "+response[key]["list"]["file"]+". List date: " + response[key]["list"]["date"]
commentH = "IoC '"+item["item"] + "' found in OSINT.DigitaiSide.it repository."
CommentHDate = "List file: "+response[key]["list"]["file"]+". Date list: " + response[key]["list"]["date"]
ret.append({"types": ["text"], "values": [comment]})
retHover.append({"types": ["text"], "values": [commentH]})
retHover.append({"types": ["text"], "values": [CommentHDate]})
retHover.append({"types": ["text"], "values": [line]})
if key in ["url", "hash"]:
if "hashes" in item:
headhash = "Hashes set"
retHover.append({"types": ["text"], "values": [headhash]})
if "md5" in item["hashes"].keys():
ret.append({"types": ["md5"], "values": [item["hashes"]["md5"]], "comment": "Related to: " + item["item"]})
strmd5 = "MD5: "+item["hashes"]["md5"]
retHover.append({"types": ["text"], "values": [strmd5]})
if "sha1" in item["hashes"].keys():
ret.append({"types": ["sha1"], "values": [item["hashes"]["sha1"]], "comment": "Related to: " + item["item"]})
strsha1 = "SHA1: "+item["hashes"]["sha1"]
retHover.append({"types": ["text"], "values": [strsha1]})
if "sha256" in item["hashes"].keys():
ret.append({"types": ["sha256"], "values": [item["hashes"]["sha256"]], "comment": "Related to: " + item["item"]})
strsha256 = "SHA256: "+item["hashes"]["sha256"]
retHover.append({"types": ["text"], "values": [strsha256]})
if "online_reports" in item:
headReports = "Online Reports (availability depends on retention)"
retHover.append({"types": ["text"], "values": [linedot]})
retHover.append({"types": ["text"], "values": [headReports]})
onlierepor = item["online_reports"]
ret.append({"category": "External analysis", "types": ["link"], "values": [onlierepor["MISP_EVENT"]], "comment": "MISP Event related to: " + item["item"]})
ret.append({"category": "External analysis", "types": ["link"], "values": [onlierepor["MISP_CSV"]], "comment": "MISP CSV related to: " + item["item"]})
ret.append({"category": "External analysis", "types": ["link"], "values": [onlierepor["OSINTDS_REPORT"]], "comment": "DigitalSide report related to: " + item["item"]})
ret.append({"category": "External analysis", "types": ["link"], "values": [onlierepor["STIX"]], "comment": "STIX2 report related to: " + item["item"]})
MISPEVENT = "MISP Event => "+onlierepor["MISP_EVENT"]
MISPCSV = "MISP CSV => "+onlierepor["MISP_CSV"]
OSINTDS = "DigitalSide report => "+onlierepor["OSINTDS_REPORT"]
STIX = "STIX report => "+onlierepor["STIX"]
retHover.append({"types": ["text"], "values": [MISPEVENT]})
retHover.append({"types": ["text"], "values": [MISPCSV]})
retHover.append({"types": ["text"], "values": [OSINTDS]})
retHover.append({"types": ["text"], "values": [STIX]})
if stix and onlierepor:
if "STIXDETAILS" in onlierepor:
retHover.append({"types": ["text"], "values": [linedot]})
headStix = "STIX2 report details"
stixobj = onlierepor["STIXDETAILS"]
stxdet = "TLP:"+stixobj["tlp"]+" | Observation: "+str(stixobj["number_observed"])+" | First seen: "+stixobj["first_observed"]+" | First seen: "+stixobj["last_observed"]
ret.append({"types": ["comment"], "values": [stxdet], "comment": "STIX2 details for: " + item["item"]})
retHover.append({"types": ["text"], "values": [headStix]})
retHover.append({"types": ["text"], "values": [stxdet]})
if stixobj["observed_time_frame"] != False:
obstf = "Observation time frame: "+str(stixobj["observed_time_frame"])
ret.append({"types": ["comment"], "values": [obstf], "comment": "STIX2 details for: " + item["item"]})
retHover.append({"types": ["text"], "values": [obstf]})
filename = stixobj["filename"]
ret.append({"category": "Payload delivery", "types": ["filename"], "values": [filename], "comment": "STIX2 details for: " + item["item"]})
Hovefilename = "Filename: "+filename
retHover.append({"types": ["text"], "values": [Hovefilename]})
filesize = stixobj["filesize"]
ret.append({"types": ["size-in-bytes"], "values": [filesize], "comment": "STIX2 details for: " + item["item"]})
Hovefilesize = "Filesize in bytes: "+str(filesize)
retHover.append({"types": ["text"], "values": [Hovefilesize]})
filetype = stixobj["mime_type"]
ret.append({"category": "Payload delivery", "types": ["mime-type"], "values": [filetype], "comment": "STIX2 details for: " + item["item"]})
Hovemime = "Filetype: "+filetype
retHover.append({"types": ["text"], "values": [Hovemime]})
if "virus_total" in stixobj:
if stixobj["virus_total"] != False:
VTratio = "VirusTotal Ratio: "+str(stixobj["virus_total"]["vt_detection_ratio"])
ret.append({"types": ["comment"], "values": [VTratio], "comment": "STIX2 details for: " + item["item"]})
retHover.append({"types": ["text"], "values": [VTratio]})
VTReport = str(stixobj["virus_total"]["vt_report"])
ret.append({"category": "External analysis", "types": ["link"], "values": [VTReport], "comment": "VirusTotal Report for: " + item["item"]})
if import_related:
if len(item["related_urls"]) > 0:
retHover.append({"types": ["text"], "values": [linedot]})
countRelated = "Related URLS count: "+str(len(item["related_urls"]))
retHover.append({"types": ["text"], "values": [countRelated]})
for urls in item["related_urls"]:
if isinstance(urls, dict):
itemToInclude = urls["url"]
ret.append({"types": ["url"], "values": [itemToInclude], "comment": "Download URL for "+urls["hashes"]["md5"]+". Related to: " + item["item"]})
retHover.append({"types": ["text"], "values": [linedot]})
relatedURL = "Related URL "+itemToInclude
retHover.append({"types": ["text"], "values": [relatedURL]})
if "hashes" in urls.keys():
if "md5" in urls["hashes"].keys():
ret.append({"types": ["md5"], "values": [urls["hashes"]["md5"]], "comment": "Related to: " + itemToInclude})
strmd5 = "MD5: "+urls["hashes"]["md5"]
retHover.append({"types": ["text"], "values": [strmd5]})
if "sha1" in urls["hashes"].keys():
ret.append({"types": ["sha1"], "values": [urls["hashes"]["sha1"]], "comment": "Related to: " + itemToInclude})
strsha1 = "SHA1: "+urls["hashes"]["sha1"]
retHover.append({"types": ["text"], "values": [strsha1]})
if "sha256" in urls["hashes"].keys():
ret.append({"types": ["sha256"], "values": [urls["hashes"]["sha256"]], "comment": "Related to: " + itemToInclude})
strsha256 = "SHA256: "+urls["hashes"]["sha256"]
retHover.append({"types": ["text"], "values": [strsha256]})
headReports = "Online Reports (availability depends on retention)"
retHover.append({"types": ["text"], "values": [linedotty]})
retHover.append({"types": ["text"], "values": [headReports]})
onlierepor = urls["online_reports"]
ret.append({"category": "External analysis", "types": ["link"], "values": [onlierepor["MISP_EVENT"]], "comment": "MISP Event related to: " + item["item"]})
ret.append({"category": "External analysis", "types": ["link"], "values": [onlierepor["MISP_CSV"]], "comment": "MISP CSV related to: " + item["item"]})
ret.append({"category": "External analysis", "types": ["link"], "values": [onlierepor["OSINTDS_REPORT"]], "comment": "DigitalSide report related to: " + item["item"]})
ret.append({"category": "External analysis", "types": ["link"], "values": [onlierepor["STIX"]], "comment": "STIX2 report related to: " + item["item"]})
MISPEVENT = "MISP Event => "+onlierepor["MISP_EVENT"]
MISPCSV = "MISP CSV => "+onlierepor["MISP_CSV"]
OSINTDS = "DigitalSide report => "+onlierepor["OSINTDS_REPORT"]
STIX = "STIX report => "+onlierepor["STIX"]
retHover.append({"types": ["text"], "values": [MISPEVENT]})
retHover.append({"types": ["text"], "values": [MISPCSV]})
retHover.append({"types": ["text"], "values": [OSINTDS]})
retHover.append({"types": ["text"], "values": [STIX]})
if stix and onlierepor:
if "STIXDETAILS" in onlierepor:
retHover.append({"types": ["text"], "values": [linedotty]})
headStix = "STIX2 report details"
stixobj = onlierepor["STIXDETAILS"]
stxdet = "TLP:"+stixobj["tlp"]+" | Observation: "+str(stixobj["number_observed"])+" | First seen: "+stixobj["first_observed"]+" | First seen: "+stixobj["last_observed"]
ret.append({"types": ["comment"], "values": [stxdet], "comment": "STIX2 details for: " + item["item"]})
retHover.append({"types": ["text"], "values": [headStix]})
retHover.append({"types": ["text"], "values": [stxdet]})
if stixobj["observed_time_frame"] != False:
obstf = "Observation time frame: "+str(stixobj["observed_time_frame"])
ret.append({"types": ["comment"], "values": [obstf], "comment": "STIX2 details for: " + item["item"]})
retHover.append({"types": ["text"], "values": [obstf]})
filename = stixobj["filename"]
ret.append({"category": "Payload delivery", "types": ["filename"], "values": [filename], "comment": "STIX2 details for: " + item["item"]})
Hovefilename = "Filename: "+filename
retHover.append({"types": ["text"], "values": [Hovefilename]})
filesize = stixobj["filesize"]
ret.append({"types": ["size-in-bytes"], "values": [filesize], "comment": "STIX2 details for: " + item["item"]})
Hovefilesize = "Filesize in bytes: "+str(filesize)
retHover.append({"types": ["text"], "values": [Hovefilesize]})
filetype = stixobj["mime_type"]
ret.append({"category": "Payload delivery", "types": ["mime-type"], "values": [filetype], "comment": "STIX2 details for: " + item["item"]})
Hovemime = "Filetype: "+filetype
retHover.append({"types": ["text"], "values": [Hovemime]})
if "virus_total" in stixobj:
if stixobj["virus_total"] != False:
VTratio = "VirusTotal Ratio: "+stixobj["virus_total"]["vt_detection_ratio"]
ret.append({"types": ["comment"], "values": [VTratio], "comment": "STIX2 details for: " + item["item"]})
retHover.append({"types": ["text"], "values": [VTratio]})
VTReport = stixobj["virus_total"]["vt_report"]
ret.append({"category": "External analysis", "types": ["link"], "values": [VTReport], "comment": "VirusTotal Report for: " + item["item"]})
else:
ret.append({"types": ["url"], "values": [urls], "comment": "Download URL for: " + item["item"]})
urlHover = "URL => "+urls
retHover.append({"types": ["text"], "values": [urlHover]})
else:
notfound = item["item"] + " IS NOT listed by OSINT.digitalside.it. Date list: " + response[key]["list"]["date"]
ret.append({"types": ["comment"], "values": [notfound]})
retHover.append({"types": ["comment"], "values": [notfound]})
if ispersistent == 0:
return ret
return retHover
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo