Last active
January 18, 2024 19:23
-
-
Save jzelinskie/3d2b11830224993fc8a7441b3268fb12 to your computer and use it in GitHub Desktop.
generate a knot-resolver (kresd) blacklist.rpz from pihole sources
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Refer to manual: https://knot-resolver.readthedocs.io/en/latest/daemon.html#configuration | |
-- Listen on all interfaces (localhost would not work in Docker) | |
net.listen('0.0.0.0') | |
net.listen('0.0.0.0', 853, {tls=true}) | |
-- Auto-maintain root TA | |
trust_anchors.file = '/data/root.keys' | |
-- Load Useful modules | |
modules = { | |
'policy', -- Block queries to local zones/bad sites | |
'serve_stale < cache', -- Allows stale-ness by up to one day, after roughly four seconds trying to contact the servers | |
'workarounds < iterate', -- Alters resolver behavior on specific broken sub-domains | |
'predict', -- Prefetch expiring/frequent records | |
'stats', -- Track internal statistics | |
http = { -- HTTP server for serving stats etc... | |
host = '192.168.2.110', | |
port = 8053, | |
cert = false, | |
}, | |
} | |
-- Smaller cache size | |
cache.size = 20 * MB | |
-- Block Ads and Malware Domains | |
policy.add(policy.rpz(policy.DENY, '/data/blacklist.rpz')) | |
-- Forward DNS to CloudFlare using TLS | |
policy.add(policy.all( | |
policy.TLS_FORWARD({ | |
{'1.1.1.1', hostname='cloudflare-dns.com', ca_file='/data/DigiCertGlobalRootCA.crt' }, | |
{'1.0.0.1', hostname='cloudflare-dns.com', ca_file='/data/DigiCertGlobalRootCA.crt' }, | |
}) | |
)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright (c) 2019 Jimmy Zelinskie | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import re | |
from contextlib import closing | |
from urllib2 import urlopen | |
domain_blacklist = [ | |
"", | |
"localhost", | |
"127.0.0.1", | |
"0.0.0.0", | |
"255.225.225.255", | |
"::1", | |
] | |
IP_REGEX = "(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])" | |
HOST_REGEX = "(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])" | |
HOSTSFILE_PREFIX = IP_REGEX + "|" + HOST_REGEX | |
HOSTSFILE_PREFIX_REGEX = re.compile(HOSTSFILE_PREFIX + "(\s+)") | |
HOSTSFILES_URLS = [ | |
"http://sysctl.org/cameleon/hosts", | |
"https://hosts-file.net/ad_servers.txt", | |
"https://hosts-file.net/grm.txt", | |
"https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts;showintro=0", | |
"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts", | |
"https://raw.githubusercontent.com/StevenBlack/hosts/master/data/KADhosts/hosts", | |
"https://raw.githubusercontent.com/anudeepND/blacklist/master/adservers.txt", | |
"https://reddestdream.github.io/Projects/MinimalHosts/etc/MinimalHostsBlocker/minimalhosts", | |
] | |
DOMAIN_LIST_URLS = [ | |
"https://gitlab.com/quidsup/notrack-blocklists/raw/master/notrack-blocklist.txt", | |
"https://mirror1.malwaredomains.com/files/justdomains", | |
"https://s3.amazonaws.com/lists.disconnect.me/simple_ad.txt", | |
"https://s3.amazonaws.com/lists.disconnect.me/simple_tracking.txt", | |
"https://v.firebog.net/hosts/AdguardDNS.txt", | |
"https://v.firebog.net/hosts/Easyprivacy.txt", | |
"https://zeustracker.abuse.ch/blocklist.php?download=domainblocklist", | |
] | |
def strip_comment(line): | |
return line.split("#")[0].strip() | |
def domains_from_domain_list(fstream): | |
for linebytes in fstream: | |
line = linebytes.decode("utf-8").strip() | |
if line.startswith("#"): | |
continue | |
domain = strip_comment(line) | |
if domain not in domain_blacklist: | |
yield domain | |
def domains_from_hostfile(fstream): | |
for linebytes in fstream: | |
line = linebytes.decode("utf-8").strip() | |
prefix = HOSTSFILE_PREFIX_REGEX.match(line) | |
if prefix is None: | |
continue | |
domain = strip_comment(line[prefix.end():]) | |
if domain in domain_blacklist: | |
continue | |
yield domain | |
def main(): | |
print "$TTL\t2\n" \ | |
"@\tIN\tSOA\tlocalhost.\troot.localhost.\t(\n" \ | |
"\t\t2\t;\tserial\n" \ | |
"\t\t2w\t;\trefresh\n" \ | |
"\t\t2w\t;\tretry\n" \ | |
"\t\t2w\t;\texpiry\n" \ | |
"\t\t2w\t;\tminimum\n" \ | |
"\t\tIN\tNS\tlocalhost.\n\n" | |
domains = set() | |
for url in HOSTSFILES_URLS: | |
with closing(urlopen(url)) as f: | |
domains = domains.union(set([domain for domain in domains_from_hostfile(f)])) | |
for url in DOMAIN_LIST_URLS: | |
with closing(urlopen(url)) as f: | |
domains = domains.union(set([domain for domain in domains_from_domain_list(f)])) | |
for domain in sorted(domains): | |
print domain + "\tCNAME\t." | |
print "*." + domain + "\tCNAME\t." | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment