跳转到内容
搜索文档

通过 API 配置已泄露凭据检查

最后更新 查看 MarkdownAgent 设置

通过 API 配置已泄露凭据检查。

使用 Rulesets API 配置已泄露凭据检查。您可以执行以下操作:

如果您使用 Terraform,请参阅 使用 Terraform 配置已泄露凭据检查

创建检查已泄露凭据的自定义规则

您可以使用 Rulesets API 创建检查已泄露凭据的规则。将这些规则包含在自定义规则集中(必须在账户级别创建),然后将自定义规则集部署到某个阶段。

当规则表达式和已泄露凭据检查结果均为真时,检查已泄露凭据的规则会发生匹配。

要在自定义规则中检查已泄露凭据,请在规则定义中包含 exposed_credential_check 对象。此对象必须具有以下属性:

  • username_expression — 选择用于凭据检查的用户 ID 的表达式。此属性最多可有 1024 个字符。
  • password_expression — 选择用于凭据检查的密码的表达式。此属性最多可有 1024 个字符。

您可以在具有以下操作之一的规则中使用 exposed_credential_check 对象:rewritelogblockjs_challenge(非交互式质询)或 challenge(交互式质询)。Cloudflare 建议仅将已泄露凭据检查与以下操作一起使用:rewritelog

要创建和部署自定义规则集,请按照 使用自定义规则集 中描述的工作流操作。

示例 A

POST 请求示例创建了一个包含检查已泄露凭据规则的新自定义规则集。当规则表达式和 exposed_credential_check 结果均为 true 时,规则会匹配。匹配时,规则会将包含已泄露凭据的请求记录到 Cloudflare 日志中。

Required API token permissions

At least one of the following token permissions is required:
  • Account WAF Write
  • Account Rulesets Write
Create an account rulesetbash
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。更多信息请参阅 部署自定义规则集

示例 B

POST 请求示例创建了一个包含检查 JSON 响应中已泄露凭据规则的新自定义规则集。当规则表达式和 exposed_credential_check 结果均为 true 时,规则会匹配。匹配时,规则会向请求添加值为 1Exposed-Credential-Check HTTP 标头。

Required API token permissions

At least one of the following token permissions is required:
  • Account WAF Write
  • Account Rulesets Write
Create an account rulesetbash
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。更多信息请参阅 部署自定义规则集

这篇文档对您有帮助吗?