2020-08-12 09:28:36 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
2023-03-30 14:58:55 +02:00
|
|
|
"""
|
|
|
|
The Tracker_Yara trackers module
|
|
|
|
===================
|
|
|
|
|
|
|
|
"""
|
2021-06-02 16:04:52 +02:00
|
|
|
|
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
2020-08-12 09:28:36 +02:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import yara
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2021-06-02 16:04:52 +02:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
2022-10-25 16:25:19 +02:00
|
|
|
from lib.objects.Items import Item
|
2021-06-02 16:04:52 +02:00
|
|
|
from lib import Tracker
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2023-03-30 14:58:55 +02:00
|
|
|
from exporter.MailExporter import MailExporterTracker
|
|
|
|
from exporter.WebHookExporter import WebHookExporterTracker
|
2020-08-12 09:28:36 +02:00
|
|
|
|
|
|
|
|
2023-03-30 14:58:55 +02:00
|
|
|
class Tracker_Yara(AbstractModule):
|
2021-06-02 16:04:52 +02:00
|
|
|
"""
|
|
|
|
Tracker_Yara module for AIL framework
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
super(Tracker_Yara, self).__init__()
|
|
|
|
self.pending_seconds = 5
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
# Load Yara rules
|
|
|
|
self.rules = Tracker.reload_yara_rules()
|
|
|
|
self.last_refresh = time.time()
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
self.item = None
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2023-03-30 14:58:55 +02:00
|
|
|
# Exporter
|
|
|
|
self.exporters = {'mail': MailExporterTracker(),
|
|
|
|
'webhook': WebHookExporterTracker()}
|
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
self.redis_logger.info(f"Module: {self.module_name} Launched")
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2022-09-14 11:41:24 +02:00
|
|
|
def compute(self, item_id, item_content=None):
|
2020-08-12 09:28:36 +02:00
|
|
|
# refresh YARA list
|
2021-06-02 16:04:52 +02:00
|
|
|
if self.last_refresh < Tracker.get_tracker_last_updated_by_type('yara'):
|
|
|
|
self.rules = Tracker.reload_yara_rules()
|
|
|
|
self.last_refresh = time.time()
|
|
|
|
self.redis_logger.debug('Tracked set refreshed')
|
2020-08-12 09:28:36 +02:00
|
|
|
print('Tracked set refreshed')
|
2021-06-02 16:04:52 +02:00
|
|
|
|
|
|
|
self.item = Item(item_id)
|
2022-09-14 11:41:24 +02:00
|
|
|
if not item_content:
|
|
|
|
item_content = self.item.get_content()
|
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
try:
|
2022-10-25 16:25:19 +02:00
|
|
|
yara_match = self.rules.match(data=item_content, callback=self.yara_rules_match,
|
|
|
|
which_callbacks=yara.CALLBACK_MATCHES, timeout=60)
|
2021-06-02 16:04:52 +02:00
|
|
|
if yara_match:
|
2022-09-09 11:25:51 +02:00
|
|
|
self.redis_logger.warning(f'tracker yara: new match {self.item.get_id()}: {yara_match}')
|
2021-06-02 16:04:52 +02:00
|
|
|
print(f'{self.item.get_id()}: {yara_match}')
|
2023-03-30 14:58:55 +02:00
|
|
|
except yara.TimeoutError:
|
2021-06-02 16:04:52 +02:00
|
|
|
print(f'{self.item.get_id()}: yara scanning timed out')
|
|
|
|
self.redis_logger.info(f'{self.item.get_id()}: yara scanning timed out')
|
|
|
|
|
|
|
|
def yara_rules_match(self, data):
|
|
|
|
tracker_uuid = data['namespace']
|
|
|
|
item_id = self.item.get_id()
|
2023-03-30 14:58:55 +02:00
|
|
|
|
|
|
|
item = Item(item_id)
|
2021-06-17 14:48:26 +02:00
|
|
|
item_source = self.item.get_source()
|
2023-03-30 14:58:55 +02:00
|
|
|
|
|
|
|
tracker = Tracker.Tracker(tracker_uuid)
|
2021-06-17 14:48:26 +02:00
|
|
|
|
|
|
|
# Source Filtering
|
2023-03-30 14:58:55 +02:00
|
|
|
tracker_sources = tracker.get_sources()
|
2021-06-17 14:48:26 +02:00
|
|
|
if tracker_sources and item_source not in tracker_sources:
|
|
|
|
print(f'Source Filtering: {data["rule"]}')
|
|
|
|
return yara.CALLBACK_CONTINUE
|
|
|
|
|
2023-03-30 14:58:55 +02:00
|
|
|
Tracker.add_tracked_item(tracker_uuid, item_id) # TODO
|
2021-06-02 16:04:52 +02:00
|
|
|
|
|
|
|
# Tags
|
2023-03-30 14:58:55 +02:00
|
|
|
for tag in tracker.get_tags():
|
|
|
|
msg = f'{tag};{item_id}'
|
2021-06-02 16:04:52 +02:00
|
|
|
self.send_message_to_queue(msg, 'Tags')
|
|
|
|
|
|
|
|
# Mails
|
2023-03-30 14:58:55 +02:00
|
|
|
if tracker.mail_export():
|
|
|
|
# TODO add matches + custom subjects
|
|
|
|
self.exporters['mail'].export(tracker, item)
|
2021-09-30 14:20:08 +02:00
|
|
|
|
|
|
|
# Webhook
|
2023-03-30 14:58:55 +02:00
|
|
|
if tracker.webhook_export():
|
|
|
|
self.exporters['webhook'].export(tracker, item)
|
2021-10-04 12:55:40 +02:00
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
return yara.CALLBACK_CONTINUE
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
module = Tracker_Yara()
|
2021-07-20 14:55:45 +02:00
|
|
|
module.run()
|