AWS SecOps P1-01a - Route 53 DNS Tunneling, Subdomain Fan-Out and High NXDOMAIN Ratio (Single-Source)
P1 01a Route53 Dns Tunneling Dga Baseline
Query
// 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 query is designed to detect suspicious DNS activities in AWS Route 53, specifically focusing on potential DNS tunneling or domain generation algorithm (DGA) activities. Here's a simplified breakdown:
-
Purpose: The query aims to identify unusual DNS behavior that might indicate data exfiltration or command and control activities using DNS tunneling or DGA techniques.
-
Trusted Sources: It filters out DNS queries from trusted domains and scanners, which are listed in a watchlist called
AWS_TrustedDomainsAndScanners. -
Data Collection: It collects DNS query data from AWS Route 53 over the past hour, ensuring that the data is recent (ingested in the last 15 minutes).
-
DNS Tunneling Detection:
- Looks for DNS queries with a high number of unique subdomains and long, encoded labels, which might suggest DNS tunneling.
- Considers queries with a high number of TXT, NULL, or CNAME query types.
- Sets thresholds for sample count, distinct subdomains, and label length to flag potential tunneling activities.
-
DGA Detection:
- Identifies queries with a high NXDOMAIN response ratio, indicating many failed domain resolutions, which is typical of DGA.
- Requires a minimum number of samples and distinct parent domains to consider the activity suspicious.
-
Output: The query outputs potential incidents with details like the AWS account, region, VPC ID, source IP address, and the suspected activity type (DNS tunneling or DGA).
-
Incident Management: If suspicious activity is detected, it creates an incident and groups related alerts by IP address and other custom details.
-
Frequency and Severity: The query runs every 15 minutes, covering the last hour of data, and is marked with medium severity.
Overall, this query is a scheduled rule to monitor and alert on potential DNS-based threats within AWS environments.