Sub topic 17
Restricting access and hardening the server
This chapter assumes you can install and run BIND and Unbound, read named.conf and unbound.conf, and use dig. It also assumes you know how to manage services with systemd and write basic firewall rules. Here you will lock down the servers you built in earlier chapters so they answer only the clients you intend.
Why a DNS server needs hardening
A DNS server is reachable by anyone who can send it a UDP packet. Two mistakes cause most of the damage:
- Open recursion. A resolver that answers any client can be used in reflection and amplification attacks. A small spoofed query produces a large answer that is sent to the victim. It also lets outsiders fill your cache and use your bandwidth.
- Open zone transfers. An authoritative server that hands a full copy of your zone to anyone gives an attacker a complete list of your hosts.
Hardening is mostly about matching each role to the clients allowed to use it. Authoritative service is public by design. Recursive service is private.
Separate the roles first
The strongest single control is not running both roles on one daemon. Run an authoritative-only BIND on the public address and a recursive Unbound on internal addresses. If one process must do both, you depend entirely on ACLs to keep them apart, and a mistake exposes both.
If you cannot separate them, bind each service to a different address with listen-on (BIND) or interface (Unbound). The public address then serves only authoritative data.
Restricting Unbound
Unbound refuses all clients by default except localhost. Write the policy explicitly anyway, so the intent is visible in the file.
server:
interface: 127.0.0.1
interface: 192.0.2.10
access-control: 127.0.0.0/8 allow
access-control: 192.0.2.0/24 allow
access-control: 0.0.0.0/0 refuse
access-control: ::0/0 refuse
hide-identity: yes
hide-version: yes
harden-glue: yes
harden-dnssec-stripped: yes
harden-referral-path: yes
use-caps-for-id: yes
unwanted-reply-threshold: 10000000
private-address: 10.0.0.0/8
private-address: 192.168.0.0/16
private-address: 172.16.0.0/12Notes on the less obvious lines:
access-controlrules match the longest prefix. Therefuseentry for0.0.0.0/0is the catch-all. The::0/0line does the same for IPv6, which is easy to forget.refusesends a DNS REFUSED answer.denydrops the query silently. Usedenyfor addresses you want to ignore, because a REFUSED reply to a spoofed source is itself traffic you generate.private-addressremoves answers from the internet that point into private ranges. This blocks DNS rebinding. It also hides legitimate internal names served by an upstream, so addprivate-domain: "corp.example."for those.use-caps-for-id: yesrandomises letter case in outgoing queries as extra entropy. A few broken authoritative servers do not echo the case and fail. Test before keeping it on a production resolver.
Restricting BIND
BIND needs three kinds of control: who may query, who may recurse, and who may transfer. Define a named ACL once and reuse it.
acl "trusted" {
127.0.0.1;
::1;
192.0.2.0/24;
};
options {
directory "/var/cache/bind";
listen-on { 127.0.0.1; 192.0.2.10; };
listen-on-v6 { ::1; };
allow-query { any; };
allow-recursion { none; };
allow-query-cache { none; };
allow-transfer { none; };
version "not disclosed";
hostname none;
server-id none;
minimal-responses yes;
rate-limit {
responses-per-second 10;
slip 2;
};
};
zone "example.com" {
type primary;
file "/etc/bind/db.example.com";
allow-transfer { 192.0.2.11; };
};How the pieces interact:
allow-querycontrols who may ask. On an authoritative server it is usuallyany.allow-recursioncontrols who may get answers that require lookups elsewhere. Settingrecursion no;is stricter, because the server never recurses at all. On a resolver, setallow-recursion { trusted; };andallow-query-cache { trusted; };instead.allow-transfer { none; }inoptionsis a safe global default. A zone-levelallow-transferoverrides it for that zone only, so a new zone never starts open.rate-limitis response rate limiting (RRL). It throttles identical answers to the same client network, which reduces your value as an amplifier.slip 2sends every second limited response as a short truncated reply. A real client then retries over TCP, while a spoofed victim gets little.version,hostnameandserver-idstopdig CH TXT version.bindfrom reporting your software version. This is minor obscurity, not protection. Patching matters far more.
Authenticating transfers
IP-based allow-transfer can be bypassed by address spoofing, and UDP-based NOTIFY and SOA traffic is easy to forge. Require TSIG so transfers are signed with a shared secret. You generated the key in the zone transfer chapter. On the primary, require it:
key "transfer-key" {
algorithm hmac-sha256;
secret "BASE64SECRET==";
};
zone "example.com" {
type primary;
file "/etc/bind/db.example.com";
allow-transfer { key "transfer-key"; };
};Keep the file holding the secret readable only by the BIND user. A common approach is to put the key statement in /etc/bind/transfer.key, set mode 0640 with group bind, and add include "/etc/bind/transfer.key"; to named.conf.
Firewall and network exposure
DNS uses UDP 53 for most queries and TCP 53 for large answers, truncated replies, and zone transfers. Blocking TCP 53 breaks DNSSEC and some normal answers. Allow both.
This nftables fragment accepts DNS from the trusted network only, which suits an internal resolver:
sudo nft add rule inet filter input ip saddr 192.0.2.0/24 udp dport 53 accept
sudo nft add rule inet filter input ip saddr 192.0.2.0/24 tcp dport 53 acceptFor a public authoritative server, accept port 53 from anywhere but still keep recursion off in the daemon. The firewall is a second layer. It must not be the only control, because rules get flushed or reordered.
Reduce the process's privileges
Both daemons drop privileges after binding port 53. Confirm this rather than assuming it:
ps -o user,pid,cmd -C named
ps -o user,pid,cmd -C unbound
sudo systemctl show named -p User -p ProtectSystemPackaged systemd units on current distributions already include sandboxing. If yours does not, add a drop-in with systemctl edit named:
[Service]
NoNewPrivileges=yes
ProtectSystem=strict
ProtectHome=yes
PrivateTmp=yes
ReadWritePaths=/var/cache/bind /var/lib/bindCheck the paths against your zone and journal files first. A too-strict ReadWritePaths makes secondary zones fail with permission errors when named tries to write the transferred zone file.
Also restrict the control channels. rndc listens only on 127.0.0.1 by default, with a key in /etc/bind/rndc.key. unbound-control should use control-interface: 127.0.0.1 or a Unix socket path, never a LAN address.
Verifying the result
Test from an address that should be refused, not only from the server itself. A test from localhost proves nothing about outside access.
# From an outside host: recursion must fail
dig @192.0.2.10 example.org A +recurse
# Expect: status: REFUSED
# From an outside host: transfer must fail
dig @192.0.2.10 example.com AXFR
# Expect: Transfer failed.
# From the secondary, using the key
dig @192.0.2.10 example.com AXFR -y hmac-sha256:transfer-key:BASE64SECRET==
# Version disclosure
dig @192.0.2.10 version.bind CH TXT +shortAlso check from the outside that you are not exposing extra ports, for example with ss -lntup on the server, and confirm only the intended addresses appear.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Only `allow-query` set on a resolver | Anyone in the list can query, but the cache still answers other clients | Set `allow-recursion` and `allow-query-cache` too |
| Forgetting the IPv6 catch-all | Resolver is open over IPv6 | Add `access-control: ::0/0 refuse` |
| Testing only from localhost | Open resolver goes unnoticed | Test from an untrusted host |
| Blocking TCP 53 | Large answers and transfers fail | Allow both protocols |
| Secret key in a world-readable file | Anyone local can sign transfers | Restrict file mode and ownership |
| Reload without checking | A syntax error stops the service | Run `named-checkconf` or `unbound-checkconf` first |
Exercise
An internal BIND resolver at 192.0.2.10 has allow-query { any; }; and recursion yes; with no other access options. A scan from the internet shows it answers dig @192.0.2.10 example.org A. Which options should you add so that only 192.0.2.0/24 can recurse, and how do you confirm the fix?
Show answer
Add allow-recursion { 192.0.2.0/24; }; and allow-query-cache { 192.0.2.0/24; }; (or reference an ACL such as trusted), and also set allow-transfer { none; }; as a safe default. Run named-checkconf, then rndc reconfig or reload. Confirm from a host outside 192.0.2.0/24 that dig @192.0.2.10 example.org A +recurse returns status: REFUSED, and from inside the network that the same query still succeeds.
Exercise
Why is access-control: 0.0.0.0/0 refuse alone not enough on a dual-stack Unbound server, and which line fixes it?
Show answer
It covers only IPv4. IPv6 clients are matched by a separate prefix, so the server may still answer them. Add access-control: ::0/0 refuse, then verify with a query over IPv6 from an untrusted host.