Why clean firewall logs mean nothing for DNS exfiltration
www.example.com, the compromised host sends queries like dG9wc2VjcmV0ZGF0YQ.exfil.attacker.com. That long subdomain label is base32 or base64 encoded payload. The authoritative nameserver for attacker.com receives the query, strips out the encoded chunk, and reassembles the exfiltrated data on the other side.
The response path works too. TXT records can carry up to around 255 bytes per string (multiple strings per record), so the attacker's nameserver can send commands or additional tooling back inside DNS responses. Tools like iodine and dnscat2 automate this entire process, creating a bidirectional tunnel over DNS that looks — to your firewall — like ordinary name resolution.
Your firewall did its job. It enforced the rules you gave it. The problem is that the rules say "allow DNS," and the attacker used DNS.
What DNS tunneling traffic actually looks like
mail.google.com is 15 characters. cdn.shopify.com is 15 characters. Legitimate subdomains rarely exceed 30-40 characters total.
DNS tunneling queries look different in three measurable ways:
Query length. RFC 1035 allows each label (the part between dots) to be up to 63 bytes, and the total query name up to 253 bytes. Tunneling tools pack as much data as possible into each query, so you see labels approaching those limits. A query like aGVsbG8gd29ybGQgdGhpcyBpcyBhIHRlc3Q.x.data.attacker.com is already suspicious just by length.
Entropy. Normal domain names use common letter patterns — vowels, consonants, recognizable words. Base32/base64 encoded data has high Shannon entropy because the character distribution is nearly uniform. A label like r4nd0mstr1ng8f has noticeably higher entropy than mail or login.
Record type distribution. Normal clients mostly send A and AAAA queries. DNS tunneling often uses TXT, CNAME, or NULL record types because they can carry more data per response. A sudden spike in TXT queries from a single internal host, especially to a domain you have never seen before, is a strong signal.
Where to find DNS logs and what to grep for
tshark (the command-line version of Wireshark) can extract DNS queries quickly:
BASH
# Extract DNS query names longer than 50 characters from a pcap file
tshark -r capture.pcap -Y 'dns.qry.name and dns.flags.response == 0' \\
-T fields -e dns.qry.name | awk 'length > 50'rndc querylog on and tail the log. On Unbound, set verbosity: 2 or higher in unbound.conf and restart. On systemd-resolved, resolvectl monitor shows queries as they happen. The exact method depends on your resolver and distribution.
Once you have query logs, look for these patterns:
BASH
# Count queries per domain, sorted by frequency — illustrative command
awk -F'.' '{print (NF-1)"."NF}' dns_queries.log | sort | uniq -c | sort -rn | head -20Measuring entropy to separate noise from signal
www.google.com), high entropy means uniform character distribution (like base64 data).
A rough threshold: normal domain labels typically have entropy below 3.5 bits per character. Encoded tunneling labels tend to land above 4.0. These are rules of thumb, not hard cutoffs — some legitimate services (CDNs, anti-spam tokens, DKIM selectors) produce high-entropy subdomains too. But when you see high entropy combined with high query volume to a single domain, and the record types are mostly TXT or NULL, that combination is rarely innocent.
You can compute entropy with a short script in Python, Perl, or awk. The math is straightforward: for each character in the label, calculate its frequency, then sum -p * log2(p) across all characters. Many DNS analysis tools (like freq.py from SANS or the dns module in Zeek/Bro) compute this automatically.
Practical detection without expensive tools
tcpdump -w /tmp/dns.pcap -i any port 53 for a bounded time window instead of permanent query logging.
2. Look for volume anomalies. Count queries per source IP per hour. A workstation sending 50 DNS queries per hour is normal. One sending 5,000 queries per hour to a single domain is not.
3. Check query name length. Any consistent pattern of queries with labels longer than 40-50 characters warrants investigation.
4. Check record types. Filter for TXT and NULL queries. If a single internal IP is generating hundreds of TXT queries to one external domain, investigate that domain.
5. Inspect the target domain. Run a WHOIS lookup. Check when it was registered. Tunneling domains are often freshly registered, use privacy protection, and have no web presence. The authoritative nameserver might be a VPS with no reverse DNS.
If you run a DNS resolver that supports response policy zones (RPZ), you can block known tunneling domains or entire categories of newly registered domains. This is a blunt tool — it will cause false positives — but it raises the cost for the attacker.
