惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
P
Proofpoint News Feed
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
GbyAI
GbyAI
B
Blog RSS Feed
S
SegmentFault 最新的问题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
Webroot Blog
Webroot Blog
S
Security @ Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
博客园 - 聂微东
F
Fortinet All Blogs
H
Hacker News: Front Page
A
About on SuperTechFans
Application and Cybersecurity Blog
Application and Cybersecurity Blog
C
Check Point Blog
V
V2EX
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
N
News | PayPal Newsroom
Cisco Talos Blog
Cisco Talos Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Cloudflare Blog
P
Privacy & Cybersecurity Law Blog
N
Netflix TechBlog - Medium
C
CXSECURITY Database RSS Feed - CXSecurity.com
Recorded Future
Recorded Future
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
美团技术团队
Security Latest
Security Latest
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
Recent Announcements
Recent Announcements
The GitHub Blog
The GitHub Blog
Google Online Security Blog
Google Online Security Blog
T
The Exploit Database - CXSecurity.com

Dejavu's Blog

甲骨文 ARM 实例部署 Gemma 4 模型 Headscale + Tailscale 组建虚拟专用网 在 Linux 上使用 Yubikey OpenPGP 应用 BuyVM VPS 块存储挂载教程 Alpine Linux 服务器配置指南 Alpine Linux 安装 Cloudflared Docker 多容器共享中心数据库 安装 Komari 服务器监控工具 Scaleway VPS 安装 Debian Linux Debian 13 下部署 AsmBB 论坛 使用 Kopia 自动化备份服务器数据 给 Docker 启用 IPv6 支持 Netcup 服务器安装自定义 ISO 镜像 烽火 HG5582A 光猫开启桥接模式 Docker 自托管 Shlink 短链服务 部署 Obsidian LiveSync 实时同步服务指南 我的 2025 年不完全回顾 Linux 下 Intel 核显驱动配置与硬件加速 Fedora Linux 安装配置记录 2025 年优雅地自托管 RSS 服务 Woodpecker CI 和 Gitea 实现 Hugo 自动部署 Gitea/Forgejo 集成 Woodpecker CI/CD 在 Blinko 中使用 Ollama 作为 AI 供应商 Docker 部署 Gitea/Forgejo Plausible CE 启用城市级地理位置识别 Blinko 开源 AI 知识库 Docker 部署指南 Netcup 免税账号注册及购买服务器全记录 Docker 自托管 Cloudreve Pro 私有网盘服务 GiffGaff SIM 卡使用体验和注意事项 简体中文互联网在变得糟糕吗? 如何低成本申请 S/MIME 证书用于个人邮件服务 迁移到 NameCrane Mail 域名邮箱套件服务 香烟与咖啡 在 Windows 中使用 Yubikey 的 OpenPGP 应用 Windows 11 IoT LTSC 养老指南 巧用浏览器生成 HAR 文件批量下载所需网页资源 聊聊静态网站的评论系统 在 Hugo 站点中优雅地展示实况照片 使用 snac 部署一个简洁优雅的 ActivityPub 实例 当 25 岁来临的时候 断网的这两年 从 Bitwarden/Vaultwarden 到 KeePassXC 也许是 Android 上最好的本地播放器 - 椒盐音乐 2023 半年小结 写一个好玩的 Hugo 货币汇率转换短代码 写一个 Hugo 短代码将不同类别的总字数统计转换为书名显示 海信 Touch Lite 墨水屏音乐阅读器的简单体验 从 Debian 11(bullseye) 无感升级到 Debian 12(bookworm) 使用 GitHub Actions 自动提交 URL 到 IndexNow 批量重写 Git 历史提交记录 使用 cgit 托管小小的也很可爱的 Git 服务器 在 Debian 上使用 stagit 和 Nginx 的简单 Git 服务器 托管简单 Git 服务器的一些尝试 使用 stagit 自托管纯静态 Git 存储库 使用 Docker 快速部署单人 Fediverse 实例 我是如何建立自己的个人博客的? 简单、隐私友好的谷歌分析替代品,Plausible 自托管部署指南 服务器使用 Cloudflare CDN 的最佳实践 甲骨文服务器分配并启用 IPv6 地址 Git 版本控制学习笔记(二) Mastodon 服务器批量导入自定义表情 低成本自托管 Mastodon 实例简明指南 自托管服务指南──有了服务器可以做什么? 使用 Miniflux + RSSHub 打造个人 RSS 阅读器 构建支持多种 CPU 架构的 Docker 镜像 一个可自托管的静态短链接应用 别了,二〇二二! Duplicacy CLI 进阶使用指南 Duplicacy CLI 备份工具的基本使用 初探 Cloudflare 零信任 - 通过 Docker 部署 Bitwarden 密码管理器 Yubikey 转运日志及上手指南 初探 Cloudflare 零信任 — 通过 Cloudflare Tunnel 搭建 SSH 聊天室 初探 Cloudflare 零信任 - 通过 Cloudflare Tunnel 访问服务 某科学的 PGP 算号指南 2022 年的 MacBook Air M1 使用体验及应用推荐 使用 Vercel 和 Supabase 自托管 Cusdis 评论系统 为什么你的搜索引擎不好用? 个人在 Windows 上常用软件清单 macOS 基础开发环境设置指南 在 macOS 上使用 Canokey 的 OpenPGP 应用 提取 Windows UWP 应用商店的安装包以供离线安装 Git 版本控制学习笔记(一) 使用 Notion 管理 GitHub Star 项目 使用 Vercel 免费部署 Giscus 评论系统 将博客评论从 Utterances 无缝迁移到 Giscus Canokey 签名和认证应用指南 Canokey 的基本使用指南 Canokey Pigeon 揽件日志及简单开箱 关于 Google Play 保护机制和 DRM 安全等级的那些事 记录一次捡垃圾装机 GitHub 使用 GPG 密钥提交签名认证 使用 Scoop 管理 Windows 下的软件和开发环境 Windows Terminal+PowerShell 7 打造 Windows 下最好用的终端 一个好用的 Telegram 文件批量上传/下载工具 Clash for Windows 优雅地使用 TUN 模式接管系统流量 全平台搭建 Hexo 静态博客指南 尝试将 Manjaro 作为主力操作系统使用 Hexo 进行 SEO 优化的基本指南 使用本地代理给 Git 和 NPM 加速 使用 flowerss 部署 Telegram RSS 订阅机器人
Cloudflare WAF 防护策略简易指南
Dejavu Moe · 2023-02-22 · via Dejavu's Blog

