You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用WAF策略Create Or Update API新增自定义规则不覆盖原有规则?

如何在Azure WAF策略中新增自定义规则而不删除原有规则?

Azure的ApplicationGatewayWebApplicationFirewallPolicies_CreateOrUpdate API采用全量更新机制,提交请求时会用传入的配置完全覆盖现有策略,包括自定义规则。要实现新增规则且保留原有规则,需按以下步骤操作:

1. 获取现有WAF策略的完整配置

先调用Get接口拉取当前策略的所有配置,包括已存在的自定义规则:

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Network/ApplicationGatewayWebApplicationFirewallPolicies/{policyName}?api-version=2023-09-01

从响应结果中提取properties.customRules数组,这就是当前所有的自定义规则集合。

2. 合并新规则到现有规则列表

  • 确保新规则的priority值不与现有规则重复(WAF规则优先级唯一,数值越小优先级越高)
  • 将新的自定义规则对象添加到提取到的customRules数组中(可根据优先级排序插入对应位置,或直接追加到末尾)

3. 提交全量配置到CreateOrUpdate API

将合并后的customRules数组替换到请求体的对应位置,同时完整保留原有policySettings、managedRules等其他配置项(不能仅传递customRules,否则其他配置会被清空)。示例请求体如下:

{
  "location": "westus",
  "properties": {
    "customRules": [
      // 原有4条自定义规则(示例)
      {
        "name": "ExistingRule1",
        "priority": 10,
        "ruleType": "MatchRule",
        "action": "Allow",
        "matchConditions": [/* 原有规则匹配条件 */],
        "state": "Enabled"
      },
      {
        "name": "ExistingRule2",
        "priority": 20,
        "ruleType": "MatchRule",
        "action": "Block",
        "matchConditions": [/* 原有规则匹配条件 */],
        "state": "Enabled"
      },
      // ...另外2条原有规则
      // 新增的规则
      {
        "name": "BlockSpec",
        "priority": 85,
        "ruleType": "MatchRule",
        "action": "Block",
        "matchConditions": [
          {
            "matchVariables": [{"variableName": "RemoteAddr"}],
            "operator": "IPMatch",
            "matchValues": ["100.60.60.62"],
            "transforms": []
          }
        ],
        "skippedManagedRuleSets": [],
        "state": "Enabled"
      }
    ],
    "policySettings": {
      "requestBodyCheck": true,
      "maxRequestBodySizeInKb": 128,
      "fileUploadLimitInMb": 100,
      "state": "Enabled",
      "mode": "Prevention",
      "requestBodyInspectLimitInKB": 128,
      "fileUploadEnforcement": true,
      "requestBodyEnforcement": true
    },
    "managedRules": {
      "managedRuleSets": [
        {
          "ruleSetType": "OWASP",
          "ruleSetVersion": "3.2",
          "ruleGroupOverrides": []
        }
      ],
      "exclusions": []
    }
  }
}

4. 更简便的替代方案

如果使用Azure CLI或PowerShell,可以直接用专门的规则创建命令,这类命令会自动处理增量添加,无需手动合并配置:

Azure CLI示例

az network application-gateway waf-policy rule create \
  --resource-group <你的资源组名称> \
  --policy-name <WAF策略名称> \
  --name BlockSpec \
  --priority 85 \
  --rule-type MatchRule \
  --action Block \
  --match-variable RemoteAddr \
  --operator IPMatch \
  --match-values 100.60.60.62

内容的提问来源于stack exchange,提问作者ThePoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 08:27:25