2018-05-04 13:53:29 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
2018-11-21 08:49:58 +01:00
|
|
|
import os
|
2019-11-05 15:18:03 +01:00
|
|
|
import sys
|
2014-08-06 11:43:40 +02:00
|
|
|
import dns.resolver
|
2020-05-04 14:30:11 +02:00
|
|
|
import dns.exception
|
2023-05-12 15:29:53 +02:00
|
|
|
import logging.config
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-14 14:11:07 +02:00
|
|
|
from datetime import timedelta
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2022-11-28 15:01:40 +01:00
|
|
|
sys.path.append(os.environ['AIL_BIN'])
|
|
|
|
##################################
|
|
|
|
# Import Project packages
|
|
|
|
##################################
|
2023-05-12 15:29:53 +02:00
|
|
|
from lib import ail_logger
|
2022-11-28 15:01:40 +01:00
|
|
|
from lib import ConfigLoader
|
2019-11-05 15:18:03 +01:00
|
|
|
|
2023-05-12 15:29:53 +02:00
|
|
|
|
|
|
|
logging.config.dictConfig(ail_logger.get_config(name='modules'))
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
2019-11-05 15:18:03 +01:00
|
|
|
config_loader = ConfigLoader.ConfigLoader()
|
2023-04-04 14:12:23 +02:00
|
|
|
dns_server = config_loader.get_config_str("DomClassifier", "dns")
|
2019-11-05 15:18:03 +01:00
|
|
|
config_loader = None
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
def is_luhn_valid(card_number):
|
|
|
|
"""Apply the Luhn algorithm to validate credit card.
|
|
|
|
|
|
|
|
:param card_number: -- (int) card number
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
r = [int(ch) for ch in str(card_number)][::-1]
|
2014-08-14 14:11:07 +02:00
|
|
|
return (sum(r[0::2]) + sum(sum(divmod(d*2, 10)) for d in r[1::2])) % 10 == 0
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
|
2020-05-04 14:05:04 +02:00
|
|
|
def checking_MX_record(r_serv, MXdomains, addr_dns):
|
2014-08-06 11:43:40 +02:00
|
|
|
"""Check if emails MX domains are responding.
|
|
|
|
|
|
|
|
:param r_serv: -- Redis connexion database
|
|
|
|
:param adress_set: -- (set) This is a set of emails adress
|
|
|
|
:return: (int) Number of adress with a responding and valid MX domains
|
|
|
|
|
|
|
|
This function will split the email adress and try to resolve their domains
|
|
|
|
names: on example@gmail.com it will try to resolve gmail.com
|
|
|
|
|
|
|
|
"""
|
2018-04-24 16:44:37 +02:00
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
score = 0
|
|
|
|
WalidMX = set([])
|
2018-07-30 09:21:22 +02:00
|
|
|
validMX = {}
|
2020-05-04 14:05:04 +02:00
|
|
|
num = len(MXdomains)
|
2014-09-04 11:46:07 +02:00
|
|
|
resolver = dns.resolver.Resolver()
|
2018-05-02 17:07:10 +02:00
|
|
|
resolver.nameservers = [addr_dns]
|
2020-05-04 14:30:11 +02:00
|
|
|
resolver.timeout = 5.0
|
|
|
|
resolver.lifetime = 2.0
|
2014-08-06 11:43:40 +02:00
|
|
|
if MXdomains != []:
|
|
|
|
|
2018-07-30 09:21:22 +02:00
|
|
|
for MXdomain in MXdomains:
|
2014-08-06 11:43:40 +02:00
|
|
|
try:
|
2018-07-30 09:21:22 +02:00
|
|
|
MXdomain = MXdomain[1:]
|
2014-08-14 14:11:07 +02:00
|
|
|
# Already in Redis living.
|
2018-07-30 09:21:22 +02:00
|
|
|
if r_serv.exists(MXdomain):
|
2014-08-06 11:43:40 +02:00
|
|
|
score += 1
|
2018-07-30 09:21:22 +02:00
|
|
|
WalidMX.add(MXdomain)
|
|
|
|
validMX[MXdomain] = validMX.get(MXdomain, 0) + 1
|
2014-08-06 11:43:40 +02:00
|
|
|
# Not already in Redis
|
|
|
|
else:
|
|
|
|
# If I'm Walid MX domain
|
2018-07-30 09:21:22 +02:00
|
|
|
if resolver.query(MXdomain, rdtype=dns.rdatatype.MX):
|
2014-08-06 11:43:40 +02:00
|
|
|
# Gonna be added in redis.
|
2018-07-30 09:21:22 +02:00
|
|
|
r_serv.setex(MXdomain, 1, timedelta(days=1))
|
2014-08-06 11:43:40 +02:00
|
|
|
score += 1
|
2018-07-30 09:21:22 +02:00
|
|
|
WalidMX.add(MXdomain)
|
|
|
|
validMX[MXdomain] = validMX.get(MXdomain, 0) + 1
|
2014-08-06 11:43:40 +02:00
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
|
|
|
except dns.resolver.NoNameservers:
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('NoNameserver, No non-broken nameservers are available to answer the query.')
|
2018-05-02 17:07:10 +02:00
|
|
|
print('NoNameserver, No non-broken nameservers are available to answer the query.')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
except dns.resolver.NoAnswer:
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('NoAnswer, The response did not contain an answer to the question.')
|
2018-05-02 17:07:10 +02:00
|
|
|
print('NoAnswer, The response did not contain an answer to the question.')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
except dns.name.EmptyLabel:
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('SyntaxError: EmptyLabel')
|
2018-05-02 17:07:10 +02:00
|
|
|
print('SyntaxError: EmptyLabel')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
except dns.resolver.NXDOMAIN:
|
2014-09-04 11:46:07 +02:00
|
|
|
r_serv.setex(MXdomain[1:], 1, timedelta(days=1))
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('The query name does not exist.')
|
2018-05-02 17:07:10 +02:00
|
|
|
print('The query name does not exist.')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
except dns.name.LabelTooLong:
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('The Label is too long')
|
2018-05-02 17:07:10 +02:00
|
|
|
print('The Label is too long')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2020-05-04 14:30:11 +02:00
|
|
|
except dns.exception.Timeout:
|
|
|
|
print('dns timeout')
|
2018-07-30 09:21:22 +02:00
|
|
|
r_serv.setex(MXdomain, 1, timedelta(days=1))
|
2014-09-04 11:46:07 +02:00
|
|
|
|
|
|
|
except Exception as e:
|
2018-04-16 14:50:04 +02:00
|
|
|
print(e)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug("emails before: {0} after: {1} (valid)".format(num, score))
|
2018-07-30 09:21:22 +02:00
|
|
|
#return (num, WalidMX)
|
|
|
|
return (num, validMX)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
def checking_A_record(r_serv, domains_set):
|
2018-11-21 08:49:58 +01:00
|
|
|
|
2014-08-06 11:43:40 +02:00
|
|
|
score = 0
|
|
|
|
num = len(domains_set)
|
|
|
|
WalidA = set([])
|
2014-09-04 11:46:07 +02:00
|
|
|
resolver = dns.resolver.Resolver()
|
2018-11-21 08:49:58 +01:00
|
|
|
resolver.nameservers = [dns_server]
|
2020-05-04 14:30:11 +02:00
|
|
|
resolver.timeout = 5.0
|
|
|
|
resolver.lifetime = 2.0
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
for Adomain in domains_set:
|
|
|
|
try:
|
2014-08-14 14:11:07 +02:00
|
|
|
# Already in Redis living.
|
2014-08-06 11:43:40 +02:00
|
|
|
if r_serv.exists(Adomain):
|
|
|
|
score += 1
|
|
|
|
WalidA.add(Adomain)
|
|
|
|
# Not already in Redis
|
|
|
|
else:
|
|
|
|
# If I'm Walid domain
|
2014-09-04 11:46:07 +02:00
|
|
|
if resolver.query(Adomain, rdtype=dns.rdatatype.A):
|
2014-08-06 11:43:40 +02:00
|
|
|
# Gonna be added in redis.
|
2014-09-04 11:46:07 +02:00
|
|
|
r_serv.setex(Adomain, 1, timedelta(days=1))
|
2014-08-06 11:43:40 +02:00
|
|
|
score += 1
|
|
|
|
WalidA.add(Adomain)
|
|
|
|
else:
|
|
|
|
pass
|
|
|
|
|
|
|
|
except dns.resolver.NoNameservers:
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('NoNameserver, No non-broken nameservers are available to answer the query.')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
except dns.resolver.NoAnswer:
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('NoAnswer, The response did not contain an answer to the question.')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
except dns.name.EmptyLabel:
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('SyntaxError: EmptyLabel')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
except dns.resolver.NXDOMAIN:
|
2014-09-04 11:46:07 +02:00
|
|
|
r_serv.setex(Adomain[1:], 1, timedelta(days=1))
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('The query name does not exist.')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
except dns.name.LabelTooLong:
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug('The Label is too long')
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-09-04 11:46:07 +02:00
|
|
|
except Exception as e:
|
2018-04-16 14:50:04 +02:00
|
|
|
print(e)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2023-05-12 15:29:53 +02:00
|
|
|
logger.debug("URLs before: {0} after: {1} (valid)".format(num, score))
|
2014-08-06 11:43:40 +02:00
|
|
|
return (num, WalidA)
|