跳转到内容
本站为社区维护的非官方中文翻译,与 Cloudflare, Inc. 无关联。本站不提供账户登录,请勿在任何非官方站点输入您的 Cloudflare 账户信息。官方文档:developers.cloudflare.com

常用策略

最后更新 查看 MarkdownAgent 设置

以下策略常用于保护 HTTP 流量。HTTP 策略从上到下按顺序评估,第一条匹配的策略生效 —— Do Not Inspect(不检测) 策略除外,它们始终最先评估。

有关一组基线推荐策略,请参阅保护你的互联网流量和 SaaS 应用。

有关自定义转换、通知和捕获,请参阅使用 Forward(转发)操作(beta)的 Worker 模板。

有关其他选择器、运算符和操作的完整列表,请参阅 HTTP 策略页面。

拦截站点

按主机名或 URL 路径拦截访问站点的尝试。根据站点的组织方式,可能需要不同方法。

按主机名拦截站点

拦截使用某一主机的所有子域。

选择器 运算符 值 操作
Host(主机) matches regex(匹配正则表达式) .*example\.com Block(拦截)

在以下 API 示例中,filters: ["http"] 表示这是 HTTP(第 7 层)策略。

Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Block sites by hostname",
		"description": "Block all subdomains that use a specific hostname",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "http.request.host matches \".*example.com\"",
		"identity": "",
		"device_posture": ""
	}'

按 URL 拦截站点

拦截站点的某一部分而不拦截整个站点。例如,你可以拦截特定 subreddit(例如 reddit.com/r/gaming),而不拦截 reddit.com。

选择器 运算符 值 操作
URL matches regex(匹配正则表达式) /r/gaming Block(拦截)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Block sites by URL",
		"description": "Block specific parts of a site without blocking the hostname",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "http.request.uri matches \"/r/gaming\"",
		"identity": "",
		"device_posture": ""
	}'

拦截内容类别

拦截违反组织可接受使用政策的内容类别。

选择器 运算符 值 操作
内容类别 (Content Categories) in(属于) 可疑内容 (Questionable Content)、安全风险 (Security Risks)、杂项 (Miscellaneous)、成人主题 (Adult Themes)、赌博 (Gambling) 阻止 (Block)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "All-HTTP-ContentCategories-Blocklist",
		"description": "Block access to questionable content and potential security risks",
		"precedence": 40,
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "any(http.request.uri.content_category[*] in {17 85 87 102 157 135 138 180 162 32 169 177 128 15 115 119 124 141 161 2 67 125 133 99})",
		"identity": "",
		"device_posture": ""
	}'
resource "cloudflare_zero_trust_gateway_policy" "block_unauthorized_apps" {
  account_id     = var.cloudflare_account_id
  name           = "All-HTTP-ContentCategories-Blocklist"
  description    = "Block access to questionable content and potential security risks"
  precedence     = 40
  enabled        = true
  action         = "block"
  filters        = ["http"]
  traffic        = "any(http.request.uri.content_category[*] in {17 85 87 102 157 135 138 180 162 32 169 177 128 15 115 119 124 141 161 2 67 125 133 99})"
  identity       = ""
  device_posture = ""
}

拦截未授权的应用程序

为了将 shadow IT 的风险降至最低,一些组织选择限制其用户对某些基于 Web 的工具和应用程序的访问。例如,以下策略会阻止已知的 AI 工具:

选择器 运算符 值 操作
应用程序 (Application) in(属于) 人工智能 (Artificial Intelligence) 阻止 (Block)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "All-HTTP-Application-Blocklist",
		"description": "Limit access to shadow IT by blocking web-based tools and applications",
		"precedence": 60,
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "any(app.type.ids[*] in {25})",
		"identity": "",
		"device_posture": ""
	}'
resource "cloudflare_zero_trust_gateway_policy" "all_http_application_blocklist" {
  account_id     = var.cloudflare_account_id
  name           = "All-HTTP-Application-Blocklist"
  description    = "Limit access to shadow IT by blocking web-based tools and applications"
  precedence     = 60
  enabled        = true
  action         = "block"
  filters        = ["http"]
  traffic        = "any(app.type.ids[*] in {25})"
  identity       = ""
  device_posture = ""
}

检查用户身份

通过向策略添加基于身份的条件,按用户或用户组配置访问权限。

选择器 运算符 值 逻辑 操作
Application(应用程序) in(属于) Salesforce And(且) Block(拦截)
User Group Names(用户组名称) in(属于) Contractors
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Check user identity",
		"description": "Block access to Salesforce by temporary employees and contractors",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "any(app.ids[] in {606})",
		"identity": "any(identity.groups.name[] in {\"Contractors\"})",
		"device_posture": ""
	}'

