Query Details

AWS SecOps P1-02 - AWS WAF Attack Campaign and Block-to-Allow Bypass Candidate

P1 02 Waf Campaign And Block To Allow Bypass

Query

// 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 query is designed to detect potential security threats related to AWS Web Application Firewall (WAF) by identifying two main types of suspicious activities:

  1. Block-to-Allow Bypass Candidates: This part of the query looks for instances where a specific client IP address or JA3 fingerprint (a method of identifying SSL/TLS clients) triggers 15 or more WAF blocks for SQL injection, cross-site scripting, or other exploit attempts on a specific web application and host. If this same client then receives "ALLOW" responses (HTTP status codes 200-399) on non-static application URLs within 30 minutes, it is flagged as a potential bypass candidate. This helps identify situations where an attacker might have found a way to bypass WAF protections.

  2. Distributed Multi-IP Web Exploit Campaigns: This part detects coordinated attacks where 15 or more distinct IP addresses target a single URL with exploit attempts. It looks for a high volume of hits (150 or more) on a specific URL, indicating a distributed attack campaign.

The query excludes known and approved scanners or penetration tests by referencing a watchlist of trusted domains and scanners. It runs every 15 minutes and looks back over the past hour to identify these patterns. If any suspicious activity is detected, an incident is created for further investigation. The query also includes configurations for grouping related alerts to manage incidents efficiently.