共用方式為


使用 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 - 封鎖所有以 URI "foo" 或 "bar" 為目標的國家/地區 ("x" 和 "y" 除外) 的流量

設想有一個案例,您需要使用 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" 除外) 的流量

使用 geomatch 自訂規則時,避免將規則動作設為 allow 並指定要排除的國家/地區清單。 例如,如果您想要允許來自美國以外的所有國家/地區 (您懷疑在這些國家/地區存在惡意活動) 的流量,則此方法可能會產生非預期的後果。 它可能會允許來自未經驗證或不安全的國家/地區或安全性標準低或無安全性標準的國家/地區的流量,進而讓您的 Web 應用程式暴露於潛在的弱點或攻擊。 針對除美國以外的所有國家/地區使用 allow 動作,以向 WAF 指示停止根據受控規則集處理要求承載。 一旦處理具有 allow 的自訂規則,所有規則評估都會停止,進而讓該應用程式暴露於不必要的惡意攻擊。

請改用更嚴格的特定規則動作 (例如封鎖),並指定允許具有否定條件的國家/地區清單。 這可確保只有來自受信任且經過驗證的來源的流量才能存取您的 Web 應用程式,同時封鎖任何可疑或不必要的流量。

下一步