2018-01-17 15:28:07 +01:00
|
|
|
#! /usr/bin/env python
|
|
|
|
|
|
|
|
# run with python generate-domains-blacklist.py > list.txt.tmp && mv -f list.txt.tmp list
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import urllib2
|
|
|
|
|
|
|
|
|
2019-02-15 00:03:02 +01:00
|
|
|
def parse_time_restricted_list(content):
|
2019-02-14 23:27:19 +01:00
|
|
|
rx_comment = re.compile(r'^(#|$)')
|
|
|
|
rx_inline_comment = re.compile(r'\s*#\s*[a-z0-9-].*$')
|
2019-02-15 00:03:02 +01:00
|
|
|
rx_trusted = re.compile(r'^([*a-z0-9.-]+)\s*(@\S+)?$')
|
2019-02-14 23:27:19 +01:00
|
|
|
|
|
|
|
names = set()
|
2019-02-15 00:03:02 +01:00
|
|
|
time_restrictions = {}
|
2019-02-14 23:27:19 +01:00
|
|
|
rx_set = [rx_trusted]
|
|
|
|
for line in content.splitlines():
|
|
|
|
line = str.lower(str.strip(line))
|
|
|
|
if rx_comment.match(line):
|
|
|
|
continue
|
|
|
|
line = rx_inline_comment.sub('', line)
|
|
|
|
for rx in rx_set:
|
|
|
|
matches = rx.match(line)
|
|
|
|
if not matches:
|
|
|
|
continue
|
|
|
|
name = matches.group(1)
|
|
|
|
names.add(name)
|
2019-02-15 00:03:02 +01:00
|
|
|
time_restriction = matches.group(2)
|
|
|
|
if time_restriction:
|
|
|
|
time_restrictions[name] = time_restriction
|
|
|
|
return names, time_restrictions
|
|
|
|
|
|
|
|
|
|
|
|
def parse_trusted_list(content):
|
|
|
|
names, _time_restrictions = parse_time_restricted_list(content)
|
|
|
|
time_restrictions = {}
|
|
|
|
return names, time_restrictions
|
2019-02-14 23:27:19 +01:00
|
|
|
|
|
|
|
|
2018-02-19 16:38:06 +01:00
|
|
|
def parse_list(content, trusted=False):
|
2018-01-17 15:28:07 +01:00
|
|
|
rx_comment = re.compile(r'^(#|$)')
|
|
|
|
rx_inline_comment = re.compile(r'\s*#\s*[a-z0-9-].*$')
|
2019-02-14 23:27:19 +01:00
|
|
|
rx_u = re.compile(
|
|
|
|
r'^@*\|\|([a-z0-9.-]+[.][a-z]{2,})\^?(\$(popup|third-party))?$')
|
2018-01-17 15:28:07 +01:00
|
|
|
rx_l = re.compile(r'^([a-z0-9.-]+[.][a-z]{2,})$')
|
2019-02-14 23:27:19 +01:00
|
|
|
rx_h = re.compile(
|
|
|
|
r'^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}\s+([a-z0-9.-]+[.][a-z]{2,})$')
|
2018-01-17 15:28:07 +01:00
|
|
|
rx_mdl = re.compile(r'^"[^"]+","([a-z0-9.-]+[.][a-z]{2,})",')
|
|
|
|
rx_b = re.compile(r'^([a-z0-9.-]+[.][a-z]{2,}),.+,[0-9: /-]+,')
|
2018-03-26 20:43:42 +02:00
|
|
|
rx_dq = re.compile(r'^address=/([a-z0-9.-]+[.][a-z]{2,})/.')
|
2019-02-14 23:27:19 +01:00
|
|
|
|
|
|
|
if trusted:
|
|
|
|
return parse_trusted_list(content)
|
2018-01-17 15:28:07 +01:00
|
|
|
|
|
|
|
names = set()
|
2019-02-15 00:03:02 +01:00
|
|
|
time_restrictions = {}
|
2018-03-26 20:43:42 +02:00
|
|
|
rx_set = [rx_u, rx_l, rx_h, rx_mdl, rx_b, rx_dq]
|
2018-01-17 15:28:07 +01:00
|
|
|
for line in content.splitlines():
|
|
|
|
line = str.lower(str.strip(line))
|
|
|
|
if rx_comment.match(line):
|
|
|
|
continue
|
|
|
|
line = rx_inline_comment.sub('', line)
|
|
|
|
for rx in rx_set:
|
|
|
|
matches = rx.match(line)
|
|
|
|
if not matches:
|
|
|
|
continue
|
|
|
|
name = matches.group(1)
|
|
|
|
names.add(name)
|
2019-02-15 00:03:02 +01:00
|
|
|
return names, time_restrictions
|
|
|
|
|
|
|
|
|
|
|
|
def print_restricted_name(name, time_restrictions):
|
|
|
|
if name in time_restrictions:
|
|
|
|
print("{}\t{}".format(name, time_restrictions[name]))
|
|
|
|
else:
|
|
|
|
print("# ignored: [{}] was in the time-restricted list, "
|
|
|
|
"but without a time restriction label".format(name))
|
2018-01-17 15:28:07 +01:00
|
|
|
|
|
|
|
|
2018-02-19 14:38:43 +01:00
|
|
|
def load_from_url(url):
|
2018-01-17 15:28:07 +01:00
|
|
|
sys.stderr.write("Loading data from [{}]\n".format(url))
|
|
|
|
req = urllib2.Request(url)
|
|
|
|
trusted = False
|
|
|
|
if req.get_type() == "file":
|
|
|
|
trusted = True
|
|
|
|
response = None
|
|
|
|
try:
|
2018-02-11 19:15:52 +01:00
|
|
|
response = urllib2.urlopen(req, timeout=int(args.timeout))
|
2018-01-17 15:28:07 +01:00
|
|
|
except urllib2.URLError as err:
|
|
|
|
raise Exception("[{}] could not be loaded: {}\n".format(url, err))
|
|
|
|
if trusted is False and response.getcode() != 200:
|
2019-02-14 23:27:19 +01:00
|
|
|
raise Exception("[{}] returned HTTP code {}\n".format(
|
|
|
|
url, response.getcode()))
|
2018-01-17 15:28:07 +01:00
|
|
|
content = response.read()
|
|
|
|
|
2018-02-19 14:38:43 +01:00
|
|
|
return (content, trusted)
|
2018-01-17 15:28:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def name_cmp(name):
|
|
|
|
parts = name.split(".")
|
|
|
|
parts.reverse()
|
|
|
|
return str.join(".", parts)
|
|
|
|
|
|
|
|
|
|
|
|
def has_suffix(names, name):
|
|
|
|
parts = str.split(name, ".")
|
|
|
|
while parts:
|
|
|
|
parts = parts[1:]
|
|
|
|
if str.join(".", parts) in names:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def whitelist_from_url(url):
|
|
|
|
if not url:
|
|
|
|
return set()
|
2018-02-19 16:38:06 +01:00
|
|
|
content, trusted = load_from_url(url)
|
2018-01-17 15:28:07 +01:00
|
|
|
|
2019-02-15 00:03:02 +01:00
|
|
|
names, _time_restrictions = parse_list(content, trusted)
|
|
|
|
return names
|
2018-01-17 15:28:07 +01:00
|
|
|
|
|
|
|
|
2018-02-19 16:38:06 +01:00
|
|
|
def blacklists_from_config_file(file, whitelist, time_restricted_url, ignore_retrieval_failure):
|
2018-01-17 15:28:07 +01:00
|
|
|
blacklists = {}
|
2018-02-19 14:38:43 +01:00
|
|
|
whitelisted_names = set()
|
2018-01-17 15:28:07 +01:00
|
|
|
all_names = set()
|
|
|
|
unique_names = set()
|
|
|
|
|
2018-02-19 14:38:43 +01:00
|
|
|
# Load conf & blacklists
|
2018-01-17 15:28:07 +01:00
|
|
|
with open(file) as fd:
|
|
|
|
for line in fd:
|
|
|
|
line = str.strip(line)
|
|
|
|
if str.startswith(line, "#") or line == "":
|
|
|
|
continue
|
|
|
|
url = line
|
|
|
|
try:
|
2018-02-19 16:38:06 +01:00
|
|
|
content, trusted = load_from_url(url)
|
2019-02-15 00:03:02 +01:00
|
|
|
names, _time_restrictions = parse_list(content, trusted)
|
2018-01-17 15:28:07 +01:00
|
|
|
blacklists[url] = names
|
|
|
|
all_names |= names
|
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write(e.message)
|
|
|
|
if not ignore_retrieval_failure:
|
|
|
|
exit(1)
|
|
|
|
|
2018-02-19 16:38:06 +01:00
|
|
|
# Time-based blacklist
|
|
|
|
if time_restricted_url and not re.match(r'^[a-z0-9]+:', time_restricted_url):
|
|
|
|
time_restricted_url = "file:" + time_restricted_url
|
|
|
|
|
|
|
|
if time_restricted_url:
|
2019-02-15 00:03:02 +01:00
|
|
|
time_restricted_content, _trusted = load_from_url(time_restricted_url)
|
|
|
|
time_restricted_names, time_restrictions = parse_time_restricted_list(
|
|
|
|
time_restricted_content)
|
2018-02-19 16:38:06 +01:00
|
|
|
|
|
|
|
if time_restricted_names:
|
|
|
|
print("########## Time-based blacklist ##########\n")
|
|
|
|
for name in time_restricted_names:
|
2019-02-15 00:03:02 +01:00
|
|
|
print_restricted_name(name, time_restrictions)
|
2018-02-19 16:38:06 +01:00
|
|
|
|
|
|
|
# Time restricted names should be whitelisted, or they could be always blocked
|
|
|
|
whitelisted_names |= time_restricted_names
|
|
|
|
|
|
|
|
# Whitelist
|
|
|
|
if whitelist and not re.match(r'^[a-z0-9]+:', whitelist):
|
|
|
|
whitelist = "file:" + whitelist
|
|
|
|
|
|
|
|
whitelisted_names |= whitelist_from_url(whitelist)
|
|
|
|
|
2018-02-19 14:38:43 +01:00
|
|
|
# Process blacklists
|
2018-01-17 15:28:07 +01:00
|
|
|
for url, names in blacklists.items():
|
|
|
|
print("\n\n########## Blacklist from {} ##########\n".format(url))
|
|
|
|
ignored, whitelisted = 0, 0
|
|
|
|
list_names = list()
|
|
|
|
for name in names:
|
|
|
|
if has_suffix(all_names, name) or name in unique_names:
|
|
|
|
ignored = ignored + 1
|
|
|
|
elif has_suffix(whitelisted_names, name) or name in whitelisted_names:
|
|
|
|
whitelisted = whitelisted + 1
|
|
|
|
else:
|
|
|
|
list_names.append(name)
|
|
|
|
unique_names.add(name)
|
|
|
|
|
|
|
|
list_names.sort(key=name_cmp)
|
|
|
|
if ignored:
|
|
|
|
print("# Ignored duplicates: {}\n".format(ignored))
|
|
|
|
if whitelisted:
|
|
|
|
print("# Ignored entries due to the whitelist: {}\n".format(whitelisted))
|
|
|
|
for name in list_names:
|
|
|
|
print(name)
|
|
|
|
|
|
|
|
|
2019-02-14 23:27:19 +01:00
|
|
|
argp = argparse.ArgumentParser(
|
|
|
|
description="Create a unified blacklist from a set of local and remote files")
|
2018-01-17 15:28:07 +01:00
|
|
|
argp.add_argument("-c", "--config", default="domains-blacklist.conf",
|
2019-02-14 23:27:19 +01:00
|
|
|
help="file containing blacklist sources")
|
2018-01-17 15:28:07 +01:00
|
|
|
argp.add_argument("-w", "--whitelist", default="domains-whitelist.txt",
|
2019-02-14 23:27:19 +01:00
|
|
|
help="file containing a set of names to exclude from the blacklist")
|
2018-02-19 14:38:43 +01:00
|
|
|
argp.add_argument("-r", "--time-restricted", default="domains-time-restricted.txt",
|
2019-02-14 23:27:19 +01:00
|
|
|
help="file containing a set of names to be time restricted")
|
2018-01-17 15:28:07 +01:00
|
|
|
argp.add_argument("-i", "--ignore-retrieval-failure", action='store_true',
|
2019-02-14 23:27:19 +01:00
|
|
|
help="generate list even if some urls couldn't be retrieved")
|
2018-02-11 20:51:48 +01:00
|
|
|
argp.add_argument("-t", "--timeout", default=30,
|
2019-02-14 23:27:19 +01:00
|
|
|
help="URL open timeout")
|
2018-01-17 15:28:07 +01:00
|
|
|
args = argp.parse_args()
|
|
|
|
|
|
|
|
conf = args.config
|
|
|
|
whitelist = args.whitelist
|
2018-02-19 14:38:43 +01:00
|
|
|
time_restricted = args.time_restricted
|
2018-01-17 15:28:07 +01:00
|
|
|
ignore_retrieval_failure = args.ignore_retrieval_failure
|
|
|
|
|
2019-02-14 23:27:19 +01:00
|
|
|
blacklists_from_config_file(
|
|
|
|
conf, whitelist, time_restricted, ignore_retrieval_failure)
|