Membuat dan menggunakan aturan kustom Web Application Firewall v2 di Application Gateway

Web Application Firewall (WAF) v2 di Azure Application Gateway memberikan perlindungan untuk aplikasi web. Perlindungan ini diberikan oleh Open Web Application Security Project (OWASP) Core Rule Set (CRS). Dalam beberapa kasus, Anda mungkin perlu membuat aturan kustom Anda sendiri untuk memenuhi kebutuhan khusus Anda. Untuk informasi selengkapnya tentang aturan kustom WAF, lihat Ringkasan aturan firewall aplikasi web kustom.

Artikel ini menampilkan beberapa contoh aturan kustom yang bisa Anda buat dan gunakan dengan WAF v2. Untuk mempelajari cara menggunakan WAF dengan aturan kustom menggunakan Azure PowerShell, lihat Mengonfigurasi aturan kustom Web Application Firewall menggunakan Azure PowerShell.

Cuplikan JSON yang ditunjukkan dalam artikel ini berasal dari sumber daya ApplicationGatewayWebApplicationFirewallPolicies.

Catatan

Jika gateway aplikasi Anda tidak menggunakan tingkat WAF, opsi untuk meningkatkan gateway aplikasi ke tingkat WAF akan muncul di panel kanan.

Enable WAF

Contoh 1

Anda mengetahui bahwa ada bot bernama evilbot yang ingin diblokir agar tidak meng-crawl situs web Anda. Dalam hal ini, Anda memblokir evilbot User-Agent di header permintaan.

Logika: p

$variable = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RequestHeaders `
   -Selector User-Agent

$condition = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable `
   -Operator Contains `
   -MatchValue "evilbot" `
   -Transform Lowercase `
   -NegationCondition $False

$rule = New-AzApplicationGatewayFirewallCustomRule `
   -Name blockEvilBot `
   -Priority 2 `
   -RuleType MatchRule `
   -MatchCondition $condition `
   -Action Block `
   -State Enabled

Dan inilah JSON yang sesuai:

{
  "customRules": [
    {
      "name": "blockEvilBot",
      "priority": 2,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RequestHeaders",
              "selector": "User-Agent"
            }
          ],
          "operator": "Contains",
          "negationConditon": false,
          "matchValues": [
            "evilbot"
          ],
          "transforms": [
            "Lowercase"
          ]
        }
      ]
    }
  ]
}

Untuk melihat WAF yang diterapkan menggunakan aturan kustom ini, lihat Mengonfigurasi aturan kustom Web Application Firewall menggunakan Azure PowerShell.

Contoh 1a

Anda dapat mencapai hal yang sama menggunakan ekspresi reguler:

$variable = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RequestHeaders `
   -Selector User-Agent

$condition = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable `
   -Operator Regex `
   -MatchValue "evilbot" `
   -Transform Lowercase `
   -NegationCondition $False

$rule = New-AzApplicationGatewayFirewallCustomRule `
   -Name blockEvilBot `
   -Priority 2 `
   -RuleType MatchRule `
   -MatchCondition $condition `
   -Action Block `
   -State Enabled

Serta JSON yang sesuai:

{
  "customRules": [
    {
      "name": "blockEvilBot",
      "priority": 2,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RequestHeaders",
              "selector": "User-Agent"
            }
          ],
          "operator": "Regex",
          "negationConditon": false,
          "matchValues": [
            "evilbot"
          ],
          "transforms": [
            "Lowercase"
          ]
        }
      ]
    }
  ]
}

Contoh 2

Anda hanya ingin mengizinkan lalu lintas dari Amerika Serikat menggunakan operator GeoMatch dan masih menerapkan aturan terkelola:

