Share via


設定 Web 應用程式防火牆排除清單

Azure Front Door 中的 Azure Web 應用程式防火牆有時可能會封鎖合法的要求。 您可以在調整 Web 應用程式防火牆 (WAF) 期間設定 WAF 以允許應用程式的要求。 WAF 排除清單可讓您從 WAF 評估略過特定要求屬性。 要求的剩餘部分會評估為正常。 如需排除清單的詳細資訊,請參閱 具有 Azure Front Door 排除清單的 Web 應用程式防火牆

您可以使用 Azure PowerShellAzure CLIREST API、Bicep、ARM 範本和 Azure 入口網站來設定排除清單。

案例

假設您已建立 API。 您的用戶端會將要求傳送至 API,其中包括名稱類似 useriduser-id 的標頭。

微調 WAF 時,您注意到某些合法的要求已遭到封鎖,因為使用者標頭包括 WAF 偵測為 SQL 插入式攻擊的字元序列。 具體而言,規則識別碼 942230 會偵測到要求標頭並封鎖該要求。 規則 942230 是 SQLI 規則群組的一部分。

您決定建立排除項目以允許這些合法要求通過,而不讓 WAF 加以封鎖。

建立排除項目

  1. 開啟您的 Azure Front Door WAF 原則。

  2. 選取 [受控規則]>管理排除項目

    Screenshot that shows the Azure portal showing the WAF policy's Managed rules page, with the Manage exclusions button highlighted.

  3. 選取 [新增]。

    Screenshot that shows the Azure portal with the exclusion list Add button.

  4. 設定排除項目的 [適用於] 區段:

    欄位
    規則集 Microsoft_DefaultRuleSet_2.0
    規則群組 SQLI
    規則 942230 偵測到條件式 SQL 插入嘗試
  5. 設定排除比對條件:

    欄位
    比對變數 要求標頭名稱
    運算子 開始於
    選取器 User
  6. 檢閱排除項目,其看起來應該類似下列螢幕擷取畫面:

    Screenshot that shows the Azure portal showing the exclusion configuration.

    此排除項目適用於開頭為 user 單字的任何要求標頭。 比對條件不區分大小寫,因此排除項目也會涵蓋開頭為 User 的標頭。 如果 WAF 規則 942230 在這些標頭值中偵測到風險,其會忽略該標頭並繼續進行。

  7. 選取 [儲存]。

定義排除選取器

使用 New-AzFrontDoorWafManagedRuleExclusionObject Cmdlet 來定義新的排除項目選取器。

下列範例會識別開頭為 user 單字的要求標頭。 比對條件不區分大小寫,因此排除項目也會涵蓋開頭為 User 的標頭。

$exclusionSelector = New-AzFrontDoorWafManagedRuleExclusionObject `
  -Variable RequestHeaderNames `
  -Operator StartsWith `
  -Selector 'user'

定義個別規則排除項目

使用 New-AzFrontDoorWafManagedRuleOverrideObject Cmdlet 來定義新的個別規則排除項目,其中包括您在上一個步驟中建立的選取器。

下列範例會建立規則識別碼 942230 的排除項目。

$exclusion = New-AzFrontDoorWafManagedRuleOverrideObject `
  -RuleId '942230' `
  -Exclusion $exclusionSelector

將排除項目套用至規則群組

使用 New-AzFrontDoorWafRuleGroupOverrideObject Cmdlet 來建立規則群組覆寫,其會將排除項目套用至適當的規則群組。

下列範例使用 SQLI 規則群組,因為該群組包含規則識別碼 942230。

$ruleGroupOverride = New-AzFrontDoorWafRuleGroupOverrideObject `
  -RuleGroupName 'SQLI' `
  -ManagedRuleOverride $exclusion

設定受控規則集

使用 New-AzFrontDoorWafManagedRuleObject Cmdlet 來設定受控規則集,包括您在上一個步驟中建立的規則群組覆寫。

下列範例使用規則群組覆寫及其排除項目來設定 DRS 2.0 規則集。

$managedRuleSet = New-AzFrontDoorWafManagedRuleObject `
  -Type 'Microsoft_DefaultRuleSet' `
  -Version '2.0' `
  -Action Block `
  -RuleGroupOverride $ruleGroupOverride

將受控規則集設定套用至 WAF 設定檔

使用 Update-AzFrontDoorWafPolicy Cmdlet 更新 WAF 原則,以包括您先前建立的設定。 請確定您針對您自己的環境使用正確的資源群組名稱和 WAF 原則名稱。

Update-AzFrontDoorWafPolicy `
  -ResourceGroupName 'FrontDoorWafPolicy' `
  -Name 'WafPolicy'
  -ManagedRule $managedRuleSet

建立排除項目

使用 az network front-door waf-policy managed-rules exclusion add 命令來更新 WAF 原則以新增新的排除項目。

該排除項目會識別開頭為 user 單字的要求標頭。 比對條件不區分大小寫,因此排除項目也會涵蓋開頭為 User 的標頭。

請確定您針對您自己的環境使用正確的資源群組名稱和 WAF 原則名稱。

az network front-door waf-policy managed-rules exclusion add \
  --resource-group FrontDoorWafPolicy \
  --policy-name WafPolicy \
  --type Microsoft_DefaultRuleSet \
  --rule-group-id SQLI \
  --rule-id 942230 \
  --match-variable RequestHeaderNames \
  --operator StartsWith \
  --value user

範例 Bicep 檔案

下列範例 Bicep 檔案示範如何:

  • 開啟 Azure Front Door WAF 原則。
  • 啟用 DRS 2.0 規則集。
  • 設定規則 942230 的排除項目,該規則存在於 SQLI 規則群組內。 此排除項目適用於開頭為 user 單字的任何要求標頭。 比對條件不區分大小寫,因此排除項目也會涵蓋開頭為 User 的標頭。 如果 WAF 規則 942230 在這些標頭值中偵測到風險,其會忽略該標頭並繼續進行。
param wafPolicyName string = 'WafPolicy'

@description('The mode that the WAF should be deployed using. In "Prevention" mode, the WAF will block requests it detects as malicious. In "Detection" mode, the WAF will not block requests and will simply log the request.')
@allowed([
  'Detection'
  'Prevention'
])
param wafMode string = 'Prevention'

resource wafPolicy 'Microsoft.Network/frontDoorWebApplicationFirewallPolicies@2022-05-01' = {
  name: wafPolicyName
  location: 'Global'
  sku: {
    name: 'Premium_AzureFrontDoor'
  }
  properties: {
    policySettings: {
      enabledState: 'Enabled'
      mode: wafMode
    }
    managedRules: {
      managedRuleSets: [
        {
          ruleSetType: 'Microsoft_DefaultRuleSet'
          ruleSetVersion: '2.0'
          ruleSetAction: 'Block'
          ruleGroupOverrides: [
            {
              ruleGroupName: 'SQLI'
              rules: [
                {
                  ruleId: '942230'
                  enabledState: 'Enabled'
                  action: 'AnomalyScoring'
                  exclusions: [
                    {
                      matchVariable: 'RequestHeaderNames'
                      selectorMatchOperator: 'StartsWith'
                      selector: 'user'
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  }
}

下一步

深入了解 Azure Front Door