Query Details

H12 Waf Rule Efficacy And Countmode Bypass Hunt

Query

// REVIEW ONLY - Hunting Query H12: AWS WAF Rule Efficacy, Non-Terminating Count-Mode Signals & Allowed Outliers
// Helps SOC/AppSec separate rules stuck in COUNT mode (NonTerminatingMatchingRules) from effective BLOCK rules.
AWSWAF
| where TimeGenerated > ago(24h)
| mv-apply Hdr = Headers on (
    summarize HostHeader = take_anyif(tostring(Hdr.value), tolower(tostring(Hdr.name)) == "host"),
              UserAgent = take_anyif(tostring(Hdr.value), tolower(tostring(Hdr.name)) == "user-agent")
)
| extend CountModeRuleCount = array_length(NonTerminatingMatchingRules),
    CleanUri = tostring(split(Uri, "?")[0])
| where CountModeRuleCount > 0 or Action != "ALLOW" or ResponseCodeSent >= 500
| summarize
    TotalRequests = count(),
    AllowedWithCountModeMatch = countif(Action == "ALLOW" and CountModeRuleCount > 0),
    BlockedRequests = countif(Action == "BLOCK"),
    Server5xxResponses = countif(ResponseCodeSent >= 500),
    DistinctClientIps = dcount(ClientIp),
    TopCountModeRules = make_set(NonTerminatingMatchingRules, 10),
    SampleUserAgents = make_set(UserAgent, 5)
    by WebAclId, HttpSourceName, HostHeader=coalesce(HostHeader, "UnknownHost"), CleanUri, TerminatingRuleId
| order by AllowedWithCountModeMatch desc, BlockedRequests desc
| take 1000

Explanation

This query is designed to help security operations and application security teams evaluate the effectiveness of AWS WAF (Web Application Firewall) rules. It focuses on distinguishing between rules that are in "COUNT" mode (which log requests but don't block them) and those that effectively block requests. Here's a simplified breakdown of what the query does:

  1. Time Frame: It looks at AWS WAF logs from the past 24 hours.

  2. Extract Headers: It extracts the "Host" and "User-Agent" headers from the request logs.

  3. Count Mode Rules: It calculates how many rules are in "COUNT" mode for each request and cleans up the URI by removing query parameters.

  4. Filter Criteria: It filters the logs to include requests that either:

    • Have rules in "COUNT" mode,
    • Are not allowed (i.e., blocked),
    • Or result in server errors (HTTP status code 500 or higher).
  5. Summarize Data: It summarizes the data by:

    • Counting total requests,
    • Counting requests that were allowed but matched "COUNT" mode rules,
    • Counting blocked requests,
    • Counting server error responses,
    • Counting distinct client IPs,
    • Listing up to 10 unique "COUNT" mode rules,
    • Listing up to 5 sample user agents.
  6. Group and Order: The data is grouped by Web ACL ID, HTTP source name, host header, cleaned URI, and terminating rule ID. It is then ordered by the number of allowed requests with "COUNT" mode matches and blocked requests, in descending order.

  7. Limit Results: Finally, it limits the output to the top 1,000 results.

This query helps identify which rules are only logging requests (in "COUNT" mode) and which are actively blocking traffic, providing insights into rule effectiveness and potential areas for improvement.