2021-06-02 14:53:17 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
|
|
|
The Tracker_Regex trackers module
|
|
|
|
===================
|
|
|
|
|
|
|
|
This Module is used for regex tracking.
|
2021-09-30 12:20:08 +00:00
|
|
|
It processes every item coming from the global module and test the regex
|
2021-06-02 14:53:17 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
2022-10-25 14:25:19 +00:00
|
|
|
from lib.objects.Items import Item
|
2021-06-02 14:53:17 +00:00
|
|
|
from packages import Term
|
|
|
|
from lib import Tracker
|
|
|
|
|
2023-02-15 10:02:47 +00:00
|
|
|
from exporter.MailExporter import MailExporterTracker
|
|
|
|
from exporter.WebHookExporter import WebHookExporterTracker
|
2021-06-02 14:53:17 +00:00
|
|
|
|
|
|
|
class Tracker_Regex(AbstractModule):
|
|
|
|
"""
|
|
|
|
Tracker_Regex module for AIL framework
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
super(Tracker_Regex, self).__init__()
|
|
|
|
|
|
|
|
self.pending_seconds = 5
|
|
|
|
|
|
|
|
self.max_execution_time = self.process.config.getint(self.module_name, "max_execution_time")
|
|
|
|
|
2021-06-17 12:48:26 +00:00
|
|
|
# refresh Tracked Regex
|
2021-06-02 14:53:17 +00:00
|
|
|
self.dict_regex_tracked = Term.get_regex_tracked_words_dict()
|
|
|
|
self.last_refresh = time.time()
|
|
|
|
|
2023-02-15 10:02:47 +00:00
|
|
|
# Exporter
|
|
|
|
self.exporters = {'mail': MailExporterTracker(),
|
|
|
|
'webhook': WebHookExporterTracker()}
|
|
|
|
|
2021-06-02 14:53:17 +00:00
|
|
|
self.redis_logger.info(f"Module: {self.module_name} Launched")
|
|
|
|
|
2023-03-16 14:50:42 +00:00
|
|
|
def compute(self, item_id, content=None):
|
2021-06-02 14:53:17 +00:00
|
|
|
# refresh Tracked regex
|
2021-06-17 12:48:26 +00:00
|
|
|
if self.last_refresh < Tracker.get_tracker_last_updated_by_type('regex'):
|
2021-06-02 14:53:17 +00:00
|
|
|
self.dict_regex_tracked = Term.get_regex_tracked_words_dict()
|
|
|
|
self.last_refresh = time.time()
|
2021-06-17 12:48:26 +00:00
|
|
|
self.redis_logger.debug('Tracked regex refreshed')
|
|
|
|
print('Tracked regex refreshed')
|
2021-06-02 14:53:17 +00:00
|
|
|
|
|
|
|
item = Item(item_id)
|
|
|
|
item_id = item.get_id()
|
2023-03-16 14:50:42 +00:00
|
|
|
if not content:
|
|
|
|
content = item.get_content()
|
2021-06-02 14:53:17 +00:00
|
|
|
|
|
|
|
for regex in self.dict_regex_tracked:
|
2023-02-15 10:02:47 +00:00
|
|
|
matched = self.regex_findall(self.dict_regex_tracked[regex], item_id, content)
|
2021-06-02 14:53:17 +00:00
|
|
|
if matched:
|
2021-06-17 12:48:26 +00:00
|
|
|
self.new_tracker_found(regex, 'regex', item)
|
2021-06-02 14:53:17 +00:00
|
|
|
|
2023-02-15 10:02:47 +00:00
|
|
|
def new_tracker_found(self, tracker_name, tracker_type, item):
|
|
|
|
uuid_list = Tracker.get_tracker_uuid_list(tracker_name, tracker_type)
|
2021-06-17 12:48:26 +00:00
|
|
|
|
|
|
|
item_id = item.get_id()
|
2023-02-15 10:02:47 +00:00
|
|
|
# date = item.get_date()
|
2022-02-16 09:19:53 +00:00
|
|
|
item_source = item.get_source()
|
2021-06-02 14:53:17 +00:00
|
|
|
|
|
|
|
for tracker_uuid in uuid_list:
|
2023-02-15 10:02:47 +00:00
|
|
|
tracker = Tracker.Tracker(tracker_uuid)
|
|
|
|
|
2021-06-17 12:48:26 +00:00
|
|
|
# Source Filtering
|
2023-02-15 10:02:47 +00:00
|
|
|
tracker_sources = tracker.get_sources()
|
2021-06-17 12:48:26 +00:00
|
|
|
if tracker_sources and item_source not in tracker_sources:
|
|
|
|
continue
|
|
|
|
|
2023-03-30 12:58:55 +00:00
|
|
|
print(f'new tracked regex found: {tracker_name} in {item_id}')
|
|
|
|
self.redis_logger.warning(f'new tracked regex found: {tracker_name} in {item_id}')
|
|
|
|
# TODO
|
|
|
|
Tracker.add_tracked_item(tracker_uuid, item_id)
|
2021-06-17 12:48:26 +00:00
|
|
|
|
2023-02-15 10:02:47 +00:00
|
|
|
for tag in tracker.get_tags():
|
2021-06-17 12:48:26 +00:00
|
|
|
msg = f'{tag};{item_id}'
|
2021-06-02 14:53:17 +00:00
|
|
|
self.send_message_to_queue(msg, 'Tags')
|
|
|
|
|
2023-02-15 10:02:47 +00:00
|
|
|
if tracker.mail_export():
|
|
|
|
# TODO add matches + custom subjects
|
|
|
|
self.exporters['mail'].export(tracker, item)
|
|
|
|
|
|
|
|
if tracker.webhook_export():
|
|
|
|
self.exporters['webhook'].export(tracker, item)
|
2021-06-02 14:53:17 +00:00
|
|
|
|
2021-09-30 12:20:08 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-06-02 14:53:17 +00:00
|
|
|
module = Tracker_Regex()
|
2021-07-20 12:55:45 +00:00
|
|
|
module.run()
|