Detecting Exfiltration over DNS with Zeek
Overview
DNS tunneling and exfiltration is a technique used by attackers to bypass firewalls and DLP controls by encoding stolen data into DNS query subdomains. Legitimate DNS queries have predictable entropy and length patterns, while exfiltration queries contain encoded data with high Shannon entropy, unusually long subdomain labels, and high volumes of unique subdomains per parent domain.
This skill analyzes Zeek dns.log files (TSV format) to detect exfiltration indicators. The agent computes Shannon entropy for each subdomain component, identifies queries exceeding the 63-character DNS label limit, counts unique subdomains per parent domain, and flags domains that exceed configurable thresholds. These techniques detect tools like dnscat2, iodine, dns2tcp, and custom DNS tunneling implementations.
When to Use
- When investigating security incidents that require detecting exfiltration over dns with zeek
- When building detection rules or threat hunting queries for this domain
- When SOC analysts need structured procedures for this analysis type
- When validating security monitoring coverage for related attack techniques
Prerequisites
- Python 3.9 or later with math and collections modules (stdlib)
- Zeek dns.log files in TSV format with standard field headers
- Network capture data processed by Zeek 5.0+ or later
- Understanding of DNS protocol structure and query types
Steps
Parse Zeek dns.log headers: Read the TSV file, extract the #fields header line to identify column positions for ts, id.orig_h, query, qtype_name, rcode_name, and answers.
Extract and decompose queries: For each DNS query, split the FQDN into subdomain labels and parent domain. Skip queries to known safe domains and internal zones.
Compute Shannon entropy: Calculate the information entropy of each subdomain label. Legitimate subdomains typically have entropy below 3.5, while encoded/encrypted data produces entropy above 4.0.
Detect long labels: Flag DNS labels exceeding 52 characters (approaching the 63-character maximum). Long labels are a strong indicator of data tunneling.
Count unique subdomains per domain: Track how many distinct subdomains each parent domain receives. Domains with more than 50 unique subdomains within the log window are suspicious.
Identify query volume anomalies: Calculate queries-per-minute per source IP per domain. Exfiltration tools generate sustained high-volume query streams that differ from normal browsing.
Score and rank domains: Combine entropy, label length, uniqueness count, and query volume into a composite risk score. Rank domains by score and output the top suspicious domains.
Generate detection report: Produce a JSON report with flagged domains, their evidence indicators, originating source IPs, and recommended response actions.
Expected Output
{
"analysis_summary": {
"total_queries_analyzed": 145832,
"unique_domains": 3421,
"flagged_domains": 3,
"entropy_threshold": 3.5
},
"flagged_domains": [
{
"domain": "data.evil-c2.com",
"unique_subdomains": 892,
"avg_entropy": 4.72,
"max_label_length": 61,
"source_ips": ["10.0.1.45"],
"risk_score": 9.4,
"indicators": ["high_entropy", "long_labels", "high_subdomain_count"]
}
]
}
1---
2name: detecting-exfiltration-over-dns-with-zeek
3description: Detect DNS-based data exfiltration by analyzing Zeek dns.log for high-entropy
4 subdomains and anomalous query patterns
5domain: cybersecurity
6subdomain: network-security
7tags:
8- dns-exfiltration
9- zeek
10- entropy-analysis
11- threat-hunting
12version: '1.0'
13author: mahipal
14license: Apache-2.0
15nist_csf:
16- PR.IR-01
17- DE.CM-01
18- ID.AM-03
19- PR.DS-02
20mitre_attack:
21- T1046
22- T1040
23- T1557
24- T1071
25- T1048
26source: https://github.com/mukul975/Anthropic-Cybersecurity-Skills
27source_commit: 04450304b12645cb2b974ab96d28c0664758a88d
28metadata:
29 source: https://skillmd.com/skills/yanacuti1121/detecting-exfiltration-over-dns-with-zeek
30---
31
32
33# Detecting Exfiltration over DNS with Zeek
34
35## Overview
36
37DNS tunneling and exfiltration is a technique used by attackers to bypass firewalls and DLP controls by encoding stolen data into DNS query subdomains. Legitimate DNS queries have predictable entropy and length patterns, while exfiltration queries contain encoded data with high Shannon entropy, unusually long subdomain labels, and high volumes of unique subdomains per parent domain.
38
39This skill analyzes Zeek dns.log files (TSV format) to detect exfiltration indicators. The agent computes Shannon entropy for each subdomain component, identifies queries exceeding the 63-character DNS label limit, counts unique subdomains per parent domain, and flags domains that exceed configurable thresholds. These techniques detect tools like dnscat2, iodine, dns2tcp, and custom DNS tunneling implementations.
40
41
42## When to Use
43
44- When investigating security incidents that require detecting exfiltration over dns with zeek
45- When building detection rules or threat hunting queries for this domain
46- When SOC analysts need structured procedures for this analysis type
47- When validating security monitoring coverage for related attack techniques
48
49## Prerequisites
50
51- Python 3.9 or later with math and collections modules (stdlib)
52- Zeek dns.log files in TSV format with standard field headers
53- Network capture data processed by Zeek 5.0+ or later
54- Understanding of DNS protocol structure and query types
55
56## Steps
57
581. **Parse Zeek dns.log headers**: Read the TSV file, extract the `#fields` header line to identify column positions for `ts`, `id.orig_h`, `query`, `qtype_name`, `rcode_name`, and `answers`.
59
602. **Extract and decompose queries**: For each DNS query, split the FQDN into subdomain labels and parent domain. Skip queries to known safe domains and internal zones.
61
623. **Compute Shannon entropy**: Calculate the information entropy of each subdomain label. Legitimate subdomains typically have entropy below 3.5, while encoded/encrypted data produces entropy above 4.0.
63
644. **Detect long labels**: Flag DNS labels exceeding 52 characters (approaching the 63-character maximum). Long labels are a strong indicator of data tunneling.
65
665. **Count unique subdomains per domain**: Track how many distinct subdomains each parent domain receives. Domains with more than 50 unique subdomains within the log window are suspicious.
67
686. **Identify query volume anomalies**: Calculate queries-per-minute per source IP per domain. Exfiltration tools generate sustained high-volume query streams that differ from normal browsing.
69
707. **Score and rank domains**: Combine entropy, label length, uniqueness count, and query volume into a composite risk score. Rank domains by score and output the top suspicious domains.
71
728. **Generate detection report**: Produce a JSON report with flagged domains, their evidence indicators, originating source IPs, and recommended response actions.
73
74## Expected Output
75
76```json
77{
78 "analysis_summary": {
79 "total_queries_analyzed": 145832,
80 "unique_domains": 3421,
81 "flagged_domains": 3,
82 "entropy_threshold": 3.5
83 },
84 "flagged_domains": [
85 {
86 "domain": "data.evil-c2.com",
87 "unique_subdomains": 892,
88 "avg_entropy": 4.72,
89 "max_label_length": 61,
90 "source_ips": ["10.0.1.45"],
91 "risk_score": 9.4,
92 "indicators": ["high_entropy", "long_labels", "high_subdomain_count"]
93 }
94 ]
95}
96```