Detecting DNS Tunneling with KQL in Microsoft Sentinel
DNS is the "phone book" of the internet, and almost no firewall blocks it. That is exactly why attackers abuse it: they encapsulate data inside DNS queries and responses to exfiltrate information or keep a Command & Control (C2) channel that flies under the radar. Let's hunt it in Microsoft Sentinel with KQL, no extra tooling required.
Why is DNS an ideal covert channel?
- It is rarely inspected at the content level and usually allowed outbound.
- Every queried subdomain can carry encoded data (Base32/Base64/hex).
- Tools like
iodine,dnscat2orDNSExfiltratorautomate it.
The footprint is characteristic: long, high-entropy subdomains, an anomalous volume of queries to a single domain, and unusual record types (TXT, NULL, CNAME).
Signal 1 — Subdomain length and character variety
A legit domain like cdn.contoso.com is short and pronounceable. A tunnel produces something like mfrggzdfmfzwk3tpnfxgk4tfnzrwk2ltmvzxi.exfil.attacker.com. We do not compute Shannon entropy: we approximate it cheaply with the length of the first label and the number of distinct characters it uses.
Long, varied subdomains (possible DNS tunnel)
- Problem
- A DNS tunnel puts the data inside the subdomain, so it leaves long names with many distinct characters.
- Goal
- Surface queries whose first label is unusually long and varied.
- Telemetry
- Microsoft Sentinel,
DnsEventstable (queries from Windows DNS servers). - Requirements
- A solution that fills
DnsEvents. With the current «Windows DNS Events via AMA» connector the data lands inASimDnsActivityLogs: adapt the query to the_Im_Dnsparser, or it will return nothing. - Level
- Medium
DnsEvents
| where TimeGenerated > ago(24h)
| where isnotempty(Name)
| extend sub = tostring(split(Name, ".")[0])
| extend subLen = strlen(sub)
| extend uniqueChars = array_length(set_difference(
extract_all(@"(.)", sub), dynamic([])))
| where subLen > 30 and uniqueChars > 15
| project TimeGenerated, ClientIP, Name, subLen, uniqueChars
| order by subLen desc
How it works. It takes the first label of the name, measures its length and counts its distinct characters. That is not Shannon entropy: it is a cheap approximation that separates encoded names (Base32, hex) from readable ones well.
Known false positives
- Antivirus, EDR and reputation services that encode data in the subdomain.
- CDNs and cloud services with long identifiers.
Limits
- It only looks at the first label: a tunnel that spreads the data over several short labels gets past it.
- The thresholds (length 30, 15 distinct characters) are a starting point: tune them after measuring your normal traffic.
- It misses encrypted DNS (DoH or DoT) that leaves the machine directly without going through your DNS servers.
Sources: Microsoft Learn — DnsEvents · Microsoft Learn — Windows DNS Events via AMA
Signal 2 — Anomalous volume per parent domain
A host making hundreds of unique queries to the same parent domain in a short window is suspicious: it is "leaking" data subdomain by subdomain.
DnsEvents
| where TimeGenerated > ago(1h)
| extend parts = split(Name, ".")
| extend parentDomain = strcat(parts[array_length(parts)-2], ".", parts[array_length(parts)-1])
| summarize queries = count(), uniqueSub = dcount(Name) by ClientIP, parentDomain
| where uniqueSub > 200
| order by uniqueSub desc
Watch out for two-level suffixes: this way of getting the parent domain keeps the last two labels, so with bbc.co.uk or gov.uk it lumps all of co.uk or gov.uk into one «domain» and inflates the count. If your traffic has them, handle them separately or use a public suffix list.
Signal 3 — Uncommon record types
Normal traffic is mostly A/AAAA. A spike of TXT or NULL from a user endpoint is a red flag:
DnsEvents
| where TimeGenerated > ago(24h)
| where QueryType in ("TXT", "NULL", "CNAME")
| summarize count() by ClientIP, QueryType, bin(TimeGenerated, 1h)
| where count_ > 50
From detection to response
- Triage: is the parent domain known? Is the process issuing the queries legitimate (correlate with Defender
DeviceNetworkEvents)? - Containment: block the domain at the resolver/EDR and isolate the host if confirmed.
- Eradication: identify the responsible binary and its persistence.
- Lessons learned: turn the hunt into a scheduled analytics rule so you never repeat the manual work.
If you want to practice the offensive/forensic side, the platform has the OP: GHOST_TRAFFIC challenge, where you analyze a capture with a real DNS tunnel.