| name | performing-dns-tunneling-detection |
| description | Detects DNS tunneling by computing Shannon entropy of DNS query names, analyzing query length distributions, inspecting TXT record payloads, and identifying high subdomain cardinality. Uses scapy for packet capture analysis and statistical methods to distinguish legitimate DNS from covert channels. Use when hunting for data exfiltration.
|
| domain | cybersecurity |
| subdomain | security-operations |
| tags | ["performing","dns","tunneling","detection"] |
| version | 1.0 |
| author | mahipal |
| license | Apache-2.0 |
| nist_csf | ["DE.CM-01","RS.MA-01","GV.OV-01","DE.AE-02"] |
Performing DNS Tunneling Detection
When to Use
- When conducting security assessments that involve performing dns tunneling detection
- When following incident response procedures for related security events
- When performing scheduled security testing or auditing activities
- When validating security controls through hands-on testing
Detection Gaps & Validation
- Encrypted DNS bypasses pcap entirely: DoH (443) and DoT (853) hide query names from packet inspection, so entropy/length analysis sees nothing. Detect the channel instead — monitor/block known DoH resolver IPs and flag clients talking 443 to them — and pull names from the resolver's own logs, not the wire.
- Low-and-slow evades volume thresholds: tunnels sending a few queries per minute stay under "high volume TXT" and subdomain-cardinality rules. Track per-client cumulative subdomain count and bytes-over-DNS across hours/days, not just per-minute rates.
- Dictionary/Base32 encoding lowers entropy: tools using word-list or Base32 encoding produce labels whose Shannon entropy looks legitimate (~3.0–3.5). Don't rely on entropy alone — combine query rate, label length, record-type mix (TXT/NULL/CNAME), and parent-domain NXDOMAIN ratio.
- Validate in a lab before trusting thresholds: stand up
iodine, dnscat2, and dns2tcp against a controlled domain, capture, and confirm your detector flags each. Then replay a day of production DNS to measure false positives (CDNs, AV/telemetry, and DGA-like SaaS domains are common FP sources) and tune to that baseline. Don't conclude "no tunneling" until you've checked encrypted-DNS egress and multi-hour aggregates.
Prerequisites
- Familiarity with security operations concepts and tools
- Access to a test or lab environment for safe execution
- Python 3.8+ with required dependencies installed
- Appropriate authorization for any testing activities
Instructions
Analyze DNS traffic for indicators of DNS tunneling using entropy analysis and
statistical methods on query name characteristics.
import math
from collections import Counter
def shannon_entropy(data):
if not data:
return
counter = Counter(data)
length = (data)
-((c/length) * math.log2(c/length) c counter.values())
(shannon_entropy())
(shannon_entropy())