2020-08-12 09:28:36 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
2021-09-30 14:20:08 +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 re
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
import yara
|
2021-09-28 22:53:01 +02:00
|
|
|
import requests
|
2020-08-12 09:28:36 +02:00
|
|
|
|
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2021-06-02 16:04:52 +02:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
|
|
|
from packages import Term
|
|
|
|
from packages.Item import Item
|
|
|
|
from lib import Tracker
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2021-09-30 14:20:08 +02:00
|
|
|
import NotificationHelper # # TODO: refactor
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
class Tracker_Yara(AbstractModule):
|
2020-08-12 09:28:36 +02:00
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
mail_body_template = "AIL Framework,\nNew YARA match: {}\nitem id: {}\nurl: {}{}"
|
2020-08-12 09:28:36 +02:00
|
|
|
|
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
|
|
|
self.full_item_url = self.process.config.get("Notifications", "ail_domain") + "/object/item?id="
|
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
|
|
|
|
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
|
|
|
|
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
def compute(self, item_id):
|
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)
|
|
|
|
item_content = self.item.get_content()
|
|
|
|
try:
|
|
|
|
yara_match = self.rules.match(data=item_content, callback=self.yara_rules_match, which_callbacks=yara.CALLBACK_MATCHES, timeout=60)
|
|
|
|
if yara_match:
|
|
|
|
self.redis_logger.info(f'{self.item.get_id()}: {yara_match}')
|
|
|
|
print(f'{self.item.get_id()}: {yara_match}')
|
|
|
|
except yara.TimeoutError as e:
|
|
|
|
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()
|
2021-06-17 14:48:26 +02:00
|
|
|
item_source = self.item.get_source()
|
2021-09-30 14:20:08 +02:00
|
|
|
item_date = self.item.get_date()
|
2021-06-17 14:48:26 +02:00
|
|
|
|
|
|
|
# Source Filtering
|
|
|
|
tracker_sources = Tracker.get_tracker_uuid_sources(tracker_uuid)
|
|
|
|
if tracker_sources and item_source not in tracker_sources:
|
|
|
|
print(f'Source Filtering: {data["rule"]}')
|
|
|
|
return yara.CALLBACK_CONTINUE
|
|
|
|
|
2021-07-14 13:58:00 +02:00
|
|
|
Tracker.add_tracked_item(tracker_uuid, item_id)
|
2021-06-02 16:04:52 +02:00
|
|
|
|
|
|
|
# Tags
|
|
|
|
tags_to_add = Tracker.get_tracker_tags(tracker_uuid)
|
|
|
|
for tag in tags_to_add:
|
|
|
|
msg = '{};{}'.format(tag, item_id)
|
|
|
|
self.send_message_to_queue(msg, 'Tags')
|
|
|
|
|
|
|
|
# Mails
|
|
|
|
mail_to_notify = Tracker.get_tracker_mails(tracker_uuid)
|
|
|
|
if mail_to_notify:
|
|
|
|
mail_subject = Tracker.get_email_subject(tracker_uuid)
|
|
|
|
mail_body = Tracker_Yara.mail_body_template.format(data['rule'], item_id, self.full_item_url, item_id)
|
|
|
|
for mail in mail_to_notify:
|
|
|
|
self.redis_logger.debug(f'Send Mail {mail_subject}')
|
|
|
|
print(f'Send Mail {mail_subject}')
|
|
|
|
NotificationHelper.sendEmailNotification(mail, mail_subject, mail_body)
|
2021-09-30 14:20:08 +02:00
|
|
|
|
|
|
|
# Webhook
|
2021-09-28 22:53:01 +02:00
|
|
|
webhook_to_post = Term.get_term_webhook(tracker_uuid)
|
|
|
|
if webhook_to_post:
|
2021-09-30 14:20:08 +02:00
|
|
|
json_request = {"trackerId": tracker_uuid,
|
|
|
|
"itemId": item_id,
|
|
|
|
"itemURL": self.full_item_url + item_id,
|
|
|
|
"dataRule": data["rule"],
|
|
|
|
"itemSource": item_source,
|
|
|
|
"itemDate": item_date,
|
|
|
|
"tags": tags_to_add,
|
|
|
|
"emailNotification": f'{mail_to_notify}',
|
|
|
|
"trackerType": "yara"
|
|
|
|
}
|
2021-10-04 12:55:40 +02:00
|
|
|
try:
|
|
|
|
response = requests.post(webhook_to_post, json=json_request)
|
|
|
|
if response.status_code >= 400:
|
|
|
|
self.redis_logger.error(f"Webhook request failed for {webhook_to_post}\nReason: {response.reason}")
|
|
|
|
except:
|
|
|
|
self.redis_logger.error(f"Webhook request failed for {webhook_to_post}\nReason: Something went wrong")
|
|
|
|
|
2021-09-30 14:20:08 +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()
|