2014-08-06 09:43:40 +00:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
# -*-coding:UTF-8 -*
|
2014-08-14 15:55:18 +00:00
|
|
|
import redis
|
|
|
|
import ConfigParser
|
|
|
|
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
|
2014-08-06 09:43:40 +00:00
|
|
|
from packages import lib_refine
|
|
|
|
from packages import ZMQ_PubSub
|
|
|
|
from pubsublogger import publisher
|
|
|
|
|
2014-08-14 12:22:11 +00:00
|
|
|
# Country and ASN lookup
|
|
|
|
from cymru.ip2asn.dns import DNSClient as ip2asn
|
|
|
|
import socket
|
|
|
|
import pycountry
|
|
|
|
import ipaddress
|
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
configfile = './packages/config.cfg'
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
|
2014-08-06 09:43:40 +00:00
|
|
|
def main():
|
|
|
|
"""Main Function"""
|
|
|
|
|
|
|
|
# CONFIG #
|
|
|
|
cfg = ConfigParser.ConfigParser()
|
|
|
|
cfg.read(configfile)
|
|
|
|
|
|
|
|
# REDIS #
|
|
|
|
r_serv = redis.StrictRedis(
|
2014-08-14 15:55:18 +00:00
|
|
|
host=cfg.get("Redis_Queues", "host"),
|
|
|
|
port=cfg.getint("Redis_Queues", "port"),
|
|
|
|
db=cfg.getint("Redis_Queues", "db"))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
r_serv1 = redis.StrictRedis(
|
2014-08-14 15:55:18 +00:00
|
|
|
host=cfg.get("Redis_Data_Merging", "host"),
|
|
|
|
port=cfg.getint("Redis_Data_Merging", "port"),
|
|
|
|
db=cfg.getint("Redis_Data_Merging", "db"))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
r_serv2 = redis.StrictRedis(
|
2014-08-14 15:55:18 +00:00
|
|
|
host=cfg.get("Redis_Cache", "host"),
|
|
|
|
port=cfg.getint("Redis_Cache", "port"),
|
|
|
|
db=cfg.getint("Redis_Cache", "db"))
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
# LOGGING #
|
|
|
|
publisher.channel = "Script"
|
|
|
|
|
|
|
|
# ZMQ #
|
2014-08-14 15:55:18 +00:00
|
|
|
# Subscriber
|
2014-08-06 09:43:40 +00:00
|
|
|
subscriber_name = "urls"
|
|
|
|
subscriber_config_section = "PubSub_Categ"
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
# Publisher
|
2014-08-06 09:43:40 +00:00
|
|
|
publisher_config_section = "PubSub_Url"
|
|
|
|
publisher_name = "adress"
|
|
|
|
pubchannel = cfg.get("PubSub_Url", "channel")
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
# Country to log as critical
|
2014-08-14 12:22:11 +00:00
|
|
|
cc_critical = cfg.get("PubSub_Url", "cc_critical")
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
sub = ZMQ_PubSub.ZMQSub(configfile, subscriber_config_section, "web_categ", subscriber_name)
|
|
|
|
pub = ZMQ_PubSub.ZMQPub(configfile, publisher_config_section, publisher_name)
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
# FUNCTIONS #
|
|
|
|
publisher.info("Script URL subscribed to channel web_categ")
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
message = sub.get_msg_from_queue(r_serv)
|
2014-08-06 09:43:40 +00:00
|
|
|
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:
|
2014-08-11 07:08:28 +00:00
|
|
|
try:
|
2014-08-14 15:55:18 +00:00
|
|
|
if message is not None:
|
|
|
|
channel, filename, word, score = message.split()
|
2014-08-11 07:08:28 +00:00
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
if prec_filename is None or filename != prec_filename:
|
2014-08-11 07:08:28 +00:00
|
|
|
domains_list = []
|
2014-08-14 15:55:18 +00:00
|
|
|
PST = Paste.Paste(filename)
|
2014-08-14 12:22:11 +00:00
|
|
|
client = ip2asn()
|
2014-08-11 07:08:28 +00:00
|
|
|
for x in PST.get_regex(url_regex):
|
|
|
|
scheme, credential, subdomain, domain, host, tld, port, resource_path, query_string, f1, f2, f3, f4 = x
|
|
|
|
domains_list.append(domain)
|
|
|
|
msg = pubchannel + " " + str(x)
|
2014-08-14 15:55:18 +00:00
|
|
|
pub.send_message(msg)
|
2014-08-11 07:08:28 +00:00
|
|
|
publisher.debug('{0} Published'.format(x))
|
|
|
|
|
|
|
|
if f1 == "onion":
|
|
|
|
print domain
|
|
|
|
|
2014-08-14 12:22:11 +00:00
|
|
|
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-14 15:55:18 +00:00
|
|
|
l = client.lookup(socket.inet_aton(ip), qType='IP')
|
2014-08-14 12:22:11 +00:00
|
|
|
except ipaddress.AddressValueError:
|
|
|
|
continue
|
2014-08-14 15:55:18 +00:00
|
|
|
cc = getattr(l, 'cc')
|
|
|
|
asn = getattr(l, 'asn')
|
2014-08-14 12:22:11 +00:00
|
|
|
|
|
|
|
# EU is not an official ISO 3166 code (but used by RIPE
|
|
|
|
# IP allocation)
|
|
|
|
if cc is not None and cc != "EU":
|
2014-08-14 15:55:18 +00:00
|
|
|
print hostl, asn, cc, pycountry.countries.get(alpha2=cc).name
|
2014-08-14 12:22:11 +00:00
|
|
|
if cc == cc_critical:
|
2014-08-14 15:55:18 +00:00
|
|
|
# FIXME: That's going to fail.
|
2014-08-14 12:22:11 +00:00
|
|
|
publisher.warning('{0};{1};{2};{3};{4}'.format("Url", PST.p_source, PST.p_date, PST.p_name, "Detected " + str(A_values[0]) + " " + hostl + " " + cc))
|
|
|
|
else:
|
2014-08-14 15:55:18 +00:00
|
|
|
print hostl, asn, cc
|
2014-08-11 07:08:28 +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-14 15:55:18 +00:00
|
|
|
PST.save_attribute_redis(r_serv1, channel, (A_values[0], list(A_values[1])))
|
2014-08-11 07:08:28 +00:00
|
|
|
|
|
|
|
pprint.pprint(A_values)
|
2014-08-14 15:55:18 +00:00
|
|
|
publisher.info('{0};{1};{2};{3};{4}'.format("Url", PST.p_source, PST.p_date, PST.p_name, "Checked " + str(A_values[0]) + " URL"))
|
2014-08-11 07:08:28 +00:00
|
|
|
prec_filename = filename
|
|
|
|
|
|
|
|
else:
|
|
|
|
if r_serv.sismember("SHUTDOWN_FLAGS", "Urls"):
|
|
|
|
r_serv.srem("SHUTDOWN_FLAGS", "Urls")
|
|
|
|
print "Shutdown Flag Up: Terminating"
|
|
|
|
publisher.warning("Shutdown Flag Up: Terminating.")
|
|
|
|
break
|
|
|
|
publisher.debug("Script url is Idling 10s")
|
|
|
|
time.sleep(10)
|
|
|
|
|
2014-08-14 15:55:18 +00:00
|
|
|
message = sub.get_msg_from_queue(r_serv)
|
2014-08-11 07:08:28 +00:00
|
|
|
except dns.exception.Timeout:
|
2014-08-14 15:55:18 +00:00
|
|
|
print "dns.exception.Timeout", A_values
|
2014-08-11 07:08:28 +00:00
|
|
|
pass
|
2014-08-06 09:43:40 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|