如何分析 CloudWatch 中的 AWS WAF 日志?
我想分析和筛选存储在 Amazon CloudWatch 中的 AWS WAF 日志。
解决方法
要分析和筛选 CloudWatch 中的特定 AWS WAF 日志请求,请使用 CloudWatch Logs Insights 或 CloudWatch 查询生成器。
使用 CloudWatch Log Insights
您可以在 CloudWatch 控制台或 AWS WAF 控制台中使用 CloudWatch Log Insights。
AWS WAF 控制台
完成以下步骤:
- 打开 AWS WAF 控制台。
- 在导航窗格中,选择 Protection packs (web ACLs)(保护包 (Web ACL))。
- 选择您的保护包。
- 选择 View dashboard, logs and sampled requests(查看控制面板、日志和采样请求)。
- 在 Log explorer(日志资源管理器)中,选择 View in CloudWatch(在 CloudWatch 中查看)。
- 在 Query editor(查询编辑器)中,输入您的查询。使用查询语法来设计查询。您也可以从 Most frequently used queries(最常用查询)列表中选择查询。
- 选择 Run query(运行查询)。
CloudWatch 控制台
完成以下步骤:
- 打开 CloudWatch 控制台。
- 在导航窗格中的 Logs(日志)下,选择 Log Insights。
- 对于 Selection criteria(选择标准),选择一个或多个要查询的日志组。或者,选择 Browse log groups(浏览日志组),然后选择您的日志组。
- (可选)为查询选择时间范围。
- 使用查询语法来设计查询。
- 要查看结果,请选择 Run query(运行查询)。
使用 CloudWatch 查询生成器
要使用自然语言生成查询和分析访问日志,请运行 CloudWatch 中的查询生成器。
查询示例
要通过 CloudWatch Logs Insights 筛选出特定信息,请使用以下示例查询。
主要客户端 IP 地址
要统计访问您的应用程序的主要客户端 IP 地址,请运行以下查询:
stats count(*) as requestCount by httpRequest.clientIp | sort requestCount desc
主要国家/地区
要统计向您的应用程序发起请求的主要来源国家/地区,请运行以下查询:
stats count(*) as RequestCount by httpRequest.country as Country | sort RequestCount desc
主要 httpmethod
要统计访问您的应用程序的主要 httpmethod,请运行以下查询:
stats count(*)as RequestCount by httpRequest.httpMethod as Method | sort RequestCount desc
主要终止规则
要统计您的日志中的主要终止规则,请运行以下查询:
stats count(*) as RequestCount by terminatingRuleId | sort RequestCount desc
主要主机
要统计访问您的应用程序的主要主机,请运行以下查询:
parse @message /\{"name":"[Hh]ost","value":"(?<Host>[^"]*)"/ | stats count(*) as RequestCount by Host | sort RequestCount desc
主要用户代理
要统计访问您的应用程序的主要用户代理,请运行以下查询:
parse @message /\{"name":"[Uu]ser\-[Aa]gent","value":"(?<UserAgent>[^"]*)"/ | stats count(*) as RequestCount by UserAgent | sort RequestCount desc
主要 X-forwarded-Ip
要统计向您的应用程序发起请求的主要 XFF IP 地址,请运行以下查询:
parse @message /\{"name":"[Xx]-[Ff]orwarded-[Ff]or","value":"(?<XFF_IP>[^"}]*)/ | stats count(*) as RequestCount by XFF | sort RequestCount desc
受基于速率的规则阻止的主要 IP 地址
要查找突破阈值的 IP 地址,如果出现 HTTP Flood DDoS,请运行以下查询:
fields httpRequest.clientIp | filter terminatingRuleType = "RATE_BASED" ## and webaclId = "<Webacl ARN>" ## uncomment to filter for specific WebACL| | stats count(*) as requestCount by httpRequest.clientIp, httpRequest.country | sort requestCount desc | limit 100
筛选被阻止的请求
要筛选所有被阻止的请求及其终止规则、URI 路径和客户端 IP,请运行以下查询:
fields @timestamp, httpRequest.clientIp as ClientIP, httpRequest.uri as URI, terminatingRuleId as rule | filter action = "BLOCK" | sort @timestamp desc
按主机筛选
要按特定主机筛选日志,请运行以下查询:
fields terminatingRuleId as Rule, action, httpRequest.country as Country, httpRequest.clientIp as ClientIP, httpRequest.uri as URI | parse @message /\{"name":"[Hh]ost","value":"(?<Host>[^"]*)"/ | filter Host = "www.example.com"
注意: 将 www.example.com 替换为您的主机名称。
按特定字符串筛选
要按特定字符串筛选日志,请运行以下查询:
fields terminatingRuleId as Rule, action, httpRequest.country as Country, httpRequest.clientIp as ClientIP, httpRequest.httpMethod as Method,httpRequest.uri as URI | parse @message /\{"name":"[Hh]ost","value":"(?<Host>[^"]*)"/ | parse @message /\{"name":"[Uu]ser\-[Aa]gent","value":"(?<UserAgent>[^"]*)"/ | filter @message like "{jndi:ldap" | sort action, URI desc
注意: 将 {jndi:ldap 替换为您的字符串。
按 POST 请求筛选
要按 POST 请求进行筛选,请运行以下查询:
fields terminatingRuleId as Rule, action, httpRequest.country as Country, httpRequest.clientIp as ClientIP, httpRequest.httpMethod as Method, httpRequest.uri as URI | parse @message /\{"name":"[Uu]ser\-[Aa]gent","value":"(?<UserAgent>[^"]*)"/ | parse @message /\{"name":"[Hh]ost","value":"(?<Host>[^"]*)"/ | filter httpRequest.httpMethod ="POST" | display Rule, action, Country, ClientIP, Method, URI, Host, UserAgent | sort Rule, action desc
注意: 您可以使用上述查询按其他 HTTP 方法进行筛选。将 POST 替换为 GET、Head 或其他 HTTP 方法。
XSS 或 SQL 注入
要在自定义规则或 AWS 托管式规则组的终止规则中查找导致跨站脚本攻击 (XSS) 或 SQL 注入的模式,请运行以下查询:
fields @timestamp | parse @message ',"terminatingRuleMatchDetails":[*],' as terminatingRuleMatchData | filter (terminatingRuleMatchData like /XSS/ or terminatingRuleMatchData like /SQL/) | display @timestamp, httpRequest.clientIp, httpRequest.country, terminatingRuleMatchData, httpRequest.requestId
此查询会显示带有时间戳、客户端 IP 地址、来源国家/地区、匹配详细信息和请求 ID 的条目。
按国家/地区筛选
要筛选出并非来自特定国家/地区的请求,请运行以下查询:
fields terminatingRuleId as Rule, action, httpRequest.country as Country, httpRequest.clientIp as ClientIP, httpRequest.uri as URI | parse @message /\{"name":"[Uu]ser\-[Aa]gent","value":"(?<UserAgent>[^"]*)"/ | parse @message /\{"name":"[Hh]ost","value":"(?<Host>[^"]*)"/ | filter Country != "US" | sort Country, action desc
注意: 将 US 替换为要筛选掉的国家/地区代码。
筛选按规则组中特定规则计数的请求
要筛选规则组中特定规则在默认情况下计数和终止的请求的日志条目,请运行以下查询:
fields @timestamp | filter (@message like 'excludedRules":[{"exclusionType":"EXCLUDED_AS_COUNT","ruleId":"NoUserAgent_HEADER"}]}' and @message like 'terminatingRuleId":"Default_Action"') | parse @message '"ruleId":*}]}' as ruleMatchDetails | display @timestamp, httpRequest.clientIp, httpRequest.country, ruleMatchDetails, httpRequest.requestId
注意: 将 ruleId 替换为您的规则 ID。
筛选具有无效 CAPTCHA 的请求
要筛选具有无效 CAPTCHA 的前 100 个请求,请运行以下查询:
fields @timestamp, httpRequest.clientIp, httpRequest.requestId, captchaResponse.failureReason, @message | filter captchaResponse.failureReason ='TOKEN_MISSING' | sort @timestamp desc| limit 100
注意: 将 limit 子句中的 100 替换为要筛选的请求数。
此查询将显示请求时间、IP 地址、请求 ID、响应代码和整条消息。
筛选 ABC、ATP、ACFP、AntiDDOS AWS 托管规则检测到的所有请求
要筛选与 ABC、ATP、ACFP 或 AntiDDoS AWS 托管规则的规则标签相匹配的前 100 个请求,请运行以下查询:
fields @timestamp | filter @message like 'awswaf:managed:aws:bot-control' | display @timestamp,httpRequest.clientIp, httpRequest.uri,Labels, @message | sort @timestamp desc | limit 100
注意: 将 limit 子句中的 100 替换为要筛选的请求数。将 awswaf:managed:aws:bot-control 替换为规则组标签。对于 ATP 规则组,请使用 awswaf:managed:aws:atp。对于 ACFP 规则组,请使用 awswaf:managed:aws:acfp。对于 AntiDDOS AMR 规则组,请使用 awswaf:managed:aws:anti-ddos。
要筛选带有特定标签的日志,请在上述查询中添加以下行:
| filter @message like 'awswaf:managed:aws:bot-control:signal:non_browser_user_agent'
- 语言
- 中文 (简体)

This article was reviewed and updated on 2026-03-20.
相关内容
AWS 官方已更新 10 个月前