Skip to content

Instantly share code, notes, and snippets.

@m-rousse
Created May 22, 2017 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m-rousse/e126f628f3f8994ef515079d8426965a to your computer and use it in GitHub Desktop.
Save m-rousse/e126f628f3f8994ef515079d8426965a to your computer and use it in GitHub Desktop.
Script to fetch blacklisted domains from public lists and generate a zone file for bind.
#!/usr/bin/python3
from urllib.request import Request, urlopen
global confFile, sinkZone
urls = [
"https://adaway.org/hosts.txt",
"https://hosts-file.net/ad_servers.txt",
"https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"
]
confFile = "/var/named/blacklisted.zones"
sinkZone = "/var/named/sinkhole.zone"
def getHostsList( url, hosts = set() ):
req = Request(url, headers={'User-Agent': 'curl/7.52.1'})
hosts_list = urlopen(req).read().decode("utf-8")
for line in hosts_list.splitlines():
if len(line) and line[0] == "#":
continue
host = line.split()
if len(host) == 0:
continue
host = host[1]
if host[len(host)-1] == ".":
host = host[:-1]
hosts.add(host.lower())
return hosts
def genConfFile( hosts ):
global confFile, sinkZone
f = open(confFile, 'w')
for host in hosts:
f.write("zone \""+host+"\" {type master; allow-update{ key \"rdnc-key\"; }; file \""+sinkZone+"\";};\n")
f.close()
hosts = set()
for url in urls:
getHostsList(url, hosts)
genConfFile( sorted(hosts) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment