2018-05-04 11:53:29 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-08-06 09:43:40 +00:00
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
"""
|
2021-05-19 12:54:34 +00:00
|
|
|
The Onion Module
|
2014-08-06 09:43:40 +00:00
|
|
|
============================
|
|
|
|
|
2021-05-19 12:54:34 +00:00
|
|
|
This module extract url from item and returning only ones which are tor
|
|
|
|
related (.onion). All These urls are send to the crawler discovery queue.
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
Requirements
|
|
|
|
------------
|
|
|
|
|
|
|
|
*Need running Redis instances. (Redis)
|
|
|
|
|
|
|
|
"""
|
2014-08-31 20:42:12 +00:00
|
|
|
import os
|
2021-05-14 12:42:16 +00:00
|
|
|
import sys
|
2018-08-21 13:54:53 +00:00
|
|
|
import re
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2021-06-02 12:42:23 +00:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
|
|
|
from modules.abstract_module import AbstractModule
|
2021-05-14 12:42:16 +00:00
|
|
|
from lib.ConfigLoader import ConfigLoader
|
2022-10-25 14:25:19 +00:00
|
|
|
from lib.objects.Items import Item
|
2021-05-14 12:42:16 +00:00
|
|
|
from lib import crawlers
|
|
|
|
|
|
|
|
class Onion(AbstractModule):
|
|
|
|
"""docstring for Onion module."""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(Onion, self).__init__()
|
|
|
|
|
|
|
|
config_loader = ConfigLoader()
|
|
|
|
self.r_cache = config_loader.get_redis_conn("Redis_Cache")
|
|
|
|
|
|
|
|
self.pending_seconds = config_loader.get_config_int("Onion", "max_execution_time")
|
|
|
|
# regex timeout
|
|
|
|
self.regex_timeout = 30
|
|
|
|
|
|
|
|
self.faup = crawlers.get_faup()
|
|
|
|
|
|
|
|
# activate_crawler = p.config.get("Crawler", "activate_crawler")
|
|
|
|
|
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
self.onion_regex = r"((http|https|ftp)?(?:\://)?([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.onion)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*)"
|
|
|
|
# self.i2p_regex = r"((http|https|ftp)?(?:\://)?([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.i2p)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*)"
|
|
|
|
re.compile(self.onion_regex)
|
|
|
|
# re.compile(self.i2p_regex)
|
2021-05-14 12:42:16 +00:00
|
|
|
|
|
|
|
self.redis_logger.info(f"Module: {self.module_name} Launched")
|
|
|
|
|
|
|
|
# TEMP var: SAVE I2P Domain (future I2P crawler)
|
2022-10-25 14:25:19 +00:00
|
|
|
# self.save_i2p = config_loader.get_config_boolean("Onion", "save_i2p")
|
2021-05-14 12:42:16 +00:00
|
|
|
|
2022-12-19 15:38:20 +00:00
|
|
|
def extract(self, obj_id, content, tag):
|
|
|
|
extracted = []
|
|
|
|
onions = self.regex_finditer(self.onion_regex, obj_id, content)
|
|
|
|
for onion in onions:
|
|
|
|
start, end, value = onion
|
|
|
|
url_unpack = crawlers.unpack_url(value)
|
|
|
|
domain = url_unpack['domain']
|
|
|
|
if crawlers.is_valid_onion_domain(domain):
|
2023-02-23 15:25:15 +00:00
|
|
|
extracted.append([start, end, value, f'tag:{tag}'])
|
2022-12-19 15:38:20 +00:00
|
|
|
return extracted
|
|
|
|
|
2021-05-14 12:42:16 +00:00
|
|
|
def compute(self, message):
|
2022-10-25 14:25:19 +00:00
|
|
|
onion_urls = []
|
|
|
|
domains = []
|
2021-05-14 12:42:16 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
item_id, score = message.split()
|
|
|
|
item = Item(item_id)
|
2021-05-14 12:42:16 +00:00
|
|
|
item_content = item.get_content()
|
|
|
|
|
|
|
|
# max execution time on regex
|
2022-10-25 14:25:19 +00:00
|
|
|
res = self.regex_findall(self.onion_regex, item.get_id(), item_content)
|
2021-05-14 12:42:16 +00:00
|
|
|
for x in res:
|
|
|
|
# String to tuple
|
|
|
|
x = x[2:-2].replace(" '", "").split("',")
|
|
|
|
url = x[0]
|
2022-10-25 14:25:19 +00:00
|
|
|
print(url)
|
2021-05-14 12:42:16 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
# TODO Crawl subdomain
|
|
|
|
url_unpack = crawlers.unpack_url(url)
|
|
|
|
domain = url_unpack['domain']
|
2021-05-14 12:42:16 +00:00
|
|
|
if crawlers.is_valid_onion_domain(domain):
|
2022-10-25 14:25:19 +00:00
|
|
|
domains.append(domain)
|
|
|
|
onion_urls.append(url)
|
|
|
|
|
|
|
|
if onion_urls:
|
|
|
|
if crawlers.is_crawler_activated():
|
2023-01-18 15:28:08 +00:00
|
|
|
for domain in domains: # TODO LOAD DEFAULT SCREENSHOT + HAR
|
2023-02-21 11:22:49 +00:00
|
|
|
task_uuid = crawlers.create_task(domain, parent=item.get_id(), priority=0)
|
2022-10-25 14:25:19 +00:00
|
|
|
if task_uuid:
|
|
|
|
print(f'{domain} added to crawler queue: {task_uuid}')
|
|
|
|
else:
|
|
|
|
to_print = f'Onion;{item.get_source()};{item.get_date()};{item.get_basename()};'
|
|
|
|
print(f'{to_print}Detected {len(domains)} .onion(s);{item.get_id()}')
|
|
|
|
self.redis_logger.warning(f'{to_print}Detected {len(domains)} .onion(s);{item.get_id()}')
|
|
|
|
|
|
|
|
# TAG Item
|
|
|
|
msg = f'infoleak:automatic-detection="onion";{item.get_id()}'
|
|
|
|
self.send_message_to_queue(msg, 'Tags')
|
2014-08-31 20:42:12 +00:00
|
|
|
|
2021-05-14 12:42:16 +00:00
|
|
|
|
2022-10-25 14:25:19 +00:00
|
|
|
if __name__ == "__main__":
|
2021-05-14 12:42:16 +00:00
|
|
|
module = Onion()
|
2022-10-25 14:25:19 +00:00
|
|
|
# module.compute('submitted/2022/10/10/submitted_705d1d92-7e9a-4a44-8c21-ccd167bfb7db.gz 9')
|
2021-05-14 12:42:16 +00:00
|
|
|
module.run()
|