$variable = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RemoteAddr `

$condition = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable `
   -Operator GeoMatch `
   -MatchValue "US" `
   -Transform Lowercase `
   -NegationCondition $True

$rule = New-AzApplicationGatewayFirewallCustomRule `
   -Name "allowUS" `
   -Priority 2 `
   -RuleType MatchRule `
   -MatchCondition $condition `
   -Action Block `
   -State Enabled

Serta JSON yang sesuai:

{
  "customRules": [
    {
      "name": "allowUS",
      "priority": 2,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RemoteAddr"
            }
          ],
          "operator": "GeoMatch",
          "negationConditon": true,
          "matchValues": [
            "US"
          ],
          "transforms": [
            "Lowercase"
          ]
        }
      ]
    }
  ]
}

Contoh 3

Anda ingin memblokir semua permintaan dari alamat IP dalam rentang 198.168.5.0/24.

Dalam contoh ini, Anda memblokir semua lalu lintas yang berasal dari rentang alamat IP. Nama aturan adalah myrule1 dan prioritas diatur ke 10.

Logika: p

$variable1 = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RemoteAddr

$condition1 = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable1 `
   -Operator IPMatch `
   -MatchValue "192.168.5.0/24" `
   -NegationCondition $False

$rule = New-AzApplicationGatewayFirewallCustomRule `
   -Name myrule1 `
   -Priority 10 `
   -RuleType MatchRule `
   -MatchCondition $condition1 `
   -Action Block `
   -State Enabled

Berikut JSON yang sesuai:

{
  "customRules": [
    {
      "name": "myrule1",
      "priority": 10,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RemoteAddr"
            }
          ],
          "operator": "IPMatch",
          "negationConditon": false,
          "matchValues": [
            "192.168.5.0/24"
          ],
          "transforms": []
        }
      ]
    }
  ]
}

Aturan CRS terkait: SecRule REMOTE_ADDR "@ipMatch 192.168.5.0/24" "id:7001,deny"

Contoh 4

Untuk contoh ini, Anda ingin memblokir User-Agent evilbot, dan lalu lintas dalam rentang 192.168.5.0/24. Untuk mencapai tindakan ini, Anda dapat membuat dua kondisi kecocokan terpisah, dan menempatkannya dalam aturan yang sama. Konfigurasi ini memastikan bahwa jika evilbot di header User-Agent dan alamat IP dari rentang 192.168.5.0/24 cocok, maka permintaan diblokir.

Logika: p dan q

$variable1 = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RemoteAddr

 $variable2 = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RequestHeaders `
   -Selector User-Agent

$condition1 = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable1 `
   -Operator IPMatch `
   -MatchValue "192.168.5.0/24" `
   -NegationCondition $False

$condition2 = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable2 `
   -Operator Contains `
   -MatchValue "evilbot" `
   -Transform Lowercase `
   -NegationCondition $False

 $rule = New-AzApplicationGatewayFirewallCustomRule `
   -Name myrule `
   -Priority 10 `
   -RuleType MatchRule `
   -MatchCondition $condition1, $condition2 `
   -Action Block `
   -State Enabled

Berikut JSON yang sesuai:

{
  "customRules": [
    {
      "name": "myrule",
      "priority": 10,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RemoteAddr"
            }
          ],
          "operator": "IPMatch",
          "negationConditon": false,
          "matchValues": [
            "192.168.5.0/24"
          ],
          "transforms": []
        },
        {
          "matchVariables": [
            {
              "variableName": "RequestHeaders",
              "selector": "User-Agent"
            }
          ],
          "operator": "Contains",
          "negationConditon": false,
          "matchValues": [
            "evilbot"
          ],
          "transforms": [
            "Lowercase"
          ]
        }
      ]
    }
  ]
}

Contoh 5

Untuk contoh ini, Anda ingin memblokir jika permintaan berada di luar rentang alamat IP 192.168.5.0/24, atau string agen pengguna bukan chrome (artinya pengguna tidak menggunakan browser Chrome). Karena logika ini menggunakan atau , kedua kondisi berada dalam aturan terpisah seperti yang terlihat dalam contoh berikut. myrule1 dan myrule2 perlu dicocokkan untuk memblokir lalu lintas.

Logika: bukan (p dan q) = bukan p atau bukan q.

$variable1 = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RemoteAddr

$variable2 = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RequestHeaders `
   -Selector User-Agent

$condition1 = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable1 `
   -Operator IPMatch `
   -MatchValue "192.168.5.0/24" `
   -NegationCondition $True

$condition2 = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable2 `
   -Operator Contains `
   -MatchValue "chrome" `
   -Transform Lowercase `
   -NegationCondition $True