前言

Cloudflare 提供的安全保护,让所有网站管理员都能享受到平等且强大的网站安全防护。在本文中,我们将探讨利用 Cloudflare WAF(Web Application Firewall)规则来保护网站和服务的几个实用方法,希望能为你带来一些启发。如果想了解更深入的实践,建议你仔细阅读 Cloudflare 的官方文档,那里有最详细且最新的信息。

更新提示

Web 应用程序防火墙(Web Application Firewall)是一种防护网络攻击的技术,通过检查和过滤 HTTP 流量中的恶意请求和攻击,保护 Web 应用程序的安全。

Cloudflare WAF 是一种安全防护服务,能够帮助保护您的网站免受常见的网络攻击,如 SQL 注入、跨站脚本攻击、恶意文件上传等。Cloudflare 的免费计划用户可以使用基础的 WAF 功能,并且托管在 Cloudflare 上的每个域名都可以单独配置最多五个 WAF 规则。

Cloudflare WAF 的 基本策略1 有五种:Allow(允许)、Bypass(绕过)、Managed Challenge(托管质询)、JS Challenge(JavaScript 质询)和 Block(阻止),每种策略都有不同的用途和优先级条件:

  • Allow(允许)策略是最宽松的一种,满足条件的流量都直接通过,不进行任何检查,它是额外的「白名单」;

  • Bypass(绕过)策略用于绕过某些规则的检查和阻止,可以用来放行某些特定的流量;

  • Managed Challenge(托管质询) 策略会让访问者在访问您的网站之前需要通过一个托管的验证码来验证其身份。如果用户通过验证,则可以继续访问您的网站,否则将被阻止;

  • JS Challenge(JavaScript 质询)策略与 Managed Challenge 类似,不同的是它要求访问者在访问您的网站之前进行 JavaScript 质询来验证其身份。如果用户通过验证,则可以继续访问您的网站,反之则被阻止;

  • Block (阻止)策略会完全阻止匹配规则的请求,这种策略通常用于防御已知的攻击或威胁,对于 Block 策略,我们使用需要非常谨慎。将正常的访问者阻拦在外与我们的目的是相悖的。

