跳至內容

如何允許遭 AWS WAF 機器人控制功能規則群組封鎖的機器人發出的請求?

4 分的閱讀內容
0

我想允許遭 AWS WAF 機器人控制功能規則群組封鎖的合法機器人發出的請求。

解決方法

若要允許遭機器人控制功能規則群組封鎖的合法機器人發出的請求,請完成以下步驟。

注意:AWS WAF 機器人控制功能規則群組會使用來自 AWS WAF 的 IP 位址來驗證機器人。部分經過驗證的機器人會透過 Proxy 或 CDN 進行路由,而在轉送請求時不會保留用戶端 IP 位址。如果您的機器人以這種方式路由,則必須明確允許該機器人。

從您的 AWS WAF 日誌中找出封鎖請求的機器人控制功能規則

若要找出封鎖合法機器人請求的規則,請分析您的 AWS WAF 日誌。使用 Amazon Athena 查詢或 Amazon CloudWatch Log Insights。

使用 Athena 查詢分析 AWS WAF 日誌

請完成下列步驟:

  1. 使用分區投影在 Athena 中建立 AWS WAF 日誌的資料表
  2. 若要尋找已封鎖的請求詳細資料,請執行以下 Athena 查詢:
    WITH waf_data AS
    (SELECT from_unixtime(waf.timestamp / 1000) as time,
    waf.terminatingRuleId,
    waf.action,
    waf.httprequest.clientip as clientip,
    waf.httprequest.requestid as requestid,
    waf.httprequest.country as country,
    rulegroup.terminatingrule.ruleid as matchedRule,
    labels as Labels,
    map_agg(LOWER(f.name),
    f.value) AS kv
    FROM waf_logs waf,
    UNNEST(waf.httprequest.headers)
    AS t(f), UNNEST(waf.rulegrouplist) AS t(rulegroup)
    WHERE rulegroup.terminatingrule.ruleid IS NOT NULL
    GROUP BY 1, 2, 3, 4, 5, 6, 7,8)
    SELECT waf_data.time,
    waf_data.action,
    waf_data.terminatingRuleId,
    waf_data.matchedRule,
    waf_data.kv['user-agent'] as UserAgent,
    waf_data.kv['user-agent'] like 'pingdom%',
    waf_data.clientip,
    waf_data.country,
    waf_data.Labels
    FROM waf_data
    Where terminatingRuleId='AWS-AWSManagedRulesBotControlRuleSet'
    and time > now() - interval '3' day
    ORDER BY time
    DESC
    **注意:**將 waf_logs 替換為您的資料表名稱,將 waf_data 替換為您的資料庫名稱,將 time > now () - interval '3' day 替換為您的時間範圍,並將 pingdom% 替換為您的機器人名稱。
    例如,Athena 查詢會篩選指定時間範圍內的記錄,請參閱查詢 AWS WAF 日誌
  3. 檢查 matchedRule 欄以找出封鎖合法機器人請求的規則。以下是步驟 2 中 Athena 查詢的輸出範例:
    #: 1
    timestamp: 2024-04-10 15:11:18.000
    Action**:** BLOCK
    terminatingRuleId: AWS-AWSManagedRulesBotControlRuleSet
    matchedRule: CategoryMonitoring
    UserAgent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/61.0.3163.100 Chrome/61.0.3163.100 Safari/537.36 PingdomPageSpeed/1.0 (pingbot/2.0; +http://www.pingdom.com/)
    _col5: false
    clientip: 192.0.2.0
    country: US
    labels: [{name=awswaf:managed:token:absent}, {name=awswaf:managed:aws:bot-control:bot:name:pingdom}, {name=awswaf:managed:aws:bot-control:bot:unverified}, {name=awswaf:managed:aws:bot-control:bot:category:monitoring},{name=awswaf:managed:captcha:absent}, {name=awswaf:managed:aws:bot-control:signal:non_browser_user_agent}]
    **注意:**Athena 查詢輸出會以資料表形式顯示。

使用 CloudWatch Log Insights 查詢

請完成下列步驟:

  1. 開啟 CloudWatch console (CloudWatch 主控台)。

  2. 在導覽窗格中,選擇 Logs (日誌),然後選擇 Log Insights

  3. 在 Log Insights 下,開啟下拉式功能表選取條件

  4. 選擇一個或多個要查詢的日誌群組,或選擇 Browse log groups (瀏覽日誌群組)。

  5. 選取您的日誌群組。

  6. (選用) 選擇查詢的時間範圍。

  7. 將以下查詢貼到查詢編輯器中。

    fields @timestamp, @message
    | parse @message '{"name":"User-Agent","value":"*"}' as userAgent
    | filter @message like 'awswaf:managed:aws:bot-control'
    | parse @message '"labels":[*]' as Labels
    | parse @message '"httpRequest":{"clientIp":"*","country":"*"' as IP, Country
    | parse @message '"terminatingRule":{"ruleId":"*","action":"*"' as ruleID, action
    | filter action = "BLOCK"
    | display @timestamp, userAgent, IP, Country, ruleID, action, terminatingRuleId, Labels
    | sort by @timestamp DESC
    | limit 100
  8. (選用) 若要從特定 IP 位址尋找機器人,請將下列篩選器新增至上述查詢:

    | filter IP = '10.0.0.0/8'

    **注意:**將 10.0.0.0/8 替換為您的 IP 位址。

  9. (選用) 若要尋找具有特定使用者代理程式值的機器人,請將以下篩選器新增至上述查詢:

    | filter userAgent like 'user-agent value'

    **注意:**將 user-agent 值替換為您的使用者代理程式值。

  10. 若要查看整個日誌訊息,請展開日誌項目。

  11. 若要尋找封鎖合法機器人請求的規則,請搜尋 terminatingRuleIdruleGroupList.x.terminatingRule.ruleId。這些欄位附帶的標籤顯示了請求遭封鎖的原因。在以下範例中,ruleGroupList.0.terminatingRule.ruleIDCategoryMonitoring 而遭封鎖:

httpSourceName                             ALB
labels.0.name                              awswaf:managed:token:absent
labels.1.name                              awswaf:managed:aws:bot-control:bot:name:pingdom
labels.2.name                              awswaf:managed:aws:bot-control:bot:unverified
labels.3.name                              awswaf:managed:aws:bot-control:bot:category:monitoring
labels.4.name                              awswaf:managed:captcha:absent
ruleGroupList.0.customerConfig.0.name      InspectionLevel
ruleGroupList.0.customerConfig.0.value     COMMON
ruleGroupList.0.customerConfig.1.name      EnableMachineLearning
ruleGroupList.0.customerConfig.1.value     null
ruleGroupList.0.ruleGroupId                AWS#AWSManagedRulesBotControlRuleSet
ruleGroupList.0.terminatingRule.action     BLOCK
ruleGroupList.0.terminatingRule.ruleId     CategoryMonitoring
terminatingRuleId                          AWS-AWSManagedRulesBotControlRuleSet
terminatingRuleType                        MANAGED_RULE_GROUP

一個請求可以附加多個標籤。在這些指示中,只有與封鎖規則相關的請求才具有參考價值。

將封鎖請求的機器人控制功能規則設定為計數

請完成下列步驟:

  1. 開啟 AWS WAF console (AWS WAF 主控台)。
  2. 在導覽窗格中,選擇 AWS WAF
  3. 選擇 Resources & protection packs (資源和保護套件)。
  4. 找到您的保護套件,然後選擇 Rules (規則) 旁的 View and edit (檢視和編輯)。
  5. 在右側窗格中,選取 AWSManagedRulesBotControlRuleSet 規則群組。
  6. Rule Overrides (規則覆寫) 下,將封鎖規則的動作改為 Count (計數)。
  7. 選擇 Save Rule (儲存規則)。

**注意:**AWS WAF 會驗證符合規則的請求,並將其標籤附加到該請求,但不會將其封鎖。

為所有相符的請求 (除了您想允許的機器人) 建立自訂規則

若要封鎖不合法的請求,請建立一個規則,配對標籤以允許具有特定特徵的請求,並封鎖其他請求。例如:

Name: name of the rule
 IF (Statement 1):the request contains the label
   AND
NOT
 IF (Statement 2):the request contains the label(enter the bot label you want to allow)
Then Action: Block

**注意:**如果封鎖的規則具有 awswaf:managed:aws:bot-control:bot:category 標籤,則機器人控制功能會將另一個標籤附加到機器人名稱。您可以使用此機器人名稱標籤來識別合法機器人。否則,請使用 user-agent 標頭來識別合法機器人。

若要建立自訂規則,請完成以下步驟:

  1. 開啟 AWS WAF console (AWS WAF 主控台)。
  2. 在導覽窗格中,選擇 AWS WAF
  3. 選擇 Resources & protection packs (資源和保護套件)。
  4. 找到您的保護套件,然後選擇 Rules (規則) 旁的 View and edit (檢視和編輯)。
  5. 在右側窗格中,選擇 Add rules (新增規則)。
    選擇 Custom rule (自訂規則),然後選取 Next (下一步)。
    再次選擇 Custom rule (自訂規則),然後選取 Next (下一步)。
  6. 將規則 Action (動作) 更新為 BLOCK (封鎖)。
  7. 輸入您的規則名稱
  8. If a request (如果請求) 中,展開下拉式功能表並選擇 matches all the statements (AND) (符合所有陳述式 (AND))。
  9. Statement 1 (陳述式 1) 中,完成以下內容:
    Inspect (檢查) 中,選擇 Has a label (有標籤)。
    Statement (陳述式),選擇 Label (標籤)。
    Match key (配對鍵),選取 awswaf:managed:aws:bot-control:bot:category: category-name
    **注意:**將 category-name 替換為您的規則類別名稱。
  10. Statement 2 (陳述式 2) 中,完成以下內容:
    Inspect (檢查),選擇 Has a label (有標籤)。在 Statement (陳述式),選擇 Label (標籤)。
    Match key (配對鍵),選取 awswaf:managed:aws:bot-control:bot:name: bot-name
    **注意:**將 bot-name 替換為您的機器人名稱。
    展開 Rule configuration (規則組態),在 Negate statement (NOT) (否定陳述式 (NOT)) 下,選取 Negate statement results (否定陳述式結果)。
  11. 選擇 Create rule (建立規則)。
  12. 若要設定規則優先順序,請按一下右側窗格中的 Edit Rule Order (編輯規則順序),然後將規則拖曳到機器人控制功能規則下方。
  13. 選擇 Save Rule order (儲存規則順序)。

以下範例是一個自訂規則,其使用標籤允許合法機器人並封鎖所有其他機器人:

{
  "Name": "expection-rule",
  "Priority": 2,
  "Statement": {
    "AndStatement": {
      "Statements": [
        {
          "LabelMatchStatement": {
            "Scope": "LABEL",
            "Key": "awswaf:managed:aws:bot-control:bot:category:monitoring"
          }
        },
        {
          "NotStatement": {
            "Statement": {
              "LabelMatchStatement": {
                "Scope": "LABEL",
                "Key": "awswaf:managed:aws:bot-control:bot:name:pingdom"
              }
            }
          }
        }
      ]
    }
  },
  "Action": {
    "Block": {}
  },
  "VisibilityConfig": {
    "SampledRequestsEnabled": true,
    "CloudWatchMetricsEnabled": true,
    "MetricName": "expection-rule"
  }
}

驗證 AWS WAF 是否允許合法機器人流量

若要驗證 AWS WAF 是否允許合法的機器人追蹤,請再次查看 AWS WAF 日誌。如果機器人仍然遭封鎖,則使用附加規則重複上述程序。有時候,會有多個規則封鎖合法流量。在這種情況下,您必須多次重複上述程序。

相關資訊

機器人控制功能範例: 允許遭封鎖的特定機器人

機器人控制功能範例: 為遭封鎖的使用者代理程式建立例外狀況

保護套件或 Web ACL 流量的日誌欄位

管理規則群組行為

AWS 官方已更新 1 年前