P1 05 Cloudfront Origin And Credential Abuse
Query
// Standalone KQL for Analytic Rule: AWS SecOps P1-05 - CloudFront Signed-URL Abuse, Path Enumeration and Automation Anomaly
// Rule File: analytics/P1-05-cloudfront-origin-and-credential-abuse.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: Medium
// Schedule: 15m / Lookback: 1h | REVIEW ONLY - Do not deploy/schedule without approval.
// REVIEW ONLY. Validate AWSCloudFront_AccessLog_CL custom table columns via D08 before enabling!
let TrustedScanners = _GetWatchlist('AWS_TrustedDomainsAndScanners')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and tostring(column_ifexists("PatternType", "")) == "SourceIp"
| project tostring(column_ifexists("IndicatorValue", ""));
let CfLogs = materialize(
AWSCloudFront_AccessLog_CL
| where TimeGenerated > ago(1h) and ingestion_time() > ago(15m)
| extend ClientIp = tostring(column_ifexists("c_ip_s", "")),
HostHeader = tostring(column_ifexists("cs_host_s", "")),
UriStem = tostring(column_ifexists("cs_uri_stem_s", "")),
UriQuery = tostring(column_ifexists("cs_uri_query_s", "")),
StatusCode = toint(column_ifexists("sc_status_d", 0)),
UserAgent = tostring(column_ifexists("cs_user_agent_s", "")),
CookieHeader = tostring(column_ifexists("cs_cookie_s", ""))
| where isnotempty(ClientIp) and ClientIp !in (TrustedScanners)
| extend SignedKeyId = coalesce(
extract(@"Key-Pair-Id=([A-Za-z0-9]+)", 1, UriQuery),
extract(@"CloudFront-Key-Pair-Id=([A-Za-z0-9]+)", 1, CookieHeader), "")
);
let SignedArtifactReplay = CfLogs
| where isnotempty(SignedKeyId)
| summarize TimeGenerated=min(TimeGenerated), DistinctClientIps=dcount(ClientIp), TotalRequests=count(),
Denied403=countif(StatusCode == 403), SampleIp=take_any(ClientIp), SampleUri=take_any(UriStem),
SampleUa=take_any(UserAgent)
by HostHeader, SignedKeyId
| where DistinctClientIps >= 8 or Denied403 >= 30
| project TimeGenerated, HostHeader, SourceIpAddress=SampleIp, TargetUri=SampleUri,
UserAgent=SampleUa, AnomalyType=strcat("SignedUrlOrCookieAnomaly(KeyId=", SignedKeyId, ",DistinctIps=", DistinctClientIps, ",403s=", Denied403, ")");
let PathEnumOrScannerUa = CfLogs
| summarize TimeGenerated=min(TimeGenerated), TotalRequests=count(), DistinctUris=dcount(UriStem),
ErrorCount=countif(StatusCode >= 400), SampleUri=take_any(UriStem), SampleUa=take_any(UserAgent),
OffensiveUaHits=countif(UserAgent matches regex @"(?i)(artex-enrich/|norma/|sqlmap|nuclei|ffuf|gobuster|nikto)")
by HostHeader, ClientIp
| extend ErrorRatio = todouble(ErrorCount) / todouble(max_of(TotalRequests, 1))
| where (TotalRequests >= 100 and DistinctUris >= 80 and ErrorRatio >= 0.70) or OffensiveUaHits >= 5
| project TimeGenerated, HostHeader, SourceIpAddress=ClientIp, TargetUri=SampleUri,
UserAgent=SampleUa, AnomalyType=strcat("CloudFrontEnumOrToolUa(Reqs=", TotalRequests, ",Uris=", DistinctUris, ",ToolHits=", OffensiveUaHits, ")");
union SignedArtifactReplay, PathEnumOrScannerUaExplanation
This KQL query is designed to detect potential security anomalies related to AWS CloudFront usage. Here's a simplified breakdown of what the query does:
-
Trusted Scanners List: It retrieves a list of trusted IP addresses from a watchlist, filtering for those marked as verified and of type "SourceIp".
-
CloudFront Logs Filtering: It processes the
AWSCloudFront_AccessLog_CLtable to extract relevant fields such as client IP, host header, URI stem, URI query, status code, user agent, and cookie header. It filters out entries from trusted IPs and focuses on logs generated in the last hour and ingested in the last 15 minutes. -
Signed URL or Cookie Anomaly Detection:
- It identifies logs with signed key IDs (from URI query or cookie header).
- It summarizes these logs by host header and signed key ID, counting distinct client IPs and requests, and specifically counting denied requests (status code 403).
- It flags anomalies where there are either 8 or more distinct client IPs or 30 or more denied requests, indicating potential abuse of signed URLs or cookies.
-
Path Enumeration or Scanner User-Agent Anomaly Detection:
- It summarizes logs by host header and client IP, counting total requests, distinct URIs, errors (status code 400 or higher), and occurrences of known offensive user agents (e.g., tools like sqlmap, nuclei).
- It calculates an error ratio and flags anomalies where there are 100 or more requests with 80 or more distinct URIs and an error ratio of 70% or higher, or if there are 5 or more hits from offensive user agents.
-
Union of Anomalies: The results from both anomaly detection processes are combined into a single output, providing details like the time generated, host header, source IP address, target URI, user agent, and a description of the anomaly type.
This query is intended for review and validation purposes and should not be deployed without approval. It aims to identify unusual patterns that could indicate security threats, such as abuse of signed URLs or automated scanning activities.