2016-06-16 06:48:40 +02:00
|
|
|
#!/usr/bin/env python
|
2016-03-01 15:32:58 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import os
|
2017-11-04 14:18:15 +01:00
|
|
|
import hashlib
|
2016-03-01 15:32:58 +01:00
|
|
|
from pymisp import PyMISP
|
2016-06-16 06:48:40 +02:00
|
|
|
from settings import url, key, ssl, outputdir, filters, valid_attribute_distribution_levels
|
|
|
|
|
2017-11-04 14:18:15 +01:00
|
|
|
objectsFields = {
|
|
|
|
'Attribute': {
|
|
|
|
'uuid',
|
|
|
|
'value',
|
|
|
|
'category',
|
|
|
|
'type',
|
|
|
|
'comment',
|
|
|
|
'data',
|
|
|
|
'timestamp',
|
|
|
|
'to_ids'
|
|
|
|
},
|
|
|
|
'Event': {
|
|
|
|
'uuid',
|
|
|
|
'info',
|
|
|
|
'threat_level_id',
|
|
|
|
'analysis',
|
|
|
|
'timestamp',
|
|
|
|
'publish_timestamp',
|
|
|
|
'published',
|
|
|
|
'date'
|
|
|
|
},
|
|
|
|
'Object': {
|
|
|
|
'name',
|
|
|
|
'meta-category',
|
|
|
|
'description',
|
|
|
|
'template_uuid',
|
|
|
|
'template_version',
|
|
|
|
'uuid',
|
|
|
|
'timestamp',
|
|
|
|
'distribution',
|
|
|
|
'sharing_group_id',
|
|
|
|
'comment'
|
|
|
|
},
|
|
|
|
'ObjectReference': {
|
|
|
|
'uuid',
|
|
|
|
'timestamp',
|
|
|
|
'relationship_type',
|
|
|
|
'comment',
|
|
|
|
'object_uuid',
|
|
|
|
'referenced_uuid'
|
|
|
|
},
|
|
|
|
'Orgc': {
|
|
|
|
'name',
|
|
|
|
'uuid'
|
|
|
|
},
|
|
|
|
'Tag': {
|
|
|
|
'name',
|
|
|
|
'colour',
|
|
|
|
'exportable'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
objectsToSave = {
|
|
|
|
'Orgc': {},
|
|
|
|
'Tag': {},
|
|
|
|
'Attribute': {
|
|
|
|
'Tag': {}
|
|
|
|
},
|
|
|
|
'Object': {
|
|
|
|
'Attribute': {
|
|
|
|
'Tag': {}
|
|
|
|
},
|
|
|
|
'ObjectReference': {}
|
|
|
|
}
|
|
|
|
}
|
2016-03-07 03:29:34 +01:00
|
|
|
|
2016-04-11 15:18:05 +02:00
|
|
|
valid_attribute_distributions = []
|
|
|
|
|
2017-11-04 14:18:15 +01:00
|
|
|
attributeHashes = []
|
2016-03-07 03:29:34 +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:
|
|
|
|
valid_attribute_distributions = valid_attribute_distribution_levels
|
|
|
|
except:
|
|
|
|
valid_attribute_distributions = ['0', '1', '2', '3', '4', '5']
|
2016-09-12 12:53:58 +02:00
|
|
|
return PyMISP(url, key, ssl)
|
2016-03-01 15:32:58 +01:00
|
|
|
|
2017-11-04 14:18:15 +01:00
|
|
|
def recursiveExtract(container, containerType, leaf, eventUuid):
|
|
|
|
temp = {}
|
|
|
|
if containerType in ['Attribute', 'Object']:
|
|
|
|
if (__blockByDistribution(container)):
|
|
|
|
return False
|
|
|
|
for field in objectsFields[containerType]:
|
|
|
|
if field in container:
|
|
|
|
temp[field] = container[field]
|
|
|
|
if (containerType == 'Attribute'):
|
|
|
|
global attributeHashes
|
|
|
|
if ('|' in container['type'] or container['type'] == 'malware-sample'):
|
|
|
|
split = container['value'].split('|')
|
|
|
|
attributeHashes.append([hashlib.md5(split[0].encode("utf-8")).hexdigest(), eventUuid])
|
|
|
|
attributeHashes.append([hashlib.md5(split[1].encode("utf-8")).hexdigest(), eventUuid])
|
|
|
|
else:
|
|
|
|
attributeHashes.append([hashlib.md5(container['value'].encode("utf-8")).hexdigest(), eventUuid])
|
|
|
|
children = leaf.keys()
|
|
|
|
for childType in children:
|
|
|
|
childContainer = container.get(childType)
|
|
|
|
if (childContainer):
|
|
|
|
if (type(childContainer) is dict):
|
|
|
|
temp[childType] = recursiveExtract(childContainer, childType, leaf[childType], eventUuid)
|
|
|
|
else:
|
|
|
|
temp[childType] = []
|
|
|
|
for element in childContainer:
|
|
|
|
processed = recursiveExtract(element, childType, leaf[childType], eventUuid)
|
|
|
|
if (processed):
|
|
|
|
temp[childType].append(processed)
|
|
|
|
return temp
|
2016-03-01 15:32:58 +01:00
|
|
|
|
|
|
|
def saveEvent(misp, uuid):
|
2017-11-04 14:18:15 +01:00
|
|
|
result = {}
|
2016-06-16 06:48:40 +02:00
|
|
|
event = misp.get_event(uuid)
|
2016-09-12 12:53:58 +02:00
|
|
|
if not event.get('Event'):
|
|
|
|
print('Error while fetching event: {}'.format(event['message']))
|
2016-03-07 03:29:34 +01:00
|
|
|
sys.exit('Could not create file for event ' + uuid + '.')
|
2017-11-04 14:18:15 +01:00
|
|
|
event['Event'] = recursiveExtract(event['Event'], 'Event', objectsToSave, event['Event']['uuid'])
|
2016-06-16 06:48:40 +02:00
|
|
|
event = json.dumps(event)
|
|
|
|
eventFile = open(os.path.join(outputdir, uuid + '.json'), 'w')
|
|
|
|
eventFile.write(event)
|
|
|
|
eventFile.close()
|
2016-03-07 03:29:34 +01:00
|
|
|
|
2017-11-04 14:18:15 +01:00
|
|
|
def __blockByDistribution(element):
|
|
|
|
if element['distribution'] not in valid_attribute_distributions:
|
2016-04-11 15:18:05 +02:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2017-11-04 14:18:15 +01:00
|
|
|
def saveHashes():
|
|
|
|
if not attributeHashes:
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
hashFile = open(os.path.join(outputdir, 'hashes.csv'), 'w')
|
|
|
|
for element in attributeHashes:
|
|
|
|
hashFile.write('{},{}\n'.format(element[0], element[1]))
|
|
|
|
hashFile.close()
|
|
|
|
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
|
|
|
|
|
|
|
def __addEventToManifest(event):
|
|
|
|
tags = []
|
|
|
|
for eventTag in event['EventTag']:
|
|
|
|
tags.append({'name': eventTag['Tag']['name'],
|
|
|
|
'colour': eventTag['Tag']['colour']})
|
2016-06-16 06:48:40 +02:00
|
|
|
return {'Orgc': event['Orgc'],
|
2016-03-07 03:29:34 +01:00
|
|
|
'Tag': tags,
|
|
|
|
'info': event['info'],
|
|
|
|
'date': event['date'],
|
|
|
|
'analysis': event['analysis'],
|
|
|
|
'threat_level_id': event['threat_level_id'],
|
|
|
|
'timestamp': event['timestamp']
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-01 15:32:58 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
misp = init()
|
|
|
|
try:
|
2016-09-12 12:53:58 +02:00
|
|
|
r = misp.get_index(filters)
|
|
|
|
events = r['response']
|
|
|
|
print(events[0])
|
|
|
|
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 = {}
|
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:
|
|
|
|
saveEvent(misp, event['uuid'])
|
2016-03-07 03:29:34 +01:00
|
|
|
manifest[event['uuid']] = __addEventToManifest(event)
|
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.')
|
|
|
|
saveHashes()
|
|
|
|
print('Hashes saved. Feed creation completed.')
|