$rule1 = New-AzApplicationGatewayFirewallCustomRule `
   -Name myrule1 `
   -Priority 10 `
   -RuleType MatchRule `
   -MatchCondition $condition1 `
   -Action Block `
   -State Enabled

$rule2 = New-AzApplicationGatewayFirewallCustomRule `
   -Name myrule2 `
   -Priority 20 `
   -RuleType MatchRule `
   -MatchCondition $condition2 `
   -Action Block `
   -State Enabled

Serta JSON yang sesuai:

{
  "customRules": [
    {
      "name": "myrule1",
      "priority": 10,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RemoteAddr"
            }
          ],
          "operator": "IPMatch",
          "negationConditon": true,
          "matchValues": [
            "192.168.5.0/24"
          ],
          "transforms": []
        }
      ]
    },
    {
      "name": "myrule2",
      "priority": 20,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RequestHeaders",
              "selector": "User-Agent"
            }
          ],
          "operator": "Contains",
          "negationConditon": true,
          "matchValues": [
            "chrome"
          ],
          "transforms": [
            "Lowercase"
          ]
        }
      ]
    }
  ]
}

Contoh 6

Anda hanya ingin mengizinkan permintaan dari agen pengguna tertentu yang diketahui.

Karena logika yang digunakan di sini adalah or, dan semua nilai ada di header User-Agent, semua MatchValues bisa berada dalam daftar yang dipisahkan koma.

Logika: p atau q atau r

$variable = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RequestHeaders `
   -Selector User-Agent
$condition = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable `
   -Operator Equal `
   -MatchValue @('user1', 'user2') `
   -NegationCondition $True

$rule = New-AzApplicationGatewayFirewallCustomRule `
   -Name BlockUnknownUserAgents `
   -Priority 2 `
   -RuleType MatchRule `
   -MatchCondition $condition `
   -Action Block `
   -State Enabled

JSON yang sesuai:

{
  "customRules": [
    {
      "name": "BlockUnknownUserAgents",
      "priority": 2,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RequestHeaders",
              "selector": "User-Agent"
            }
          ],
          "operator": "Equal",
          "negationConditon": true,
          "matchValues": [
            "user1",
            "user2"
          ],
          "transforms": []
        }
      ]
    }
  ]
}

Contoh 7

Tidak jarang melihat Azure Front Door disebarkan di depan Application Gateway. Untuk memastikan lalu lintas yang diterima oleh Application Gateway berasal dari penerapan Front Door, praktik terbaik adalah memeriksa apakah header X-Azure-FDID berisi nilai unik yang diharapkan. Untuk informasi selengkapnya tentang mengamankan akses ke aplikasi Anda menggunakan Azure Front Door, lihat Cara mengunci akses ke backend saya hanya ke Azure Front Door

Logika: bukan p

$expectedFDID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$variable = New-AzApplicationGatewayFirewallMatchVariable `
   -VariableName RequestHeaders `
   -Selector X-Azure-FDID

$condition = New-AzApplicationGatewayFirewallCondition `
   -MatchVariable $variable `
   -Operator Equal `
   -MatchValue $expectedFDID `
   -Transform Lowercase `
   -NegationCondition $True

$rule = New-AzApplicationGatewayFirewallCustomRule `
   -Name blockNonAFDTraffic `
   -Priority 2 `
   -RuleType MatchRule `
   -MatchCondition $condition `
   -Action Block `
   -State Enabled

Dan inilah JSON yang sesuai:

{
  "customRules": [
    {
      "name": "blockNonAFDTraffic",
      "priority": 2,
      "ruleType": "MatchRule",
      "action": "Block",
      "state": "Enabled",
      "matchConditions": [
        {
          "matchVariables": [
            {
              "variableName": "RequestHeaders",
              "selector": "X-Azure-FDID"
            }
          ],
          "operator": "Equal",
          "negationConditon": true,
          "matchValues": [
            "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
          ],
          "transforms": [
            "Lowercase"
          ]
        }
      ]
    }
  ]
}

Langkah berikutnya

Setelah membuat aturan kustom, Anda dapat mempelajari cara melihat log WAF. Untuk info selengkapnya, lihat Diagnostik Application Gateway.