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 Web Module
|
|
|
|
============================
|
|
|
|
|
|
|
|
This module tries to parse URLs and warns if some defined contry code are present.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
import redis
|
|
|
|
import pprint
|
|
|
|
import time
|
2016-08-09 11:59:36 +02:00
|
|
|
import os
|
2014-08-11 09:27:50 +02:00
|
|
|
import dns.exception
|
2014-08-14 17:55:18 +02:00
|
|
|
from packages import Paste
|
2014-08-06 11:43:40 +02:00
|
|
|
from packages import lib_refine
|
|
|
|
from pubsublogger import publisher
|
2016-07-01 16:59:08 +02:00
|
|
|
from pyfaup.faup import Faup
|
|
|
|
import re
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-14 14:22:11 +02:00
|
|
|
# Country and ASN lookup
|
|
|
|
from cymru.ip2asn.dns import DNSClient as ip2asn
|
|
|
|
import socket
|
|
|
|
import pycountry
|
|
|
|
import ipaddress
|
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
from Helper import Process
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2016-07-04 09:18:23 +02:00
|
|
|
# Used to prevent concat with empty fields due to url parsing
|
2016-07-12 11:52:19 +02:00
|
|
|
def avoidNone(a_string):
|
|
|
|
if a_string is None:
|
2016-07-01 16:59:08 +02:00
|
|
|
return ""
|
|
|
|
else:
|
2016-07-12 11:52:19 +02:00
|
|
|
return a_string
|
2016-07-01 16:59:08 +02:00
|
|
|
|
2014-08-20 15:14:57 +02:00
|
|
|
if __name__ == "__main__":
|
2014-08-22 17:35:40 +02:00
|
|
|
publisher.port = 6380
|
2014-08-20 15:14:57 +02:00
|
|
|
publisher.channel = "Script"
|
2014-08-14 17:55:18 +02:00
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
config_section = 'Web'
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-29 19:37:56 +02:00
|
|
|
p = Process(config_section)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2014-08-20 15:14:57 +02:00
|
|
|
# REDIS #
|
2014-08-06 11:43:40 +02:00
|
|
|
r_serv2 = redis.StrictRedis(
|
2014-08-29 19:37:56 +02:00
|
|
|
host=p.config.get("Redis_Cache", "host"),
|
|
|
|
port=p.config.getint("Redis_Cache", "port"),
|
2018-05-04 13:53:29 +02:00
|
|
|
db=p.config.getint("Redis_Cache", "db"),
|
|
|
|
decode_responses=True)
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2016-08-09 11:59:36 +02:00
|
|
|
# Protocol file path
|
|
|
|
protocolsfile_path = os.path.join(os.environ['AIL_HOME'],
|
|
|
|
p.config.get("Directories", "protocolsfile"))
|
|
|
|
|
2014-08-14 17:55:18 +02:00
|
|
|
# Country to log as critical
|
2014-09-05 10:42:01 +02:00
|
|
|
cc_critical = p.config.get("Url", "cc_critical")
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
# FUNCTIONS #
|
2021-02-03 14:58:27 +01:00
|
|
|
publisher.info("Script URL Started")
|
2014-08-29 19:37:56 +02:00
|
|
|
|
|
|
|
message = p.get_from_set()
|
2014-08-06 11:43:40 +02:00
|
|
|
prec_filename = None
|
2016-07-01 16:59:08 +02:00
|
|
|
faup = Faup()
|
2014-08-06 11:43:40 +02:00
|
|
|
|
2016-08-09 11:59:36 +02:00
|
|
|
# Get all uri from protocolsfile (Used for Curve)
|
|
|
|
uri_scheme = ""
|
|
|
|
with open(protocolsfile_path, 'r') as scheme_file:
|
|
|
|
for scheme in scheme_file:
|
|
|
|
uri_scheme += scheme[:-1]+"|"
|
|
|
|
uri_scheme = uri_scheme[:-1]
|
|
|
|
|
|
|
|
url_regex = "("+uri_scheme+")\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*"
|
2014-08-06 11:43:40 +02:00
|
|
|
|
|
|
|
while True:
|
2014-09-04 11:46:07 +02:00
|
|
|
if message is not None:
|
2014-09-05 17:05:45 +02:00
|
|
|
filename, score = message.split()
|
2014-09-04 11:46:07 +02:00
|
|
|
|
|
|
|
if prec_filename is None or filename != prec_filename:
|
|
|
|
domains_list = []
|
|
|
|
PST = Paste.Paste(filename)
|
|
|
|
client = ip2asn()
|
|
|
|
for x in PST.get_regex(url_regex):
|
2016-07-01 16:59:08 +02:00
|
|
|
matching_url = re.search(url_regex, PST.get_p_content())
|
|
|
|
url = matching_url.group(0)
|
|
|
|
|
2016-08-02 15:43:11 +02:00
|
|
|
to_send = "{} {} {}".format(url, PST._get_p_date(), filename)
|
2016-07-01 16:59:08 +02:00
|
|
|
p.populate_set_out(to_send, 'Url')
|
|
|
|
|
|
|
|
faup.decode(url)
|
|
|
|
domain = faup.get_domain()
|
|
|
|
subdomain = faup.get_subdomain()
|
|
|
|
|
|
|
|
publisher.debug('{} Published'.format(url))
|
2014-09-04 11:46:07 +02:00
|
|
|
|
2018-04-16 17:00:44 +02:00
|
|
|
if subdomain is not None:
|
2019-05-06 13:38:13 +02:00
|
|
|
## TODO: # FIXME: remove me
|
|
|
|
try:
|
|
|
|
subdomain = subdomain.decode()
|
|
|
|
except:
|
|
|
|
pass
|
2018-04-16 17:00:44 +02:00
|
|
|
|
|
|
|
if domain is not None:
|
2019-05-06 13:38:13 +02:00
|
|
|
## TODO: # FIXME: remove me
|
|
|
|
try:
|
|
|
|
domain = domain.decode()
|
|
|
|
except:
|
|
|
|
pass
|
2018-04-16 17:00:44 +02:00
|
|
|
domains_list.append(domain)
|
|
|
|
|
|
|
|
hostl = avoidNone(subdomain) + avoidNone(domain)
|
|
|
|
|
2014-09-04 11:46:07 +02:00
|
|
|
try:
|
2016-07-01 16:59:08 +02:00
|
|
|
socket.setdefaulttimeout(1)
|
2018-04-16 17:00:44 +02:00
|
|
|
ip = socket.gethostbyname(hostl)
|
2014-09-04 11:46:07 +02:00
|
|
|
except:
|
|
|
|
# If the resolver is not giving any IPv4 address,
|
|
|
|
# ASN/CC lookup is skip.
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
l = client.lookup(ip, qType='IP')
|
2018-04-16 17:00:44 +02:00
|
|
|
|
2014-09-04 11:46:07 +02:00
|
|
|
except ipaddress.AddressValueError:
|
|
|
|
continue
|
|
|
|
cc = getattr(l, 'cc')
|
2018-07-19 16:50:42 +02:00
|
|
|
asn = ''
|
2018-04-16 17:00:44 +02:00
|
|
|
if getattr(l, 'asn') is not None:
|
|
|
|
asn = getattr(l, 'asn')[2:] #remobe b'
|
2014-09-04 11:46:07 +02:00
|
|
|
|
|
|
|
# EU is not an official ISO 3166 code (but used by RIPE
|
|
|
|
# IP allocation)
|
|
|
|
if cc is not None and cc != "EU":
|
2018-04-16 14:50:04 +02:00
|
|
|
print(hostl, asn, cc, \
|
|
|
|
pycountry.countries.get(alpha_2=cc).name)
|
2014-09-04 11:46:07 +02:00
|
|
|
if cc == cc_critical:
|
2016-08-08 09:17:44 +02:00
|
|
|
to_print = 'Url;{};{};{};Detected {} {}'.format(
|
2014-09-04 11:46:07 +02:00
|
|
|
PST.p_source, PST.p_date, PST.p_name,
|
2016-08-08 09:17:44 +02:00
|
|
|
hostl, cc)
|
|
|
|
#publisher.warning(to_print)
|
2018-04-16 14:50:04 +02:00
|
|
|
print(to_print)
|
2014-09-04 11:46:07 +02:00
|
|
|
else:
|
2018-04-16 14:50:04 +02:00
|
|
|
print(hostl, asn, cc)
|
2014-09-04 11:46:07 +02:00
|
|
|
|
|
|
|
A_values = lib_refine.checking_A_record(r_serv2,
|
|
|
|
domains_list)
|
2018-04-16 17:00:44 +02:00
|
|
|
|
2014-09-04 11:46:07 +02:00
|
|
|
if A_values[0] >= 1:
|
|
|
|
pprint.pprint(A_values)
|
2016-10-27 11:50:24 +02:00
|
|
|
publisher.info('Url;{};{};{};Checked {} URL;{}'.format(
|
2018-11-02 16:07:27 +01:00
|
|
|
PST.p_source, PST.p_date, PST.p_name, A_values[0], PST.p_rel_path))
|
2014-09-04 11:46:07 +02:00
|
|
|
prec_filename = filename
|
|
|
|
|
|
|
|
else:
|
|
|
|
publisher.debug("Script url is Idling 10s")
|
2018-04-16 14:50:04 +02:00
|
|
|
print('Sleeping')
|
2014-09-04 11:46:07 +02:00
|
|
|
time.sleep(10)
|
|
|
|
|
|
|
|
message = p.get_from_set()
|