如何分析 CloudWatch 中的 AWS WAF 日誌?
我將 AWS WAF 日誌儲存在 Amazon CloudWatch 中。我想分析和篩選這些日誌。
解決方法
若要在 CloudWatch 中分析和篩選特定日誌請求,請使用 CloudWatch Linsights 或 CloudWatch 查詢產生器。
使用 CloudWatch Logs Insights 分析 AWS WAF 存取日誌
您可以從 CloudWatch 主控台或 AWS WAF 主控台中的 CloudWatch Logs Insights 索引標籤使用 CloudWatch Logs Insights。
從 AWS WAF
請完成下列步驟:
- 開啟 AWS WAF 主控台。
- 在導覽窗格的 AWS WAF 下,選擇 Web ACL。
- 針對區域,選取包含您的 Web 存取控制清單 (Web ACL) 的 AWS 區域。
注意:如果您為 Amazon CloudFront 設定 Web ACL,請選取全域。 - 選取您的 Web ACL。
- 從頁面頂端的索引標籤,導覽至 CloudWatch Logs Insights。
- 在查詢編輯器中,輸入您的查詢。使用查詢語法來設計您的查詢。您也可以從最常用的查詢清單中選取查詢。
- 選擇執行查詢。
- 若要檢視結果,請在導覽窗格中選擇日誌。
從 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\":\"(?Host[^\"]*)\"/ | stats count(*) as RequestCount by Host | sort RequestCount desc
常用方法
若要計算針對受保護資源提出要求的常用 HTTP 方法,請執行下列查詢:
stats count(*)as RequestCount by httpRequest.httpMethod as Method | sort RequestCount desc
常用使用者客服
若要計算對受保護資源提出要求的常用使用者客服,請執行下列查詢:
parse @message /\"name\":\"[Uu]ser\-[Aa]gent\",\"value\":\"(?UserAgent[^\"]*)\"/ | stats count(*) as RequestCount by UserAgent | sort RequestCount desc
常用 URI 路徑
若要列出受保護資源上最常造訪的 URI 路徑,請執行下列查詢:
fields httpRequest.uri | stats count(*) as uriVisits by httpRequest.uri | sort uriVisits desc | limit 50
**常用終止規則 **
若要計算日誌中最常見的終止規則,請執行下列查詢:
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":\"(?Host[^\"]*)\"/ | filter Host = "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 "Postman" | display Rule, action, Country, ClientIP, Method, URI, Host, UserAgent | sort action, URI desc
**注意:**使用您的使用者客服取代 Postman。
篩選 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\":\"[Hh]ost\",\"value\":\"(?Host[^\"]*)\"/ | parse @message /\"name\":\"[Uu]ser\-[Aa]gent\",\"value\":\"(?UserAgent[^\"]*)\"/ | filter httpRequest.httpMethod ="POST" | display Rule, action, Country, ClientIP, Method, URI, Host, UserAgent | 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[^\"]*)\"/ | parse @message /\"name\":\"[Uu]ser\-[Aa]gent\",\"value\":\"(?UserAgent[^\"]*)\"/ | 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 隱碼攻擊的模式,請執行下列查詢。查詢會傳回包含時間戳記、用戶端 IP 位址、來源國家、比對詳細資料和請求 ID 的項目:
fields @timestamp | parse @message ',"terminatingRuleMatchDetails":[*],' as terminatingRuleMatchData | filter (terminatingRuleMatchData like /XSS/ or terminatingRuleMatchData like /SQL/) | display @timestamp, httpRequest.clientIp, httpRequest.country, terminatingRuleMatchData, httpRequest.requestId
依規則群組中的特定規則篩選計算的請求
若要篩選由規則群組中特定規則計算的請求,然後由預設動作終止的請求,請執行下列查詢:
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
**注意:**使用您的規則 ID 取代 ruleId。
使用無效的 CAPTCHA 篩選請求
若要篩選具有無效 CAPTCHA 的前 100 個請求,請執行下列查詢。此查詢會傳回提出請求的時間、IP 位址、請求 ID、回應代碼以及整個訊息:
fields @timestamp, httpRequest.clientIp, httpRequest.requestId, captchaResponse.failureReason, @message | filter captchaResponse.failureReason ='TOKEN_MISSING' | sort @timestamp desc | limit 100
**注意:**使用您要篩選的請求數字取代 100。
使用 CloudWatch 查詢產生器分析 AWS WAF 存取日誌
若要使用生成式 AI 來分析您的存取日誌,請在 CloudWatch 中執行查詢產生器。
相關內容
- 已提問 1 年前lg...
- 已提問 3 個月前lg...
- 已提問 3 個月前lg...
- AWS 官方已更新 10 個月前
- AWS 官方已更新 7 個月前
- AWS 官方已更新 7 個月前