你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure WAF geomatch 自定义规则增强网络安全性

Web 应用程序防火墙 (WAF) 是一种帮助保护 Web 应用程序免受有害攻击的重要工具。 防火墙可以使用预设规则和自定义规则筛选、监视和阻止 Web 流量。 你可以创建自己的规则,使 WAF 检查它收到的每个请求。 自定义规则比托管规则集的优先级要高,并且首先进行检查。

Azure Web 应用程序防火墙最强大的功能之一是 geomatch 自定义规则。 利用这些规则,可以将 Web 请求与其来源的地理位置进行匹配。 你可能希望阻止来自某些已知存在有害活动的位置的请求,或者你可能希望允许来自对业务非常重要的位置的请求。 Geomatch 自定义规则还可以根据使用 Web 应用程序的人员的位置限制对 Web 应用程序的访问,从而帮助你遵守数据主权和隐私法律。

使用 geomatch 自定义规则时,请明智地使用优先级参数,以避免不必要的处理或冲突。 Azure WAF 按照优先级参数确定的顺序评估规则,参数数值范围为 1 到 100,值越低表示优先级越高。 优先级在所有自定义规则中必须唯一。 为 Web 应用程序安全性的重要或特定规则分配较高的优先级,为不太重要或一般的规则分配较低的优先级。 这样可以确保 WAF 对 Web 流量应用最合适的操作。 例如,识别显式 URI 路径的方案最具体,其优先级规则应高于其他类型的模式。 这样可以保护应用程序上具有最高优先级的关键路径,同时允许跨其他自定义规则或托管规则集评估更多通用流量。

为了让使用现在时和主动语态的方法受众更容易理解此段落,可以将其重写如下:

在将规则应用于生产之前,请务必对其进行测试,并定期监视其性能和影响。 通过遵循这些最佳做法,可以利用 geomatch 自定义规则的强大功能增强 Web 应用程序的安全性。

本文介绍 Azure WAF geomatch 自定义规则,并演示如何使用 Azure 门户、Bicep 和 Azure PowerShell 创建和管理这些规则。

Geomatch 自定义规则模式

借助 geomatch 自定义规则,可以实现各种安全目标,例如阻止来自高风险区域的请求以及允许来自受信任位置的请求。 它们在缓解分布式拒绝服务 (DDoS) 攻击方面特别有效,这种攻击会尝试利用来自各种来源的大量请求淹没 Web 应用程序。 使用 geomatch 自定义规则,可以及时查明并阻止产生最多 DDoS 流量的区域,同时仍向合法用户授予访问权限。 本文介绍各种自定义规则模式,这些模式可用于使用 geomatch 自定义规则优化 Azure WAF。

方案 1 - 阻止来自除“x”以外的所有国家/地区的流量

当你的目标是阻止来自所有国家/地区(除了一个国家/地区)的流量时,geomatch 自定义规则非常有用。 例如,如果 Web 应用程序专门面向美国用户,则可以制定一个 geomatch 自定义规则来阻止所有来自非美国的请求。 此策略可有效地尽量减少 Web 应用程序的攻击面,并阻止来自其他区域的未经授权的访问。 此特定方法采用否定条件来辅助这种流量模式。 若要创建阻止来自美国以外所有国家/地区的流量的 geomatch 自定义规则,请参阅以下门户、Bicep 和 PowerShell 示例:

门户示例 - 应用程序网关

Screenshot showing the Application Gateway WAF add custom rule screen.

门户示例 - Front Door

Screenshot showing the Front Door WAF add custom rule screen.

注意

请注意,在 Azure Front Door WAF 上,使用 SocketAddr 作为匹配变量,而不是 RemoteAddrRemoteAddr 变量是通常通过 X-Forwarded-For 请求标头发送的原始客户端 IP 地址。 SocketAddr 变量是 WAF 发现的源 IP 地址。

Bicep 示例 - 应用程序网关

