如何分析 CloudWatch 中的 AWS WAF 日誌?
我想分析和篩選儲存在 Amazon CloudWatch 中的 AWS WAF 日誌。
解決方法
若要在 CloudWatch 中分析和篩選特定的 AWS WAF 日誌請求,請使用 CloudWatch Logs Insights 或 CloudWatch 查詢生成器。
CloudWatch Logs Insights
您可以在 CloudWatch 主控台中或 AWS WAF 的 Log Insights 中,使用 CloudWatch Log Insights。
AWS WAF
請完成下列步驟:
- 開啟 AWS WAF console (AWS WAF 主控台)。
- 在導覽窗格中,選擇 AWS WAF。
- 選擇 Resources & protection packs (資源和保護套件)。
- 選取您的保護套件。
- 選擇 view dashboard (檢視儀表板)。
- 展開頁面底部的 Log explorer (日誌瀏覽器)。
- 選擇 View in CloudWatch (CloudWatch 中的檢視)。
- 在 Query editor (查詢編輯器) 中,輸入您的查詢。使用查詢語法來設計您的查詢。您也可以從最常用的查詢清單中選取查詢。
- 選擇 Run query (執行查詢)。
CloudWatch
請完成下列步驟:
- 開啟 CloudWatch console (CloudWatch 主控台)。
- 在導覽窗格中,選擇 Logs (日誌)。
- 選擇 Log Insights。
- 在 Log Insights 下,開啟 Selection criteria (選取條件) 下拉式功能表。
- 選取一個或多個要查詢的日誌群組。或者,選擇 Browse log groups (瀏覽日誌群組),然後選取您的日誌群組。
- (選用) 選擇查詢的時間範圍。
- 使用查詢語法來設計您的查詢。
- 若要檢視結果,請選擇 Run query (執行查詢)。
CloudWatch 查詢生成器
若要使用生成式 AI 來分析您的存取日誌,請在 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 injection 隱碼攻擊
若要在自訂規則或 AWS 受管規則群組的終止規則中,尋找造成 XSS 或 SQL injection 隱碼攻擊的模式,請執行下列查詢:
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、回應代碼和整個訊息。
