以下 Cloudflare Gateway DNS 策略通常用于保护 DNS 流量的安全。每个示例均包含仪表板和 API 指南,您可以根据您的组织进行调整。
有关推荐的基本策略集,请参阅保护您的互联网流量和 SaaS 应用程序。
有关其他选择器、运算符和动作的完整列表,请参阅 DNS 策略页面。
此策略允许用户访问官方的公司域名。通过以高执行顺序部署此策略,您可以确保员工能够访问受信任的域,即使这些域属于被拦截的类别,例如 新发现的域名(Newly seen domains) 或 登录页面(Login pages)。
| 选择器 | 运算符 | 值 | 动作 | 优先级 |
|---|---|---|---|---|
| Domain(域名) | in list(在列表中) | Allowed domains | Allow(允许) | 1 |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Allow corporate domains",
"description": "Allow any internal corporate domains added to a list",
"precedence": 0,
"enabled": true,
"action": "allow",
"filters": [
"dns"
],
"traffic": "any(dns.domains[*] in $<LIST_UUID>)",
"identity": ""
}'要获取您列表的 UUID,请使用 列出 Zero Trust 列表 端点。
根据 Cloudflare 的威胁情报,拦截命令与控制(Command & Control)、僵尸网络(Botnet)和恶意软件(Malware)等安全类别。
| 选择器 | 运算符 | 值 | 操作 |
|---|---|---|---|
| 安全类别 (Security Categories) | in | 所有安全风险 (All security risks) | 阻止 (Block) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "All-DNS-SecurityCategories-Blocklist",
"description": "Block security categories based on Cloudflare'\''s threat intelligence",
"precedence": 20,
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(dns.security_category[*] in {68 178 80 83 176 175 117 131 134 151 153})",
"identity": ""
}'resource "cloudflare_zero_trust_gateway_policy" "block_security_threats" {
account_id = var.cloudflare_account_id
name = "All-DNS-SecurityCategories-Blocklist"
description = "Block security categories based on Cloudflare's threat intelligence"
precedence = 20
enabled = true
action = "block"
filters = ["dns"]
traffic = "any(dns.security_category[*] in {68 178 80 83 176 175 117 131 134 151 153})"
}此策略中包含的类别并不总是构成安全威胁,但拦截它们有助于最大程度降低您组织面临的风险。有关更多信息,请参阅域类别。
| 选择器 | 运算符 | 值 | 操作 |
|---|---|---|---|
| 内容类别 (Content Categories) | in(属于) | 可疑内容 (Questionable Content)、安全风险 (Security Risks)、杂项 (Miscellaneous) | 阻止 (Block) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "All-DNS-ContentCategories-Blocklist",
"description": "Block common content categories that may pose a risk",
"precedence": 30,
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(dns.content_category[*] in {17 85 87 102 157 135 138 180 162 32 169 177 128 15 115 119 124 141 161})",
"identity": ""
}'resource "cloudflare_zero_trust_gateway_policy" "block_content_categories" {
account_id = var.cloudflare_account_id
name = "All-DNS-ContentCategories-Blocklist"
description = "Block common content categories that may pose a risk"
enabled = true
action = "block"
filters = ["dns"]
traffic = "any(dns.content_category[*] in {17 85 87 102 157 135 138 180 162 32 169 177 128 15 115 119 124 141 161})"
identity = ""
}您可以使用 OPT 代码 65050 将类别 ID 列表作为 JSON 对象添加到发送给 Gateway 的请求的 EDNS(DNS 扩展机制) ↗ 标头中。EDNS 允许在标准字段之外向 DNS 查询附加额外元数据。例如:
{
"categories": [2, 67, 125, 133]
}通过 Request Context Categories(请求上下文类别) 选择器,您可以拦截与 EDNS 一起发送的类别 ID。这在按创建策略时未知的类别进行过滤,或者在不达到账户限制的情况下执行特定于设备的 DNS 内容过滤时很有用。当 Gateway 使用此选择器拦截 DNS 查询时,请求将返回 Extended DNS Error (EDE) Code 15(Blocked,被拦截),以及一个包含匹配类别数组的字段。
| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Request Context Category(请求上下文类别) | is(是) | Present(存在) | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "All-DNS-Bock-Category-Matches-In-Request",
"description": "Block all category matches in the request EDNS context",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "dns.categories_in_request_context_matches",
"identity": ""
}'resource "cloudflare_zero_trust_gateway_policy" "block_content_categories" {
account_id = var.cloudflare_account_id
name = "All-DNS-Bock-Category-Matches-In-Request"
description = "Block all category matches in the request EDNS context"
enabled = true
action = "block"
filters = ["dns"]
traffic = "dns.categories_in_request_context_matches"
identity = ""
}为了将 shadow IT 的风险降至最低,一些组织选择限制其用户对某些基于 Web 的工具和应用程序的访问。例如,以下策略会阻止已知的 AI 工具:
| 选择器 | 运算符 | 值 | 操作 |
|---|---|---|---|
| 应用程序 (Application) | in(属于) | 人工智能 (Artificial Intelligence) | 阻止 (Block) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "All-DNS-Application-Blocklist",
"description": "Block access to unauthorized AI applications",
"precedence": 40,
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(app.type.ids[*] in {25})",
"identity": ""
}'resource "cloudflare_zero_trust_gateway_policy" "block_unauthorized_apps" {
account_id = var.cloudflare_account_id
name = "All-DNS-Application-Blocklist"
description = "Block access to unauthorized AI applications"
enabled = true
action = "block"
filters = ["dns"]
traffic = "any(app.type.ids[*] in {25})"
identity = ""
}您可以实施策略来拦截托管在被归类为高风险国家/地区的网站。这些国家/地区的指定可能是由于您组织的要求,或者是通过法规,包括 EAR(出口管理条例) ↗、OFAC(外国资产控制办公室) ↗ 和 ITAR(国际武器贸易条例) ↗。此策略会拦截解析为位于您指定国家/地区的 IP 地址的 DNS 查询。
| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Resolved Country IP Geolocation(已解析国家/地区 IP 地理位置) | in(属于) | Afghanistan, Belarus, Congo (Kinshasa), Cuba, Iran, Iraq, Korea, North, Myanmar, Russian Federation, Sudan, Syria, Ukraine, Zimbabwe | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block banned countries",
"description": "Block access to banned countries",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(dns.dst.geo.country[*] in {\"AF\" \"BY\" \"CD\" \"CU\" \"IR\" \"IQ\" \"KP\" \"MM\" \"RU\" \"SD\" \"SY\" \"UA\" \"ZW\"})",
"identity": ""
}'拦截经常被滥用 ↗的顶级域(TLD)—— 即域名中的最后一部分,例如 .com 或 .ru —— 可以降低安全风险,尤其是当允许访问没有任何显而易见的好处时。同样,为了符合 ITAR ↗ 或 OFAC ↗ 等法规,可能需要限制对特定国家/地区级别 TLD 的访问。
| 选择器 | 运算符 | 值 | 逻辑 | 动作 |
|---|---|---|---|---|
| Domain(域名) | matches regex(匹配正则) | [.](cn|ru)$ |
Or(或) | Block(阻止) |
| Domain(域名) | matches regex(匹配正则) | [.](rest|hair|top|live|cfd|boats|beauty|mom|skin|okinawa)$ |
Or(或) | |
| Domain(域名) | matches regex(匹配正则) | [.](zip|mobi)$ |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block top-level domains",
"description": "Block top-level domains that are frequently used for malicious practices",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(dns.domains[*] matches \"[.](cn|ru)$\") or any(dns.domains[*] matches \"[.](rest|hair|top|live|cfd|boats|beauty|mom|skin|okinawa)$\") or any(dns.domains[*] matches \"[.](zip|mobi)$\")",
"identity": ""
}'为了防御复杂的网络钓鱼攻击 ↗,您可以阻止用户访问专门针对您组织的钓鱼域。以下策略拦截与组织或其身份验证服务(例如 okta、2fa、cloudflare 或 sso)相关的特定关键字,同时仍然允许访问官方的公司域名。
| 选择器 | 运算符 | 值 | 逻辑 | 动作 |
|---|---|---|---|---|
| Domain(域名) | not in list(不在列表中) | Corporate Domains | And(且) | Block(阻止) |
| Domain(域名) | matches regex(匹配正则) | .*okta.*|.*cloudflare.*|.*mfa.*|.sso.* |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block phishing attacks",
"description": "Block attempts to phish specific domains targeting your organization",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "not(any(dns.domains[*] in $<LIST_UUID>)) and any(dns.domains[*] matches \".*okta.*\\|.*cloudflare.*\\|.*mfa.*\\|.sso.*\")",
"identity": ""
}'要获取您列表的 UUID,请使用 列出 Zero Trust 列表 端点。
为了保护用户隐私,一些组织会拦截追踪域(例如 dig.whatsapp.com)以及在操作系统级别嵌入的其他追踪域。此策略是通过创建自定义拦截列表来实施的。有关您可以添加到拦截列表中的广泛使用的追踪域的列表,请参阅此存储库 ↗。
| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Domain(域名) | in list(在列表中) | Top tracking domains | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block online tracking",
"description": "Block domains used for tracking at an OS level",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(dns.domains[*] in $<LIST_UUID>)",
"identity": ""
}'要获取您列表的 UUID,请使用 列出 Zero Trust 列表 端点。
拦截已知是恶意的或对您的组织构成威胁的特定 IP 地址。此策略通常通过创建自定义拦截列表,或者通过使用威胁情报合作伙伴或区域计算机应急和响应小组(CERT)提供的拦截列表来实现。
| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Resolved IP(已解析 IP) | in list(在列表中) | DShield | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block malicious IPs",
"description": "Block specific IP addresses that are known to be malicious or pose a threat to your organization",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(dns.resolved_ips[*] in $<LIST_UUID>)",
"identity": ""
}'要获取您列表的 UUID,请使用 列出 Zero Trust 列表 端点。
CIPA(儿童互联网保护法)过滤器是一系列子类别的集合,涵盖了可能对未成年人有害或不当的广泛主题。它是安全学校网络项目(Project Cybersafe Schools)的一部分,用于拦截对不良或有害在线内容的访问。创建此策略后,您的组织将满足最低限度的 CIPA 合规性 ↗。
| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Content Categories(内容类别) | in(属于) | CIPA Filter | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Turn on CIPA filter",
"description": "Block access to unwanted or harmful online content for children",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(dns.content_category[*] in {182})",
"identity": ""
}'SafeSearch(安全搜索)是搜索引擎的一项功能,可帮助您过滤显性或攻击性内容。您可以在 Google、Bing、Yandex、YouTube 和 DuckDuckGo 等搜索引擎上强制启用 SafeSearch:
| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Content Categories(内容类别) | in(属于) | Search Engines | Safe Search(安全搜索) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Hide explicit search results",
"description": "Force SafeSearch on search engines to filter explicit or offensive content",
"enabled": true,
"action": "safesearch",
"filters": [
"dns"
],
"traffic": "any(dns.content_category[*] in {145})",
"identity": ""
}'通过在策略中添加 基于身份的条件,按每个用户或组配置访问权限。
| 选择器 | 运算符 | 值 | 逻辑 | 动作 |
|---|---|---|---|---|
| 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": "Filter traffic based on a user identity group name",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(app.ids[*] in {606})",
"identity": "any(identity.groups.name[*] in {\"Contractors\"})"
}'过滤 DNS 查询以仅允许特定用户访问。
以下示例包括两个策略。第一个策略允许指定的组,而第二个策略拦截所有其他用户。为确保策略得到正确评估,请将 Allow(允许)策略放在 Block(拦截)策略之上。有关更多信息,请参阅执行顺序。
| 选择器 | 运算符 | 值 | 逻辑 | 动作 |
|---|---|---|---|---|
| Content Categories(内容类别) | in(属于) | Social Networks | And(且) | Allow(允许) |
| User Group Names(用户组名称) | in(属于) | Marketing |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Allow social media for Marketing",
"description": "Allow access to social media sites for users in the Marketing group",
"precedence": 1,
"enabled": true,
"action": "allow",
"filters": [
"dns"
],
"traffic": "any(dns.content_category[*] in {149})",
"identity": "any(identity.groups.name[*] in {\"Marketing\"})"
}'| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Content Categories(内容类别) | in(属于) | Social Networks | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Block social media",
"description": "Block social media for all other users",
"precedence": 2,
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "any(dns.content_category[*] in {149})",
"identity": ""
}'Enterprise 用户可以将这些策略与出口策略(egress policy)结合使用,以控制 Gateway 连接到目的服务器时使用的 IP 版本。
(可选)您可以使用 Domain 选择器来控制特定网站 IP 版本的路由。
通过拦截 AAAA (IPv6) 记录解析,强制用户使用 IPv4 进行连接。
| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Query Record Type(查询记录类型) | is(是) | AAAA | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Force IPv4",
"description": "Force users to connect with IPv4 by blocking IPv6 resolution",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "dns.query_rtype == \"AAAA\"",
"identity": ""
}'通过拦截 A (IPv4) 记录解析,强制用户使用 IPv6 进行连接。
| 选择器 | 运算符 | 值 | 动作 |
|---|---|---|---|
| Query Record Type(查询记录类型) | is(是) | A | Block(阻止) |
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/gateway/rules" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"name": "Force IPv6",
"description": "Force users to connect with IPv6 by blocking IPv4 resolution",
"enabled": true,
"action": "block",
"filters": [
"dns"
],
"traffic": "dns.query_rtype == \"A\"",
"identity": ""
}'