2020-08-12 07:28:36 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
2023-03-30 12:58:55 +00:00
|
|
|
"""
|
|
|
|
The Tracker_Yara trackers module
|
|
|
|
===================
|
|
|
|
|
|
|
|
"""
|
2021-06-02 14:04:52 +00:00
|
|
|
|
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
2020-08-12 07:28:36 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import yara
|
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2021-06-02 14:04:52 +00:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
2023-05-04 14:35:56 +00:00
|
|
|
from lib.objects import ail_objects
|
2021-06-02 14:04:52 +00:00
|
|
|
from lib import Tracker
|
2020-08-12 07:28:36 +00:00
|
|
|
|
2023-03-30 12:58:55 +00:00
|
|
|
from exporter.MailExporter import MailExporterTracker
|
|
|
|
from exporter.WebHookExporter import WebHookExporterTracker
|
2020-08-12 07:28:36 +00:00
|
|
|
|
|
|
|
|
2023-03-30 12:58:55 +00:00
|
|
|
class Tracker_Yara(AbstractModule):
|
2021-06-02 14:04:52 +00:00
|
|
|
"""
|
|
|
|
Tracker_Yara module for AIL framework
|
|
|
|
"""
|
2023-04-13 12:42:57 +00:00
|
|
|
def __init__(self, queue=True):
|
|
|
|
super(Tracker_Yara, self).__init__(queue=queue)
|
2021-06-02 14:04:52 +00:00
|
|
|
self.pending_seconds = 5
|
2020-08-12 07:28:36 +00:00
|
|
|
|
2021-06-02 14:04:52 +00:00
|
|
|
# Load Yara rules
|
2023-05-04 14:35:56 +00:00
|
|
|
self.rules = Tracker.get_tracked_yara_rules()
|
2021-06-02 14:04:52 +00:00
|
|
|
self.last_refresh = time.time()
|
2020-08-12 07:28:36 +00:00
|
|
|
|
2023-05-04 14:35:56 +00:00
|
|
|
self.obj = None
|
2020-08-12 07:28:36 +00:00
|
|
|
|
2023-03-30 12:58:55 +00:00
|
|
|
# Exporter
|
|
|
|
self.exporters = {'mail': MailExporterTracker(),
|
|
|
|
'webhook': WebHookExporterTracker()}
|
|
|
|
|
2021-06-02 14:04:52 +00:00
|
|
|
self.redis_logger.info(f"Module: {self.module_name} Launched")
|
2020-08-12 07:28:36 +00:00
|
|
|
|
2023-06-22 13:38:04 +00:00
|
|
|
def compute(self, message):
|
2020-08-12 07:28:36 +00:00
|
|
|
# refresh YARA list
|
2021-06-02 14:04:52 +00:00
|
|
|
if self.last_refresh < Tracker.get_tracker_last_updated_by_type('yara'):
|
2023-05-04 14:35:56 +00:00
|
|
|
self.rules = Tracker.get_tracked_yara_rules()
|
2021-06-02 14:04:52 +00:00
|
|
|
self.last_refresh = time.time()
|
|
|
|
self.redis_logger.debug('Tracked set refreshed')
|
2020-08-12 07:28:36 +00:00
|
|
|
print('Tracked set refreshed')
|
2021-06-02 14:04:52 +00:00
|
|
|
|
2023-06-22 13:38:04 +00:00
|
|
|
self.obj = self.get_obj()
|
2023-05-04 14:35:56 +00:00
|
|
|
obj_type = self.obj.get_type()
|
|
|
|
|
|
|
|
# Object Filter
|
|
|
|
if obj_type not in self.rules:
|
|
|
|
return None
|
|
|
|
|
2023-05-12 13:29:53 +00:00
|
|
|
content = self.obj.get_content(r_type='bytes')
|
2022-09-14 09:41:24 +00:00
|
|
|
|
2021-06-02 14:04:52 +00:00
|
|
|
try:
|
2023-05-04 14:35:56 +00:00
|
|
|
yara_match = self.rules[obj_type].match(data=content, callback=self.yara_rules_match,
|
|
|
|
which_callbacks=yara.CALLBACK_MATCHES, timeout=60)
|
2021-06-02 14:04:52 +00:00
|
|
|
if yara_match:
|
2023-05-04 14:35:56 +00:00
|
|
|
self.redis_logger.warning(f'tracker yara: new match {self.obj.get_id()}: {yara_match}')
|
|
|
|
print(f'{self.obj.get_id()}: {yara_match}')
|
2023-03-30 12:58:55 +00:00
|
|
|
except yara.TimeoutError:
|
2023-05-04 14:35:56 +00:00
|
|
|
print(f'{self.obj.get_id()}: yara scanning timed out')
|
|
|
|
self.redis_logger.info(f'{self.obj.get_id()}: yara scanning timed out')
|
2021-06-02 14:04:52 +00:00
|
|
|
|
|
|
|
def yara_rules_match(self, data):
|
2023-05-04 14:35:56 +00:00
|
|
|
tracker_name = data['namespace']
|
|
|
|
obj_id = self.obj.get_id()
|
|
|
|
for tracker_uuid in Tracker.get_trackers_by_tracked_obj_type('yara', self.obj.get_type(), tracker_name):
|
|
|
|
tracker = Tracker.Tracker(tracker_uuid)
|
|
|
|
|
|
|
|
# Filter Object
|
|
|
|
filters = tracker.get_filters()
|
|
|
|
if ail_objects.is_filtered(self.obj, filters):
|
|
|
|
continue
|
|
|
|
|
2023-05-11 14:21:43 +00:00
|
|
|
tracker.add(self.obj.get_type(), self.obj.get_subtype(r_str=True), obj_id)
|
2023-05-04 14:35:56 +00:00
|
|
|
|
|
|
|
# Tags
|
|
|
|
for tag in tracker.get_tags():
|
|
|
|
if self.obj.get_type() == 'item':
|
2023-06-22 13:38:04 +00:00
|
|
|
self.add_message_to_queue(message=tag, queue='Tags')
|
2023-05-04 14:35:56 +00:00
|
|
|
else:
|
|
|
|
self.obj.add_tag(tag)
|
|
|
|
|
|
|
|
# Mails
|
|
|
|
if tracker.mail_export():
|
|
|
|
# TODO add matches + custom subjects
|
|
|
|
self.exporters['mail'].export(tracker, self.obj)
|
|
|
|
|
|
|
|
# Webhook
|
|
|
|
if tracker.webhook_export():
|
|
|
|
self.exporters['webhook'].export(tracker, self.obj)
|
2021-10-04 10:55:40 +00:00
|
|
|
|
2021-06-02 14:04:52 +00:00
|
|
|
return yara.CALLBACK_CONTINUE
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
module = Tracker_Yara()
|
2021-07-20 12:55:45 +00:00
|
|
|
module.run()
|