DNS Tunneling and Exfiltration
Smuggling data over DNS.
DNS Tunneling and Exfiltration is a free Cyber Security Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cyber Security Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Smuggling Data Over DNS
DNS tunneling encodes arbitrary data inside DNS queries and responses, turning the name system into a covert communication channel. Because nearly every network allows DNS to leave the perimeter, it is a favorite path for data exfiltration and command-and-control (C2).
If your firewall blocks everything but the resolver still answers, attackers can route a full bidirectional channel through DNS.
How the Channel Works
The attacker controls the authoritative name server for a domain, say tunnel.evil.com. The implant encodes outbound data into the subdomain (left-hand label) of queries:
- Data goes out as
<base32-chunk>.tunnel.evil.com. - The authoritative server returns instructions inside
TXT,CNAME, orNULLrecords.
Each query/answer pair carries a small payload chunk.
ZXhmaWx0cmF0ZWQtc2VjcmV0.tunnel.evil.com
# Encoded chunk smuggled in the query labelEncoding Choices
DNS labels are case-insensitive and limited in length, so binary data is usually Base32 encoded (Base64 mixes case and uses unsafe characters). Constraints to respect:
- Each label up to 63 characters.
- Total name up to 255 characters.
- So each query carries only a few hundred bytes at most.
This low throughput is why exfiltration over DNS is slow and chatty, which also makes it detectable.
Record Types Used
Different record types maximize downstream capacity:
TXT— large free-text payloads, the workhorse for C2 replies.NULL— raw bytes, used by older tools like iodine.CNAME/MX— moderate capacity, blend in with legitimate traffic.A/AAAA— low capacity but very common and inconspicuous.
Common Tooling
Well-known tunneling utilities you should recognize in detections:
- iodine — IP-over-DNS tunnel using NULL/TXT records.
- dnscat2 — encrypted C2 channel over DNS.
- DNSExfiltrator — staged file exfiltration.
- Cobalt Strike DNS beacon — common in red team and real intrusions.
These tools have recognizable query patterns and entropy signatures.
Slow and Fast Modes
Attackers tune the tunnel: a low-and-slow profile sends few queries per minute to evade volume thresholds, suitable for stealthy exfiltration of small secrets like credentials or keys.
A high-throughput profile maximizes queries for interactive C2 or bulk transfer, trading stealth for speed. Detection logic must catch both extremes.
Detection Signal: Entropy
Encoded subdomains look like random strings, so they have high entropy compared to natural domain names. Calculating Shannon entropy per label is a strong heuristic.
Legitimate names tend toward pronounceable, low-entropy strings; xj4k9d2mq7zp1.tunnel.evil.com stands out. Flag domains whose subdomain entropy exceeds a tuned threshold.
Detection Signal: Volume and Length
Other behavioral indicators:
- Unusually high query count to a single domain from one host.
- Long query names approaching the 255-byte limit.
- Disproportionate use of
TXTorNULLrecords. - Many unique subdomains under one parent (high cardinality).
- Queries to a young/low-reputation domain.
# Top talkers by unique subdomain count per parent domain
sort dns.log | awk '{print $domain}' | sort | uniq -c | sort -rn | headWhy Egress Filtering Helps
Endpoints should never talk DNS directly to the internet. Force all clients through internal resolvers and block outbound UDP/TCP 53 at the firewall.
This funnels DNS through a controlled choke point where you can log, inspect, and apply threat intelligence. It also breaks implants that hardcode external resolvers like 8.8.8.8.
Layered Mitigations
Combine controls to shut down tunneling:
- Centralize resolution; block direct external port 53.
- Deploy a DNS firewall / RPZ with reputation feeds.
- Alert on entropy, query length, NXDOMAIN rate, and per-domain cardinality.
- Rate-limit and cap query size where possible.
- Watch for DoH to unknown endpoints, which hides tunneling inside HTTPS.
DoH: A Detection Blind Spot
DNS over HTTPS wraps queries in TLS to 443, so traditional port-53 monitoring sees nothing. An implant using DoH to a rogue endpoint can tunnel data invisibly to passive DNS logging.
Mitigate by pinning endpoints to an approved DoH resolver, blocking known public DoH IPs/hostnames on managed devices, and inspecting TLS SNI/destination reputation.
Quick Check
Test your detection instincts.
Recap
DNS tunneling abuses query subdomains and answer records (TXT, NULL, CNAME) to create a covert channel for exfiltration and C2. Throughput is low, which makes it slow but stealthy. Tools like iodine and dnscat2 leave recognizable patterns.
Detect with entropy, query length, volume, and cardinality analytics; prevent with centralized resolvers, egress filtering, DNS firewalls/RPZ, and control over DoH. Next: how DNSSEC and filtering defend the name system as a whole.
Frequently asked questions
Is the “DNS Tunneling and Exfiltration” lesson free?
Yes — the full text of “DNS Tunneling and Exfiltration” is free to read here on the web, and the Cyber Security Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cyber Security Academy course, upgrade to CoddyKit PRO.
What will I learn in “DNS Tunneling and Exfiltration”?
Smuggling data over DNS. You practise Cyber Security Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cyber Security Academy?
No prior experience is required. Cyber Security Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “DNS Tunneling and Exfiltration” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cyber Security Academy lesson?
Yes. Every Cyber Security Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- How DNS Works and Its Risks
- DNS Spoofing and Cache Poisoning
- DNS Tunneling and Exfiltration
- DNSSEC and DNS Filtering