Query Details

AWS SecOps P1-05 - CloudFront Signed-URL Abuse, Path Enumeration and Automation Anomaly

P1 05 Cloudfront Origin And Credential Abuse

Query

// 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, PathEnumOrScannerUa

Explanation

This query is designed to detect potential security anomalies in AWS CloudFront access logs. It focuses on identifying three main types of suspicious activities:

  1. Signed URL/Cookie Replay: It looks for instances where a signed URL or cookie is accessed from 8 or more different client IP addresses, which could indicate unauthorized sharing or misuse. It also checks for a high number of 403 errors (access denied), suggesting attempts to access restricted content.

  2. Path Enumeration with High Error Rates: It identifies bursts of requests to 80 or more distinct URIs with a high error rate (70% or more 4xx/5xx errors), which might indicate attempts to discover hidden paths or resources on the server.

  3. Offensive Framework User-Agent Strings: It checks for known offensive or automated tool user-agent strings (like sqlmap, nuclei, etc.) that could indicate scanning or probing activities.

The query runs every 15 minutes and looks back over the past hour. It uses custom table columns and is resilient to naming differences across deployments. If any anomalies are detected, it creates an incident with details like the host header, target URI, user agent, and type of anomaly. The incidents are grouped by host header and anomaly type to manage alerts effectively.