2022-03-07 14:12:01 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2023-05-04 14:35:56 +00:00
|
|
|
import re
|
2022-03-07 14:12:01 +00:00
|
|
|
|
2022-07-08 07:47:47 +00:00
|
|
|
from flask import url_for
|
|
|
|
from pymisp import MISPObject
|
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from lib.ConfigLoader import ConfigLoader
|
2022-08-19 14:53:31 +00:00
|
|
|
from lib.objects.abstract_subtype_object import AbstractSubtypeObject, get_all_id
|
2022-03-07 14:12:01 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
config_loader = ConfigLoader()
|
2023-01-16 15:27:49 +00:00
|
|
|
baseurl = config_loader.get_config_str("Notifications", "ail_domain")
|
2022-03-07 14:12:01 +00:00
|
|
|
config_loader = None
|
|
|
|
|
|
|
|
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
################################################################################
|
|
|
|
|
2022-07-08 07:47:47 +00:00
|
|
|
class Username(AbstractSubtypeObject):
|
2022-03-07 14:12:01 +00:00
|
|
|
"""
|
|
|
|
AIL Username Object. (strings)
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, id, subtype):
|
2022-07-08 07:47:47 +00:00
|
|
|
super(Username, self).__init__('username', id, subtype)
|
2022-03-07 14:12:01 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
def get_link(self, flask_context=False):
|
|
|
|
if flask_context:
|
2022-10-25 14:25:19 +00:00
|
|
|
url = url_for('correlation.show_correlation', type=self.type, subtype=self.subtype, id=self.id)
|
2022-03-07 14:12:01 +00:00
|
|
|
else:
|
2022-10-25 14:25:19 +00:00
|
|
|
url = f'{baseurl}/correlation/show?type={self.type}&subtype={self.subtype}&id={self.id}'
|
2022-03-07 14:12:01 +00:00
|
|
|
return url
|
|
|
|
|
|
|
|
def get_svg_icon(self):
|
|
|
|
if self.subtype == 'telegram':
|
|
|
|
style = 'fab'
|
|
|
|
icon = '\uf2c6'
|
|
|
|
elif self.subtype == 'twitter':
|
|
|
|
style = 'fab'
|
|
|
|
icon = '\uf099'
|
|
|
|
else:
|
|
|
|
style = 'fas'
|
|
|
|
icon = '\uf007'
|
2023-05-04 14:35:56 +00:00
|
|
|
return {'style': style, 'icon': icon, 'color': '#4dffff', 'radius': 5}
|
2022-03-07 14:12:01 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
def get_meta(self, options=set()):
|
2023-02-28 10:01:27 +00:00
|
|
|
meta = self._get_meta(options=options)
|
2022-10-25 14:25:19 +00:00
|
|
|
meta['id'] = self.id
|
|
|
|
meta['subtype'] = self.subtype
|
2022-11-22 09:47:15 +00:00
|
|
|
meta['tags'] = self.get_tags(r_list=True)
|
2022-10-25 14:25:19 +00:00
|
|
|
return meta
|
|
|
|
|
2022-07-08 07:47:47 +00:00
|
|
|
def get_misp_object(self):
|
|
|
|
obj_attrs = []
|
|
|
|
if self.subtype == 'telegram':
|
|
|
|
obj = MISPObject('telegram-account', standalone=True)
|
2023-05-04 14:35:56 +00:00
|
|
|
obj_attrs.append(obj.add_attribute('username', value=self.id))
|
2022-07-08 07:47:47 +00:00
|
|
|
|
|
|
|
elif self.subtype == 'twitter':
|
|
|
|
obj = MISPObject('twitter-account', standalone=True)
|
2023-05-04 14:35:56 +00:00
|
|
|
obj_attrs.append(obj.add_attribute('name', value=self.id))
|
2022-07-08 07:47:47 +00:00
|
|
|
|
|
|
|
else:
|
|
|
|
obj = MISPObject('user-account', standalone=True)
|
2023-05-04 14:35:56 +00:00
|
|
|
obj_attrs.append(obj.add_attribute('username', value=self.id))
|
2022-07-08 07:47:47 +00:00
|
|
|
|
|
|
|
obj.first_seen = self.get_first_seen()
|
|
|
|
obj.last_seen = self.get_last_seen()
|
|
|
|
for obj_attr in obj_attrs:
|
|
|
|
for tag in self.get_tags():
|
|
|
|
obj_attr.add_tag(tag)
|
|
|
|
return obj
|
|
|
|
|
2022-03-07 14:12:01 +00:00
|
|
|
############################################################################
|
|
|
|
############################################################################
|
|
|
|
|
2022-08-19 14:53:31 +00:00
|
|
|
def get_all_subtypes():
|
2022-09-01 12:04:00 +00:00
|
|
|
#return ail_core.get_object_all_subtypes(self.type)
|
2022-08-19 14:53:31 +00:00
|
|
|
return ['telegram', 'twitter', 'jabber']
|
2022-03-07 14:12:01 +00:00
|
|
|
|
2022-08-19 14:53:31 +00:00
|
|
|
def get_all_usernames():
|
|
|
|
users = {}
|
|
|
|
for subtype in get_all_subtypes():
|
|
|
|
users[subtype] = get_all_usernames_by_subtype(subtype)
|
|
|
|
return users
|
|
|
|
|
|
|
|
def get_all_usernames_by_subtype(subtype):
|
|
|
|
return get_all_id('username', subtype)
|
2022-03-07 14:12:01 +00:00
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
# TODO FILTER NAME + Key + mail
|
|
|
|
def sanitize_username_name_to_search(name_to_search, subtype): # TODO FILTER NAME
|
|
|
|
|
|
|
|
return name_to_search
|
|
|
|
|
|
|
|
def search_usernames_by_name(name_to_search, subtype, r_pos=False):
|
|
|
|
usernames = {}
|
|
|
|
# for subtype in subtypes:
|
|
|
|
r_name = sanitize_username_name_to_search(name_to_search, subtype)
|
|
|
|
if not name_to_search or isinstance(r_name, dict):
|
|
|
|
# break
|
|
|
|
return usernames
|
|
|
|
r_name = re.compile(r_name)
|
|
|
|
for user_name in get_all_usernames_by_subtype(subtype):
|
|
|
|
res = re.search(r_name, user_name)
|
|
|
|
if res:
|
|
|
|
usernames[user_name] = {}
|
|
|
|
if r_pos:
|
|
|
|
usernames[user_name]['hl-start'] = res.start()
|
|
|
|
usernames[user_name]['hl-end'] = res.end()
|
|
|
|
return usernames
|
|
|
|
|
|
|
|
|
|
|
|
# if __name__ == '__main__':
|
|
|
|
# name_to_search = 'co'
|
|
|
|
# subtype = 'telegram'
|
|
|
|
# print(search_usernames_by_name(name_to_search, subtype))
|