2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2016-02-05 19:58:21 +01:00
|
|
|
# -*-coding:UTF-8 -*
|
2017-05-09 11:13:16 +02:00
|
|
|
|
2016-02-05 19:58:21 +01:00
|
|
|
"""
|
2017-05-09 11:13:16 +02:00
|
|
|
The Phone Module
|
|
|
|
================
|
|
|
|
|
|
|
|
This module is consuming the Redis-list created by the Categ module.
|
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
It apply phone number regexes on item content and warn if above a threshold.
|
2017-05-09 11:13:16 +02:00
|
|
|
|
2016-02-05 19:58:21 +01:00
|
|
|
"""
|
|
|
|
|
2021-04-02 09:52:05 +02:00
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
2021-06-02 16:04:52 +02:00
|
|
|
import os
|
|
|
|
import sys
|
2017-05-03 14:25:18 +02:00
|
|
|
import phonenumbers
|
2021-04-02 09:52:05 +02:00
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2021-04-02 09:52:05 +02:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
2021-06-02 16:04:52 +02:00
|
|
|
from modules.abstract_module import AbstractModule
|
2022-10-25 16:25:19 +02:00
|
|
|
from lib.objects.Items import Item
|
2016-02-05 19:58:21 +01:00
|
|
|
|
2021-06-02 16:04:52 +02:00
|
|
|
# # TODO: # FIXME: improve regex / filter false positives
|
2021-04-02 09:52:05 +02:00
|
|
|
class Phone(AbstractModule):
|
|
|
|
"""
|
|
|
|
Phone module for AIL framework
|
|
|
|
"""
|
|
|
|
|
2016-02-05 19:58:21 +01:00
|
|
|
# regex to find phone numbers, may raise many false positives (shalt thou seek optimization, upgrading is required)
|
2021-04-02 09:52:05 +02:00
|
|
|
# reg_phone = re.compile(r'(\+\d{1,4}(\(\d\))?\d?|0\d?)(\d{6,8}|([-/\. ]{1}\d{2,3}){3,4})')
|
2023-05-24 10:48:29 +02:00
|
|
|
# REG_PHONE = re.compile(r'(\+\d{1,4}(\(\d\))?\d?|0\d?)(\d{6,8}|([-/\. ]{1}\(?\d{2,4}\)?){3,4})')
|
2021-04-02 09:52:05 +02:00
|
|
|
|
2023-05-24 10:48:29 +02:00
|
|
|
def __init__(self, queue=True):
|
|
|
|
super(Phone, self).__init__(queue=queue)
|
2021-04-02 09:52:05 +02:00
|
|
|
|
2022-10-25 16:25:19 +02:00
|
|
|
# Waiting time in seconds between to message processed
|
2021-04-02 09:52:05 +02:00
|
|
|
self.pending_seconds = 1
|
|
|
|
|
2023-05-24 10:48:29 +02:00
|
|
|
def extract(self, obj_id, content, tag):
|
|
|
|
extracted = []
|
2023-05-25 14:33:12 +02:00
|
|
|
phones = self.regex_phone_iter('ZZ', obj_id, content)
|
2023-05-24 10:48:29 +02:00
|
|
|
for phone in phones:
|
|
|
|
extracted.append([phone[0], phone[1], phone[2], f'tag:{tag}'])
|
|
|
|
return extracted
|
|
|
|
|
2021-04-02 09:52:05 +02:00
|
|
|
def compute(self, message):
|
2023-06-22 15:38:04 +02:00
|
|
|
item = self.get_obj()
|
2021-06-02 16:04:52 +02:00
|
|
|
content = item.get_content()
|
2021-04-02 09:52:05 +02:00
|
|
|
|
2023-05-24 10:48:29 +02:00
|
|
|
# TODO use language detection to choose the country code ?
|
2023-05-24 13:31:10 +02:00
|
|
|
results = self.regex_phone_iter('ZZ', item.id, content)
|
2023-05-24 10:48:29 +02:00
|
|
|
for phone in results:
|
|
|
|
print(phone[2])
|
2021-04-02 09:52:05 +02:00
|
|
|
|
2023-05-24 10:48:29 +02:00
|
|
|
if results:
|
|
|
|
# TAGS
|
2023-06-22 15:38:04 +02:00
|
|
|
tag = 'infoleak:automatic-detection="phone-number"'
|
|
|
|
self.add_message_to_queue(message=tag, queue='Tags')
|
2021-04-02 09:52:05 +02:00
|
|
|
|
2023-05-24 10:48:29 +02:00
|
|
|
self.redis_logger.warning(f'{item.get_id()} contains {len(phone)} Phone numbers')
|
|
|
|
|
|
|
|
# # List of the regex results in the Item, may be null
|
|
|
|
# results = self.REG_PHONE.findall(content)
|
|
|
|
#
|
|
|
|
# # If the list is greater than 4, we consider the Item may contain a list of phone numbers
|
|
|
|
# if len(results) > 4:
|
|
|
|
# self.logger.debug(results)
|
|
|
|
# self.redis_logger.warning(f'{item.get_id()} contains PID (phone numbers)')
|
|
|
|
#
|
|
|
|
# msg = f'infoleak:automatic-detection="phone-number";{item.get_id()}'
|
|
|
|
# self.add_message_to_queue(msg, 'Tags')
|
|
|
|
#
|
|
|
|
# stats = {}
|
|
|
|
# for phone_number in results:
|
|
|
|
# try:
|
|
|
|
# x = phonenumbers.parse(phone_number, None)
|
|
|
|
# country_code = x.country_code
|
|
|
|
# if stats.get(country_code) is None:
|
|
|
|
# stats[country_code] = 1
|
|
|
|
# else:
|
|
|
|
# stats[country_code] = stats[country_code] + 1
|
|
|
|
# except:
|
|
|
|
# pass
|
|
|
|
# for country_code in stats:
|
|
|
|
# if stats[country_code] > 4:
|
|
|
|
# self.redis_logger.warning(f'{item.get_id()} contains Phone numbers with country code {country_code}')
|
2021-04-02 09:52:05 +02:00
|
|
|
|
2016-02-05 19:58:21 +01:00
|
|
|
|
2016-02-10 16:39:06 +01:00
|
|
|
if __name__ == '__main__':
|
2021-04-02 09:52:05 +02:00
|
|
|
module = Phone()
|
2023-05-24 13:26:47 +02:00
|
|
|
module.run()
|