2018-10-28 13:01:26 +01:00
|
|
|
#!/usr/bin/env python3
|
2016-03-01 15:32:58 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import os
|
2019-11-20 12:50:22 +01:00
|
|
|
from pymisp import ExpandedPyMISP
|
2021-11-17 12:38:25 +01:00
|
|
|
from settings import url, key, ssl, outputdir, filters, valid_attribute_distribution_levels
|
2021-11-29 15:54:34 +01:00
|
|
|
try:
|
|
|
|
from settings import with_distribution
|
2021-11-29 16:16:54 +01:00
|
|
|
except ImportError:
|
2021-11-29 15:54:34 +01:00
|
|
|
with_distribution = False
|
2016-06-16 06:48:40 +02:00
|
|
|
|
2022-01-19 22:30:30 +01:00
|
|
|
try:
|
|
|
|
from settings import with_local_tags
|
|
|
|
except ImportError:
|
|
|
|
with_local_tags = True
|
|
|
|
|
2020-06-19 13:41:58 +02:00
|
|
|
try:
|
|
|
|
from settings import include_deleted
|
|
|
|
except ImportError:
|
|
|
|
include_deleted = False
|
|
|
|
|
2021-11-05 11:37:10 +01:00
|
|
|
try:
|
2021-11-17 12:38:25 +01:00
|
|
|
from settings import exclude_attribute_types
|
2021-11-05 11:37:10 +01:00
|
|
|
except ImportError:
|
2021-11-17 12:38:25 +01:00
|
|
|
exclude_attribute_types = []
|
2021-11-05 11:37:10 +01:00
|
|
|
|
2016-04-11 15:18:05 +02:00
|
|
|
valid_attribute_distributions = []
|
|
|
|
|
2018-10-28 13:01:26 +01:00
|
|
|
|
2016-03-01 15:32:58 +01:00
|
|
|
def init():
|
2016-04-11 15:18:05 +02:00
|
|
|
# If we have an old settings.py file then this variable won't exist
|
|
|
|
global valid_attribute_distributions
|
|
|
|
try:
|
2019-11-22 17:36:24 +01:00
|
|
|
valid_attribute_distributions = [int(v) for v in valid_attribute_distribution_levels]
|
2018-10-28 13:01:26 +01:00
|
|
|
except Exception:
|
2019-11-22 17:36:24 +01:00
|
|
|
valid_attribute_distributions = [0, 1, 2, 3, 4, 5]
|
2019-11-20 12:50:22 +01:00
|
|
|
return ExpandedPyMISP(url, key, ssl)
|
2018-10-28 13:01:26 +01:00
|
|
|
|
2016-03-07 03:29:34 +01:00
|
|
|
|
2019-11-20 12:50:22 +01:00
|
|
|
def saveEvent(event):
|
|
|
|
try:
|
2019-12-24 00:16:05 +01:00
|
|
|
with open(os.path.join(outputdir, f'{event["Event"]["uuid"]}.json'), 'w') as f:
|
2019-11-20 12:50:22 +01:00
|
|
|
json.dump(event, f, indent=2)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
sys.exit('Could not create the event dump.')
|
2016-04-11 15:18:05 +02:00
|
|
|
|
2018-10-28 13:01:26 +01:00
|
|
|
|
2019-11-20 12:50:22 +01:00
|
|
|
def saveHashes(hashes):
|
2017-11-04 14:18:15 +01:00
|
|
|
try:
|
2019-11-20 12:50:22 +01:00
|
|
|
with open(os.path.join(outputdir, 'hashes.csv'), 'w') as hashFile:
|
|
|
|
for element in hashes:
|
|
|
|
hashFile.write('{},{}\n'.format(element[0], element[1]))
|
2017-11-04 14:18:15 +01:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
sys.exit('Could not create the quick hash lookup file.')
|
2016-04-11 15:18:05 +02:00
|
|
|
|
2016-03-01 15:32:58 +01:00
|
|
|
|
|
|
|
def saveManifest(manifest):
|
|
|
|
try:
|
|
|
|
manifestFile = open(os.path.join(outputdir, 'manifest.json'), 'w')
|
|
|
|
manifestFile.write(json.dumps(manifest))
|
|
|
|
manifestFile.close()
|
2016-06-16 06:48:40 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2016-03-01 15:32:58 +01:00
|
|
|
sys.exit('Could not create the manifest file.')
|
|
|
|
|
2016-03-07 03:29:34 +01:00
|
|
|
|
2016-03-01 15:32:58 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
misp = init()
|
|
|
|
try:
|
2021-11-17 12:38:25 +01:00
|
|
|
events = misp.search_index(minimal=True, **filters, pythonify=False)
|
2016-09-12 12:53:58 +02:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
2016-03-01 15:32:58 +01:00
|
|
|
sys.exit("Invalid response received from MISP.")
|
|
|
|
if len(events) == 0:
|
|
|
|
sys.exit("No events returned.")
|
|
|
|
manifest = {}
|
2019-11-20 12:50:22 +01:00
|
|
|
hashes = []
|
2016-03-07 03:29:34 +01:00
|
|
|
counter = 1
|
|
|
|
total = len(events)
|
2016-03-01 15:32:58 +01:00
|
|
|
for event in events:
|
2020-02-05 13:28:11 +01:00
|
|
|
try:
|
2021-11-17 12:38:25 +01:00
|
|
|
e = misp.get_event(event['uuid'], deleted=include_deleted, pythonify=True)
|
|
|
|
if exclude_attribute_types:
|
2021-11-05 11:37:10 +01:00
|
|
|
for i, attribute in enumerate(e.attributes):
|
2021-11-17 12:38:25 +01:00
|
|
|
if attribute.type in exclude_attribute_types:
|
|
|
|
e.attributes.pop(i)
|
2022-01-19 22:30:30 +01:00
|
|
|
e_feed = e.to_feed(valid_distributions=valid_attribute_distributions, with_meta=True, with_distribution=with_distribution, with_local_tags=with_local_tags)
|
2021-11-05 11:37:10 +01:00
|
|
|
except Exception as err:
|
2021-11-17 12:38:25 +01:00
|
|
|
print(err, event['uuid'])
|
2020-02-05 13:28:11 +01:00
|
|
|
continue
|
2019-11-22 17:36:24 +01:00
|
|
|
if not e_feed:
|
|
|
|
print(f'Invalid distribution {e.distribution}, skipping')
|
|
|
|
continue
|
2019-12-24 00:16:05 +01:00
|
|
|
hashes += [[h, e.uuid] for h in e_feed['Event'].pop('_hashes')]
|
|
|
|
manifest.update(e_feed['Event'].pop('_manifest'))
|
2019-11-20 12:50:22 +01:00
|
|
|
saveEvent(e_feed)
|
2016-06-16 06:48:40 +02:00
|
|
|
print("Event " + str(counter) + "/" + str(total) + " exported.")
|
2016-03-07 03:29:34 +01:00
|
|
|
counter += 1
|
2016-03-01 15:32:58 +01:00
|
|
|
saveManifest(manifest)
|
2017-11-04 14:18:15 +01:00
|
|
|
print('Manifest saved.')
|
2019-11-20 12:50:22 +01:00
|
|
|
saveHashes(hashes)
|
2017-11-04 14:18:15 +01:00
|
|
|
print('Hashes saved. Feed creation completed.')
|