2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2017-11-16 09:52:37 +01:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2019-11-05 15:18:03 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2020-06-19 13:36:03 +02:00
|
|
|
from pymisp import MISPEvent, MISPObject
|
2017-11-16 11:18:13 +01:00
|
|
|
from pymisp.tools.abstractgenerator import AbstractMISPObjectGenerator
|
2020-06-19 13:36:03 +02:00
|
|
|
|
2017-11-20 12:12:53 +01:00
|
|
|
import datetime
|
2017-11-16 09:52:37 +01:00
|
|
|
|
2022-11-28 15:01:40 +01:00
|
|
|
from lib.objects.Items import Item
|
|
|
|
from lib import ConfigLoader
|
2017-11-20 12:12:53 +01:00
|
|
|
|
2017-11-20 14:57:25 +01:00
|
|
|
class ObjectWrapper:
|
|
|
|
def __init__(self, pymisp):
|
|
|
|
self.pymisp = pymisp
|
|
|
|
self.currentID_date = None
|
|
|
|
self.eventID_to_push = self.get_daily_event_id()
|
2019-11-05 15:18:03 +01:00
|
|
|
config_loader = ConfigLoader.ConfigLoader()
|
|
|
|
self.maxDuplicateToPushToMISP = config_loader.get_config_int("ailleakObject", "maxDuplicateToPushToMISP")
|
|
|
|
config_loader = None
|
2018-06-14 16:51:06 +02:00
|
|
|
self.attribute_to_tag = None
|
2017-11-20 14:57:25 +01:00
|
|
|
|
2022-11-28 15:01:40 +01:00
|
|
|
def add_new_object(self, uuid_ail, item_id):
|
2018-06-14 16:51:06 +02:00
|
|
|
self.uuid_ail = uuid_ail
|
2017-11-20 14:57:25 +01:00
|
|
|
|
2022-11-28 15:01:40 +01:00
|
|
|
item = Item(item_id)
|
|
|
|
return item.get_misp_object()
|
2017-11-20 12:12:53 +01:00
|
|
|
|
|
|
|
def date_to_str(self, date):
|
|
|
|
return "{0}-{1}-{2}".format(date.year, date.month, date.day)
|
|
|
|
|
2020-06-19 13:36:03 +02:00
|
|
|
def get_all_related_events(self, to_search):
|
|
|
|
result = self.pymisp.search(controller='events', eventinfo=to_search, metadata=False)
|
2017-11-20 12:12:53 +01:00
|
|
|
events = []
|
2020-06-19 13:36:03 +02:00
|
|
|
if result:
|
|
|
|
for e in result:
|
|
|
|
events.append({'id': e['Event']['id'], 'org_id': e['Event']['org_id'], 'info': e['Event']['info']})
|
2017-11-20 12:12:53 +01:00
|
|
|
return events
|
|
|
|
|
|
|
|
def get_daily_event_id(self):
|
|
|
|
to_match = "Daily AIL-leaks {}".format(datetime.date.today())
|
2020-06-19 13:36:03 +02:00
|
|
|
events = self.get_all_related_events(to_match)
|
2017-11-20 12:12:53 +01:00
|
|
|
for dic in events:
|
|
|
|
info = dic['info']
|
|
|
|
e_id = dic['id']
|
|
|
|
if info == to_match:
|
|
|
|
print('Found: ', info, '->', e_id)
|
2017-11-20 14:57:25 +01:00
|
|
|
self.currentID_date = datetime.date.today()
|
2017-11-20 12:12:53 +01:00
|
|
|
return e_id
|
2020-06-19 13:36:03 +02:00
|
|
|
created_event = self.create_daily_event()
|
|
|
|
new_id = created_event['Event']['id']
|
2017-11-20 12:12:53 +01:00
|
|
|
print('New event created:', new_id)
|
2017-11-20 14:57:25 +01:00
|
|
|
self.currentID_date = datetime.date.today()
|
2017-11-20 12:12:53 +01:00
|
|
|
return new_id
|
|
|
|
|
|
|
|
|
|
|
|
def create_daily_event(self):
|
|
|
|
today = datetime.date.today()
|
|
|
|
# [0-3]
|
|
|
|
distribution = 0
|
|
|
|
info = "Daily AIL-leaks {}".format(today)
|
|
|
|
# [0-2]
|
|
|
|
analysis = 0
|
|
|
|
# [1-4]
|
|
|
|
threat = 3
|
|
|
|
published = False
|
|
|
|
org_id = None
|
|
|
|
orgc_id = None
|
|
|
|
sharing_group_id = None
|
|
|
|
date = None
|
2020-06-19 13:36:03 +02:00
|
|
|
|
|
|
|
event = MISPEvent()
|
|
|
|
event.distribution = distribution
|
|
|
|
event.info = info
|
|
|
|
event.analysis = analysis
|
|
|
|
event.threat = threat
|
|
|
|
event.published = published
|
|
|
|
|
|
|
|
event.add_tag('infoleak:output-format="ail-daily"')
|
|
|
|
existing_event = self.pymisp.add_event(event)
|
|
|
|
return existing_event
|
2017-11-20 12:12:53 +01:00
|
|
|
|
|
|
|
# Publish object to MISP
|
2020-06-19 13:36:03 +02:00
|
|
|
def pushToMISP(self, uuid_ail, item_id, tag):
|
2018-06-14 16:51:06 +02:00
|
|
|
|
2017-11-20 14:57:25 +01:00
|
|
|
if self.currentID_date != datetime.date.today(): #refresh id
|
|
|
|
self.eventID_to_push = self.get_daily_event_id()
|
|
|
|
|
2017-11-20 12:12:53 +01:00
|
|
|
mispTYPE = 'ail-leak'
|
2018-06-14 16:51:06 +02:00
|
|
|
|
|
|
|
# paste object already exist
|
2020-06-19 13:36:03 +02:00
|
|
|
if self.paste_object_exist(self.eventID_to_push, item_id):
|
2018-06-14 16:51:06 +02:00
|
|
|
# add new tag
|
|
|
|
self.tag(self.attribute_to_tag, tag)
|
2020-06-19 13:36:03 +02:00
|
|
|
print(item_id + ' tagged: ' + tag)
|
2022-11-28 15:01:40 +01:00
|
|
|
# create object
|
2017-11-20 12:12:53 +01:00
|
|
|
else:
|
2022-11-28 15:01:40 +01:00
|
|
|
misp_obj = self.add_new_object(uuid_ail, item_id)
|
2020-06-19 13:36:03 +02:00
|
|
|
|
|
|
|
# deprecated
|
|
|
|
# try:
|
|
|
|
# templateID = [x['ObjectTemplate']['id'] for x in self.pymisp.get_object_templates_list() if x['ObjectTemplate']['name'] == mispTYPE][0]
|
|
|
|
# except IndexError:
|
|
|
|
# valid_types = ", ".join([x['ObjectTemplate']['name'] for x in self.pymisp.get_object_templates_list()])
|
|
|
|
# print ("Template for type %s not found! Valid types are: %s" % (mispTYPE, valid_types))
|
2018-06-14 16:51:06 +02:00
|
|
|
|
|
|
|
|
2020-06-19 13:36:03 +02:00
|
|
|
r = self.pymisp.add_object(self.eventID_to_push, misp_obj, pythonify=True)
|
2018-06-14 16:51:06 +02:00
|
|
|
if 'errors' in r:
|
|
|
|
print(r)
|
|
|
|
else:
|
2020-06-19 13:36:03 +02:00
|
|
|
print('Pushed:', tag, '->', item_id)
|
2018-06-14 16:51:06 +02:00
|
|
|
|
2020-06-19 13:36:03 +02:00
|
|
|
def paste_object_exist(self, eventId, item_id):
|
|
|
|
res = self.pymisp.search(controller='attributes', eventid=eventId, value=item_id)
|
2018-06-14 16:51:06 +02:00
|
|
|
# object already exist
|
2020-06-19 13:36:03 +02:00
|
|
|
if res.get('Attribute', []):
|
|
|
|
self.attribute_to_tag = res['Attribute'][0]['uuid']
|
2018-06-14 16:51:06 +02:00
|
|
|
return True
|
|
|
|
# new object
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def tag(self, uuid, tag):
|
|
|
|
self.pymisp.tag(uuid, tag)
|