Query Details

P1 02 Waf Campaign And Block To Allow Bypass

Query

// Standalone KQL for Analytic Rule: AWS SecOps P1-02 - AWS WAF Attack Campaign and Block-to-Allow Bypass Candidate
// Rule File: analytics/P1-02-waf-campaign-and-block-to-allow-bypass.yaml | Rule ID: REPLACE_WITH_RULE_GUID | Severity: High
// Schedule: 15m / Lookback: 1h | REVIEW ONLY - Do not deploy/schedule without approval.
// REVIEW ONLY. Validate AWSWAF schema via discovery/D06 first.
let ApprovedScanners = _GetWatchlist('AWS_TrustedDomainsAndScanners')
| where tolower(tostring(column_ifexists("Verified", ""))) == "true" and tostring(column_ifexists("PatternType", "")) == "SourceIp"
| project IndicatorValue=tostring(column_ifexists("IndicatorValue", ""));
let WafEvents = materialize(
    AWSWAF
    | where TimeGenerated > ago(1h) and ingestion_time() > ago(15m)
    | where ClientIp !in (ApprovedScanners)
    | mv-apply Hdr = Headers on (
        summarize HostHeader = take_anyif(tostring(Hdr.value), tolower(tostring(Hdr.name)) == "host")
    )
    | extend CleanUri = tostring(split(Uri, "?")[0]),
        RuleSignals = strcat(TerminatingRuleId, " ", tostring(RuleGroupList), " ", tostring(NonTerminatingMatchingRules), " ", tostring(Labels))
    | extend IsExploitSignature = RuleSignals matches regex @"(?i)(SQLi|XSS|CrossSiteScripting|LFI|PathTraversal|Traversal|CommandInjection|Log4j|RCE|AdminProtection|BadInputs)"
    | project TimeGenerated, WebAclId, HttpSourceName, HttpSourceId, HostHeader=coalesce(HostHeader, "UnknownHost"),
        CleanUri, ClientIp, Country, Ja3Fingerprint, Action, ResponseCodeSent, TerminatingRuleId, IsExploitSignature
);
let BlockBursts = WafEvents
| where Action == "BLOCK" and IsExploitSignature
| summarize FirstBlockUtc=min(TimeGenerated), LastBlockUtc=max(TimeGenerated),
    BlockedExploitCount=count(), MatchedBlockRules=make_set(TerminatingRuleId, 10)
    by WebAclId, HttpSourceId, HostHeader, ClientIp, Country, Ja3Fingerprint
| where BlockedExploitCount >= 15;
let SubsequentAllows = WafEvents
| where Action == "ALLOW" and ResponseCodeSent between (200 .. 399)
| where not(CleanUri matches regex @"(?i)\.(css|js|png|jpg|jpeg|svg|ico|woff2?)$")
| summarize FirstAllowUtc=min(TimeGenerated), LastAllowUtc=max(TimeGenerated),
    AllowedAppRequests=count(), AllowedUris=make_set(CleanUri, 15)
    by WebAclId, HttpSourceId, HostHeader, ClientIp;
let BypassCandidates = BlockBursts
| join kind=inner SubsequentAllows on WebAclId, HttpSourceId, HostHeader, ClientIp
| where LastAllowUtc between (FirstBlockUtc .. (LastBlockUtc + 30m))
| project TimeGenerated=LastAllowUtc, WebAclId, HttpSourceId, HostHeader, SourceIpAddress=ClientIp,
    Country, Ja3Fingerprint, CampaignType="BlockToAllowBypassCandidate",
    BlockedCountStr=tostring(BlockedExploitCount), AllowedCountStr=tostring(AllowedAppRequests),
    RuleSummary=tostring(MatchedBlockRules), TargetUris=tostring(AllowedUris);
let DistributedSpray = WafEvents
| where IsExploitSignature
| summarize TimeGenerated=min(TimeGenerated), DistinctIps=dcount(ClientIp), TotalHits=count(),
    AllowedHits=countif(Action == "ALLOW"), SampleIp=take_any(ClientIp), Countries=make_set(Country, 10),
    MatchedRules=make_set(TerminatingRuleId, 10)
    by WebAclId, HttpSourceId, HostHeader, CleanUri
| where DistinctIps >= 15 and TotalHits >= 150
| project TimeGenerated, WebAclId, HttpSourceId, HostHeader, SourceIpAddress=SampleIp,
    Country=tostring(Countries[0]), Ja3Fingerprint="DistributedSources",
    CampaignType="DistributedMultiIpWebExploitCampaign",
    BlockedCountStr=tostring(TotalHits - AllowedHits), AllowedCountStr=tostring(AllowedHits),
    RuleSummary=tostring(MatchedRules), TargetUris=CleanUri;
union BypassCandidates, DistributedSpray

Explanation

This KQL query is designed to identify potential security threats and suspicious activities related to AWS Web Application Firewall (WAF) events. Here's a simplified breakdown of what the query does:

  1. Approved Scanners List: It first retrieves a list of trusted IP addresses from a watchlist, filtering for those verified as "true" and of type "SourceIp".

  2. WAF Events Filtering: It collects WAF events from the past hour that were ingested in the last 15 minutes, excluding those from approved scanners. It processes these events to extract relevant information like the host header, clean URI, and rule signals, and checks if any exploit signatures (like SQL injection or cross-site scripting) are present.

  3. Block Bursts Detection: It identifies instances where a client IP was blocked 15 or more times due to exploit signatures. It summarizes these "block bursts" by recording the first and last block times, the count of blocked exploits, and the rules that matched.

  4. Subsequent Allows Detection: It looks for subsequent "ALLOW" actions for the same client IPs that were previously blocked, focusing on successful responses (HTTP status codes 200-399) and excluding requests for static resources like images or scripts.

  5. Bypass Candidates Identification: It identifies potential "Block-to-Allow Bypass Candidates" by finding cases where a client IP that was blocked for exploits later received allowed responses within 30 minutes after the last block. It compiles details like the number of blocked and allowed requests, and the rules and URIs involved.

  6. Distributed Spray Detection: It detects distributed attack patterns where multiple distinct IPs (15 or more) are involved in a web exploit campaign, generating a significant number of hits (150 or more) on the WAF. It summarizes these events by the number of distinct IPs, total hits, allowed hits, and the rules matched.

  7. Union of Results: Finally, it combines the results from the bypass candidates and distributed spray detections into a single output, providing a comprehensive view of potential security threats related to AWS WAF.

This query is part of an analytic rule with high severity, intended for review and validation before deployment.