如何分析 CloudWatch 中的 AWS WAF 日志?
我想分析和筛选存储在 Amazon CloudWatch 中的 AWS WAF 日志。
解决方法
要分析和筛选 CloudWatch 中的特定 AWS WAF 日志请求,请使用 CloudWatch Logs Insights 或 CloudWatch 查询生成器。
CloudWatch Log Insights
您可以在 CloudWatch 控制台中或在 AWS WAF 的 Log Insights 中使用 CloudWatch Log Insights。
AWS WAF
完成以下步骤:
- 打开 AWS WAF 控制台。
- 在导航窗格中,选择 AWS WAF。
- 选择 Resources & protection packs(资源和保护包)。
- 选择您的 Protection pack(保护包)。
- 选择 view dashboard(查看控制面板)。
- 展开页面底部的日志资源管理器。
- 选择 View in CloudWatch(在 CloudWatch 中查看)。
- 在 Query editor(查询编辑器)中,输入您的查询。使用查询语法来设计查询。您也可以从 Most frequently used queries(最常用查询)列表中选择查询。
- 选择 Run query(运行查询)。
CloudWatch
完成以下步骤:
- 打开 CloudWatch 控制台。
- 在导航窗格中,选择 Logs(日志)。
- 选择 Log Insights。
- 在 Log Insights 下,打开下拉菜单 Selection criteria(选择条件)。
- 选择一个或多个要查询的日志组。或者,选择 Browse log groups(浏览日志组),然后选择您的日志组。
- (可选)为查询选择时间范围。
- 使用查询语法来设计查询。
- 要查看结果,请选择 Run query(运行查询)。
CloudWatch 查询生成器
要使用生成式人工智能来分析您的访问日志,请在 CloudWatch 中运行查询生成器。
用于搜索数据的查询示例
要通过 CloudWatch Logs Insights 筛选出特定信息,请使用以下示例查询。
主要客户端 IP
要统计访问您的日志的主要客户端 IP,请运行以下查询:
fields httpRequest.clientIp| stats count(*) as requestCount by httpRequest.clientIp | sort requestCount desc
主要国家/地区
要统计访问您的日志的主要国家/地区,请运行以下查询:
stats count(*) as RequestCount by httpRequest.country as Country| sort RequestCount desc
主要主机
要统计访问您的日志的主要主机,请运行以下查询:
parse @message /\{"name":"[Hh]ost\",\"value":\"(?[^"}]*)/|stats count(*) as RequestCount by Host | sort RequestCount desc
主要方法
要统计访问您的日志的主要方法,请运行以下查询:
stats count(*)as RequestCount by httpRequest.httpMethod as Method| sort RequestCount desc
主要用户代理
要统计访问您的日志的主要用户代理,请运行以下查询:
parse @message /\{"name":"[Uu]ser\-[Aa]gent\",\"value\"\:\"(?[^"}]*)/| stats count(*) as RequestCount by UserAgent | sort RequestCount desc
主要终止规则
要统计您的日志中的主要终止规则,请运行以下查询:
stats count(*) as RequestCount by terminatingRuleId| sort RequestCount desc
筛选被阻止的请求
要筛选所有被阻止的请求及其终止规则、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":\"(?[^"}]*)/ | 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":\"(?[^"}])/ | parse @message /\{"name":"[Uu]ser\-[Aa]gent\",\"value\"\:\"(?[^"}])/ | 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\"\:\"(?[^"}]*)/| parse @message /\{"name":"[Hh]ost\",\"value":\"(?[^"}]*)/ | filter httpRequest.httpMethod ="POST" | display Rule, action, Country, ClientIP, Method, URI, Host, UserAgent | sort Rule, action desc
按国家/地区筛选
要筛选出并非来自特定国家/地区的请求,请运行以下查询:
fields terminatingRuleId as Rule, action, httpRequest.country as Country, httpRequest.clientIp as ClientIP, httpRequest.uri as URI| parse @message /\{"name":"[Hh]ost\",\"value":\"(?[^"}]*)/ | parse @message /\{"name":"[Uu]ser\-[Aa]gent\",\"value\"\:\"(?[^"}]*)/ | filter Country != "US" | sort Country, action desc
**注意:**请将 US 替换为要筛选掉的国家/地区代码。
筛选被基于速率的规则阻止的请求
要筛选日志中被基于速率的规则阻止的请求,请运行以下查询:
fields @timestamp, httpRequest.clientIp as ClientIP, httpRequest.uri as URI, terminatingRuleId as rule, httpRequest.country as Country| filter action = "BLOCK" | filter terminatingRuleType = "RATE_BASED" | sort @timestamp desc
跨站脚本攻击 (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 @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、响应代码和整条消息。
