-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFSISAC_STIX_Parser.py
523 lines (394 loc) · 17.8 KB
/
FSISAC_STIX_Parser.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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#!/usr/bin/env python
"""
FSISAC STIX Parser (FSISAC_STIX_Parser.py)
Written by Michael Lim ([email protected])
"""
import os
import sys
import socket
import types
import collections
import json
import re
import io
import urllib2
import dateutil
import datetime
import time
import pytz
import pprint
import getpass
import csv
import iocextract
import dicttoxml
import libtaxii as t
import libtaxii.messages_11 as tm11
import libtaxii.clients as tc
import lxml.etree
from stix.core import STIXPackage
from StringIO import StringIO
from urlparse import urlparse
from optparse import OptionParser
from optparse import BadOptionError
from optparse import AmbiguousOptionError
from stix.core import STIXPackage, STIXHeader
from stix.utils.parser import EntityParser
from stix.common import vocabs
from stix.common.vocabs import VocabString
from stix.common.vocabs import IndicatorType
from xml.etree.ElementTree import XML, XMLParser, tostring, TreeBuilder
class FSISAC_STIX_Parser:
def __init__(self):
pass
""" Extract observables from STIX. Used by extractObservables function """
def extractObservable(self, obs, values):
typ = obs["properties"]["xsi:type"]
val = None
if typ == "AddressObjectType":
# Handle if Address_Value is a plain string or one with datatype
if isinstance(obs["properties"]["address_value"], basestring):
val = obs["properties"]["address_value"]
elif 'value' in obs["properties"]["address_value"]:
val = obs["properties"]["address_value"]["value"]
elif typ == "URIObjectType" or typ == "DomainNameObjectType" or typ == "HostnameObjectType":
val = obs["properties"]["value"]
if 'value' in val:
val = obs["properties"]["value"]["value"]
else:
val = obs["properties"]["value"]
elif typ == "UserAccountObjectType":
val = obs["properties"]["username"]
elif typ == "FileObjectType":
val = []
theList = obs["properties"]["hashes"][0]
if len(theList['simple_hash_value']) > 2:
val.append( theList['simple_hash_value'] )
else:
val.append( obs["properties"]["hashes"][0]['simple_hash_value']['value'] )
if val:
if ( not isinstance(val, basestring) ) and isinstance(val, collections.Iterable):
for addr in val:
values.append( addr )
else:
values.append( val )
else:
if args[0].strict:
raise Exception("Encountered unsupported CybOX observable type: " + typ)
else:
print >> sys.stderr, "Encountered unsupported CybOX observable type: " + typ + ", ignoring..."
""" Extract observables from STIX """
def extractObservables(self, indicators):
values = []
STIX_TYPE = ""
for indicator in indicators:
# Check if we were passed a list of indicators, or observables
obs = indicator
# print("===========================")
# print("OBS:")
# pprint.pprint(obs)
# print("===========================")
# print("")
if "observable" in indicator:
obs = indicator["observable"]
### To handle FSISAC which put data in 'description' ###
IS_FSISAC = False
if "observable" in indicator:
tmp_obs = indicator['observable']
if 'idref' in tmp_obs:
if "fsisac" or "NCCIC" in tmp_obs['idref']:
IS_FSISAC = True
if IS_FSISAC == True:
STIX_TYPE = "type1"
# print "FOUND FSISAC"
#iocs = dict()
#title = "TESTING"
#iocs = {'title' : '', 'domain':[], 'ip':[], 'email':[], 'hash':[], 'url':[], 'hash':[], 'yara':[], 'other' : []}
title = indicator['title']
description = indicator["description"]
iocs = self.parse_indicators_from_description_string(description, title)
return (STIX_TYPE, iocs)
sys.exit(0)
#return iocs
else:
try:
STIX_TYPE = "other"
if 'object' in obs:
self.extractObservable(obs["object"], values)
elif 'observable_composition' in obs:
for observable in obs["observable_composition"]["observables"]:
if 'object' in observable:
self.extractObservable(observable["object"], values )
else:
print "EXCEPTION999"
print "-" * 100
print "INDICATOR:"
print indicator
print "-" * 100
raise Exception("Unknown Object Type!! Please Investigate")
# if IS_FSISAC == True:
# print "FOUND FSISAC"
# description = indicator["description"]
# title = indicator["title"]
# print "-" * 100
# print "INDICATOR:"
# print indicator
# print "-" * 100
# raise Exception("BYEBYEBYE")
# iocs = self.parse_indicators_from_description_string(description)
# iocs['title'] = title
# # return iocs
# else:
# raise Exception("Unknown Object Type!! Please Investigate")
except:
print >> sys.stderr, "Could not handle observable/indicator:\n"
pprint.pprint( indicator, sys.stderr )
raise
# print "=" * 100
# print "extractObservables - values:"
# print values
# print "=" * 100
return (STIX_TYPE, values)
# Processes a STIX package dictionary
def process_stix_dict(self, stix_dict):
iocs = {'title' : '', 'domain':[], 'ip':[], 'email':[], 'hash':[], 'url':[], 'hash':[], 'yara':[], 'other' : []}
result = []
key = ""
value = ""
""" Retrieve title """
try:
title = stix_dict['observables']['observables'][0]['title']
iocs['title'] = title
except:
# Do something if necessary
pass
if "observables" in stix_dict:
result.extend(self.extractObservables(stix_dict["observables"]["observables"]))
if "indicators" in stix_dict:
result.extend(self.extractObservables(stix_dict["indicators"]))
# print "=" * 100
# print "VALUES2"
# print result
# print "=" * 100
stix_type = result[0]
if stix_type == "type1": # No need to process, already in IOC dict format
return result[1]
values = result[1]
if len(values) > 0:
for item in values:
try:
## send data to stdout if needed and/or save to a simple text file.
if re.match("^(http|https)", item):
u = urlparse(item)
# print 'Web Site: %s | Path: %s' % ( u.netloc, u.path )
iocs['url'].append(u.netloc)
elif re.match("[^@]+@[^@]+\.[^@]+", item ):
# print 'Email Address: %s' % ( item )
iocs['email'].append(item)
elif re.match("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", item):
# print 'IP Address: %s' % ( item )
iocs['ip'].append(item)
elif re.match("^:", item):
item = item[2:]
myitem = 'http://' + item
d = urlparse(myitem)
item = d.netloc
# print 'Domain: %s' % ( d.netloc )
iocs['domain'].append(d.netloc)
# elif re.match("^(([a-z0-9]\-*[a-z0-9]*){1,63}\.){1,255}$", item):
# # print 'Domain: %s' % ( item )
# iocs['domain'].append(item)
elif re.match("^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}$", item):
data = item.split(":")
#print data
# print 'IP Address: %s | Dest Port: %s' % ( data[0], data[1] )
iocs['ip'].append(data[0])
elif re.match(r"^([a-fA-F\d]{32})$", item):
# print 'Hash: %s' % ( item )
iocs['hash'].append(item)
elif re.match(r"^([a-fA-F\d]{40})$", item):
# print 'Hash: %s' % ( item )
iocs['hash'].append(item)
elif re.match(r"^([a-fA-F\d]{64})$", item):
# print 'Hash: %s' % ( item )
iocs['hash'].append(item)
else:
# print 'Indicator: %s' % ( item )
iocs['other'].append(item)
except ValueError:
print >> sys.stderr, "Could not parse values.."
print >> sys.stderr, item
raise
# print "END" * 100
# print iocs
# print "END" * 100
return iocs
""" Extract IOC(s) from the DESCRIPTION string (string type) """
def parse_indicators_from_description_string(self, description_string, title):
# print type(description_string)
iocs = {'title' : title, 'domain':[], 'ip':[], 'email':[], 'hash':[], 'url':[], 'hash':[], 'yara':[], 'other' : []}
on9strings = {'[.]':'.', 'hxxp':'http', '[@]':'@'}
# Convert the first STIXPackage dictionary into another STIXPackage via the from_dict() method.
# Pattern for domain / email and IP addresses
raw_iocs = re.findall(r'[a-zA-Z0-9-\.]*\[\.?\@?\][a-zA-Z0-9-\.\[\.\@\]]*[-a-zA-Z0-9@:%_\+.~#?&//=]*', description_string)
# print(len(raw_iocs))
# for i in range(len(raw_iocs)):
# # Replace the on9 strings
# for on9string in on9strings:
# raw_iocs[i] = raw_iocs[i].replace(on9string, on9strings[on9string])
# # Import those IOCs into the array.
# if re.match(r'.*[@]+', raw_iocs[i]):
# iocs['email'].append(raw_iocs[i])
# elif re.match(r'.*[//].*', raw_iocs[i]):
# iocs['url'].append(raw_iocs[i])
# elif re.match(r'.*[a-zA-Z]', raw_iocs[i]):
# iocs['domain'].append(raw_iocs[i])
# # Extract hashes by their plugin
# for hash_extracted in iocextract.extract_hashes(description_string):
# iocs['hash'].append(hash_extracted)
# # Extract Yara rule
# for yara_extracted in iocextract.extract_yara_rules(description_string):
# iocs['yara'].append(yara_extracted)
# # Extract IP
# for ip_extracted in iocextract.extract_ips(description_string, refang=True):
# iocs['ip'].append(ip_extracted)
for i in range(len(raw_iocs)):
# Replace the on9 strings
for on9string in on9strings:
raw_iocs[i] = raw_iocs[i].replace(on9string, on9strings[on9string])
# Import those IOCs into the array.
if re.match(r'.*[@]+', raw_iocs[i]):
iocs['email'].append(raw_iocs[i])
iocs['email'] = list(set(iocs['email']))
elif re.match(r'.*[//].*', raw_iocs[i]):
iocs['url'].append(raw_iocs[i])
iocs['url'] = list(set(iocs['url']))
elif re.match(r'.*[a-zA-Z]', raw_iocs[i]):
if re.match("^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$", raw_iocs[i]):
iocs['domain'].append(raw_iocs[i])
iocs['domain'] = list(set(iocs['domain']))
# Extract hashes by their plugin
for hash_extracted in iocextract.extract_hashes(description_string):
iocs['hash'].append(hash_extracted)
iocs['hash'] = list(set(iocs['hash']))
# Extract Yara rule
for yara_extracted in iocextract.extract_yara_rules(description_string):
iocs['yara'].append(yara_extracted)
iocs['yara'] = list(set(iocs['yara']))
# Extract IP
for ip_extracted in iocextract.extract_ips(description_string, refang=True):
# Use regex to validate the IP format
if re.match(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", ip_extracted):
iocs['ip'].append(ip_extracted)
iocs['ip'] = list(set(iocs['ip']))
# for key in iocs:
# for item in iocs[key]:
# print(key + ":" + item)
return iocs
""" Extract IOC(s) from the DESCRIPTION section in FSISAC Stix """
def _parse_indicators_from_stix_description(self, xml_content):
iocs = {'title' : '', 'domain':[], 'ip':[], 'email':[], 'hash':[], 'url':[], 'hash':[], 'yara':[], 'other' : []}
on9strings = {'[.]':'.', 'hxxp':'http', '[@]':'@'}
# Parse input file
stix_package = STIXPackage.from_xml(xml_content)
# Convert STIXPackage to a Python
stix_dict = stix_package.to_dict()
# Extract description from the indicator (suitable for indicator only)
# print "-" * 100
# print stix_dict
# print "-" * 100
description = stix_dict["indicators"][0]["description"]
# Extract title
title = stix_dict["indicators"][0]["title"]
iocs['title'] = [title]
# Convert the first STIXPackage dictionary into another STIXPackage via the from_dict() method.
# Pattern for domain / email and IP addresses
raw_iocs = re.findall(r'[a-zA-Z0-9-\.]*\[\.?\@?\][a-zA-Z0-9-\.\[\.\@\]]*[-a-zA-Z0-9@:%_\+.~#?&//=]*', description)
# print(len(raw_iocs))
for i in range(len(raw_iocs)):
# Replace the on9 strings
for on9string in on9strings:
raw_iocs[i] = raw_iocs[i].replace(on9string, on9strings[on9string])
# Import those IOCs into the array.
if re.match(r'.*[@]+', raw_iocs[i]):
iocs['email'].append(raw_iocs[i])
iocs['email'] = list(set(iocs['email']))
elif re.match(r'.*[//].*', raw_iocs[i]):
iocs['url'].append(raw_iocs[i])
iocs['url'] = list(set(iocs['url']))
elif re.match(r'.*[a-zA-Z]', raw_iocs[i]):
if re.match("^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$", raw_iocs[i]):
iocs['domain'].append(raw_iocs[i])
iocs['domain'] = list(set(iocs['domain']))
# Extract hashes by their plugin
for hash_extracted in iocextract.extract_hashes(description):
iocs['hash'].append(hash_extracted)
iocs['hash'] = list(set(iocs['hash']))
# Extract Yara rule
for yara_extracted in iocextract.extract_yara_rules(description):
iocs['yara'].append(yara_extracted)
iocs['yara'] = list(set(iocs['yara']))
# Extract IP
for ip_extracted in iocextract.extract_ips(description, refang=True):
# Use regex to validate the IP format
if re.match(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", ip_extracted):
iocs['ip'].append(ip_extracted)
iocs['ip'] = list(set(iocs['ip']))
# for key in iocs:
# for item in iocs[key]:
# print(key + ":" + item)
return iocs
""" Convert iocs dict to JSON """
def convert_to_json(self, iocs):
result = {}
# Get title first
title = ""
for ioc in iocs:
if ioc == "title":
try:
title = iocs[ioc]
except:
pass
l = []
for ioc in iocs:
if ioc != "title":
for item in iocs[ioc]:
new_dict_item = dict()
new_dict_item['title'] = title
new_dict_item['type'] = ioc
new_dict_item['value'] = item
l.append(new_dict_item)
result = json.dumps({'IOCS' : l})
return result
""" Parse by stix file """
def parse_stix_file(self, filename):
stix_package = STIXPackage.from_xml(filename)
stixParser = FSISAC_STIX_Parser()
iocs = stixParser.process_stix_dict(stix_package.to_dict())
j = stixParser.convert_to_json(iocs)
return j
def test():
# Process a XML file on disk
stix_package = STIXPackage.from_xml(sys.argv[1])
stixParser = FSISAC_STIX_Parser()
iocs = stixParser.process_stix_dict(stix_package.to_dict())
j = stixParser.convert_to_json(iocs)
print j
# def test2():
# content = open(sys.argv[1]).read()
# sio = StringIO(content)
# stixParser = FSISAC_STIX_Parser()
# iocs = stixParser._parse_indicators_from_stix_description(sio)
# j = stixParser.convert_to_json(iocs)
# parsed = json.loads(j)
# print(json.dumps(parsed, indent=4, sort_keys=True))
# def test3():
# from glob import glob
# stixParser = FSISAC_STIX_Parser()
# stix_files = glob("stix_files/*.xml")
# for s in stix_files:
# print "Processing file...(%s)" % s
# r = stixParser.parse_stix_file(s)
# print (r)
# print ""
# if __name__ == "__main__":
# test()