This guide explains how to use DNS telemetry to spot malware that tunnels data or uses DNS as a command-and-control channel. You’ll learn what to collect, which patterns indicate suspicious activity, and how to set up detection rules that reduce false positives.
What You’ll Need
- Access to DNS logs from your recursive resolvers or network sensors (e.g., Windows DNS Server, BIND, Unbound, or a passive DNS tap).
- A log management or SIEM platform (e.g., Splunk, Elastic, Graylog) or a scripting environment (Python, PowerShell) to parse and analyze the logs.
- Basic understanding of DNS query structure: QNAME, QTYPE, source IP, timestamp, response code.
- Optional: a threat intelligence feed for known malicious domains, but the techniques here work without it.
Step-by-Step
- Collect DNS logs with enough detail. Ensure your DNS server logs include at minimum: timestamp, client IP, query name (QNAME), query type (A, AAAA, TXT, etc.), response code (NOERROR, NXDOMAIN), and response data (if available). If you use a passive sensor, capture full packet data and extract DNS fields.
- Normalize and centralize the logs. Send logs to your SIEM or store them in a searchable format. Create a consistent schema so fields like qname, qtype, and src_ip are uniformly named across all sources.
- Establish a baseline of normal DNS behavior. For at least one week, record typical query volumes per client, common domains, and average query lengths. This baseline helps you spot anomalies later.
- Detect long or subdomain-heavy queries. Malware often uses subdomains to encode data. Write a query that extracts the length of the full QNAME and the number of labels (dots + 1). Flag any QNAME longer than 60 characters or with more than 5 labels, especially if the domain is not a known benign service.
- Look for TXT and other unusual record types. Malware frequently uses TXT records to exfiltrate data. Create a rule that alerts on any TXT query from internal hosts to external domains, and optionally inspect the response size. Also watch for NULL, CNAME, and MX queries from workstations—they are rare and suspicious.
- Identify periodic or repetitive queries. Use a script or SIEM query to group queries by client and domain, then calculate the time difference between consecutive queries. Flag patterns with regular intervals (e.g., every 60 seconds) that persist for more than a few minutes.
- Detect high query volume from a single host. Set a threshold, such as more than 1000 queries per hour from one IP, or a sudden spike compared to baseline. This can indicate a beaconing or a data tunnel.
- Monitor for NXDOMAIN storms. Malware often generates random domains for DGA (domain generation algorithm). A sudden increase in NXDOMAIN responses from a single client, especially to domains with random-looking names, is a strong indicator. Group by client and count NXDOMAIN responses per hour.
- Inspect entropy of domain names. Apply a simple entropy calculation (e.g., Shannon entropy) to the leftmost label of the QNAME. Random strings have high entropy. Flag domains with entropy above 4.0 (on a scale up to ~4.7 for random alphanumerics) that are not from known CDNs or cloud providers.
- Correlate with other network events. When a DNS alert fires, check for simultaneous connections to the same domain over HTTP/HTTPS, unusual outbound traffic, or new processes on the host. This helps confirm whether the DNS activity is malicious.
- Implement automated alerts. Turn your detection queries into scheduled alerts that send notifications to your security team. Include the client IP, domain, and a link to the raw logs for quick investigation.
- Test your detection. Use a benign tool or script to generate DNS queries that mimic tunneling (e.g., using `dig` to query a domain with long subdomains) and verify that your alerts fire. Adjust thresholds to reduce false positives.
Common Mistakes
- Ignoring response data. DNS tunneling often hides data in the response, not just the query. Ensure you log response contents, especially for TXT records.
- Setting thresholds too low. Long subdomains are common in CDN and cloud services (e.g., Akamai, AWS). Whitelist known benign domains before alerting on length.
- Not baselining. Without a baseline, you’ll drown in false positives or miss slow, low-volume tunneling.
- Forgetting about IPv6. DNS queries over IPv6 (AAAA) are just as important. Make sure your logs cover both.
- Only monitoring external DNS. Internal DNS queries can also be used for tunneling, especially if an internal server is compromised. Monitor both.
Tips
- Use a threat intelligence feed to enrich domains, but don’t rely solely on it—tunneling domains are often unique and not yet blacklisted.
- Keep a rolling window of raw DNS logs for at least 30 days to support retroactive investigations.
- Consider using a machine learning model on top of your rules to catch novel patterns, but start with simple heuristics.
- Regularly review and update your whitelist of legitimate dynamic DNS and CDN domains to reduce noise.
- Document your detection logic so other analysts can understand and refine it.
Troubleshooting
- No data in your SIEM? Verify that the DNS server is actually logging to the expected file or socket. Test with a manual query and check that the log line appears.
- Too many false positives? Increase thresholds (e.g., length > 80, entropy > 4.5) and add more benign domains to the whitelist. Look at the actual queries that triggered—if they are all from one client, it might be a misconfigured application.
- Alerts not firing? Check your query syntax and time range. Ensure the field names match your log schema. Test with a known malicious pattern like a long subdomain query.
- Missing NXDOMAIN responses? Some resolvers cache negative responses or don’t log NXDOMAIN by default. Enable full logging or use a passive sensor.
- Logs are too large? Sample or aggregate logs at the source, but keep full logs for high-risk clients. Alternatively, store raw logs in a compressed format and only load summaries into the SIEM.
Frequently Asked Questions
- What is DNS tunneling exactly? It’s a technique where malware encodes data in DNS queries or responses, often using subdomains or TXT records, to communicate with a command-and-control server while evading traditional firewalls.
- Can DNS tunneling be detected without a SIEM? Yes, you can use simple scripts to parse DNS logs and flag suspicious patterns. Tools like Python with dnspython or even grep/awk can work for small networks.
- How much DNS traffic is normal? It varies widely. A typical workstation might generate a few hundred queries per hour, while a DNS server itself could handle thousands. Baseline your own environment.
- What is the best threshold for subdomain length? There’s no universal value, but a common starting point is flagging QNAMEs over 60 characters or with more than 5 labels. Adjust based on your legitimate traffic.
- Do I need to inspect response data? Yes, because some malware uses DNS responses to receive commands. Log and review TXT record responses, and optionally check for encoded data.
- How can I reduce false positives from CDNs? Maintain a whitelist of known CDN and cloud domains (e.g., akamai.net, amazonaws.com) and exclude them from length and entropy alerts, or only alert if the query comes from a non-browser process.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Business partnership due diligence checklist what to verify before you sign?
- Does the FCC Protect Internet Privacy?
- Disaster Robbers Codes? Meaning & Origin
- Repair a Disk with Mac Disk Utility
- Qualcomm First Gaming Processor?
- Best Anonymous & Private Email Services