2021-06-02 16:53:17 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
The Tracker_Regex trackers module
|
|
|
|
===================
|
|
|
|
|
|
|
|
This Module is used for regex tracking.
|
2021-09-30 14:20:08 +02:00
|
|
|
It processes every item coming from the global module and test the regex
|
2021-06-02 16:53:17 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
2023-05-04 16:35:56 +02:00
|
|
|
from lib.objects import ail_objects
|
2023-04-13 14:25:02 +02:00
|
|
|
from lib.ConfigLoader import ConfigLoader
|
2021-06-02 16:53:17 +02:00
|
|
|
from lib import Tracker
|
|
|
|
|
2023-02-15 11:02:47 +01:00
|
|
|
from exporter.MailExporter import MailExporterTracker
|
|
|
|
from exporter.WebHookExporter import WebHookExporterTracker
|
2021-06-02 16:53:17 +02:00
|
|
|
|
|
|
|
class Tracker_Regex(AbstractModule):
|
|
|
|
"""
|
|
|
|
Tracker_Regex module for AIL framework
|
|
|
|
"""
|
2023-04-13 14:42:57 +02:00
|
|
|
def __init__(self, queue=True):
|
|
|
|
super(Tracker_Regex, self).__init__(queue=queue)
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2023-04-13 14:25:02 +02:00
|
|
|
config_loader = ConfigLoader()
|
|
|
|
|
2021-06-02 16:53:17 +02:00
|
|
|
self.pending_seconds = 5
|
|
|
|
|
2023-04-13 14:25:02 +02:00
|
|
|
self.max_execution_time = config_loader.get_config_int(self.module_name, "max_execution_time")
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2021-06-17 14:48:26 +02:00
|
|
|
# refresh Tracked Regex
|
2023-05-04 16:35:56 +02:00
|
|
|
self.tracked_regexs = Tracker.get_tracked_regexs()
|
2021-06-02 16:53:17 +02:00
|
|
|
self.last_refresh = time.time()
|
|
|
|
|
2023-08-24 14:37:50 +02:00
|
|
|
self.obj = None
|
|
|
|
|
2023-02-15 11:02:47 +01:00
|
|
|
# Exporter
|
|
|
|
self.exporters = {'mail': MailExporterTracker(),
|
|
|
|
'webhook': WebHookExporterTracker()}
|
|
|
|
|
2021-06-02 16:53:17 +02:00
|
|
|
self.redis_logger.info(f"Module: {self.module_name} Launched")
|
|
|
|
|
2023-06-22 15:38:04 +02:00
|
|
|
def compute(self, message):
|
2021-06-02 16:53:17 +02:00
|
|
|
# refresh Tracked regex
|
2021-06-17 14:48:26 +02:00
|
|
|
if self.last_refresh < Tracker.get_tracker_last_updated_by_type('regex'):
|
2023-05-04 16:35:56 +02:00
|
|
|
self.tracked_regexs = Tracker.get_tracked_regexs()
|
2021-06-02 16:53:17 +02:00
|
|
|
self.last_refresh = time.time()
|
2021-06-17 14:48:26 +02:00
|
|
|
self.redis_logger.debug('Tracked regex refreshed')
|
|
|
|
print('Tracked regex refreshed')
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2023-06-22 15:38:04 +02:00
|
|
|
obj = self.get_obj()
|
2023-05-04 16:35:56 +02:00
|
|
|
obj_id = obj.get_id()
|
|
|
|
obj_type = obj.get_type()
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2023-05-04 16:35:56 +02:00
|
|
|
# Object Filter
|
|
|
|
if obj_type not in self.tracked_regexs:
|
|
|
|
return None
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2023-05-12 15:29:53 +02:00
|
|
|
content = obj.get_content()
|
2021-06-17 14:48:26 +02:00
|
|
|
|
2023-05-04 16:35:56 +02:00
|
|
|
for dict_regex in self.tracked_regexs[obj_type]:
|
2023-08-24 14:37:50 +02:00
|
|
|
matches = self.regex_finditer(dict_regex['regex'], obj_id, content)
|
|
|
|
if matches:
|
|
|
|
self.new_tracker_found(dict_regex['tracked'], 'regex', obj, matches)
|
|
|
|
|
|
|
|
def extract_matches(self, re_matches, limit=500, lines=5):
|
|
|
|
matches = []
|
|
|
|
content = self.obj.get_content()
|
|
|
|
l_content = len(content)
|
|
|
|
for match in re_matches:
|
|
|
|
start = match[0]
|
|
|
|
value = match[2]
|
|
|
|
end = match[1]
|
|
|
|
|
|
|
|
# Start
|
|
|
|
if start > limit:
|
|
|
|
i_start = start - limit
|
|
|
|
else:
|
|
|
|
i_start = 0
|
|
|
|
str_start = content[i_start:start].splitlines()
|
|
|
|
if len(str_start) > lines:
|
|
|
|
str_start = '\n'.join(str_start[-lines + 1:])
|
|
|
|
else:
|
|
|
|
str_start = content[i_start:start]
|
|
|
|
|
|
|
|
# End
|
|
|
|
if end + limit > l_content:
|
|
|
|
i_end = l_content
|
|
|
|
else:
|
|
|
|
i_end = end + limit
|
|
|
|
str_end = content[end:i_end].splitlines()
|
|
|
|
if len(str_end) > lines:
|
|
|
|
str_end = '\n'.join(str_end[:lines + 1])
|
|
|
|
else:
|
|
|
|
str_end = content[end:i_end]
|
|
|
|
matches.append((value, f'{str_start}{value}{str_end}'))
|
|
|
|
return matches
|
|
|
|
|
|
|
|
def new_tracker_found(self, tracker_name, tracker_type, obj, re_matches):
|
2023-05-04 16:35:56 +02:00
|
|
|
obj_id = obj.get_id()
|
2023-08-24 14:37:50 +02:00
|
|
|
matches = None
|
2023-05-04 16:35:56 +02:00
|
|
|
for tracker_uuid in Tracker.get_trackers_by_tracked_obj_type(tracker_type, obj.get_type(), tracker_name):
|
2023-02-15 11:02:47 +01:00
|
|
|
tracker = Tracker.Tracker(tracker_uuid)
|
|
|
|
|
2023-05-04 16:35:56 +02:00
|
|
|
# Filter Object
|
|
|
|
filters = tracker.get_filters()
|
|
|
|
if ail_objects.is_filtered(obj, filters):
|
2021-06-17 14:48:26 +02:00
|
|
|
continue
|
|
|
|
|
2024-03-13 11:58:40 +01:00
|
|
|
print(f'new tracked regex found: {tracker_name} in {self.obj.get_global_id()}')
|
|
|
|
self.redis_logger.warning(f'new tracked regex found: {tracker_name} in {self.obj.get_global_id()}')
|
2023-05-04 16:35:56 +02:00
|
|
|
|
2023-05-11 16:21:43 +02:00
|
|
|
tracker.add(obj.get_type(), obj.get_subtype(r_str=True), obj_id)
|
2021-06-17 14:48:26 +02:00
|
|
|
|
2023-02-15 11:02:47 +01:00
|
|
|
for tag in tracker.get_tags():
|
2023-05-04 16:35:56 +02:00
|
|
|
if obj.get_type() == 'item':
|
2023-06-22 15:38:04 +02:00
|
|
|
self.add_message_to_queue(message=tag, queue='Tags')
|
2023-05-04 16:35:56 +02:00
|
|
|
else:
|
|
|
|
obj.add_tag(tag)
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2023-02-15 11:02:47 +01:00
|
|
|
if tracker.mail_export():
|
2023-08-24 14:37:50 +02:00
|
|
|
if not matches:
|
|
|
|
matches = self.extract_matches(re_matches)
|
|
|
|
self.exporters['mail'].export(tracker, obj, matches)
|
2023-02-15 11:02:47 +01:00
|
|
|
|
|
|
|
if tracker.webhook_export():
|
2024-07-10 15:10:07 +02:00
|
|
|
if not matches:
|
|
|
|
matches = self.extract_matches(re_matches)
|
|
|
|
self.exporters['webhook'].export(tracker, obj, matches)
|
2021-06-02 16:53:17 +02:00
|
|
|
|
2021-09-30 14:20:08 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-06-02 16:53:17 +02:00
|
|
|
module = Tracker_Regex()
|
2021-07-20 14:55:45 +02:00
|
|
|
module.run()
|