AIL-framework/bin/BankAccount.py

123 lines
3.4 KiB
Python
Raw Normal View History

2018-07-26 15:31:58 +02:00
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
"""
2018-07-27 10:20:03 +02:00
The BankAccount Module
2018-07-26 15:31:58 +02:00
======================
It apply IBAN regexes on item content and warn if above a threshold.
2018-07-26 15:31:58 +02:00
"""
import redis
import time
2018-08-01 15:26:29 +02:00
import datetime
2018-07-26 15:31:58 +02:00
import re
import string
2018-07-27 10:20:03 +02:00
from itertools import chain
2018-07-26 15:31:58 +02:00
2022-09-08 10:31:57 +02:00
sys.path.append(os.environ['AIL_BIN'])
##################################
# Import Project packages #
##################################
from lib import Statistics
from packages import Item
2018-07-26 15:31:58 +02:00
from pubsublogger import publisher
from Helper import Process
import signal
class TimeoutException(Exception):
pass
def timeout_handler(signum, frame):
raise TimeoutException
signal.signal(signal.SIGALRM, timeout_handler)
2018-07-27 10:20:03 +02:00
_LETTERS_IBAN = chain(enumerate(string.digits + string.ascii_uppercase),
enumerate(string.ascii_lowercase, 10))
LETTERS_IBAN = {ord(d): str(i) for i, d in _LETTERS_IBAN}
2018-07-26 15:31:58 +02:00
def iban_number(iban):
2018-07-27 10:20:03 +02:00
return (iban[4:] + iban[:4]).translate(LETTERS_IBAN)
2018-07-26 15:31:58 +02:00
def is_valid_iban(iban):
iban_numb = iban_number(iban)
iban_numb_check = iban_number(iban[:2] + '00' + iban[4:])
check_digit = '{:0>2}'.format(98 - (int(iban_numb_check) % 97))
if check_digit == iban[2:4] and int(iban_numb) % 97 == 1:
# valid iban
print('valid iban')
return True
return False
2022-09-08 10:31:57 +02:00
# # TODO: SET
def check_all_iban(l_iban, obj_id):
2018-07-26 15:31:58 +02:00
nb_valid_iban = 0
for iban in l_iban:
2018-07-27 10:20:03 +02:00
iban = iban[0]+iban[1]+iban[2]
iban = ''.join(e for e in iban if e.isalnum())
#iban = iban.upper()
res = iban_regex_verify.findall(iban)
2018-08-01 15:26:29 +02:00
date = datetime.datetime.now().strftime("%Y%m")
2018-07-27 10:20:03 +02:00
if res:
print('checking '+iban)
if is_valid_iban(iban):
print('------')
nb_valid_iban = nb_valid_iban + 1
2022-09-08 10:31:57 +02:00
Statistics.add_iban_country_stats_by_date(date, iban[0:2], 1)
2018-07-27 10:20:03 +02:00
2018-07-26 15:31:58 +02:00
if(nb_valid_iban > 0):
to_print = 'Iban;{};{};{};'.format(Item.get_source(obj_id), Item.get_item_date(obj_id), Item.get_basename(obj_id))
2018-07-26 15:31:58 +02:00
publisher.warning('{}Checked found {} IBAN;{}'.format(
to_print, nb_valid_iban, obj_id))
msg = 'infoleak:automatic-detection="iban";{}'.format(obj_id)
2018-07-26 15:31:58 +02:00
p.populate_set_out(msg, 'Tags')
if __name__ == "__main__":
publisher.port = 6380
publisher.channel = "Script"
config_section = 'BankAccount'
p = Process(config_section)
max_execution_time = p.config.getint("BankAccount", "max_execution_time")
publisher.info("BankAccount started")
2018-07-27 10:20:03 +02:00
#iban_regex = re.compile(r'\b[A-Za-z]{2}[0-9]{2}(?:[ ]?[0-9]{4}){4}(?:[ ]?[0-9]{1,2})?\b')
iban_regex = re.compile(r'\b([A-Za-z]{2}[ \-]?[0-9]{2})(?=(?:[ \-]?[A-Za-z0-9]){9,30})((?:[ \-]?[A-Za-z0-9]{3,5}){2,6})([ \-]?[A-Za-z0-9]{1,3})\b')
iban_regex_verify = re.compile(r'^([A-Z]{2})([0-9]{2})([A-Z0-9]{9,30})$')
2018-07-26 15:31:58 +02:00
while True:
message = p.get_from_set()
if message is not None:
obj_id = Item.get_item_id(message)
content = Item.get_item_content(obj_id)
2018-07-26 15:31:58 +02:00
signal.alarm(max_execution_time)
try:
l_iban = iban_regex.findall(content)
except TimeoutException:
print ("{0} processing timeout".format(obj_id))
2018-07-26 15:31:58 +02:00
continue
else:
signal.alarm(0)
if(len(l_iban) > 0):
check_all_iban(l_iban, obj_id)
2018-07-26 15:31:58 +02:00
else:
2018-07-27 10:20:03 +02:00
publisher.debug("Script BankAccount is Idling 10s")
2018-07-26 15:31:58 +02:00
time.sleep(10)