ail-framework/bin/trackers/Tracker_Typo_Squatting.py

119 lines
4.5 KiB
Python
Raw Normal View History

2022-06-07 14:18:52 +00:00
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
2022-07-08 08:55:19 +00:00
2022-06-07 14:18:52 +00:00
"""
2022-06-15 13:28:34 +00:00
The Tracker_Typo_Squatting Module
2022-06-07 14:18:52 +00:00
===================
"""
##################################
# Import External packages
##################################
import os
import sys
import time
import requests
sys.path.append(os.environ['AIL_BIN'])
##################################
# Import Project packages
##################################
from modules.abstract_module import AbstractModule
import NotificationHelper
from packages.Item import Item
from lib import Tracker
2022-06-15 13:28:34 +00:00
class Tracker_Typo_Squatting(AbstractModule):
2022-06-07 14:18:52 +00:00
mail_body_template = "AIL Framework,\nNew occurrence for tracked Typo: {}\nitem id: {}\nurl: {}{}"
"""
2022-06-15 13:28:34 +00:00
Tracker_Typo_Squatting module for AIL framework
2022-06-07 14:18:52 +00:00
"""
def __init__(self):
2022-06-15 13:28:34 +00:00
super(Tracker_Typo_Squatting, self).__init__()
2022-06-07 14:18:52 +00:00
self.pending_seconds = 5
self.full_item_url = self.process.config.get("Notifications", "ail_domain") + "/object/item?id="
# loads typosquatting
2022-06-07 14:18:52 +00:00
self.typosquat_tracked_words_list = Tracker.get_typosquatting_tracked_words_list()
self.last_refresh_typosquat = time.time()
self.redis_logger.info(f"Module: {self.module_name} Launched")
def compute(self, message):
# refresh Tracked typo
2022-07-08 08:55:19 +00:00
if self.last_refresh_typosquat < Tracker.get_tracker_last_updated_by_type('typosquatting'):
2022-06-07 14:18:52 +00:00
self.typosquat_tracked_words_list = Tracker.get_typosquatting_tracked_words_list()
self.last_refresh_typosquat = time.time()
self.redis_logger.debug('Tracked typosquatting refreshed')
print('Tracked typosquatting refreshed')
host, id = message.split()
2022-07-08 08:55:19 +00:00
2022-06-07 14:18:52 +00:00
# Cast message as Item
2022-07-08 09:27:04 +00:00
for tracker in self.typosquat_tracked_words_list:
if host in self.typosquat_tracked_words_list[tracker]:
item = Item(id)
self.new_tracker_found(tracker, 'typosquatting', item)
2022-06-07 14:18:52 +00:00
2022-07-08 08:55:19 +00:00
def new_tracker_found(self, tracker, tracker_type, item):
2022-06-07 14:18:52 +00:00
item_id = item.get_id()
item_date = item.get_date()
item_source = item.get_source()
2022-07-08 08:55:19 +00:00
#self.redis_logger.info(f'new tracked typo found: {tracker} in {item_id}')
print(f'new tracked typosquatting found: {tracker} in {item_id}')
print(Tracker.get_tracker_uuid_list(tracker, tracker_type))
for tracker_uuid in Tracker.get_tracker_uuid_list(tracker, tracker_type):
# Source Filtering
tracker_sources = Tracker.get_tracker_uuid_sources(tracker)
if tracker_sources and item_source not in tracker_sources:
continue
Tracker.add_tracked_item(tracker_uuid, item_id)
# Tags
tags_to_add = Tracker.get_tracker_tags(tracker_uuid)
for tag in tags_to_add:
msg = f'{tag};{item_id}'
self.send_message_to_queue(msg, 'Tags')
mail_to_notify = Tracker.get_tracker_mails(tracker_uuid)
if mail_to_notify:
mail_subject = Tracker.get_email_subject(tracker_uuid)
mail_body = Tracker_Typo_Squatting.mail_body_template.format(tracker, item_id, self.full_item_url, item_id)
for mail in mail_to_notify:
NotificationHelper.sendEmailNotification(mail, mail_subject, mail_body)
# Webhook
webhook_to_post = Tracker.get_tracker_webhook(tracker_uuid)
if webhook_to_post:
json_request = {"trackerId": tracker_uuid,
"itemId": item_id,
"itemURL": self.full_item_url + item_id,
"tracker": tracker,
"itemSource": item_source,
"itemDate": item_date,
"tags": tags_to_add,
"emailNotification": f'{mail_to_notify}',
"trackerType": tracker_type
}
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")
2022-06-07 14:18:52 +00:00
if __name__ == '__main__':
2022-06-15 13:28:34 +00:00
module = Tracker_Typo_Squatting()
2022-06-07 14:18:52 +00:00
module.run()
2022-07-08 08:55:19 +00:00
#module.compute('g00gle.com tests/2020/01/01/test.gz')