跳过对应用程序组的检查

某些客户端应用程序(例如 Zoom 或 Apple 服务)依赖证书固定。这些应用程序会验证它们正在直接连接到自己的服务器,并会拒绝 Gateway 的 TLS 检查证书。为避免连接错误,你必须为这些应用程序添加 Do Not Inspect HTTP 策略。

Gateway 会首先评估 Do Not Inspect 策略,无论它们在策略列表中的位置如何。Cloudflare 建议将 Do Not Inspect 策略移到列表顶部,以减少混淆。

选择器 运算符 值 操作
Application(应用程序) in(属于) Do Not Inspect Do Not Inspect(不检测)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Bypass incompatible applications",
		"description": "Skip TLS decryption for applications that are incompatible with Gateway",
		"enabled": true,
		"action": "off",
		"filters": [
				"http"
		],
		"traffic": "any(app.type.ids[*] in {16})",
		"identity": "",
		"device_posture": ""
	}'

检查设备状况

要求设备安装特定软件或具有其他配置属性。有关设置设备状况检查的说明,请参阅强制执行设备状况。

强制最低操作系统版本

执行操作系统版本检查,以确保用户至少运行最低版本。

选择器 运算符 值 操作
Passed Device Posture Checks(已通过的设备状况检查) in(属于) Minimum OS version Allow(允许)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Require OS version",
		"description": "Perform an OS version check for minimum version",
		"enabled": true,
		"action": "allow",
		"filters": [
				"http"
		],
		"traffic": "",
		"identity": "",
		"device_posture": "any(device_posture.checks.passed[*] in {\"<POSTURE_CHECK_UUID>\"})"
	}'

要获取设备状况检查的 UUID,请使用 List device posture rules 端点。

检查特定文件

执行文件检查,以确保用户设备上有某个文件。

由于每个操作系统的文件路径不同,你可以为每个系统配置文件检查,并使用 Or(或) 逻辑运算符,只要求其中一项检查通过。

选择器 运算符 值 逻辑 操作
Passed Device Posture Checks(已通过的设备状况检查) in(属于) macOS File Check Or(或) Allow(允许)
Passed Device Posture Checks(已通过的设备状况检查) in(属于) Linux File Check
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Check for specific file",
		"description": "Ensure users have a specific file on their device regardless of operating system",
		"enabled": true,
		"action": "allow",
		"filters": [
				"http"
		],
		"traffic": "",
		"identity": "",
		"device_posture": "any(device_posture.checks.passed[] in {\"<POSTURE_CHECK_1_UUID>\"}) or any(device_posture.checks.passed[] in {\"<POSTURE_CHECK_2_UUID>\"})"
	}'

要获取设备状况检查的 UUID,请使用 List device posture rules 端点。

强制会话持续时间

在经过一定时间后要求用户重新进行身份验证。

在远程浏览器中隔离高风险站点

如果你使用 Browser Isolation 附加组件,请参阅我们的常用 Isolate 策略列表。

为自签名证书绕过检查

访问证书并非由公共证书颁发机构签名的源站服务器时,必须绕过 TLS 解密。

选择器 运算符 值 操作
Domain(域) in(属于) internal.example.com Do Not Inspect(不检测)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Bypass internal site inspection",
		"description": "Bypass TLS decryption for internal sites with self-signed certificates",
		"enabled": true,
		"action": "off",
		"filters": [
				"http"
		],
		"traffic": "any(http.request.domains[*] in {\"internal.example.com\"})",
		"identity": "",
		"device_posture": ""
	}'

拦截文件类型

根据文件类型阻止文件上传或下载。

选择器 运算符 值 逻辑 操作
上传文件类型 (Upload File Types) in(属于) Microsoft Office Word 文档 (docx) 且 (And) 阻止 (Block)
下载文件类型 (Download File Types) in(属于) PDF (pdf)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Block file types",
		"description": "Block the upload or download of files based on their type",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "any(http.upload.file.types[*] in {\"docx\"}) and any(http.download.file.types[*] in {\"pdf\"})",
		"identity": "",
		"device_posture": ""
	}'

有关受支持文件类型的更多信息,请参阅 Download and Upload File Types。

隔离或拦截影子 IT 应用程序

隔离应用程序库发现的尚未审核或正在审核中的影子 IT 应用程序,并拦截组织未批准的应用程序。

有关审核影子 IT 应用程序的更多信息,请参阅审核应用程序。

1. 隔离未审核或审核中的应用程序

