以下策略通常用于保护网络流量。网络策略按从上到下的顺序进行评估,并应用第一个匹配的策略。请将更具体的 **Allow(允许)**策略放在更宽泛的 **Block(阻止)**策略之上。
有关推荐的基本策略集,请参阅保护您的互联网流量和 SaaS 应用程序。
有关其他选择器、运算符和操作的完整列表,请参阅网络策略页面。
为了将 shadow IT 的风险降至最低,一些组织选择限制其用户对某些基于 Web 的工具和应用程序的访问。例如,以下策略会阻止已知的 AI 工具:
| 选择器 | 运算符 | 值 | 操作 |
|---|---|---|---|
| Application(应用程序) | in(属于) | Artificial Intelligence(人工智能) | Block(阻止) |
在以下 API 示例中,filters: ["l4"] 表示这是一个网络(第 4 层)策略。
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block unauthorized applications",
"description": "Block access to unauthorized AI applications",
"enabled": true,
"action": "block",
"filters": [
"l4"
],
"traffic": "any(app.type.ids[*] in {25})",
"identity": "",
"device_posture": ""
}'通过在策略中添加 基于身份的条件,按每个用户或组配置访问权限。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Application(应用程序) | in(属于) | Salesforce | And(且) | Block(阻止) |
| User Group Names(用户组名称) | in(属于) | Contractors(合同工) |
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": [
"l4"
],
"traffic": "any(app.ids[*] in {606})",
"identity": "any(identity.groups.name[*] in {\"Contractors\"})",
"device_posture": ""
}'要求设备安装某些软件或具有其他配置属性。有关启用设备姿态检查的说明,请参阅设备姿态部分。例如,您可以使用设备序列号列表来确保用户只有在从公司设备使用 Cloudflare One Client 连接时才能访问应用程序:
在以下示例中,您可以使用 设备序列号 列表,以确保用户只有在从公司设备使用 Cloudflare One 客户端连接时才能访问应用程序:
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| SNI 域名 (SNI Domain) | is | internalapp.com |
且 (And) | 阻止 (Block) |
| 通过设备状态检测 (Passed Device Posture Checks) | not in | 设备序列号 (Device serial numbers) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "All-NET-ApplicationAccess-Allow",
"description": "Ensure access to the application comes from authorized WARP clients",
"precedence": 70,
"enabled": false,
"action": "block",
"filters": [
"l4"
],
"traffic": "any(net.sni.domains[*] == \"internalapp.com\")",
"device_posture": "not(any(device_posture.checks.passed[*] in {\"<DEVICE_SERIAL_NUMBERS_LIST_UUID>\"}))"
}'要获取设备状态检测的 UUID,请使用 列出设备状态规则 端点。
resource "cloudflare_zero_trust_gateway_policy" "all_net_applicationaccess_allow" {
account_id = var.cloudflare_account_id
name = "All-NET-ApplicationAccess-Allow"
description = "Ensure access to the application comes from authorized WARP clients"
precedence = 70
enabled = false
action = "block"
filters = ["l4"]
traffic = "any(net.sni.domains[*] == \"internalapp.com\")"
posture = "not(any(device_posture.checks.passed[*] in {\"${"$"}${cloudflare_zero_trust_list.allowed_devices_sn_list.id}\"}))"
}要要求用户在经过一定时间后重新进行身份验证,请配置 Cloudflare One Client 会话。
限制用户只能访问您的 HTTP 策略 中配置的特定网站或应用程序。此模式使用两条策略:一条 **Allow(允许)**HTTP/HTTPS 流量的策略,以及一条 **Block(阻止)**其他所有流量的策略。请将 Allow 策略放在 Block 策略之上,以便匹配的流量在兜底拦截规则生效之前被允许通过。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Detected Protocol(检测到的协议) | is(是) | TLS | And(且) | Allow(允许) |
| Destination Port(目标端口) | in(属于) | 80, 443 |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Allow HTTP and HTTPS traffic",
"description": "Restrict traffic to HTTP and HTTPS traffic",
"enabled": true,
"action": "allow",
"filters": [
"l4"
],
"traffic": "net.detected_protocol == \"tls\" and net.dst.port in {80 443}",
"identity": "",
"device_posture": ""
}'| 选择器 | 运算符 | 值 | 操作 |
|---|---|---|---|
| Protocol(协议) | in(属于) | TCP, UDP | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block all other traffic",
"description": "Block all other traffic that is not HTTP or HTTPS",
"enabled": true,
"action": "block",
"filters": [
"l4"
],
"traffic": "net.protocol in {\"tcp\" \"udp\"}",
"identity": "",
"device_posture": ""
}'如果您的组织默认使用网络策略阻止流量,并且您希望在所有端口上检测 HTTP 流量,则需要明确允许 HTTP 和 TLS 流量才能对其进行过滤。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Detected Protocol(检测到的协议) | is(是) | TLS | Or(或) | Allow(允许) |
| Detected Protocol(检测到的协议) | is(是) | HTTP |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Allow on inspect all ports",
"description": "Filter HTTPS traffic when using inspect all ports",
"enabled": true,
"action": "allow",
"filters": [
"l4"
],
"traffic": "net.detected_protocol == \"tls\" or net.detected_protocol == \"http\"",
"identity": "",
"device_posture": ""
}'当使用代理端点时,默认情况下,添加到代理端点的所有设备都可以访问通过 Cloudflare Tunnel 连接的内部应用程序和服务。为了限制访问并增加额外的安全层,请创建以下策略。
使用源 IP 代理端点时,限制访问仅允许通过特定源 IP 的代理端点连接的用户。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Proxy Endpoint(代理端点) | in(属于) | Proxy Endpoint(代理端点) | And(且) | Allow(允许) |
| Source IP(源 IP) | in(属于) | 203.0.113.0/24 |
And(且) | |
| Destination IP(目标 IP) | in(属于) | 10.0.0.0/8 |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Allow proxy endpoint traffic from specific source IPs",
"description": "Allow traffic from proxy endpoint users with specific source IPs to reach private network",
"enabled": true,
"action": "allow",
"filters": [
"l4"
],
"traffic": "net.proxy_endpoint.ids[*] in {\"<PROXY_ENDPOINT_ID>\"} and net.src.ip in {203.0.113.0/24} and net.dst.ip in {10.0.0.0/8}",
"identity": "",
"device_posture": ""
}'将 <PROXY_ENDPOINT_ID> 替换为您的代理端点 ID。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Proxy Endpoint(代理端点) | in(属于) | Proxy Endpoint(代理端点) | And(且) | Block(阻止) |
| Destination IP(目标 IP) | in(属于) | 10.0.0.0/8 |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block all other proxy endpoint traffic",
"description": "Block any other proxy endpoint traffic from accessing the private network",
"enabled": true,
"action": "block",
"filters": [
"l4"
],
"traffic": "net.proxy_endpoint.ids[*] in {\"<PROXY_ENDPOINT_ID>\"} and net.dst.ip in {10.0.0.0/8}",
"identity": "",
"device_posture": ""
}'将 <PROXY_ENDPOINT_ID> 替换为您的代理端点 ID。
当使用授权代理端点时,通过限制仅允许从特定源 IP 连接的用户访问来增加额外的安全层。这可以防止即使在用户凭据泄露的情况下发生未经授权的访问。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Proxy Endpoint(代理端点) | in(属于) | Proxy Endpoint(代理端点) | And(且) | Allow(允许) |
| Source IP(源 IP) | in(属于) | 203.0.113.0/24 |
And(且) | |
| Destination IP(目标 IP) | in(属于) | 10.0.0.0/8 |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Allow authorized proxy endpoint traffic from specific source IPs",
"description": "Allow traffic from authorization proxy endpoint users with specific source IPs to reach private network",
"enabled": true,
"action": "allow",
"filters": [
"l4"
],
"traffic": "net.proxy_endpoint.ids[*] in {\"<PROXY_ENDPOINT_ID>\"} and net.src.ip in {203.0.113.0/24} and net.dst.ip in {10.0.0.0/8}",
"identity": "",
"device_posture": ""
}'将 <PROXY_ENDPOINT_ID> 替换为您的代理端点 ID。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Proxy Endpoint(代理端点) | in(属于) | Proxy Endpoint(代理端点) | And(且) | Block(阻止) |
| Destination IP(目标 IP) | in(属于) | 10.0.0.0/8 |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block all other authorized proxy endpoint traffic",
"description": "Block any other authorization proxy endpoint traffic from accessing the private network",
"enabled": true,
"action": "block",
"filters": [
"l4"
],
"traffic": "net.proxy_endpoint.ids[*] in {\"<PROXY_ENDPOINT_ID>\"} and net.dst.ip in {10.0.0.0/8}",
"identity": "",
"device_posture": ""
}'将 <PROXY_ENDPOINT_ID> 替换为您的代理端点 ID。
限制访问您通过 Cloudflare Tunnel 连接的资源。
以下示例由两条策略组成:第一条允许特定用户访问您的应用程序,第二条阻止所有其他流量。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Destination IP(目标 IP) | in | 10.0.0.0/8 |
且 (And) | 允许 (Allow) |
| 用户电子邮件 (User Email) | matches regex | .*@example.com |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Allow company employees",
"description": "Allow any users with an organization email to reach the application",
"enabled": true,
"action": "allow",
"filters": [
"l4"
],
"traffic": "net.dst.ip in {10.0.0.0/8}",
"identity": "identity.email matches \".*@example.com\"",
"device_posture": ""
}'| 选择器 | 运算符 | 值 | 操作 |
|---|---|---|---|
| 目标 IP (Destination IP) | in | 10.0.0.0/8 |
阻止 (Block) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block everyone else",
"description": "Block any other users from accessing the application",
"enabled": true,
"action": "block",
"filters": [
"l4"
],
"traffic": "net.dst.ip in {10.0.0.0/8}",
"identity": "",
"device_posture": ""
}'用不同的 IP 地址覆盖指向特定 IP 地址的流量。
| 选择器 | 运算符 | 值 | 逻辑 | 操作 |
|---|---|---|---|---|
| Destination IP(目标 IP) | in(属于) | 203.0.113.17 |
And(且) | Network Override(网络覆盖) |
| Destination Port(目标端口) | is(是) | 80 |
| 覆盖 IP(Override IP) | 覆盖端口(Override port) |
|---|---|
1.1.1.1 |
80 |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Override example.com with 1.1.1.1",
"description": "Override a site'\''s IP address with another IP",
"enabled": true,
"action": "l4_override",
"filters": [
"l4"
],
"traffic": "net.dst.ip in {203.0.113.17} and net.dst.port == 80",
"identity": "",
"device_posture": "",
"rule_settings": {
"l4override": {
"ip": "1.1.1.1",
"port": 80
},
"override_host": "",
"override_ips": null
}
}'