ail-framework/bin/ZMQ_Sub_Urls.py

121 lines
4.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python2
# -*-coding:UTF-8 -*
2014-08-14 15:55:18 +00:00
import redis
import pprint
import time
2014-08-11 07:27:50 +00:00
import dns.exception
2014-08-14 15:55:18 +00:00
from packages import Paste
from packages import lib_refine
from pubsublogger import publisher
# Country and ASN lookup
from cymru.ip2asn.dns import DNSClient as ip2asn
import socket
import pycountry
import ipaddress
2014-08-20 13:14:57 +00:00
import Helper
2014-08-20 13:14:57 +00:00
if __name__ == "__main__":
publisher.port = 6380
2014-08-20 13:14:57 +00:00
publisher.channel = "Script"
2014-08-14 15:55:18 +00:00
2014-08-20 13:14:57 +00:00
config_section = 'PubSub_Categ'
config_channel = 'channel_3'
subscriber_name = "urls"
2014-08-20 13:14:57 +00:00
h = Helper.Redis_Queues(config_section, config_channel, subscriber_name)
2014-08-20 13:14:57 +00:00
# Subscriber
h.zmq_sub(config_section)
# REDIS #
r_serv2 = redis.StrictRedis(
2014-08-20 13:14:57 +00:00
host=h.config.get("Redis_Cache", "host"),
port=h.config.getint("Redis_Cache", "port"),
db=h.config.getint("Redis_Cache", "db"))
2014-08-14 15:55:18 +00:00
# Country to log as critical
2014-08-20 13:14:57 +00:00
cc_critical = h.config.get("PubSub_Url", "cc_critical")
# FUNCTIONS #
publisher.info("Script URL subscribed to channel web_categ")
2014-08-20 13:14:57 +00:00
message = h.redis_rpop()
prec_filename = None
url_regex = "(http|https|ftp)\://([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\.\,\?\'\\\+&%\$#\=~_\-]+))*"
while True:
try:
2014-08-14 15:55:18 +00:00
if message is not None:
channel, filename, word, score = message.split()
2014-08-14 15:55:18 +00:00
if prec_filename is None or filename != prec_filename:
domains_list = []
2014-08-14 15:55:18 +00:00
PST = Paste.Paste(filename)
client = ip2asn()
for x in PST.get_regex(url_regex):
2014-08-20 13:14:57 +00:00
scheme, credential, subdomain, domain, host, tld, \
port, resource_path, query_string, f1, f2, f3, \
f4 = x
domains_list.append(domain)
print (str(x))
2014-08-20 13:14:57 +00:00
publisher.debug('{} Published'.format(x))
if f1 == "onion":
print domain
hostl = unicode(subdomain+domain)
try:
socket.setdefaulttimeout(2)
ip = socket.gethostbyname(unicode(hostl))
except:
# If the resolver is not giving any IPv4 address,
# ASN/CC lookup is skip.
continue
try:
2014-08-25 08:05:36 +00:00
l = client.lookup(ip, qType='IP')
except ipaddress.AddressValueError:
continue
2014-08-14 15:55:18 +00:00
cc = getattr(l, 'cc')
asn = getattr(l, 'asn')
# EU is not an official ISO 3166 code (but used by RIPE
# IP allocation)
if cc is not None and cc != "EU":
2014-08-20 13:14:57 +00:00
print hostl, asn, cc, \
pycountry.countries.get(alpha2=cc).name
if cc == cc_critical:
2014-08-20 13:14:57 +00:00
publisher.warning(
'Url;{};{};{};Detected {} {}'.format(
2014-08-20 13:14:57 +00:00
PST.p_source, PST.p_date, PST.p_name,
hostl, cc))
else:
2014-08-14 15:55:18 +00:00
print hostl, asn, cc
2014-08-20 13:14:57 +00:00
A_values = lib_refine.checking_A_record(r_serv2,
domains_list)
if A_values[0] >= 1:
PST.__setattr__(channel, A_values)
2014-08-21 10:22:07 +00:00
PST.save_attribute_redis(channel, (A_values[0],
list(A_values[1])))
pprint.pprint(A_values)
2014-08-20 13:14:57 +00:00
publisher.info('Url;{};{};{};Checked {} URL'.format(
PST.p_source, PST.p_date, PST.p_name, A_values[0]))
prec_filename = filename
else:
2014-08-20 13:14:57 +00:00
if h.redis_queue_shutdown():
print "Shutdown Flag Up: Terminating"
publisher.warning("Shutdown Flag Up: Terminating.")
break
publisher.debug("Script url is Idling 10s")
time.sleep(10)
2014-08-20 13:14:57 +00:00
message = h.redis_rpop()
except dns.exception.Timeout:
print "dns.exception.Timeout ", hostl