如果应用程序的批准状态为 Unreviewed(未审核) 或 In review(审核中),则将其隔离。

选择器 运算符 值 逻辑 操作
Application Status(应用程序状态) is(是) Unreviewed Or(或) Isolate(隔离)
Application Status(应用程序状态) is(是) In review
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Isolate unreviewed or in review application status",
		"description": "Isolate Shadow IT applications that have not been reviewed or are in review in the Application Library",
		"enabled": true,
		"action": "isolate",
		"filters": [
				"http"
		],
		"traffic": "any(app.statuses[*] == \"unreviewed\") or any(app.statuses[*] == \"in review\")",
		"identity": "",
		"device_posture": ""
	}'

2. 拦截未批准的应用程序

如果应用程序的批准状态为 Unapproved(未批准),则将其拦截。

选择器 运算符 值 操作
Application Status(应用程序状态) is(是) Unapproved Block(拦截)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Block unapproved application status",
		"description": "Block Shadow IT applications that have been marked as unapproved in the Application Library",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "any(app.statuses[*] == \"unapproved\")",
		"identity": "",
		"device_posture": ""
	}'

拦截 Google 服务

要为 Google Drive 流量启用 Gateway 检查,你必须向 Google Drive 添加 Cloudflare 证书。

拦截 Google Drive 下载

拦截来自 Google Drive 的文件下载。

选择器 运算符 值 逻辑 操作
Application(应用程序) in(属于) Google Drive And(且) Block(拦截)
URL Path & Query matches regex(匹配正则表达式) .*(e=download|export).*
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Block Google Drive downloads",
		"description": "Block file downloads from Google Drive",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "any(app.ids[] in {554}) and http.request.uri.path_and_query matches \".(e=download|export).*\"",
		"identity": "",
		"device_posture": ""
	}'

拦截 Google Drive 上传

拦截发往 Google Drive 的文件上传。

选择器 运算符 值 逻辑 操作
Application(应用程序) in(属于) Google Drive And(且) Block(拦截)
Upload Mime Type(上传 Mime 类型) matches regex(匹配正则表达式) .* And(且)
Host(主机) is not(不是) drivefrontend-pa.clients6.google.com
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Block Google Drive uploads",
		"description": "Block file uploads to Google Drive",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "any(app.ids[] in {554}) and http.upload.mime matches \".\" and not(http.request.host == \"drivefrontend-pa.clients6.google.com\")",
		"identity": "",
		"device_posture": ""
	}'

拦截 Gmail 下载

拦截来自 Gmail 的文件下载。

选择器 运算符 值 逻辑 操作
Host(主机) is(是) mail-attachment.googleusercontent.com And(且) Block(拦截)
URL Path & Query is(是) /attachment/u/0
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Block Gmail downloads",
		"description": "Block file downloads from Gmail",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "http.request.host == \"mail-attachment.googleusercontent.com\" and http.request.uri.path_and_query matches \"/attachment/u/0\"",
		"identity": "",
		"device_posture": ""
	}'

拦截 Google Translate 代理

拦截使用 Google Translate 翻译整个网页。

翻译网站时,Google Translate 会使用 translate.goog 域代理网页。用户可能使用此服务绕过其他 Gateway 策略。如果你拦截 translate.goog,用户仍可访问其他 Google Translate 功能。

选择器 运算符 值 操作
Domain(域) matches regex(匹配正则表达式) ^(.+\.)?translate\.goog$ Block(拦截)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Block Google Translate for websites",
		"description": "Block use of Google Translate to translate entire webpages",
		"enabled": true,
		"action": "block",
		"filters": [
				"http"
		],
		"traffic": "any(http.request.domains[*] matches \"^(.+\\.)?translate\\.goog$\")",
		"identity": "",
		"device_posture": ""
	}'

过滤 WebSocket 流量

Gateway 不检查或记录 WebSocket ↗ 流量。相反,Gateway 只会记录用于建立 WebSocket 连接的 HTTP 详细信息,以及网络会话信息。要过滤 WebSocket 流量,请创建带有 101 HTTP 响应码的策略。

选择器 运算符 值 操作
HTTP Response is(是) 101 SWITCHING_PROTOCOLS Allow(允许)
Create a Zero Trust Gateway rulebash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"name": "Filter WebSocket",
		"description": "Filter WebSocket traffic with HTTP response code 101",
		"enabled": true,
		"action": "allow",
		"filters": [
				"http"
		],
		"traffic": "http.response.status_code == 101",
		"identity": "",
		"device_posture": ""
	}'

这篇文档对您有帮助吗?