P1 01a Route53 Dns Tunneling Dga Baseline
Query
// Standalone KQL for Analytic Rule: AWS SecOps P1-01a - Route 53 DNS Tunneling, Subdomain Fan-Out and High NXDOMAIN Ratio (Single-Source)
// Rule File: analytics/P1-01a-route53-dns-tunneling-dga-baseline.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: Medium
// Schedule: 15m / Lookback: 1h | REVIEW ONLY - Do not deploy/schedule without approval.
// REVIEW ONLY. Validate AWSRoute53Resolver schema via discovery/D05 first.
let TrustedDns = _GetWatchlist('AWS_TrustedDomainsAndScanners')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and tostring(column_ifexists("PatternType", "")) in ("ParentDomain", "SourceIp")
| project PatternType=tostring(column_ifexists("PatternType", "")), IndicatorValue=tolower(tostring(column_ifexists("IndicatorValue", "")));
let TrustedDomains = TrustedDns | where PatternType == "ParentDomain" | project IndicatorValue;
let TrustedScanners = TrustedDns | where PatternType == "SourceIp" | project IndicatorValue;
let RawDns = materialize(
AWSRoute53Resolver
| where TimeGenerated > ago(1h) and ingestion_time() > ago(15m)
| where tolower(SrcAddr) !in (TrustedScanners)
| extend CleanQname = trim_end(@"\.", tolower(QueryName))
| where isnotempty(CleanQname) and not(CleanQname endswith ".amazonaws.com") and not(CleanQname endswith ".internal")
| extend Labels = split(CleanQname, ".")
| extend LabelCount = array_length(Labels)
| where LabelCount >= 2
| extend ParentDomain = strcat(tostring(Labels[LabelCount - 2]), ".", tostring(Labels[LabelCount - 1])),
LeftLabel = tostring(Labels[0]),
InstanceId = tostring(SrcIds.instance)
| where ParentDomain !in (TrustedDomains)
);
let TunnelingCandidates = RawDns
| extend LeftLen = strlen(LeftLabel),
HexOrDigitCount = countof(LeftLabel, @"[0-9a-f\-_]", "regex")
| summarize
TimeGenerated = min(TimeGenerated),
SampleCount = count(),
DistinctSubdomains = dcount(CleanQname),
AvgLabelLen = round(avg(todouble(LeftLen)), 1),
LongEncodedLabels = countif(LeftLen >= 28 and todouble(HexOrDigitCount) / todouble(max_of(LeftLen, 1)) >= 0.45),
TxtNullCount = countif(QueryType in~ ("TXT", "NULL", "CNAME")),
NxDomainCount = countif(Rcode =~ "NXDOMAIN")
by AccountId, Region, VpcId, SrcAddr, InstanceId, ParentDomain
| where SampleCount >= 60 and DistinctSubdomains >= 40 and (AvgLabelLen >= 24.0 or LongEncodedLabels >= 25 or TxtNullCount >= 30)
| extend HeuristicMode = "SuspectedDnsTunnelingSubdomainFanOut",
MetricSummary = strcat("Samples=", SampleCount, ";Subdomains=", DistinctSubdomains, ";AvgLabel=", AvgLabelLen, ";LongEncoded=", LongEncodedLabels);
let DgaCandidates = RawDns
| summarize
TimeGenerated = min(TimeGenerated),
SampleCount = count(),
NxDomainCount = countif(Rcode =~ "NXDOMAIN"),
DistinctParents = dcount(ParentDomain),
SampleParents = make_set(ParentDomain, 10)
by AccountId, Region, VpcId, SrcAddr, InstanceId
| extend NxRatio = todouble(NxDomainCount) / todouble(max_of(SampleCount, 1))
| where SampleCount >= 100 and NxDomainCount >= 70 and NxRatio >= 0.70 and DistinctParents >= 25
| extend ParentDomain = tostring(SampleParents[0]),
HeuristicMode = "SuspectedDgaHighNxDomainBurst",
MetricSummary = strcat("Samples=", SampleCount, ";NxDomains=", NxDomainCount, ";DistinctParents=", DistinctParents, ";NxRatio=", round(NxRatio, 2));
union TunnelingCandidates, DgaCandidates
| project TimeGenerated, AWSAccountId=AccountId, AWSRegion=Region, VpcId, SourceIpAddress=SrcAddr,
InstanceId, ParentDomain, HeuristicMode, MetricSummaryExplanation
This KQL query is designed to detect suspicious DNS activities in AWS Route 53, specifically focusing on DNS tunneling and domain generation algorithm (DGA) patterns. Here's a simplified breakdown:
-
Trusted Domains and Scanners: The query starts by identifying trusted domains and IP addresses from a watchlist. These are domains and IPs that are verified and should not be flagged as suspicious.
-
Raw DNS Data: It collects DNS query data from AWS Route 53 within the last hour, excluding queries from trusted scanners. It processes the query names to extract relevant parts like the parent domain and the first label.
-
Tunneling Candidates: This part of the query looks for DNS queries that might indicate tunneling activity. It identifies queries with a high number of distinct subdomains, long label lengths, or a significant number of TXT/NULL/CNAME query types. These are flagged as potential DNS tunneling activities.
-
DGA Candidates: This section identifies patterns that might suggest the use of a domain generation algorithm. It looks for a high number of NXDOMAIN responses, a high ratio of NXDOMAINs to total queries, and a large number of distinct parent domains.
-
Results Union: The results from both tunneling and DGA detection are combined. The final output includes details like the AWS account, region, VPC ID, source IP address, instance ID, parent domain, and a summary of the metrics that triggered the detection.
Overall, the query is designed to flag potential security threats related to DNS tunneling and DGA by analyzing DNS query patterns and characteristics.