2022-09-01 12:04:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2024-02-26 14:35:48 +00:00
|
|
|
import uuid
|
2022-09-01 12:04:00 +00:00
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from lib.ConfigLoader import ConfigLoader
|
|
|
|
|
|
|
|
config_loader = ConfigLoader()
|
2023-01-16 15:27:49 +00:00
|
|
|
r_serv_db = config_loader.get_db_conn("Kvrocks_DB")
|
2023-11-02 15:28:33 +00:00
|
|
|
r_object = config_loader.get_db_conn("Kvrocks_Objects")
|
2022-09-01 12:04:00 +00:00
|
|
|
config_loader = None
|
|
|
|
|
2023-11-29 15:28:25 +00:00
|
|
|
AIL_OBJECTS = sorted({'chat', 'chat-subchannel', 'chat-thread', 'cookie-name', 'cve', 'cryptocurrency', 'decoded',
|
2024-10-01 13:12:15 +00:00
|
|
|
'domain', 'etag', 'favicon', 'file-name', 'hhhash','item', 'image', 'message', 'ocr', 'pgp',
|
|
|
|
'qrcode', 'screenshot', 'title', 'user-account', 'username'})
|
2022-11-22 09:47:15 +00:00
|
|
|
|
2024-02-29 13:56:45 +00:00
|
|
|
AIL_OBJECTS_WITH_SUBTYPES = {'chat', 'chat-subchannel', 'cryptocurrency', 'pgp', 'username', 'user-account'}
|
|
|
|
|
2024-03-11 14:33:30 +00:00
|
|
|
# TODO by object TYPE ????
|
|
|
|
AIL_OBJECTS_CORRELATIONS_DEFAULT = sorted({'chat', 'chat-subchannel', 'chat-thread', 'cve', 'cryptocurrency', 'decoded',
|
2024-10-01 13:12:15 +00:00
|
|
|
'domain', 'favicon', 'file-name', 'item', 'image', 'message', 'ocr', 'pgp',
|
|
|
|
'qrcode', 'screenshot', 'title', 'user-account', 'username'})
|
2024-03-11 14:33:30 +00:00
|
|
|
|
2022-09-01 12:04:00 +00:00
|
|
|
def get_ail_uuid():
|
2023-05-15 08:37:03 +00:00
|
|
|
ail_uuid = r_serv_db.get('ail:uuid')
|
|
|
|
if not ail_uuid:
|
|
|
|
ail_uuid = _set_ail_uuid()
|
|
|
|
return ail_uuid
|
|
|
|
|
|
|
|
def _set_ail_uuid():
|
|
|
|
ail_uuid = generate_uuid()
|
|
|
|
r_serv_db.set('ail:uuid', ail_uuid)
|
|
|
|
return ail_uuid
|
2022-09-01 12:04:00 +00:00
|
|
|
|
2024-09-16 13:30:12 +00:00
|
|
|
def get_ail_uuid_int():
|
|
|
|
ail_uuid = get_ail_uuid()
|
|
|
|
header_uuid = ail_uuid.replace('-', '')
|
|
|
|
return uuid.UUID(hex=header_uuid, version=4).int
|
|
|
|
|
2024-02-26 14:35:48 +00:00
|
|
|
def is_valid_uuid_v4(header_uuid):
|
|
|
|
try:
|
|
|
|
header_uuid = header_uuid.replace('-', '')
|
|
|
|
uuid_test = uuid.UUID(hex=header_uuid, version=4)
|
|
|
|
return uuid_test.hex == header_uuid
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
2023-04-04 08:25:01 +00:00
|
|
|
def generate_uuid():
|
2024-02-26 14:35:48 +00:00
|
|
|
return str(uuid.uuid4())
|
2023-04-04 08:25:01 +00:00
|
|
|
|
2022-09-01 12:04:00 +00:00
|
|
|
#### AIL OBJECTS ####
|
|
|
|
|
|
|
|
def get_all_objects():
|
2022-11-22 09:47:15 +00:00
|
|
|
return AIL_OBJECTS
|
2022-09-01 12:04:00 +00:00
|
|
|
|
2024-03-13 13:04:23 +00:00
|
|
|
def is_object_type(obj_type):
|
|
|
|
return obj_type in AIL_OBJECTS
|
|
|
|
|
2023-02-28 10:01:27 +00:00
|
|
|
def get_objects_with_subtypes():
|
2024-02-29 13:56:45 +00:00
|
|
|
return AIL_OBJECTS_WITH_SUBTYPES
|
2023-02-28 10:01:27 +00:00
|
|
|
|
2023-11-02 15:28:33 +00:00
|
|
|
def get_object_all_subtypes(obj_type): # TODO Dynamic subtype
|
2023-08-18 09:05:21 +00:00
|
|
|
if obj_type == 'chat':
|
2023-11-02 15:28:33 +00:00
|
|
|
return r_object.smembers(f'all_chat:subtypes')
|
|
|
|
if obj_type == 'chat-subchannel':
|
|
|
|
return r_object.smembers(f'all_chat-subchannel:subtypes')
|
2024-02-29 15:18:22 +00:00
|
|
|
if obj_type == 'chat-thread':
|
|
|
|
return r_object.smembers(f'all_chat-thread:subtypes')
|
2022-09-01 12:04:00 +00:00
|
|
|
if obj_type == 'cryptocurrency':
|
2024-05-08 13:14:51 +00:00
|
|
|
return ['bitcoin', 'bitcoin-cash', 'dash', 'ethereum', 'litecoin', 'monero', 'tron', 'zcash']
|
2022-09-01 12:04:00 +00:00
|
|
|
if obj_type == 'pgp':
|
|
|
|
return ['key', 'mail', 'name']
|
|
|
|
if obj_type == 'username':
|
|
|
|
return ['telegram', 'twitter', 'jabber']
|
2023-12-06 15:26:26 +00:00
|
|
|
if obj_type == 'user-account':
|
|
|
|
return r_object.smembers(f'all_chat:subtypes')
|
2023-04-06 14:26:56 +00:00
|
|
|
return []
|
2022-09-01 12:04:00 +00:00
|
|
|
|
2024-03-11 14:33:30 +00:00
|
|
|
def get_default_correlation_objects():
|
|
|
|
return AIL_OBJECTS_CORRELATIONS_DEFAULT
|
|
|
|
|
2024-01-08 12:50:56 +00:00
|
|
|
def get_obj_queued():
|
2024-10-07 09:03:56 +00:00
|
|
|
return ['item', 'image', 'message', 'ocr', 'qrcode'] # screenshot ???
|
2024-01-08 12:50:56 +00:00
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
def get_objects_tracked():
|
2024-10-01 13:12:15 +00:00
|
|
|
return ['decoded', 'item', 'pgp', 'message', 'ocr', 'qrcode', 'title']
|
2023-05-04 14:35:56 +00:00
|
|
|
|
2023-05-10 14:26:46 +00:00
|
|
|
def get_objects_retro_hunted():
|
2024-02-15 15:22:00 +00:00
|
|
|
return ['decoded', 'item', 'message']
|
2023-05-10 14:26:46 +00:00
|
|
|
|
2023-01-16 15:27:49 +00:00
|
|
|
def get_all_objects_with_subtypes_tuple():
|
|
|
|
str_objs = []
|
|
|
|
for obj_type in get_all_objects():
|
|
|
|
subtypes = get_object_all_subtypes(obj_type)
|
|
|
|
if subtypes:
|
|
|
|
for subtype in subtypes:
|
|
|
|
str_objs.append((obj_type, subtype))
|
|
|
|
else:
|
|
|
|
str_objs.append((obj_type, ''))
|
|
|
|
return str_objs
|
|
|
|
|
2023-08-18 09:05:21 +00:00
|
|
|
def unpack_obj_global_id(global_id, r_type='tuple'):
|
|
|
|
if r_type == 'dict':
|
|
|
|
obj = global_id.split(':', 2)
|
2023-12-06 15:26:26 +00:00
|
|
|
return {'type': obj[0], 'subtype': obj[1], 'id': obj[2]}
|
2023-08-18 09:05:21 +00:00
|
|
|
else: # tuple(type, subtype, id)
|
2024-04-08 15:16:07 +00:00
|
|
|
return global_id.split(':', 2) # TODO REPLACE get_obj_type_subtype_id_from_global_id(global_id)
|
2023-08-18 09:05:21 +00:00
|
|
|
|
2023-12-06 15:26:26 +00:00
|
|
|
def unpack_objs_global_id(objs_global_id, r_type='tuple'):
|
|
|
|
objs = []
|
|
|
|
for global_id in objs_global_id:
|
|
|
|
objs.append(unpack_obj_global_id(global_id, r_type=r_type))
|
|
|
|
return objs
|
|
|
|
|
|
|
|
def unpack_correl_obj__id(obj_type, global_id, r_type='tuple'):
|
|
|
|
obj = global_id.split(':', 1)
|
|
|
|
if r_type == 'dict':
|
|
|
|
return {'type': obj_type, 'subtype': obj[0], 'id': obj[1]}
|
|
|
|
else: # tuple(type, subtype, id)
|
|
|
|
return obj_type, obj[0], obj[1]
|
|
|
|
|
|
|
|
def unpack_correl_objs_id(obj_type, correl_objs_id, r_type='tuple'):
|
|
|
|
objs = []
|
|
|
|
for correl_obj_id in correl_objs_id:
|
|
|
|
objs.append(unpack_correl_obj__id(obj_type, correl_obj_id, r_type=r_type))
|
|
|
|
return objs
|
2023-08-18 09:05:21 +00:00
|
|
|
|
2022-09-01 12:04:00 +00:00
|
|
|
##-- AIL OBJECTS --##
|
2022-11-28 14:01:40 +00:00
|
|
|
|
2023-05-10 14:26:46 +00:00
|
|
|
#### Redis ####
|
|
|
|
|
|
|
|
def _parse_zscan(response):
|
|
|
|
cursor, r = response
|
|
|
|
it = iter(r)
|
|
|
|
return str(cursor), list(it)
|
|
|
|
|
|
|
|
def zscan_iter(r_redis, name): # count ???
|
|
|
|
cursor = 0
|
|
|
|
while cursor != "0":
|
|
|
|
cursor, data = _parse_zscan(r_redis.zscan(name, cursor=cursor))
|
|
|
|
yield from data
|
|
|
|
|
|
|
|
## -- Redis -- ##
|
|
|
|
|
2023-10-11 10:06:01 +00:00
|
|
|
def rreplace(s, old, new, occurrence):
|
|
|
|
li = s.rsplit(old, occurrence)
|
|
|
|
return new.join(li)
|
|
|
|
|
2022-11-28 14:01:40 +00:00
|
|
|
def paginate_iterator(iter_elems, nb_obj=50, page=1):
|
2023-01-16 15:27:49 +00:00
|
|
|
dict_page = {'nb_all_elem': len(iter_elems)}
|
2022-11-28 14:01:40 +00:00
|
|
|
nb_pages = dict_page['nb_all_elem'] / nb_obj
|
|
|
|
if not nb_pages.is_integer():
|
|
|
|
nb_pages = int(nb_pages)+1
|
|
|
|
else:
|
|
|
|
nb_pages = int(nb_pages)
|
|
|
|
if page > nb_pages:
|
|
|
|
page = nb_pages
|
|
|
|
|
|
|
|
# multiple pages
|
|
|
|
if nb_pages > 1:
|
|
|
|
dict_page['list_elem'] = []
|
|
|
|
start = nb_obj*(page - 1)
|
|
|
|
stop = (nb_obj*page) - 1
|
|
|
|
current_index = 0
|
|
|
|
for elem in iter_elems:
|
|
|
|
if current_index > stop:
|
|
|
|
break
|
|
|
|
if start <= current_index <= stop:
|
|
|
|
dict_page['list_elem'].append(elem)
|
|
|
|
current_index += 1
|
|
|
|
stop += 1
|
|
|
|
if stop > dict_page['nb_all_elem']:
|
|
|
|
stop = dict_page['nb_all_elem']
|
|
|
|
|
|
|
|
else:
|
|
|
|
start = 0
|
|
|
|
stop = dict_page['nb_all_elem']
|
|
|
|
dict_page['list_elem'] = list(iter_elems)
|
|
|
|
dict_page['page'] = page
|
|
|
|
dict_page['nb_pages'] = nb_pages
|
|
|
|
# UI
|
|
|
|
dict_page['nb_first_elem'] = start+1
|
|
|
|
dict_page['nb_last_elem'] = stop
|
|
|
|
return dict_page
|