跳至內容

如何分析 CloudWatch 中的 AWS WAF 日誌?

4 分的閱讀內容
0

我想分析和篩選儲存在 Amazon CloudWatch 中的 AWS WAF 日誌。

解決方法

若要在 CloudWatch 中分析及篩選特定的 AWS WAF 日誌請求,請使用 CloudWatch Logs Insights 或 CloudWatch 查詢產生器。

使用 CloudWatch Logs Insights

您可以在 CloudWatch 主控台或 AWS WAF 主控台中使用 CloudWatch Logs Insights。

AWS WAF 主控台

請完成以下步驟:

  1. 開啟 AWS WAF console (AWS WAF 主控台)。
  2. 在導覽窗格中,選擇 Protection packs (web ACLs) (保護套件 (Web ACL))。
  3. 選擇您的保護套件。
  4. 選擇 View dashboard, logs and sampled requests (檢視儀表板、日誌和取樣請求)。
  5. Log explorer (日誌總管) 中,選擇 View in CloudWatch (在 CloudWatch 中檢視)。
  6. Query editor (查詢編輯器) 中輸入查詢。使用查詢語法設計您的查詢。您也可以從 Most frequently used queries (最常使用的查詢) 清單中選擇查詢。
  7. 選擇 Run query (執行查詢)。

CloudWatch 主控台

請完成以下步驟:

  1. 開啟 CloudWatch console (CloudWatch 主控台)。
  2. 在導覽窗格的 Logs (日誌) 下方,選擇 Log Insights
  3. Selection criteria (選取條件) 中,選取一或多個要查詢的日誌群組。或者,選擇 Browse log groups (瀏覽日誌群組),然後選取您的日誌群組。
  4. (選用) 選擇查詢的時間範圍。
  5. 使用查詢語法設計您的查詢。
  6. 若要檢視結果,請選擇 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

最常見的 httpmethods

若要統計存取您應用程式次數最多的 httpmethods,請執行以下查詢:

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-Ips

若要統計向您的應用程式提出請求次數最多的 XFF IP 位址,請執行以下查詢:

parse @message /\{"name":"[Xx]-[Ff]orwarded-[Ff]or","value":"(?<XFF_IP>[^"}]*)/
| stats count(*) as RequestCount by XFF
| sort RequestCount desc

遭速率型規則封鎖次數最多的 IP 位址

若要在發生 **HTTP 洪水 DDoS 攻擊時,**找出超過閾值的 IP 位址,請執行以下查詢:

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 替換為 GETHead 或其他 HTTP 方法。

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 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'