2022-03-07 15:12:01 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
2023-05-04 16:35:56 +02:00
|
|
|
import re
|
2022-03-07 15:12:01 +01:00
|
|
|
import sys
|
|
|
|
|
2022-03-08 10:44:41 +01:00
|
|
|
from flask import url_for
|
2022-12-21 14:20:13 +01:00
|
|
|
from pymisp import MISPObject
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2022-12-21 14:20:13 +01:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from lib.ConfigLoader import ConfigLoader
|
2023-05-10 16:26:46 +02:00
|
|
|
from lib.objects.abstract_subtype_object import AbstractSubtypeObject, get_all_id, get_all_id_iterator
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2022-12-21 14:20:13 +01:00
|
|
|
config_loader = ConfigLoader()
|
|
|
|
baseurl = config_loader.get_config_str("Notifications", "ail_domain")
|
2022-03-07 15:12:01 +01:00
|
|
|
config_loader = None
|
|
|
|
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
|
2022-08-19 16:53:31 +02:00
|
|
|
class Pgp(AbstractSubtypeObject):
|
2022-03-07 15:12:01 +01:00
|
|
|
"""
|
|
|
|
AIL Pgp Object. (strings)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, id, subtype):
|
|
|
|
super(Pgp, self).__init__('pgp', id, subtype=subtype)
|
|
|
|
|
|
|
|
# def get_ail_2_ail_payload(self):
|
|
|
|
# payload = {'raw': self.get_gzip_content(b64=True),
|
|
|
|
# 'compress': 'gzip'}
|
|
|
|
# return payload
|
|
|
|
|
|
|
|
# # WARNING: UNCLEAN DELETE /!\ TEST ONLY /!\
|
|
|
|
def delete(self):
|
|
|
|
# # TODO:
|
|
|
|
pass
|
|
|
|
|
2022-09-19 09:50:46 +02:00
|
|
|
# # TODO:
|
2022-10-25 16:25:19 +02:00
|
|
|
def get_meta(self, options=set()):
|
2023-02-28 11:01:27 +01:00
|
|
|
meta = self._get_meta(options=options)
|
2022-10-25 16:25:19 +02:00
|
|
|
meta['id'] = self.id
|
|
|
|
meta['subtype'] = self.subtype
|
2022-11-22 10:47:15 +01:00
|
|
|
meta['tags'] = self.get_tags(r_list=True)
|
2022-10-25 16:25:19 +02:00
|
|
|
return meta
|
2022-09-19 09:50:46 +02:00
|
|
|
|
2022-03-07 15:12:01 +01:00
|
|
|
def get_link(self, flask_context=False):
|
|
|
|
if flask_context:
|
2022-10-25 16:25:19 +02:00
|
|
|
url = url_for('correlation.show_correlation', type=self.type, subtype=self.subtype, id=self.id)
|
2022-03-07 15:12:01 +01:00
|
|
|
else:
|
2022-10-25 16:25:19 +02:00
|
|
|
url = f'{baseurl}/correlation/show?type={self.type}&subtype={self.subtype}&id={self.id}'
|
2022-03-07 15:12:01 +01:00
|
|
|
return url
|
|
|
|
|
|
|
|
def get_svg_icon(self):
|
|
|
|
if self.subtype == 'key':
|
|
|
|
icon = '\uf084'
|
|
|
|
elif self.subtype == 'name':
|
|
|
|
icon = '\uf507'
|
|
|
|
elif self.subtype == 'mail':
|
|
|
|
icon = '\uf1fa'
|
|
|
|
else:
|
|
|
|
icon = 'times'
|
2022-12-21 14:20:13 +01:00
|
|
|
return {'style': 'fas', 'icon': icon, 'color': '#44AA99', 'radius': 5}
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2022-07-08 09:47:47 +02:00
|
|
|
def get_misp_object(self):
|
|
|
|
obj_attrs = []
|
|
|
|
obj = MISPObject('pgp-meta')
|
2023-06-09 11:19:22 +02:00
|
|
|
first_seen = self.get_first_seen()
|
|
|
|
last_seen = self.get_last_seen()
|
|
|
|
if first_seen:
|
|
|
|
obj.first_seen = first_seen
|
|
|
|
if last_seen:
|
|
|
|
obj.last_seen = last_seen
|
|
|
|
if not first_seen or not last_seen:
|
|
|
|
self.logger.warning(
|
|
|
|
f'Export error, None seen {self.type}:{self.subtype}:{self.id}, first={first_seen}, last={last_seen}')
|
2022-07-08 09:47:47 +02:00
|
|
|
|
2022-12-21 14:20:13 +01:00
|
|
|
if self.subtype == 'key':
|
|
|
|
obj_attrs.append(obj.add_attribute('key-id', value=self.id))
|
|
|
|
elif self.subtype == 'name':
|
|
|
|
obj_attrs.append(obj.add_attribute('user-id-name', value=self.id))
|
|
|
|
else: # mail
|
|
|
|
obj_attrs.append(obj.add_attribute('user-id-email', value=self.id))
|
2022-07-08 09:47:47 +02:00
|
|
|
|
|
|
|
for obj_attr in obj_attrs:
|
|
|
|
for tag in self.get_tags():
|
|
|
|
obj_attr.add_tag(tag)
|
|
|
|
return obj
|
|
|
|
|
2022-03-07 15:12:01 +01:00
|
|
|
############################################################################
|
|
|
|
############################################################################
|
|
|
|
|
2022-08-19 16:53:31 +02:00
|
|
|
def get_all_subtypes():
|
|
|
|
return ['key', 'mail', 'name']
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2022-08-19 16:53:31 +02:00
|
|
|
def get_all_pgps():
|
|
|
|
pgps = {}
|
|
|
|
for subtype in get_all_subtypes():
|
|
|
|
pgps[subtype] = get_all_pgps_by_subtype(subtype)
|
|
|
|
return pgps
|
|
|
|
|
|
|
|
def get_all_pgps_by_subtype(subtype):
|
|
|
|
return get_all_id('pgp', subtype)
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2023-05-04 16:35:56 +02:00
|
|
|
# TODO FILTER NAME + Key + mail
|
|
|
|
def sanitize_pgp_name_to_search(name_to_search, subtype): # TODO FILTER NAME + Key + mail
|
|
|
|
if subtype == 'key':
|
|
|
|
pass
|
|
|
|
elif subtype == 'name':
|
|
|
|
pass
|
|
|
|
elif subtype == 'mail':
|
|
|
|
pass
|
|
|
|
return name_to_search
|
|
|
|
|
|
|
|
def search_pgps_by_name(name_to_search, subtype, r_pos=False):
|
|
|
|
pgps = {}
|
|
|
|
# for subtype in subtypes:
|
|
|
|
r_name = sanitize_pgp_name_to_search(name_to_search, subtype)
|
|
|
|
if not name_to_search or isinstance(r_name, dict):
|
|
|
|
# break
|
|
|
|
return pgps
|
|
|
|
r_name = re.compile(r_name)
|
|
|
|
for pgp_name in get_all_pgps_by_subtype(subtype):
|
|
|
|
res = re.search(r_name, pgp_name)
|
|
|
|
if res:
|
|
|
|
pgps[pgp_name] = {}
|
|
|
|
if r_pos:
|
|
|
|
pgps[pgp_name]['hl-start'] = res.start()
|
|
|
|
pgps[pgp_name]['hl-end'] = res.end()
|
|
|
|
return pgps
|
|
|
|
|
2023-05-10 16:26:46 +02:00
|
|
|
def get_all_pgps_objects(filters={}):
|
|
|
|
if 'subtypes' in filters:
|
|
|
|
subtypes = filters['subtypes']
|
|
|
|
else:
|
|
|
|
subtypes = get_all_subtypes()
|
|
|
|
for subtype in subtypes:
|
|
|
|
for z_tuple in get_all_id_iterator('pgp', subtype):
|
|
|
|
obj_id, _ = z_tuple
|
|
|
|
yield Pgp(obj_id, subtype)
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2023-05-10 16:26:46 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-05-04 16:35:56 +02:00
|
|
|
# name_to_search = 'ex'
|
|
|
|
# subtype = 'name'
|
|
|
|
# print(search_pgps_by_name(name_to_search, subtype))
|
2023-05-10 16:26:46 +02:00
|
|
|
gen = get_all_pgps_objects(filters={'subtypes': ['key']})
|
|
|
|
for f in gen:
|
|
|
|
print(f)
|
|
|
|
|