As another piece of work I’ve been doing for the excellent Strongarm anti-malware team we recently converted the service so that it can be used to get instant protection wherever you are. Part of this involved my work in converting the core (customized) DNS server into an open resolver. This is usually strongly advised against as you can unwittingly become part of some very serious Denial of Service attacks, however in this blog post I show you how to implement some pretty simple restrictions and limitations to prevent this from happening so you can run a DNS open resolver without running this risk.
Here’s a copy of the article:
One of the challenges of running an open DNS resolver is that it can be used in a number of different attacks, compared to a server that is only allowed access from a known set of IPs. One of the most well known is the DNS amplification attack. As this article explains, â€œThe fact that a DNS reply may be many times larger than a DNS query allows the attacker to achieve amplification by spoofing a relatively small query that is known to generate a large answer in responseâ€. That means that if I can send a DNS question that takes 50 bytes, and I send it pretending to be the computer that I want to attack, and the answer to that question is 1000 bytes, then I have effectively multiplied the traffic that I can attack with by 20 times. Especially as DNSSEC (Domain Name System Security Extensions) become more common, the RRSIG and DNSKEY DNS response codes can contain a lot of data that can be used in this type of attack.
In this post, Iâ€™d like to present a couple of ways to easily protect your open DNS resolver from being involved in malware attacks like the DNS amplification attack.
Configuring a DNS Resolver
Many DNS servers, or frontends such as PowerDNS or dnsdist, have the built-in or user-configurable ability to limit some types of attacks. In the case of dnsdist, the loadbalancer sits in front of the DNS servers and monitors the traffic going to and from them in order to blacklist hosts that are abusing the platform.
However, when configuring this within Strongarmâ€™s servers, we wanted the ultimate scalability and flexibility on our DNS infrastructure, so we decided not to use dnsdist but instead use a pure networking approach. Here are a few steps that you can take to protect your DNS infrastructure no matter whether you use a DNS loadbalancer or servers interfacing directly to the internet.
The first step you can take in protecting your server is to ensure that ANY queries cannot be used in an attack. An ANY query returns all the records of a particular domain so naturally it returns more data than a standard query. This is usually easy to configure with an option like â€˜any-to-tcpâ€™ in PowerDNS. This setting says that if the recursive server receives an ANY query, it will automatically send back a small redirect: â€œTCP is requiredâ€.
To understand why this helps prevent attacks we need to understand the following three things.
- An ANY query will usually return larger responses as it asks for all records under a particular domain.
- 99% of the time, an ANY query is not legitimate traffic. Usually, a host will only want a specific type of record such as A or MX.
- Whereas itâ€™s easy to spoof UDP traffic, itâ€™s virtually impossible to spoof TCP. This is because establishing a TCP connection requires a 3-way handshake. For example, if the client says â€œIâ€™d like to open a connectionâ€, and the server says â€œOkay, youâ€™d like to open a connection, itâ€™s now openâ€, then the client says, â€œThanks, the connection is now openâ€. While you can spoof the initiation of the connection, when the server says â€œOkay, youâ€™d like to open a connection, itâ€™s now open,â€ the host that has been spoofed will reply â€œWhat?! I didnâ€™t ask to open a connection!â€ and it wonâ€™t go any further.
Putting this all together, we can see that this can be a very effective preventative measure for abusing an open DNS resolver. Legitimate clients will fall back to using TCP and attackers will simply give up. We canâ€™t use this for all connections because having to do every DNS lookup over TCP would noticeably slow down internet browsing speed, but we can do this easily enough on connections that have a high probability of being attack traffic.
In a similar vein, another useful option for many DNS servers is the ability to limit the size of a return packet over UDP. Typically, you would configure this to say, â€œIf the return packet is more than X bytes, send a TCP redirect and only allow this over TCP.â€
Firewall Limiting of Potential Attack Traffic
In addition to doing the above, we implemented a pure firewall-based approach to throttling attack traffic. To do this, we needed to configure our firewall to be stateless, as we described how to do in a previous post.
As opposed to dnsdist or other frontend servers, this allows you to deploy either on a single server or on a frontend router that covers multiple resolvers. This also should be much more efficient as all processing occurs in-kernel via netfilter rather than having to go through a program which may crash or be somehow limited in the speed at which it can process data. As we showed in a previous post this is very efficient at packet processing.
We start by creating an â€˜ipsetâ€™ of IPs that we have currently blacklisted. Weâ€™ll use the â€˜timeoutâ€™ option to specify that after we have added an IP into this blacklist, it will automatically expire after a certain time. Weâ€™ll also limit it to a maximum 100,000 IPs so that an attacker cannot use this to take our server offline:
ipset create throttled-ips hash:ip timeout 600 family inet maxelem 100000
Then, if an IP is on this list, weâ€™ll block it from doing any UDP traffic to our server:
iptables -t raw -A PREROUTING -p udp -m set --match-set throttled-ips src -j DROP
Now for the clever part: weâ€™ll look for DNS responses that are over a certain threshold packet size (700 bytes) and start monitoring them to see the rate at which someone is sending them:
iptables -N LARGE_DNS_PACKET_TRACKING # Create the destination chain
iptables -A OUTPUT -p udp --sport 53 \
Â Â Â Â Â Â Â -m length --length 700:0xffff \
Â Â Â Â Â Â Â -j LARGE_DNS_PACKET_TRACKING
This points to a new iptables chain called â€œLARGE_DNS_PACKET_TRACKINGâ€ which weâ€™ll set up as follows:
iptables -A LARGE_DNS_PACKET_TRACKING -m hashlimit --hashlimit-mode dstip --hashlimit-dstmask 32 \
Â Â --hashlimit-upto 50kb/min --hashlimit-burst 10 --hashlimit-name large-dns-packets --hashlimit-htable-max 100000 \
Â Â -j ACCEPT
This first rule allows up to 50kb of large DNS responses per minute to a single IP (the 32 means a /32, i.e. a single IP address), and always allows the first 10 large response packets through. Again, it tracks, at most, 100,000 IPs in order to avoid an attack vector against our server.
After a host goes over this threshold, weâ€™ll pass the traffic through to the next stage of the chain:
iptables -A LARGE_DNS_PACKET_TRACKING -j SET --add-set throttled-ips dstip --timeout 600 --exist
This is where the magic happens. If the client breaches the threshold set above, then it will add its IP to the ipset we created earlier, meaning that it will be blocked for 10 minutes. Finally, letâ€™s note this in the system log and then drop the packet:
iptables -A LARGE_DNS_PACKET_TRACKING -j LOG --log-prefix "DNS-amplification protection: "
iptables -A LARGE_DNS_PACKET_TRACKING -j DROP
With the right protection in place, itâ€™s not such a bad thing to run an open DNS resolver on the internet. If you look in your serverâ€™s configuration manual, you should find a few options that can also help in preventing attacks. Additionally, we recommend setting up a firewall-based system like I detailed above so that you can limit the amount of traffic you send out. Otherwise, you may easily find your server being disconnected by your ISP for being part of an attack.