properties: {
    customRules: [
      {
        name: 'GeoRule1'
        priority: 10
        ruleType: 'MatchRule'
        action: 'Block'
        matchConditions: [
          {
            matchVariables: [
              {
                variableName: 'RemoteAddr'
              }
            ]
            operator: 'GeoMatch'
            negationConditon: true
            matchValues: [
              'US'
            ]
            transforms: []
          }
        ]
        state: 'Enabled'
      }

Bicep 示例 - Front Door

properties: {
    customRules: {
      rules: [
        {
          name: 'GeoRule1'
          enabledState: 'Enabled'
          priority: 10
          ruleType: 'MatchRule'
          matchConditions: [
            {
              matchVariable: 'SocketAddr'
              operator: 'GeoMatch'
              negateCondition: true
              matchValue: [
                'US'
              ]
              transforms: []
            }
          ]
          action: 'Block'
        }

Azure PowerShell 示例 - 应用程序网关

$RGname = "rg-waf "
$policyName = "waf-pol"
$variable = New-AzApplicationGatewayFirewallMatchVariable -VariableName RemoteAddr
$condition = New-AzApplicationGatewayFirewallCondition -MatchVariable $variable -Operator GeoMatch -MatchValue "US" -NegationCondition $true
$rule = New-AzApplicationGatewayFirewallCustomRule -Name GeoRule1 -Priority 10 -RuleType MatchRule -MatchCondition $condition -Action Block
$policy = Get-AzApplicationGatewayFirewallPolicy -Name $policyName -ResourceGroupName $RGname
$policy.CustomRules.Add($rule)
Set-AzApplicationGatewayFirewallPolicy -InputObject $policy

Azure PowerShell 示例 - Front Door

$RGname = "rg-waf"
$policyName = "wafafdpol"
$matchCondition = New-AzFrontDoorWafMatchConditionObject -MatchVariable SocketAddr -OperatorProperty GeoMatch -MatchValue "US" -NegateCondition $true
$customRuleObject = New-AzFrontDoorWafCustomRuleObject -Name "GeoRule1" -RuleType MatchRule -MatchCondition $matchCondition -Action Block -Priority 10
$afdWAFPolicy= Get-AzFrontDoorWafPolicy -Name $policyName -ResourceGroupName $RGname
Update-AzFrontDoorWafPolicy -InputObject $afdWAFPolicy -Customrule $customRuleObject

方案 2 - 阻止来自除“x”和“y”以外的所有国家/地区的针对 URI“foo”或“bar”的流量

假设需要使用 geomatch 自定义规则来阻止来自所有国家/地区(两个或更多特定国家/地区除外)的针对特定 URI 的流量。 假设 Web 应用程序具有仅供美国和加拿大用户使用的特定 URI 路径。 在这种情况下,创建一个 geomatch 自定义规则,以阻止所有不是来自这些国家/地区的请求。

此模式通过托管规则集处理来自美国和加拿大的请求有效负载,捕获任何恶意攻击,同时阻止来自所有其他国家/地区的请求。 此方法可确保只有目标受众可以访问 Web 应用程序,从而避免来自其他区域的不需要的流量。

若要尽量减少潜在的误报,请在列表中包含国家/地区代码 ZZ,以捕获尚未映射到 Azure 数据集中的国家/地区的 IP 地址。 此方法对地理位置类型使用否定条件,对 URI 匹配使用非否定条件。

若要创建阻止来自除美国和加拿大以外的所有国家/地区到指定 URI 的流量的 geomatch 自定义规则,请参阅下面提供的门户、Bicep 和 Azure PowerShell 示例。

门户示例 - 应用程序网关

Screenshot showing add custom rule for Application Gateway.

门户示例 - Front Door

Screenshot showing add custom rule for Front Door.

Bicep 示例 - 应用程序网关

properties: {
    customRules: [
      {
        name: 'GeoRule2'
        priority: 11
        ruleType: 'MatchRule'
        action: 'Block'
        matchConditions: [
          {
            matchVariables: [
              {
                variableName: 'RemoteAddr'
              }
            ]
            operator: 'GeoMatch'
            negationConditon: true
            matchValues: [
              'US'
              'CA'
            ]
            transforms: []
          }
          {
            matchVariables: [
              {
                variableName: 'RequestUri'
              }
            ]
            operator: 'Contains'
            negationConditon: false
            matchValues: [
              '/foo'
              '/bar'
            ]
            transforms: []
          }
        ]
        state: 'Enabled'
      }

Bicep 示例 - Front Door

properties: {
    customRules: {
      rules: [
        {
          name: 'GeoRule2'
          enabledState: 'Enabled'
          priority: 11
          ruleType: 'MatchRule'
          matchConditions: [
            {
              matchVariable: 'SocketAddr'
              operator: 'GeoMatch'
              negateCondition: true
              matchValue: [
                'US'
                'CA'
              ]
              transforms: []
            }
            {
              matchVariable: 'RequestUri'
              operator: 'Contains'
              negateCondition: false
              matchValue: [
                '/foo'
                '/bar'
              ]
              transforms: []
            }
          ]
          action: 'Block'
        }

Azure PowerShell 示例 - 应用程序网关

$RGname = "rg-waf "
$policyName = "waf-pol"
$variable1a = New-AzApplicationGatewayFirewallMatchVariable -VariableName RemoteAddr
$condition1a = New-AzApplicationGatewayFirewallCondition -MatchVariable $variable1a -Operator GeoMatch -MatchValue @(“US”, “CA”) -NegationCondition $true
$variable1b = New-AzApplicationGatewayFirewallMatchVariable -VariableName RequestUri
$condition1b = New-AzApplicationGatewayFirewallCondition -MatchVariable $variable1b -Operator Contains -MatchValue @(“/foo”, “/bar”) -NegationCondition $false
$rule1 = New-AzApplicationGatewayFirewallCustomRule -Name GeoRule2 -Priority 11 -RuleType MatchRule -MatchCondition $condition1a, $condition1b -Action Block
$policy = Get-AzApplicationGatewayFirewallPolicy -Name $policyName -ResourceGroupName $RGname
$policy.CustomRules.Add($rule1)
Set-AzApplicationGatewayFirewallPolicy -InputObject $policy

Azure PowerShell 示例 - Front Door

$RGname = "rg-waf"
$policyName = "wafafdpol"
$matchCondition1a = New-AzFrontDoorWafMatchConditionObject -MatchVariable SocketAddr -OperatorProperty GeoMatch -MatchValue @(“US”, "CA") -NegateCondition $true
$matchCondition1b = New-AzFrontDoorWafMatchConditionObject -MatchVariable RequestUri -OperatorProperty Contains -MatchValue @(“/foo”, “/bar”) -NegateCondition $false
$customRuleObject1 = New-AzFrontDoorWafCustomRuleObject -Name "GeoRule2" -RuleType MatchRule -MatchCondition $matchCondition1a, $matchCondition1b -Action Block -Priority 11
$afdWAFPolicy= Get-AzFrontDoorWafPolicy -Name $policyName -ResourceGroupName $RGname
Update-AzFrontDoorWafPolicy -InputObject $afdWAFPolicy -Customrule $customRuleObject1

方案 3 - 阻止来自国家/地区“x”的流量

可以使用 geomatch 自定义规则阻止来自特定国家/地区的流量。 例如,如果 Web 应用程序收到来自国家/地区“x”的许多恶意请求,请创建一个 geomatch 自定义规则来阻止来自该国家/地区的所有请求。 这样可以保护 Web 应用程序免受潜在攻击,并减少资源负载。 应用此模式来阻止多个恶意国家/地区。 此方法需要流量模式的匹配条件。 若要阻止来自国家/地区“x”的流量,请参阅以下门户、Bicep 和 Azure PowerShell 示例。

门户示例 - 应用程序网关

Screenshot showing the application gateway add custom rule screen.

门户示例 - Front Door

Screenshot showing the front door add custom rule screen.

Bicep 示例 - 应用程序网关

properties: {
    customRules: [
      {
        name: 'GeoRule3'
        priority: 12
        ruleType: 'MatchRule'
        action: 'Block'
        matchConditions: [
          {
            matchVariables: [
              {
                variableName: 'RemoteAddr'
              }
            ]
            operator: 'GeoMatch'
            negationConditon: false
            matchValues: [
              'US'
            ]
            transforms: []
          }
        ]
        state: 'Enabled'
      }

Bicep 示例 - Front Door

properties: {
    customRules: {
      rules: [
        {
          name: 'GeoRule3'
          enabledState: 'Enabled'
          priority: 12
          ruleType: 'MatchRule'
          matchConditions: [
            {
              matchVariable: 'SocketAddr'
              operator: 'GeoMatch'
              negateCondition: false
              matchValue: [
                'US'
              ]
              transforms: []
            }
          ]
          action: 'Block'
        }

Azure PowerShell 示例 - 应用程序网关

$RGname = "rg-waf "
$policyName = "waf-pol"
$variable2 = New-AzApplicationGatewayFirewallMatchVariable -VariableName RemoteAddr
$condition2 = New-AzApplicationGatewayFirewallCondition -MatchVariable $variable2 -Operator GeoMatch -MatchValue "US" -NegationCondition $false
$rule2 = New-AzApplicationGatewayFirewallCustomRule -Name GeoRule3 -Priority 12 -RuleType MatchRule -MatchCondition $condition2 -Action Block
$policy = Get-AzApplicationGatewayFirewallPolicy -Name $policyName -ResourceGroupName $RGname
$policy.CustomRules.Add($rule2)
Set-AzApplicationGatewayFirewallPolicy -InputObject $policy

Azure PowerShell 示例 - Front Door

$RGname = "rg-waf"
$policyName = "wafafdpol"
$matchCondition2 = New-AzFrontDoorWafMatchConditionObject -MatchVariable SocketAddr -OperatorProperty GeoMatch -MatchValue "US" -NegateCondition $false
$customRuleObject2 = New-AzFrontDoorWafCustomRuleObject -Name "GeoRule3" -RuleType MatchRule -MatchCondition $matchCondition2 -Action Block -Priority 12
$afdWAFPolicy= Get-AzFrontDoorWafPolicy -Name $policyName -ResourceGroupName $RGname
Update-AzFrontDoorWafPolicy -InputObject $afdWAFPolicy -Customrule $customRuleObject2

Geomatch 自定义规则反模式

使用 geomatch 自定义规则时避免反模式,例如将自定义规则操作设置为 allow 而不是 block。 这可能会产生意外后果,例如允许流量绕过 WAF 并可能使 Web 应用程序面临其他威胁。

不要使用 allow 操作,而是使用带有否定条件的 block 操作,如前面的模式所示。 这样可以确保仅允许来自所需国家/地区的流量,并且 WAF 会阻止所有其他流量。

方案 4 - 允许来自国家/地区“x”的流量

避免设置 geomatch 自定义规则以允许来自特定国家/地区的流量。 例如,如果由于你在美国拥有庞大的客户群而希望允许来自美国的流量,则使用 allow 操作和 United States 值创建自定义规则可能是解决方案。 但是,此规则允许来自美国的所有流量,无论其是否具有恶意负载,因为 allow 操作会绕过托管规则集的进一步规则处理。 此外,WAF 仍处理来自所有其他国家/地区的流量,消耗资源。 这会使 Web 应用程序暴露于来自美国的恶意请求,否则 WAF 会阻止这些请求。

方案 5 - 允许来自除“x”以外的所有国家/地区的流量

请避免将规则操作设置为 allow,并指定在使用 geomatch 自定义规则时要排除的国家/地区列表。 例如,如果想允许来自除美国以外的所有国家/地区的流量,如果怀疑恶意活动,此方法可能会产生意外的后果。 它可能允许来自未经验证或不安全的国家/地区或安全标准低或无安全标准的国家/地区的流量,从而使 Web 应用程序面临潜在的漏洞或攻击。 对除美国以外的所有国家/地区使用 allow 操作,指示 WAF 停止根据托管规则集处理请求有效负载。 处理具有 allow 的自定义规则后,所有规则评估都会停止,从而导致应用程序受到不必要的恶意攻击。

请改用限制更高且更具体的规则操作(例如阻止),并指定使用否定条件允许的国家/地区列表。 这样可以确保只有来自受信任且经验证的来源的流量才能访问 Web 应用程序,同时阻止任何可疑或不需要的流量。

后续步骤