漏えいした認証情報のチェックは Rulesets API で設定します。次のことができます。
Terraform を使う場合は Terraform で漏えいした認証情報のチェックを設定する を参照してください。
漏えいした認証情報をチェックするルールは Rulesets API で作成できます。これらのルールはカスタムルールセットに含めます。カスタムルールセットはアカウントレベルで作成し、フェーズへデプロイします。
漏えいした認証情報をチェックするルールは、ルール式と漏えいした認証情報チェックの結果がどちらも真のときに一致します。
カスタムルールで漏えいした認証情報をチェックするには、ルール定義に exposed_credential_check オブジェクトを含めます。このオブジェクトには次のプロパティが必要です。
username_expression— 認証情報チェックに使うユーザー ID を選ぶ式です。このプロパティは最大 1024 文字です。password_expression— 認証情報チェックに使うパスワードを選ぶ式です。このプロパティは最大 1024 文字です。
exposed_credential_check オブジェクトは、次のいずれかのアクションを持つルールで使えます。rewrite、log、block、js_challenge(非インタラクティブチャレンジ)、challenge(インタラクティブチャレンジ)。漏えいした認証情報のチェックは、rewrite と log のアクションでのみ使うことを Cloudflare は推奨します。
カスタムルールセットの作成とデプロイは、カスタムルールセットを扱う の手順に従ってください。
この POST リクエストの例は、漏えいした認証情報をチェックするルールを含む新しいカスタムルールセットを作成します。ルール式と exposed_credential_check の結果がどちらも true のときに一致します。一致すると、漏えいした認証情報を含むリクエストを Cloudflare のログに記録します。
Required API token permissions
At least one of the following token permissions is required:Account WAF WriteAccount Rulesets Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Custom Ruleset A",
"kind": "custom",
"description": "This ruleset includes a rule checking for exposed credentials.",
"rules": [
{
"action": "log",
"description": "Exposed credentials check on login.php page",
"expression": "http.request.method == \"POST\" && http.request.uri == \"/login.php\"",
"exposed_credential_check": {
"username_expression": "url_decode(http.request.body.form[\"username\"][0])",
"password_expression": "url_decode(http.request.body.form[\"password\"][0])"
}
}
],
"phase": "http_request_firewall_custom"
}'レスポンスは作成したルールセットを返します。ルール定義に exposed_credential_check オブジェクトがある点に注目してください。
{
"result": {
"id": "<CUSTOM_RULESET_ID>",
"name": "Custom Ruleset A",
"description": "This ruleset includes a rule checking for exposed credentials.",
"kind": "custom",
"version": "1",
"rules": [
{
"id": "<CUSTOM_RULE_ID>",
"version": "1",
"action": "log",
"description": "Exposed credentials check on login.php page",
"expression": "http.request.method == \"POST\" && http.request.uri == \"/login.php\"",
"exposed_credential_check": {
"username_expression": "url_decode(http.request.body.form[\"username\"][0])",
"password_expression": "url_decode(http.request.body.form[\"password\"][0])"
},
"last_updated": "2021-03-19T10:48:04.057775Z",
"ref": "<CUSTOM_RULE_REF>",
"enabled": true
}
],
"last_updated": "2021-03-19T10:48:04.057775Z",
"phase": "http_request_firewall_custom"
},
"success": true,
"errors": [],
"messages": []
}この例が url_decode() 関数を使うのは、コンテンツタイプが application/x-www-form-urlencoded のとき、リクエスト本文のフィールド(http.request.body.form で利用可能)が URL エンコードされるためです。
カスタムルールセットを作成したあと、実行されるようにフェーズへデプロイします。デプロイにはルールセット ID が必要です。詳細は カスタムルールセットをデプロイする を参照してください。
この POST リクエストの例は、JSON 本文の漏えいした認証情報をチェックするルールを含む新しいカスタムルールセットを作成します。ルール式と exposed_credential_check の結果がどちらも true のときに一致します。一致すると、リクエストに値 1 の Exposed-Credential-Check HTTP ヘッダーを追加します。
Required API token permissions
At least one of the following token permissions is required:Account WAF WriteAccount Rulesets Write
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/rulesets" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Custom Ruleset B",
"kind": "custom",
"description": "This ruleset includes a rule checking for exposed credentials.",
"rules": [
{
"action": "rewrite",
"action_parameters": {
"headers": {
"Exposed-Credential-Check": {
"operation": "set",
"value": "1"
}
}
},
"description": "Exposed credentials check on login endpoint with JSON body",
"expression": "http.request.method == \"POST\" && http.request.uri == \"/login.php\" && any(http.request.headers[\"content-type\"][*] == \"application/json\")",
"exposed_credential_check": {
"username_expression": "lookup_json_string(http.request.body.raw, \"username\")",
"password_expression": "lookup_json_string(http.request.body.raw, \"password\")"
}
}
],
"phase": "http_request_firewall_custom"
}'レスポンスは作成したルールセットを返します。ルール定義には次の要素があります。
rewriteアクション。- 漏えいした認証情報を含むリクエストへ追加する HTTP ヘッダーを設定する
action_parametersオブジェクト。 exposed_credential_checkオブジェクト。
{
"result": {
"id": "<CUSTOM_RULESET_ID>",
"name": "Custom Ruleset B",
"description": "This ruleset includes a rule checking for exposed credentials.",
"kind": "custom",
"version": "1",
"rules": [
{
"id": "<CUSTOM_RULE_ID>",
"version": "1",
"action": "rewrite",
"action_parameters": {
"headers": {
"Exposed-Credential-Check": {
"operation": "set",
"value": "1"
}
}
},
"description": "Exposed credentials check on login endpoint with JSON body",
"expression": "http.request.method == \"POST\" && http.request.uri == \"/login.php\" && any(http.request.headers[\"content-type\"][*] == \"application/json\")",
"exposed_credential_check": {
"username_expression": "lookup_json_string(http.request.body.raw, \"username\")",
"password_expression": "lookup_json_string(http.request.body.raw, \"password\")"
},
"last_updated": "2022-03-19T12:48:04.057775Z",
"ref": "<CUSTOM_RULE_REF>",
"enabled": true
}
],
"last_updated": "2022-03-19T12:48:04.057775Z",
"phase": "http_request_firewall_custom"
},
"success": true,
"errors": [],
"messages": []
}カスタムルールセットを作成したあと、実行されるようにフェーズへデプロイします。デプロイにはルールセット ID が必要です。詳細は カスタムルールセットをデプロイする を参照してください。