各种策略的优先级并不是绝对的,我们可以手动控制 优先级 顺序,对于 Cloudflare 免费计划,我们每个域名最多只有 5 个规则,上下拖拽规则模块即可控制优先级顺序。

下图是本文设置规则的示例顺序,我们来对 WAF 的五种基本策略一一介绍

Order Priority

注意:不一定 需要设置所有的基本策略,仅取决于您的网站的性质和实际需求。

创建规则

在 Cloudflare 仪表盘上按照下列步骤创建 WAF 防火墙规则

Create Rule

Allow(允许)

Allow 允许 策略放在第一个,使它拥有最高的优先级,可用于后续细粒度的放行规则。

Edit Rule

在上图的规则编辑器里编辑匹配规则,可以即时看到下面 规则表达式 的变化。规则表达式比规则编辑器更高级,使用可视化编辑器难以表现的复杂规则,能够编辑规则表达式来实现。

暂时不会写表达式没关系,现在只需要知道:配置好的规则表达式,可以复制到另一个站点的 WAF 规则里继续使用,也可以分享给别人使用。

在上面的示例中,我们选择了 Known Bots 字段,并且勾选 Value 值为真(True)。

Known Bots 是一个预定义的策略变量,用于检测请求是否来自 Cloudfare 已知的爬虫或机器人,它是一个 Boolean 值:TrueFalse

这个规则允许经过 Cloudflare 验证的爬虫机器人直接通过 WAF,它会使用 Cloudflare 自己维护的可信机器人列表。对大多数网站是一个推荐的策略,不会影响搜索引擎的正常收录。

Bypass(绕过)

Bypass 绕过 策略更适合做第二优先级策略

(cf.tls_client_auth.cert_verified)

下面示例表示:当 TLS 客户端证书经过验证时(一般是 Cloudflare 颁发的源服务器证书),流量直接绕过 WAF 的 部分特性,比如浏览器用户代理阻止策略、浏览器完整性检查策略、速率限制策略等等。

Bypass Rule

需要注意的是 Bypass 策略不能绕过 机器人挑战模式 (Bot Fight Mode) 和 IP 访问策略 ( IP Access Rules)。

Managed Challenge(托管质询)

Managed Challenge 托管质询 策略,是 Cloudflare 自己提供的验证页面。用户访问的时候,首先会看到一个页面询问他们是否是真人。对于正常访客(人类)来说,几乎无感,很适合阻止 潜在的恶意机器人 访问。

Managed Challenge

上面是一个简单的托管质询规则

(not cf.client.bot and cf.threat_score gt 3) or (ip.geoip.country in {"RU" "UA" "T1"}) or (http.x_forwarded_for eq ".")

先了解几个名词:

  • Threat Score:一种反映某个 IP 地址或请求的安全威胁程度的指标,通常用于衡量恶意攻击的可能性和严重性,越大越坏
  • GeoIP:一种根据 IP 地址确定其地理位置信息的方法,如国家、地区、城市等;
  • X-Forwarded-For:HTTP 请求头中的一个字段,用于标识请求经过的代理服务器地址,通常用于在 Web 应用程序中获取客户端的真实 IP 地址,当这个字段为空时,通常意味着请求可能使用了代理或者来自恶意攻击者
  • AndOr:在逻辑运算中,And 表示 逻辑与(同时满足两边条件为真),Or 表示 逻辑或(满足两边任意一个条件即为真)

