2018-05-04 11:53:29 +00:00
|
|
|
#!/usr/bin/env python3
|
2014-08-06 09:43:40 +00:00
|
|
|
# -*-coding:UTF-8 -*
|
2017-05-09 09:13:16 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
The CreditCards Module
|
|
|
|
======================
|
|
|
|
|
|
|
|
This module is consuming the Redis-list created by the Categ module.
|
|
|
|
|
|
|
|
It apply credit card regexes on paste content and warn if above a threshold.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
import pprint
|
|
|
|
import time
|
|
|
|
from packages import Paste
|
2014-08-06 09:43:40 +00:00
|
|
|
from packages import lib_refine
|
|
|
|
from pubsublogger import publisher
|
2014-09-05 15:05:45 +00:00
|
|
|
import re
|
2018-04-16 12:50:04 +00:00
|
|
|
import sys
|
2014-09-05 15:05:45 +00:00
|
|
|
|
2014-08-29 17:37:56 +00:00
|
|
|
from Helper import Process
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-20 13:14:57 +00:00
|
|
|
if __name__ == "__main__":
|
2014-08-22 15:35:40 +00:00
|
|
|
publisher.port = 6380
|
2014-08-20 13:14:57 +00:00
|
|
|
publisher.channel = "Script"
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-29 17:37:56 +00:00
|
|
|
config_section = 'CreditCards'
|
2014-08-14 15:55:18 +00:00
|
|
|
|
2014-08-29 17:37:56 +00:00
|
|
|
p = Process(config_section)
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
# FUNCTIONS #
|
|
|
|
publisher.info("Creditcard script subscribed to channel creditcard_categ")
|
|
|
|
|
|
|
|
|
|
|
|
creditcard_regex = "4[0-9]{12}(?:[0-9]{3})?"
|
|
|
|
|
2014-08-29 17:37:56 +00:00
|
|
|
# FIXME For retro compatibility
|
|
|
|
channel = 'creditcard_categ'
|
|
|
|
|
2014-09-05 15:05:45 +00:00
|
|
|
# Source: http://www.richardsramblings.com/regex/credit-card-numbers/
|
|
|
|
cards = [
|
2014-09-08 14:51:43 +00:00
|
|
|
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
|
2014-09-05 15:05:45 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
regex = re.compile('|'.join(cards))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
while True:
|
2014-09-05 15:05:45 +00:00
|
|
|
message = p.get_from_set()
|
2014-08-14 15:55:18 +00:00
|
|
|
if message is not None:
|
2014-09-05 15:05:45 +00:00
|
|
|
filename, score = message.split()
|
|
|
|
paste = Paste.Paste(filename)
|
|
|
|
content = paste.get_p_content()
|
|
|
|
all_cards = re.findall(regex, content)
|
|
|
|
if len(all_cards) > 0:
|
2018-04-16 12:50:04 +00:00
|
|
|
print('All matching', all_cards)
|
2014-08-14 15:55:18 +00:00
|
|
|
creditcard_set = set([])
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-09-05 15:05:45 +00:00
|
|
|
for card in all_cards:
|
|
|
|
clean_card = re.sub('[^0-9]', '', card)
|
2018-04-16 12:50:04 +00:00
|
|
|
clean_card = clean_card
|
2014-09-05 15:05:45 +00:00
|
|
|
if lib_refine.is_luhn_valid(clean_card):
|
2018-04-16 12:50:04 +00:00
|
|
|
print(clean_card, 'is valid')
|
2014-09-05 15:05:45 +00:00
|
|
|
creditcard_set.add(clean_card)
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-09-05 15:05:45 +00:00
|
|
|
paste.__setattr__(channel, creditcard_set)
|
|
|
|
paste.save_attribute_redis(channel, creditcard_set)
|
2014-08-06 09:43:40 +00:00
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
pprint.pprint(creditcard_set)
|
2014-08-20 13:14:57 +00:00
|
|
|
to_print = 'CreditCard;{};{};{};'.format(
|
2014-09-05 15:05:45 +00:00
|
|
|
paste.p_source, paste.p_date, paste.p_name)
|
2014-08-14 15:55:18 +00:00
|
|
|
if (len(creditcard_set) > 0):
|
2016-10-27 09:50:24 +00:00
|
|
|
publisher.warning('{}Checked {} valid number(s);{}'.format(
|
|
|
|
to_print, len(creditcard_set), paste.p_path))
|
2018-04-16 12:50:04 +00:00
|
|
|
print('{}Checked {} valid number(s);{}'.format(
|
|
|
|
to_print, len(creditcard_set), paste.p_path))
|
2016-07-18 14:22:33 +00:00
|
|
|
#Send to duplicate
|
2016-08-24 13:35:23 +00:00
|
|
|
p.populate_set_out(filename, 'Duplicate')
|
2016-08-08 07:17:44 +00:00
|
|
|
#send to Browse_warning_paste
|
2018-04-16 12:50:04 +00:00
|
|
|
msg = 'creditcard;{}'.format(filename)
|
|
|
|
p.populate_set_out(msg, 'alertHandler')
|
2014-08-06 09:43:40 +00:00
|
|
|
else:
|
2016-10-27 09:50:24 +00:00
|
|
|
publisher.info('{}CreditCard related;{}'.format(to_print, paste.p_path))
|
2014-08-06 09:43:40 +00:00
|
|
|
else:
|
|
|
|
publisher.debug("Script creditcard is idling 1m")
|
2014-09-05 15:05:45 +00:00
|
|
|
time.sleep(10)
|