2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/env python3
|
2014-08-06 11:43:40 +02:00
|
|
|
# -*-coding:UTF-8 -*
|
2017-05-09 11:13:16 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
The CreditCards Module
|
|
|
|
======================
|
|
|
|
|
|
|
|
This module is consuming the Redis-list created by the Categ module.
|
|
|
|
|
2024-12-09 13:54:54 +01:00
|
|
|
It apply credit card regexes on object content and warn if a valid card number is found.
|
2017-05-09 11:13:16 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-04-28 15:24:33 +02:00
|
|
|
##################################
|
|
|
|
# Import External packages
|
|
|
|
##################################
|
2021-06-02 14:42:23 +02:00
|
|
|
import os
|
2014-09-05 17:05:45 +02:00
|
|
|
import re
|
2018-04-16 14:50:04 +02:00
|
|
|
import sys
|
2014-09-05 17:05:45 +02:00
|
|
|
|
2021-06-02 14:42:23 +02:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
2021-04-28 15:24:33 +02:00
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
2021-06-02 14:42:23 +02:00
|
|
|
from modules.abstract_module import AbstractModule
|
2021-04-28 15:24:33 +02:00
|
|
|
from packages import lib_refine
|
|
|
|
|
|
|
|
class CreditCards(AbstractModule):
|
|
|
|
"""
|
|
|
|
CreditCards module for AIL framework
|
|
|
|
"""
|
|
|
|
|
2023-04-13 14:25:02 +02:00
|
|
|
def __init__(self, queue=True):
|
|
|
|
super(CreditCards, self).__init__(queue=queue)
|
2021-04-28 15:24:33 +02:00
|
|
|
|
|
|
|
# Source: http://www.richardsramblings.com/regex/credit-card-numbers/
|
|
|
|
cards = [
|
|
|
|
r'\b4\d{3}(?:[\ \-]?)\d{4}(?:[\ \-]?)\d{4}(?:[\ \-]?)\d{4}\b', # 16-digit VISA, with separators
|
|
|
|
r'\b5[1-5]\d{2}(?:[\ \-]?)\d{4}(?:[\ \-]?)\d{4}(?:[\ \-]?)\d{4}\b', # 16 digits MasterCard
|
|
|
|
r'\b6(?:011|22(?:(?=[\ \-]?(?:2[6-9]|[3-9]))|[2-8]|9(?=[\ \-]?(?:[01]|2[0-5])))|4[4-9]\d|5\d\d)(?:[\ \-]?)\d{4}(?:[\ \-]?)\d{4}(?:[\ \-]?)\d{4}\b', # Discover Card
|
|
|
|
r'\b35(?:2[89]|[3-8]\d)(?:[\ \-]?)\d{4}(?:[\ \-]?)\d{4}(?:[\ \-]?)\d{4}\b', # Japan Credit Bureau (JCB)
|
|
|
|
r'\b3[47]\d\d(?:[\ \-]?)\d{6}(?:[\ \-]?)\d{5}\b', # American Express
|
|
|
|
r'\b(?:5[0678]\d\d|6304|6390|67\d\d)\d{8,15}\b', # Maestro
|
|
|
|
]
|
|
|
|
|
|
|
|
self.regex = re.compile('|'.join(cards))
|
2022-12-19 16:38:20 +01:00
|
|
|
self.re_clean_card = r'[^0-9]'
|
2021-04-28 15:24:33 +02:00
|
|
|
|
2022-12-19 16:38:20 +01:00
|
|
|
# Waiting time in seconds between to message processed
|
2021-04-28 15:24:33 +02:00
|
|
|
self.pending_seconds = 10
|
|
|
|
|
|
|
|
# Send module state to logs
|
2023-05-12 15:29:53 +02:00
|
|
|
self.logger.info(f"Module {self.module_name} initialized")
|
2021-04-28 15:24:33 +02:00
|
|
|
|
2022-12-19 16:38:20 +01:00
|
|
|
def get_valid_card(self, card):
|
|
|
|
clean_card = re.sub(self.re_clean_card, '', card)
|
|
|
|
if lib_refine.is_luhn_valid(clean_card):
|
|
|
|
return clean_card
|
|
|
|
|
2024-03-27 13:42:15 +01:00
|
|
|
def extract(self, obj, content, tag):
|
2022-12-19 16:38:20 +01:00
|
|
|
extracted = []
|
2024-03-27 13:42:15 +01:00
|
|
|
cards = self.regex_finditer(self.regex, obj.get_global_id(), content)
|
2022-12-19 16:38:20 +01:00
|
|
|
for card in cards:
|
|
|
|
start, end, value = card
|
|
|
|
if self.get_valid_card(value):
|
2023-02-23 16:25:15 +01:00
|
|
|
extracted.append([start, end, value, f'tag:{tag}'])
|
2022-12-19 16:38:20 +01:00
|
|
|
return extracted
|
|
|
|
|
2021-05-27 17:28:20 +02:00
|
|
|
def compute(self, message, r_result=False):
|
2024-12-09 13:54:54 +01:00
|
|
|
obj = self.get_obj()
|
|
|
|
content = obj.get_content()
|
|
|
|
all_cards = self.regex_findall(self.regex, obj.id, content)
|
2021-04-28 15:24:33 +02:00
|
|
|
|
|
|
|
if len(all_cards) > 0:
|
2023-05-12 15:29:53 +02:00
|
|
|
# self.logger.debug(f'All matching {all_cards}')
|
2022-12-19 16:38:20 +01:00
|
|
|
creditcard_set = set()
|
2023-05-15 09:51:10 +02:00
|
|
|
all_cards = set(all_cards)
|
2021-04-28 15:24:33 +02:00
|
|
|
for card in all_cards:
|
2022-12-19 16:38:20 +01:00
|
|
|
print(card)
|
|
|
|
valid_card = self.get_valid_card(card)
|
|
|
|
if valid_card:
|
|
|
|
creditcard_set.add(valid_card)
|
2021-04-28 15:24:33 +02:00
|
|
|
|
2022-12-19 16:38:20 +01:00
|
|
|
# print(creditcard_set)
|
|
|
|
if creditcard_set:
|
2024-12-09 13:54:54 +01:00
|
|
|
print(f'{len(creditcard_set)} valid number(s);{self.obj.get_global_id()}')
|
2021-04-28 15:24:33 +02:00
|
|
|
|
2023-06-22 15:38:04 +02:00
|
|
|
tag = 'infoleak:automatic-detection="credit-card"'
|
|
|
|
self.add_message_to_queue(message=tag, queue='Tags')
|
2021-04-28 15:24:33 +02:00
|
|
|
|
2021-05-27 17:28:20 +02:00
|
|
|
if r_result:
|
|
|
|
return creditcard_set
|
2021-04-28 15:24:33 +02:00
|
|
|
else:
|
2024-12-09 13:54:54 +01:00
|
|
|
print(f'CreditCard related;{self.obj.get_global_id()}')
|
2021-04-28 15:24:33 +02:00
|
|
|
|
2021-05-27 17:28:20 +02:00
|
|
|
|
2022-10-25 16:25:19 +02:00
|
|
|
if __name__ == '__main__':
|
2021-04-28 15:24:33 +02:00
|
|
|
module = CreditCards()
|
|
|
|
module.run()
|