23 DNS Dns Tunneling And Nxdomain Suspects
Query
// REVIEW ONLY - AWS Security Operations Workbook Visual Query
// Tab: DNS | Panel: dns-tunneling-and-nxdomain-suspects | Visualization: table | Mode: All
// Security Question: Which workload IPs/instances are generating high unique-subdomain fan-out under one parent domain or high NXDOMAIN ratios?
// Drill-Through: Click SrcAddr to pivot into Tab 5 (Network Raw Flows) and verify whether direct IP C2 also occurred.
// Empty/No-Data Contract: No workloads exceeded minimum DNS tunneling or NXDOMAIN heuristic sample thresholds.
let Start = {TimeRange:start};
let End = {TimeRange:end};
let OrgFilter = base64_decode_tostring("{Organization:base64}");
let AccountFilter = base64_decode_tostring("{AwsAccount:base64}");
let RegionFilter = base64_decode_tostring("{AwsRegion:base64}");
let EnvFilter = base64_decode_tostring("{Environment:base64}");
let WorkloadFilter = base64_decode_tostring("{Workload:base64}");
let SeverityFilter = base64_decode_tostring("{Severity:base64}");
let SourceFilter = base64_decode_tostring("{SourceTable:base64}");
let SummaryPref = base64_decode_tostring("{SummaryMode:base64}");
let SelectedPrincipalFilter = base64_decode_tostring("{SelectedPrincipal:base64}");
let SelectedIpFilter = base64_decode_tostring("{SelectedIp:base64}");
union isfuzzy=true
(datatable(TimeGenerated:datetime, AccountId:string, Region:string, VpcId:string, SrcAddr:string, SrcIds:dynamic, QueryName:string, Rcode:string, QueryType:string, FirewallRuleAction:string)[]),
(AWSRoute53Resolver | where TimeGenerated between (Start .. End)
| project TimeGenerated, AccountId, Region, VpcId, SrcAddr, SrcIds=todynamic(SrcIds), QueryName, Rcode, QueryType, FirewallRuleAction)
| where (AccountFilter == "*" or AccountId == AccountFilter) and (RegionFilter == "*" or Region == RegionFilter)
| extend CleanQname = trim_end('.', tolower(QueryName))
| where isnotempty(CleanQname) and not(CleanQname endswith ".amazonaws.com") and not(CleanQname endswith ".internal")
| extend Labels = split(CleanQname, ".")
| where array_length(Labels) >= 2
| extend ParentDomain = strcat(tostring(Labels[-2]), ".", tostring(Labels[-1])),
LeftLen = strlen(tostring(Labels[0])),
InstanceId = tostring(SrcIds.instance)
| summarize TotalQueries=count(), DistinctSubdomains=dcount(CleanQname), AvgLeftLabelLen=round(avg(todouble(LeftLen)), 1),
NxDomains=countif(Rcode =~ "NXDOMAIN"), TxtNullQueries=countif(QueryType in~ ("TXT", "NULL")),
FirewallActions=make_set(FirewallRuleAction, 5)
by AccountId, Region, VpcId, SrcAddr, InstanceId, ParentDomain
| where (TotalQueries >= 40 and (DistinctSubdomains >= 25 or AvgLeftLabelLen >= 22.0)) or NxDomains >= 35
| order by DistinctSubdomains desc, NxDomains desc
| take 5000Explanation
This query is designed to identify potential security threats related to DNS activity within an AWS environment. Here's a simplified breakdown of what the query does:
-
Time and Filters Setup: It starts by defining a time range and several filters based on organization, account, region, environment, workload, severity, source, summary mode, selected principal, and selected IP. These filters are decoded from base64 strings.
-
Data Collection: It gathers data from two sources: an empty datatable (likely a placeholder) and AWS Route 53 Resolver logs. The data is filtered to only include records within the specified time range and matching the account and region filters.
-
Data Cleaning and Preparation:
- It cleans up the DNS query names by converting them to lowercase and removing trailing periods.
- It excludes queries ending with ".amazonaws.com" or ".internal" to focus on external domains.
- It splits the cleaned query names into labels (parts of the domain name) and calculates the parent domain (the last two labels).
-
Data Analysis:
- It summarizes the data by counting the total queries, distinct subdomains, average length of the leftmost label, number of NXDOMAIN responses (indicating non-existent domains), and specific query types (TXT and NULL).
- It also collects firewall actions associated with these queries.
-
Suspicious Activity Identification:
- It identifies suspicious workloads by looking for those with a high number of distinct subdomains or long leftmost labels (indicative of DNS tunneling) or a high number of NXDOMAIN responses.
- Specifically, it flags workloads with at least 40 total queries and either 25 distinct subdomains or an average left label length of 22 or more, or at least 35 NXDOMAIN responses.
-
Results Sorting and Limiting:
- The results are sorted by the number of distinct subdomains and NXDOMAIN responses in descending order.
- It limits the output to the top 5000 records.
In summary, this query is used to detect potential DNS tunneling or other suspicious DNS activities by analyzing DNS query patterns and responses, focusing on workloads that show unusual DNS behavior.