我们再来看下上面的匹配规则:

  1. 当不是已知机器人(cf.client.bot),且(and)威胁分数(cf.threat_score)大于 3 时,会匹配该规则
  2. 当请求的来源 IP 地址所对应的国家是俄罗斯、乌克兰或者 Tor 匿名网络时,会匹配该规则
  3. 当请求头中的 X-Forwarded-For 字段为空时,也会匹配该规则
  4. 上面三个规则使用逻辑或(Or)连接,代表三个条件任意满足一个就会触发托管质询

提问: 对于上面的策略,在俄罗斯的 Yandex 爬虫机器人会被托管质询阻止访问吗?

答案: 并不会!我们前面设置了优先级最高的 Allow 允许策略,它允许了经过 Cloudflare 验证的爬虫机器人通过 WAF 规则。

JS Challenge(JavaScript 质询)

JavaScript 质询 策略比托管质询还要更严格一点(机器人表示很难受 😭),由于示例规则太长,不方便截图,您可以粘贴到您的 WAF 规则编辑器里查看

(not cf.client.bot and cf.threat_score gt 10) or (http.user_agent eq "") or (http.user_agent eq "") or (http.user_agent contains "fuck") or (http.user_agent contains "lient" and http.user_agent contains "ttp") or (http.user_agent contains "java") or (http.user_agent contains "Joomla") or (http.user_agent contains "libweb") or (http.user_agent contains "libwww") or (http.user_agent contains "PHPCrawl") or (http.user_agent contains "PyCurl") or (http.user_agent contains "python") or (http.user_agent contains "wrk") or (http.user_agent contains "hey/") or (http.user_agent contains "Acunetix") or (http.user_agent contains "apache") or (http.user_agent contains "BackDoorBot") or (http.user_agent contains "cobion") or (http.user_agent contains "masscan") or (http.user_agent contains "FHscan") or (http.user_agent contains "scanbot") or (http.user_agent contains "Gscan") or (http.user_agent contains "Researchscan") or (http.user_agent contains "WPScan") or (http.user_agent contains "ScanAlert") or (http.user_agent contains "Wprecon") or (http.user_agent contains "virusdie") or (http.user_agent contains "VoidEYE") or (http.user_agent contains "WebShag") or (http.user_agent contains "Zeus") or (http.user_agent contains "zgrab") or (http.user_agent contains "zmap") or (http.user_agent contains "nmap") or (http.user_agent contains "fimap") or (http.user_agent contains "ZmEu") or (http.user_agent contains "ZumBot") or (http.user_agent contains "Zyborg") or (http.user_agent contains "attachment") or (http.user_agent eq "undefined")

上面有个新的匹配项 UA(User-Agent),即 用户代理,它指定了发送请求的客户端应用程序或浏览器的名称和版本号。

可以看到示例规则匹配到信誉度不高的 IP 或 UA后,会进行进行 JavaScript 质询。

Block(阻止)

Block 阻止 策略放在最后的优先级,这是较为合理的,下面的截图是部分示例规则

Block Rule

ASN:自治系统号(Autonomous System Number)的缩写,使用 32 位数字分配唯一标识的自治系统(AS),下面是完整的示例规则表达式

