コンテンツにスキップ

WAFカスタムルール

このページでは、Terraformを使用してゾーンまたはアカウント内でWAFカスタムルールを作成する例を提供します。例は以下のシナリオをカバーしています:

カスタムルールに関する詳細は、Cloudflare WAFドキュメントのカスタムルールを参照してください。

始める前に

必要なアカウントまたはゾーンIDを取得する

The Terraform configurations provided in this page need the zone ID (or account ID) of the zone/account where you will deploy rulesets.

  • To retrieve the list of accounts you have access to, including their IDs, use the List accounts operation.
  • To retrieve the list of zones you have access to, including their IDs, use the List zones operation.

既存のルールセットをインポートまたは削除する

Terraform assumes that it has complete control over account and zone rulesets. If you already have rulesets configured in your account or zone, do one of the following:

  • Import existing rulesets to Terraform using the cf-terraforming tool. Recent versions of the tool can generate resource definitions for existing rulesets and import their configuration to Terraform state.
  • Start from scratch by deleting existing rulesets (account and zone rulesets with "kind": "root" and "kind": "zone", respectively) and then defining your rulesets configuration in Terraform.

ゾーンレベルの構成

ゾーンにカスタムルールを追加する

以下の例は、IDが<ZONE_ID>のゾーンのhttp_request_firewall_customフェーズのゾーンエントリポイントルールセットにカスタムルールを構成します。このルールは、非標準のHTTP(S)ポートでのすべてのトラフィックをブロックします:

resource "cloudflare_ruleset" "zone_custom_firewall" {
zone_id = "<ZONE_ID>"
name = "私のゾーンのカスタムルール用のフェーズエントリポイントルールセット"
description = ""
kind = "zone"
phase = "http_request_firewall_custom"
rules {
action = "block"
expression = "(not cf.edge.server_port in {80 443})"
description = "80および443以外のポートをブロック"
enabled = true
}
}

To create another カスタムルール, add a new rules object to the same cloudflare_ruleset resource.


アカウントレベルの構成

カスタムルールセットを作成してデプロイする

以下の例は、IDが<ACCOUNT_ID>のアカウントに単一のカスタムルールを含むカスタムルールセットを作成します。このカスタムルールセットは、別のcloudflare_ruleset Terraformリソースを使用してデプロイされます。カスタムルールセットをデプロイしない場合、それは実行されません。

以下の構成は、単一のルールを持つカスタムルールセットを作成します:

resource "cloudflare_ruleset" "account_firewall_custom_ruleset" {
account_id = "<ACCOUNT_ID>"
name = "非標準HTTP(S)ポートでのトラフィックをブロックするカスタムルールセット"
description = ""
kind = "custom"
phase = "http_request_firewall_custom"
rules {
action = "block"
expression = "(not cf.edge.server_port in {80 443})"
description = "80および443以外のポートをブロック"
enabled = true
}
}

To create another カスタムルールセット内のカスタムルール, add a new rules object to the same cloudflare_ruleset resource.


以下の構成は、アカウントレベルでカスタムルールセットをデプロイします。account_firewall_custom_rulesetリソースに依存関係を定義し、action_parametersで作成されたカスタムルールセットのIDを使用します:

resource "cloudflare_ruleset" "account_firewall_custom_entrypoint" {
account_id = "<ACCOUNT_ID>"
name = "カスタムルールセットをデプロイするhttp_request_firewall_customフェーズのアカウントレベルエントリポイントルールセット"
description = ""
kind = "root"
phase = "http_request_firewall_custom"
depends_on = [cloudflare_ruleset.account_firewall_custom_ruleset]
rules {
action = "execute"
action_parameters {
id = cloudflare_ruleset.account_firewall_custom_ruleset.id
}
expression = "(cf.zone.name eq \"example.com\")"
description = "example.comのカスタムルールセットをデプロイ"
enabled = true
}
}

カスタムルールセットの構成とデプロイに関する詳細は、Ruleset Engineドキュメントのカスタムルールセットを操作するを参照してください。

公開された資格情報をチェックするカスタムルールを追加する

以下の構成は、単一のルールを持つカスタムルールセットを作成し、公開された資格情報をチェックするものです。

resource "cloudflare_ruleset" "account_firewall_custom_ruleset_exposed_creds" {
account_id = "<ACCOUNT_ID>"
name = "公開された資格情報をチェックするカスタムルールセット"
description = ""
kind = "custom"
phase = "http_request_firewall_custom"
rules {
action = "rewrite"
action_parameters {
headers {
name = "Exposed-Credential-Check"
operation = "set"
value = "1"
}
}
exposed_credential_check {
username_expression = "url_decode(http.request.body.form[\"username\"][0])"
password_expression = "url_decode(http.request.body.form[\"password\"][0])"
}
expression = "http.request.method == \"POST\" && http.request.uri == \"/login.php\""
description = "ルールが一致し、公開された資格情報が検出された場合にヘッダーを追加"
enabled = true
}
}

To create another ルール, add a new rules object to the same cloudflare_ruleset resource.


以下の構成は、カスタムルールセットをデプロイします。account_firewall_custom_ruleset_exposed_credsリソースに依存関係を定義し、作成されたカスタムルールセットのIDを取得します:

resource "cloudflare_ruleset" "account_firewall_custom_entrypoint" {
account_id = "<ACCOUNT_ID>"
name = "公開された資格情報をチェックするカスタムルールセットをデプロイするhttp_request_firewall_customフェーズのアカウントレベルエントリポイントルールセット"
description = ""
kind = "root"
phase = "http_request_firewall_custom"
depends_on = [cloudflare_ruleset.account_firewall_custom_ruleset_exposed_creds]
rules {
action = "execute"
action_parameters {
id = cloudflare_ruleset.account_firewall_custom_ruleset_exposed_creds.id
}
expression = "(cf.zone.name eq \"example.com\")"
description = "example.comのカスタムルールセットをデプロイ"
enabled = true
}
}