Hunting for DNS Tunneling with Zeek
When to Use
- When hunting for data exfiltration over DNS covert channels
- After threat intelligence indicates DNS-based C2 frameworks targeting your industry
- When dns.log shows unusually high query volumes to specific domains
- During investigation of suspected data theft where no HTTP/S exfiltration is found
- When monitoring for tools like iodine, dnscat2, DNSExfiltrator, or DNS-over-HTTPS tunneling
Prerequisites
- Zeek deployed on network tap or SPAN port capturing DNS traffic
- Zeek dns.log with full query and response fields
- SIEM platform for dns.log analysis (Splunk, Elastic)
- RITA (Real Intelligence Threat Analytics) for automated DNS analysis
- Passive DNS data for historical domain resolution context
Workflow
- Analyze Query Length Distribution: DNS tunneling encodes data in subdomain labels, producing queries significantly longer than normal. Normal DNS queries average 20-30 characters; tunneling queries often exceed 50+ characters. Calculate mean and standard deviation of query lengths per domain.
- Calculate Subdomain Entropy: Tunneling encodes data using Base32/Base64, producing high-entropy subdomain strings. Calculate Shannon entropy of subdomain labels -- values above 3.5 bits/character strongly suggest encoded data.
- Count Unique Subdomains Per Domain: Legitimate domains have relatively few unique subdomains. DNS tunneling generates hundreds or thousands of unique subdomains under a single parent domain.
- Monitor DNS Record Type Distribution: TXT, NULL, CNAME, and MX records can carry more data than A records. Excessive TXT queries to a single domain indicate data transfer via DNS.
- Detect High Query Volume: Flag domains receiving more than 100 queries per hour from a single source, especially when combined with high subdomain uniqueness.
- Analyze Query Timing: DNS tunneling tools produce regular query patterns (beaconing) or burst patterns (data transfer). Apply frequency analysis to DNS query timestamps.
- Cross-Reference with conn.log: Correlate DNS queries with connection metadata to identify the process or endpoint generating suspicious queries.
- Validate with Domain Intelligence: Check suspicious domains against WHOIS data, certificate transparency, and threat intelligence feeds.
Key Concepts
| Concept |
Description |
| T1071.004 |
Application Layer Protocol: DNS |
| T1048.003 |
Exfiltration Over Alternative Protocol: DNS |
| T1572 |
Protocol Tunneling |
| Shannon Entropy |
Measure of randomness in subdomain strings |
| Zeek dns.log |
DNS query/response metadata |
| RITA |
Automated DNS tunneling detection from Zeek logs |
| iodine |
IPv4-over-DNS tunneling tool |
| dnscat2 |
DNS-based command-and-control tool |
| DNSExfiltrator |
Data exfiltration tool using DNS requests |
Detection Queries
Zeek Script -- DNS Tunnel Detection
@load base/protocols/dns
module DNSTunnel;
export {
redef enum Notice::Type += { DNSTunnel::Long_DNS_Query };
const query_length_threshold = 50 &redef;
const query_count_threshold = 100 &redef;
}
event dns_request(c: connection, msg: dns_msg, query: string, qtype: count, qclass: count) {
if ( |query| > query_length_threshold ) {
NOTICE([$note=DNSTunnel::Long_DNS_Query,
$msg=fmt("Long DNS query detected: %s (%d chars)", query, |query|),
$conn=c]);
}
}
Splunk -- DNS Tunneling Indicators from Zeek
index=zeek sourcetype=bro_dns
| rex field=query "(?<subdomain>[^.]+)\.(?<basedomain>[^.]+\.[^.]+)$"
| stats count dc(subdomain) as unique_subs avg(len(query)) as avg_len max(len(query)) as max_len by src basedomain
| where count > 100 AND (unique_subs > 50 OR avg_len > 40)
| sort -unique_subs
Splunk -- High Entropy Subdomain Detection
index=zeek sourcetype=bro_dns
| rex field=query "^(?<subdomain>[^.]+)"
| where len(subdomain) > 20
| eval char_count=len(subdomain)
| stats count dc(query) as unique_queries avg(char_count) as avg_sub_len by src query_type_name basedomain
| where unique_queries > 30 AND avg_sub_len > 25
| sort -unique_queries
RITA Analysis
rita import /path/to/zeek/logs dataset_name
rita show-dns-fqdn-ips-long dataset_name
rita show-exploded-dns dataset_name
rita show-dns-tunneling dataset_name --csv > dns_tunnel_results.csv
Common Scenarios
- dnscat2 C2: Encodes command-and-control traffic in DNS CNAME/TXT queries with Base64-encoded subdomain labels. Produces high query volumes with long, high-entropy subdomains.
- iodine IPv4 Tunnel: Creates a virtual network interface tunneling all IP traffic through DNS. Generates massive DNS query volumes with NULL record types.
- Data Exfiltration via DNS: Sensitive data encoded in subdomain labels (e.g.,
aGVsbG8gd29ybGQ.exfil.attacker.com), sent as A or TXT queries. Each query carries ~63 bytes of data.
- DNS-over-HTTPS Tunneling: Bypasses traditional DNS monitoring by sending DNS queries over HTTPS to public resolvers (8.8.8.8, 1.1.1.1), requiring TLS inspection for detection.
- Cobalt Strike DNS Beacon: Uses DNS A/TXT records for C2 communication with configurable subdomain encoding schemes.
Output Format
Hunt ID: TH-DNSTUNNEL-[DATE]-[SEQ]
Source IP: [Internal IP]
Source Host: [Hostname]
Target Domain: [Base domain]
Query Count: [Total queries in window]
Unique Subdomains: [Count]
Avg Query Length: [Characters]
Max Query Length: [Characters]
Subdomain Entropy: [Bits per character]
Primary Record Type: [A/TXT/CNAME/NULL]
Data Volume Estimate: [Bytes exfiltrated]
Risk Level: [Critical/High/Medium/Low]
1---
2name: hunting-for-dns-tunneling-with-zeek
3description: Detect DNS tunneling and data exfiltration by analyzing Zeek dns.log
4 for high-entropy subdomain queries, excessive query volume, long query lengths,
5 and unusual DNS record types indicating covert channel communication.
6domain: cybersecurity
7subdomain: threat-hunting
8tags:
9- threat-hunting
10- dns-tunneling
11- zeek
12- data-exfiltration
13- covert-channel
14- mitre-t1071-004
15- network-monitoring
16version: '1.0'
17author: mahipal
18license: Apache-2.0
19d3fend_techniques:
20- Application Protocol Command Analysis
21- Network Isolation
22- Network Traffic Analysis
23- Client-server Payload Profiling
24- DNS Traffic Analysis
25nist_csf:
26- DE.CM-01
27- DE.AE-02
28- DE.AE-07
29- ID.RA-05
30mitre_attack:
31- T1046
32- T1057
33- T1082
34- T1083
35- T1048
36source: https://github.com/mukul975/Anthropic-Cybersecurity-Skills
37source_commit: 04450304b12645cb2b974ab96d28c0664758a88d
38metadata:
39 source: https://skillmd.com/skills/yanacuti1121/hunting-for-dns-tunneling-with-zeek
40---
41
42# Hunting for DNS Tunneling with Zeek
43
44## When to Use
45
46- When hunting for data exfiltration over DNS covert channels
47- After threat intelligence indicates DNS-based C2 frameworks targeting your industry
48- When dns.log shows unusually high query volumes to specific domains
49- During investigation of suspected data theft where no HTTP/S exfiltration is found
50- When monitoring for tools like iodine, dnscat2, DNSExfiltrator, or DNS-over-HTTPS tunneling
51
52## Prerequisites
53
54- Zeek deployed on network tap or SPAN port capturing DNS traffic
55- Zeek dns.log with full query and response fields
56- SIEM platform for dns.log analysis (Splunk, Elastic)
57- RITA (Real Intelligence Threat Analytics) for automated DNS analysis
58- Passive DNS data for historical domain resolution context
59
60## Workflow
61
621. **Analyze Query Length Distribution**: DNS tunneling encodes data in subdomain labels, producing queries significantly longer than normal. Normal DNS queries average 20-30 characters; tunneling queries often exceed 50+ characters. Calculate mean and standard deviation of query lengths per domain.
632. **Calculate Subdomain Entropy**: Tunneling encodes data using Base32/Base64, producing high-entropy subdomain strings. Calculate Shannon entropy of subdomain labels -- values above 3.5 bits/character strongly suggest encoded data.
643. **Count Unique Subdomains Per Domain**: Legitimate domains have relatively few unique subdomains. DNS tunneling generates hundreds or thousands of unique subdomains under a single parent domain.
654. **Monitor DNS Record Type Distribution**: TXT, NULL, CNAME, and MX records can carry more data than A records. Excessive TXT queries to a single domain indicate data transfer via DNS.
665. **Detect High Query Volume**: Flag domains receiving more than 100 queries per hour from a single source, especially when combined with high subdomain uniqueness.
676. **Analyze Query Timing**: DNS tunneling tools produce regular query patterns (beaconing) or burst patterns (data transfer). Apply frequency analysis to DNS query timestamps.
687. **Cross-Reference with conn.log**: Correlate DNS queries with connection metadata to identify the process or endpoint generating suspicious queries.
698. **Validate with Domain Intelligence**: Check suspicious domains against WHOIS data, certificate transparency, and threat intelligence feeds.
70
71## Key Concepts
72
73| Concept | Description |
74|---------|-------------|
75| T1071.004 | Application Layer Protocol: DNS |
76| T1048.003 | Exfiltration Over Alternative Protocol: DNS |
77| T1572 | Protocol Tunneling |
78| Shannon Entropy | Measure of randomness in subdomain strings |
79| Zeek dns.log | DNS query/response metadata |
80| RITA | Automated DNS tunneling detection from Zeek logs |
81| iodine | IPv4-over-DNS tunneling tool |
82| dnscat2 | DNS-based command-and-control tool |
83| DNSExfiltrator | Data exfiltration tool using DNS requests |
84
85## Detection Queries
86
87### Zeek Script -- DNS Tunnel Detection
88```zeek
89@load base/protocols/dns
90module DNSTunnel;
91
92export {
93 redef enum Notice::Type += { DNSTunnel::Long_DNS_Query };
94 const query_length_threshold = 50 &redef;
95 const query_count_threshold = 100 &redef;
96}
97
98event dns_request(c: connection, msg: dns_msg, query: string, qtype: count, qclass: count) {
99 if ( |query| > query_length_threshold ) {
100 NOTICE([$note=DNSTunnel::Long_DNS_Query,
101 $msg=fmt("Long DNS query detected: %s (%d chars)", query, |query|),
102 $conn=c]);
103 }
104}
105```
106
107### Splunk -- DNS Tunneling Indicators from Zeek
108```spl
109index=zeek sourcetype=bro_dns
110| rex field=query "(?<subdomain>[^.]+)\.(?<basedomain>[^.]+\.[^.]+)$"
111| stats count dc(subdomain) as unique_subs avg(len(query)) as avg_len max(len(query)) as max_len by src basedomain
112| where count > 100 AND (unique_subs > 50 OR avg_len > 40)
113| sort -unique_subs
114```
115
116### Splunk -- High Entropy Subdomain Detection
117```spl
118index=zeek sourcetype=bro_dns
119| rex field=query "^(?<subdomain>[^.]+)"
120| where len(subdomain) > 20
121| eval char_count=len(subdomain)
122| stats count dc(query) as unique_queries avg(char_count) as avg_sub_len by src query_type_name basedomain
123| where unique_queries > 30 AND avg_sub_len > 25
124| sort -unique_queries
125```
126
127### RITA Analysis
128```bash
129rita import /path/to/zeek/logs dataset_name
130rita show-dns-fqdn-ips-long dataset_name
131rita show-exploded-dns dataset_name
132rita show-dns-tunneling dataset_name --csv > dns_tunnel_results.csv
133```
134
135## Common Scenarios
136
1371. **dnscat2 C2**: Encodes command-and-control traffic in DNS CNAME/TXT queries with Base64-encoded subdomain labels. Produces high query volumes with long, high-entropy subdomains.
1382. **iodine IPv4 Tunnel**: Creates a virtual network interface tunneling all IP traffic through DNS. Generates massive DNS query volumes with NULL record types.
1393. **Data Exfiltration via DNS**: Sensitive data encoded in subdomain labels (e.g., `aGVsbG8gd29ybGQ.exfil.attacker.com`), sent as A or TXT queries. Each query carries ~63 bytes of data.
1404. **DNS-over-HTTPS Tunneling**: Bypasses traditional DNS monitoring by sending DNS queries over HTTPS to public resolvers (8.8.8.8, 1.1.1.1), requiring TLS inspection for detection.
1415. **Cobalt Strike DNS Beacon**: Uses DNS A/TXT records for C2 communication with configurable subdomain encoding schemes.
142
143## Output Format
144
145```
146Hunt ID: TH-DNSTUNNEL-[DATE]-[SEQ]
147Source IP: [Internal IP]
148Source Host: [Hostname]
149Target Domain: [Base domain]
150Query Count: [Total queries in window]
151Unique Subdomains: [Count]
152Avg Query Length: [Characters]
153Max Query Length: [Characters]
154Subdomain Entropy: [Bits per character]
155Primary Record Type: [A/TXT/CNAME/NULL]
156Data Volume Estimate: [Bytes exfiltrated]
157Risk Level: [Critical/High/Medium/Low]
158```