(not cf.client.bot and cf.threat_score gt 15) or (ip.geoip.asnum in {59055 59054 59053 59052 59051 59028 45104 45103 45102 37963 34947 211914 134963 63727 63655 61348 55990 269939 265443 206798 206204 200756 149167 141180 140723 139144 139124 136907 131444 45090 137876 133478 132591 132203}) or (http.user_agent contains "80legs") or (http.user_agent contains "Abonti") or (http.user_agent contains "admantx") or (http.user_agent contains "aipbot") or (http.user_agent contains "AllSubmitter") or (http.user_agent contains "Backlink") or (http.user_agent contains "backlink") or (http.user_agent contains "Badass") or (http.user_agent contains "Bigfoot") or (http.user_agent contains "blexbot") or (http.user_agent contains "Buddy") or (http.user_agent contains "CherryPicker") or (http.user_agent contains "cloudsystemnetwork") or (http.user_agent contains "cognitiveseo") or (http.user_agent contains "Collector") or (http.user_agent contains "cosmos") or (http.user_agent contains "CrazyWebCrawler") or (http.user_agent contains "Crescent") or (http.user_agent contains "Devil") or (http.user_agent contains "spider") or (http.user_agent contains "stat") or (http.user_agent contains "Appender") or (http.user_agent contains "Crawler") or (http.user_agent contains "DittoSpyder") or (http.user_agent contains "Konqueror") or (http.user_agent contains "Easou") or (http.user_agent contains "Yisou") or (http.user_agent contains "Etao") or (http.user_agent contains "mail" and http.user_agent contains "olf") or (http.user_agent contains "exabot.com") or (http.user_agent contains "getintent") or (http.user_agent contains "Grabber") or (http.user_agent contains "GrabNet") or (http.user_agent contains "HEADMasterSEO") or (http.user_agent contains "heritrix") or (http.user_agent contains "htmlparser") or (http.user_agent contains "hubspot") or (http.user_agent contains "Jyxobot") or (http.user_agent contains "kraken") or (http.user_agent contains "larbin") or (http.user_agent contains "ltx71") or (http.user_agent contains "leiki") or (http.user_agent contains "LinkScan") or (http.user_agent contains "Magnet") or (http.user_agent contains "Mag-Net") or (http.user_agent contains "Mechanize") or (http.user_agent contains "MegaIndex") or (http.user_agent contains "Metasearch") or (http.user_agent contains "MJ12bot") or (http.user_agent contains "moz.com") or (http.user_agent contains "Navroad") or (http.user_agent contains "Netcraft") or (http.user_agent contains "niki-bot") or (http.user_agent contains "NimbleCrawler") or (http.user_agent contains "Nimbostratus") or (http.user_agent contains "Ninja") or (http.user_agent contains "Openfind") or (http.user_agent contains "Analyzer") or (http.user_agent contains "Pixray") or (http.user_agent contains "probethenet") or (http.user_agent contains "proximic") or (http.user_agent contains "psbot") or (http.user_agent contains "RankActive") or (http.user_agent contains "RankingBot") or (http.user_agent contains "RankurBot") or (http.user_agent contains "Reaper") or (http.user_agent contains "SalesIntelligent") or (http.user_agent contains "Semrush") or (http.user_agent contains "SEOkicks") or (http.user_agent contains "spbot") or (http.user_agent contains "SEOstats") or (http.user_agent contains "Snapbot") or (http.user_agent contains "Stripper") or (http.user_agent contains "Siteimprove") or (http.user_agent contains "sitesell") or (http.user_agent contains "Siphon") or (http.user_agent contains "Sucker") or (http.user_agent contains "TenFourFox") or (http.user_agent contains "TurnitinBot") or (http.user_agent contains "trendiction") or (http.user_agent contains "twingly") or (http.user_agent contains "VidibleScraper") or (http.user_agent contains "WebLeacher") or (http.user_agent contains "WebmasterWorldForum") or (http.user_agent contains "webmeup") or (http.user_agent contains "Webster") or (http.user_agent contains "Widow") or (http.user_agent contains "Xaldon") or (http.user_agent contains "Xenu") or (http.user_agent contains "xtractor") or (http.user_agent contains "Zermelo")

