使用 PowerShell 自定义 Web 应用程序防火墙规则Customize Web Application Firewall rules using PowerShell

Azure 应用程序网关 Web 应用程序防火墙 (WAF) 可为 Web 应用程序提供保护。The Azure Application Gateway Web Application Firewall (WAF) provides protection for web applications. 这些保护通过打开 Web 应用程序安全性项目 (OWASP) 核心规则集 (CRS) 来提供。These protections are provided by the Open Web Application Security Project (OWASP) Core Rule Set (CRS). 某些规则可能会导致误报,并会阻止实际流量。Some rules can cause false positives and block real traffic. 出于此原因,应用程序网关提供了自定义规则组和规则的功能。For this reason, Application Gateway provides the capability to customize rule groups and rules. 有关特定规则组和规则的详细信息,请参阅 Web 应用程序防火墙 CRS 规则组和规则列表For more information on the specific rule groups and rules, see List of Web Application Firewall CRS Rule groups and rules.

查看规则组和规则View rule groups and rules

以下代码示例演示了如何查看可在启用了 WAF 的应用程序网关上配置的规则和规则组。The following code examples show how to view rules and rule groups that are configurable on a WAF-enabled application gateway.

查看规则组View rule groups

以下示例演示了如何查看规则组:The following example shows how to view rule groups:

Get-AzApplicationGatewayAvailableWafRuleSets

以下输出截取自前一示例的响应:The following output is a truncated response from the preceding example:

OWASP (Ver. 3.0):

    General:
        Description:

        Rules:
            RuleId     Description
            ------     -----------
            200004     Possible Multipart Unmatched Boundary.

    REQUEST-911-METHOD-ENFORCEMENT:
        Description:

        Rules:
            RuleId     Description
            ------     -----------
            911011     Rule 911011
            911012     Rule 911012
            911100     Method is not allowed by policy
            911013     Rule 911013
            911014     Rule 911014
            911015     Rule 911015
            911016     Rule 911016
            911017     Rule 911017
            911018     Rule 911018

    REQUEST-913-SCANNER-DETECTION:
        Description:

        Rules:
            RuleId     Description
            ------     -----------
            913011     Rule 913011
            913012     Rule 913012
            913100     Found User-Agent associated with security scanner
            913110     Found request header associated with security scanner
            913120     Found request filename/argument associated with security scanner
            913013     Rule 913013
            913014     Rule 913014
            913101     Found User-Agent associated with scripting/generic HTTP client
            913102     Found User-Agent associated with web crawler/bot
            913015     Rule 913015
            913016     Rule 913016
            913017     Rule 913017
            913018     Rule 913018

            ...        ...

禁用规则Disable rules

以下示例在应用程序网关上禁用了规则 911011911012The following example disables rules 911011 and 911012 on an application gateway:

$disabledrules=New-AzApplicationGatewayFirewallDisabledRuleGroupConfig -RuleGroupName REQUEST-911-METHOD-ENFORCEMENT -Rules 911011,911012
Set-AzApplicationGatewayWebApplicationFirewallConfiguration -ApplicationGateway $gw -Enabled $true -FirewallMode Detection -RuleSetVersion 3.0 -RuleSetType OWASP -DisabledRuleGroups $disabledrules
Set-AzApplicationGateway -ApplicationGateway $gw

强制性规则Mandatory rules

以下列表包含导致 WAF 在防护模式下阻止请求的条件(在检测模式下,它们作为异常记录)。The following list contains conditions that cause the WAF to block the request while in Prevention Mode (in Detection Mode they are logged as exceptions). 无法配置或禁用这些规则:These can't be configured or disabled:

  • 除非关闭正文检查(XML、JSON、表单数据),否则无法分析请求正文会导致请求被阻止Failure to parse the request body results in the request being blocked, unless body inspection is turned off (XML, JSON, form data)
  • 请求正文(不带文件)数据长度大于配置的限制Request body (with no files) data length is larger than the configured limit
  • 请求正文(包括文件)大于限制Request body (including files) is larger than the limit
  • WAF 引擎发生内部错误An internal error happened in the WAF engine

CRS 3.x 特定:CRS 3.x specific:

  • 入站异常分数超出阈值Inbound anomaly score exceeded threshold

后续步骤Next steps

配置你禁用的规则后,可以了解如何查看 WAF 日志。After you configure your disabled rules, you can learn how to view your WAF logs. 有关详细信息,请参阅应用程序网关诊断For more information, see Application Gateway Diagnostics.