-- dnsdist configuration file, an example can be found in /usr/share/doc/dnsdist/examples/ -- dnsdist front end for AdGuard Home. Plain DNS on 53 only. -- AdGuard Home keeps DoH (443), DoT (853/tcp) and DoQ (853/udp) itself. -- Console for dnsdist -c, localhost only controlSocket("127.0.0.1:5199") setKey("REDACTED") -- Primary: AdGuard Home's own DoH on loopback. X-Forwarded-For carries the -- client's real IP, which AGH trusts from 127.0.0.1 (trusted_proxies). newServer({ address = "127.0.0.1:443", name = "adguard-doh", tls = "openssl", subjectName = "us1.blissdns.net", dohPath = "/dns-query", validateCertificates = true, addXForwardedHeaders = true, order = 1, checkName = "dnsdist-health.blissdns.net.", checkInterval = 60 }) -- Fallback: AGH's plain DNS listener. Only used if the DoH backend fails its -- health checks. AGH sees 127.0.0.1 for everything while this is in use. newServer({ address = "127.0.0.1:5300", name = "adguard-plain", order = 2, checkName = "dnsdist-health.blissdns.net.", checkInterval = 60 }) -- Always use the lowest-order backend that's up. Without this, dnsdist -- splits traffic across both and half your queries lose their real IP. setServerPolicy(firstAvailable) setLocal("0.0.0.0:53") addLocal("[::]:53") -- dnsdist only answers private ranges by default. A public resolver has to open this up. setACL({"0.0.0.0/0", "::/0"}) -- EMERGENCY ABUSE ONLY: Uncomment to force TC=1 on all UDP queries and require TCP. addAction(TCPRule(false), TCAction()) -- Blocklist, regenerated by update-blocklist.sh blocked = newNMG() local f = io.open("/etc/dnsdist/blocked.txt", "r") if f then for line in f:lines() do line = line:gsub("%s+", "") if line ~= "" and line:sub(1, 1) ~= "#" then blocked:addMask(line) end end f:close() else warnlog("/etc/dnsdist/blocked.txt not found, starting with an empty blocklist") end addAction(NetmaskGroupRule(blocked), DropAction()) -- ANY has no legitimate use on a public resolver addAction(QTypeRule(DNSQType.ANY), DropAction()) -- Big-answer types over UDP always get TC, real clients retry over TCP -- addAction(AndRule({OrRule({QTypeRule(DNSQType.TXT), QTypeRule(DNSQType.DNSKEY)}), TCPRule(false)}), TCAction()) addAction(AndRule({OrRule({QTypeRule(DNSQType.TXT), QTypeRule(DNSQType.DNSKEY), QTypeRule(DNSQType.RRSIG)}), TCPRule(false)}), TCAction()) -- Over 20 qps per IP on UDP gets TC instead of dropped, so NAT users still resolve addAction(AndRule({MaxQPSIPRule(20, 32, 64, 100), TCPRule(false)}), TCAction()) -- Hard ceiling on TCP, kept well above the UDP limit addAction(MaxQPSIPRule(100, 32, 64, 200), DropAction()) -- disable security status polling via DNS setSecurityPollSuffix("") -- Bigger history so the 10 second windows below actually hold 10 seconds of -- traffic. The default of 10000 entries can be less than that when busy. setRingBuffersSize(100000) -- Per client: over 10 NXDOMAIN/s for 10 seconds gets UDP truncated for 10 -- minutes. Real clients retry over TCP and keep working, spoofed sources and -- UDP-only bots stop. Logs a warning from 5/s so you can see who's close. local nxd_soft = dynBlockRulesGroup() nxd_soft:setRCodeRate(DNSRCode.NXDOMAIN, 10, 10, "NXDOMAIN flood, UDP truncated", 600, DNSAction.Truncate, 5) -- Per client: over 50 NXDOMAIN/s gets dropped outright for an hour. Catches -- bots that retry over TCP. Separate group, since a second NXDOMAIN rule in -- the same group would replace the first. local nxd_hard = dynBlockRulesGroup() nxd_hard:setRCodeRate(DNSRCode.NXDOMAIN, 50, 10, "NXDOMAIN flood, dropped", 3600, DNSAction.Drop) -- Public suffixes (com, co.uk, com.au...) from the publicsuffix package, so the -- per-domain rule only ever flags registrable domains like example.co.uk. -- Wildcard and exception entries are skipped, so names under those never match. local psl = {} local pf = io.open("/usr/share/publicsuffix/public_suffix_list.dat", "r") if pf then for line in pf:lines() do line = line:gsub("%s+", "") if line ~= "" and not line:find("^//") and not line:find("[*!]") then psl[line:lower()] = true end end pf:close() else warnlog("public_suffix_list.dat not found, per-domain flood rule will never fire") end -- Per domain: one registrable domain getting over 1000 queries in 10 seconds, -- over 90% NXDOMAIN, from any mix of clients. Log-only (NoOp) until you trust it. local prsd = dynBlockRulesGroup() prsd:setSuffixMatchRule(10, "random subdomain flood", 600, DNSAction.NoOp, function(node, self, children) local name = node.fullname:lower():gsub("%.$", "") local parent = name:match("^[^.]+%.(.+)$") if parent and psl[parent] and not psl[name] and children.queries > 1000 and children.nxdomains > children.queries * 0.9 then return true, "random subdomain flood on " .. node.fullname end return false end) -- dnsdist calls this about once a second function maintenance() nxd_soft:apply() nxd_hard:apply() prsd:apply() end