将它粘贴到 WAF 规则表达式编辑器,更容易看到匹配规则:

  1. 如果不是经过 Cloudflare 验证的机器人(not cf.client.bot)并且(and) IP 危险分高于 15 分(cf.threat_score gt 15),会匹配规被封禁;
  2. 如果 ASN 来自 XX 云、YY 云、ZZ 云,会匹配规被封禁;
  3. 如果匹配到后面的一些恶意用户代理(User-Agent),会匹配规被封禁

限制

Cloudflare WAF 针对每条规则表达式都有长度限制。这个限制取决于您的 Cloudflare WAF 套餐类型,不同套餐计划的用户的规则表达式长度限制也不同:

  • Free Plan: 2,000 字符(你我他都在这里😭)
  • Pro Plan: 4,000 字符
  • Business Plan: 8,000 字符
  • Enterprise Plan: 16,000 字符

如果您的规则表达式超过了限制,您可能需要缩短规则或将其分解成多个规则以确保其可用性。

维护规则

通过上面的一些简单示例,我们已经熟悉了 WAF 基本策略的使用。在日常维护中,我们需要常关注 WAF 防火墙日志和事件:

WAF Event

WAF 的目的是为了过滤不正常的流量,保护正常的访问,同时保证正常访问不被规则「误杀」,造成用户体验下降。举个例子,在上面的演示规则中,我就发现出现「误杀」情况了

Wrong Block

发生什么了呢?来看看 WAF 事件日志

Lookup Event

显然,这是一个运行在 XX 云(新加坡)服务器上的 RSS 订阅服务,被上面 Block 阻止规则的 ASN 匹配后封禁了。

我不打算解禁 XX 云,又希望访客能正常在他们的服务器上通过 RSS 订阅我的博客,怎么办呢?

别忘了,我们还有个设置优先级最高的 Allow 允许策略啊!再了解几个名词:

  • Hostname:主机名,指网络上的一个设备或服务的名称,通常与域名对应
  • URI:Uniform Resource Identifier 的缩写,是一个标识某个资源的字符串,可以是 Web 地址(URL)的一部分
  • URI Path:请求URI中的路径部分

Allow RSS

在之前的 Allow 允许策略里,我添加了一条策略

(cf.client.bot) or (http.host eq "www.dejavu.moe" and http.request.uri.path eq "/index.xml")

如果访问我博客的域名,并且访问资源路径是 /index.xml,则直接通过 WAF,由于它的优先级最高,因此不会受后面其他规则的影响。

怎么样, 现在您应该知道怎么维护规则的 健壮性 了吧。

CSR(质询分数率)

在 WAF 规则的管理页面,我们可以注意到 CSR (Challenge Score Rate),它是一个百分比值,用于表示被Cloudflare WAF 拦截的 HTTP 请求中触发 JavaScript 质询、旧版 CAPTCHA 或托管质询的比例。

CSR 的值 越小越好,比如,CSR为 1% 表示每 100 个 HTTP 请求中有 1 个请求被 Cloudflare WAF 拦截并要求进行质询验证。此指标可帮助我们评估 WAF 规则的有效性,以及是否需要对规则的标准或操作进行调整。

为了保证正常访客的体验,我们建议保证它的值在 0%~ 3% 的范围,如果超过这个范围,一般表示规则需要进行调整。

其他规则

WAF 规则菜单里还有些规则,免费计划可配置性比较有限,粗略介绍一下

Rate limiting rules

Rate Limiting 速率限制规则,用于限制特定客户端在一定时间内对您网站的请求数。可以减轻您的服务器负载以应对恶意攻击。比如:

(not cf.bot_management.verified_bot and http.request.uri.path eq "/")

后面可以根据需要定制限速规则,还可以自定义响应码2、时间段、有效期等参数。

注意:限速规则也可能对合法的请求进行限制,请谨慎配置。

DDoS Protect

DDoS 保护策略,按照下图配置即可,Cloudflare 这个规则很精准的,基本没「误杀」情况

DDoS Protect

免费计划差不多就这样吧,请尽情享受!有钱可以支持付费版 Cloudflare,可以获得更高级、更多的 WAF 规则。