2022-03-07 15:12:01 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
2022-10-25 16:25:19 +02:00
|
|
|
import base64
|
2022-03-07 15:12:01 +01:00
|
|
|
import os
|
2023-05-04 16:35:56 +02:00
|
|
|
import re
|
2022-03-07 15:12:01 +01:00
|
|
|
import sys
|
|
|
|
|
2022-10-25 16:25:19 +02:00
|
|
|
from hashlib import sha256
|
2022-07-08 09:47:47 +02:00
|
|
|
from io import BytesIO
|
2022-08-19 16:53:31 +02:00
|
|
|
from flask import url_for
|
2023-06-04 17:34:21 +02:00
|
|
|
from pymisp import MISPObject
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2022-07-08 09:47:47 +02:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2022-10-25 16:25:19 +02:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
2022-07-08 09:47:47 +02:00
|
|
|
from lib.ConfigLoader import ConfigLoader
|
|
|
|
from lib.objects.abstract_object import AbstractObject
|
2022-12-19 16:38:20 +01:00
|
|
|
# from lib import data_retention_engine
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2022-07-08 09:47:47 +02:00
|
|
|
config_loader = ConfigLoader()
|
2022-11-29 16:01:01 +01:00
|
|
|
r_serv_metadata = config_loader.get_db_conn("Kvrocks_Objects")
|
2022-07-08 09:47:47 +02:00
|
|
|
SCREENSHOT_FOLDER = config_loader.get_files_directory('screenshot')
|
2022-03-07 15:12:01 +01:00
|
|
|
config_loader = None
|
|
|
|
|
2022-10-25 16:25:19 +02:00
|
|
|
|
2022-03-07 15:12:01 +01:00
|
|
|
class Screenshot(AbstractObject):
|
|
|
|
"""
|
|
|
|
AIL Screenshot Object. (strings)
|
|
|
|
"""
|
|
|
|
|
2022-07-08 09:47:47 +02:00
|
|
|
# ID = SHA256
|
2022-10-25 16:25:19 +02:00
|
|
|
def __init__(self, screenshot_id):
|
|
|
|
super(Screenshot, self).__init__('screenshot', screenshot_id)
|
2022-03-07 15:12:01 +01: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
|
|
|
|
|
2022-09-20 16:11:48 +02:00
|
|
|
def exists(self):
|
|
|
|
return os.path.isfile(self.get_filepath())
|
|
|
|
|
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, 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}&id={self.id}'
|
2022-03-07 15:12:01 +01:00
|
|
|
return url
|
|
|
|
|
|
|
|
def get_svg_icon(self):
|
2022-10-25 16:25:19 +02:00
|
|
|
return {'style': 'fas', 'icon': '\uf03e', 'color': '#E1F5DF', 'radius': 5}
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2022-07-08 09:47:47 +02:00
|
|
|
def get_rel_path(self, add_extension=False):
|
|
|
|
rel_path = os.path.join(self.id[0:2], self.id[2:4], self.id[4:6], self.id[6:8], self.id[8:10], self.id[10:12], self.id[12:])
|
|
|
|
if add_extension:
|
|
|
|
rel_path = f'{rel_path}.png'
|
|
|
|
return rel_path
|
|
|
|
|
|
|
|
def get_filepath(self):
|
|
|
|
filename = os.path.join(SCREENSHOT_FOLDER, self.get_rel_path(add_extension=True))
|
|
|
|
return os.path.realpath(filename)
|
|
|
|
|
|
|
|
def get_file_content(self):
|
|
|
|
filepath = self.get_filepath()
|
|
|
|
with open(filepath, 'rb') as f:
|
|
|
|
file_content = BytesIO(f.read())
|
|
|
|
return file_content
|
|
|
|
|
2023-05-04 16:35:56 +02:00
|
|
|
def get_content(self):
|
|
|
|
return self.get_file_content()
|
|
|
|
|
2022-07-08 09:47:47 +02:00
|
|
|
def get_misp_object(self):
|
|
|
|
obj_attrs = []
|
|
|
|
obj = MISPObject('file')
|
|
|
|
|
2023-06-09 11:19:22 +02:00
|
|
|
obj_attrs.append(obj.add_attribute('sha256', value=self.id))
|
|
|
|
obj_attrs.append(obj.add_attribute('attachment', value=self.id, data=self.get_file_content()))
|
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-08-19 16:53:31 +02:00
|
|
|
def get_meta(self, options=set()):
|
2023-06-26 16:28:31 +02:00
|
|
|
meta = self.get_default_meta()
|
2022-10-25 16:25:19 +02:00
|
|
|
meta['img'] = get_screenshot_rel_path(self.id) ######### # TODO: Rename ME ??????
|
2022-09-20 16:11:48 +02:00
|
|
|
meta['tags'] = self.get_tags(r_list=True)
|
2023-04-21 15:38:48 +02:00
|
|
|
if 'tags_safe' in options:
|
|
|
|
meta['tags_safe'] = self.is_tags_safe(meta['tags'])
|
2022-08-19 16:53:31 +02:00
|
|
|
return meta
|
|
|
|
|
|
|
|
def get_screenshot_dir():
|
|
|
|
return SCREENSHOT_FOLDER
|
|
|
|
|
|
|
|
# get screenshot relative path
|
|
|
|
def get_screenshot_rel_path(sha256_str, add_extension=False):
|
2022-10-25 16:25:19 +02:00
|
|
|
screenshot_path = os.path.join(sha256_str[0:2], sha256_str[2:4], sha256_str[4:6], sha256_str[6:8], sha256_str[8:10], sha256_str[10:12], sha256_str[12:])
|
2022-08-19 16:53:31 +02:00
|
|
|
if add_extension:
|
|
|
|
screenshot_path = f'{screenshot_path}.png'
|
|
|
|
return screenshot_path
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_screenshots():
|
|
|
|
screenshots = []
|
|
|
|
screenshot_dir = os.path.join(os.environ['AIL_HOME'], SCREENSHOT_FOLDER)
|
|
|
|
for root, dirs, files in os.walk(screenshot_dir):
|
|
|
|
for file in files:
|
|
|
|
screenshot_path = f'{root}{file}'
|
|
|
|
screenshot_id = screenshot_path.replace(SCREENSHOT_FOLDER, '').replace('/', '')[:-4]
|
|
|
|
screenshots.append(screenshot_id)
|
|
|
|
return screenshots
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2022-10-25 16:25:19 +02:00
|
|
|
# FIXME STR SIZE LIMIT
|
|
|
|
def create_screenshot(content, size_limit=5000000, b64=True, force=False):
|
|
|
|
size = (len(content)*3) / 4
|
|
|
|
if size <= size_limit or size_limit < 0 or force:
|
|
|
|
if b64:
|
|
|
|
content = base64.standard_b64decode(content.encode())
|
|
|
|
screenshot_id = sha256(content).hexdigest()
|
|
|
|
screenshot = Screenshot(screenshot_id)
|
|
|
|
if not screenshot.exists():
|
|
|
|
filepath = screenshot.get_filepath()
|
|
|
|
dirname = os.path.dirname(filepath)
|
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
|
|
|
with open(filepath, 'wb') as f:
|
|
|
|
f.write(content)
|
|
|
|
return screenshot
|
|
|
|
return None
|
2022-03-07 15:12:01 +01:00
|
|
|
|
2023-05-04 16:35:56 +02:00
|
|
|
def sanitize_screenshot_name_to_search(name_to_search): # TODO FILTER NAME
|
|
|
|
return name_to_search
|
|
|
|
|
|
|
|
def search_screenshots_by_name(name_to_search, r_pos=False):
|
|
|
|
screenshots = {}
|
|
|
|
# for subtype in subtypes:
|
|
|
|
r_name = sanitize_screenshot_name_to_search(name_to_search)
|
|
|
|
if not name_to_search or isinstance(r_name, dict):
|
|
|
|
return screenshots
|
|
|
|
r_name = re.compile(r_name)
|
|
|
|
for screenshot_name in get_all_screenshots():
|
|
|
|
res = re.search(r_name, screenshot_name)
|
|
|
|
if res:
|
|
|
|
screenshots[screenshot_name] = {}
|
|
|
|
if r_pos:
|
|
|
|
screenshots[screenshot_name]['hl-start'] = res.start()
|
|
|
|
screenshots[screenshot_name]['hl-end'] = res.end()
|
|
|
|
return screenshots
|
|
|
|
|
|
|
|
|
|
|
|
# if __name__ == '__main__':
|
|
|
|
# name_to_search = '29ba'
|
|
|
|
# print(search_screenshots_by_name(name_to_search))
|