最近网站经常遇到扫描器探测、恶意爬虫、CC 请求等骚扰,于是整理了一套 Cloudflare WAF 自定义规则(共 5 条),目前运行效果非常稳定,分享给大家参考。

我的 WAF 配置思路整体策略是:
- 先放行可信流量(白名单)
- 再拦截明确恶意行为与恶意 ASN(扫描器/UA)
- 对不确定流量进行 Challenge 验证
- 对高风险地区进行托管质询
规则清单(5 条)
1️⃣ 白名单|管理员 IP 放行
目的:避免自己登录后台时被误伤
规则:
(ip.src eq 127.0.0.1)
动作:
✅ Skip(跳过 WAF 检查)
要跳过的 WAF 组件
所有其余自定义规则
所有速率限制规则
所有托管规则
所有超级自动程序攻击模式规则
2️⃣ 白名单|SEO 与静态资源放行
目的:保证搜索引擎正常抓取,静态资源不被挑战
放行内容包括:
rss.xmlsitemap.xmlrobots.txt- favicon 图标
- PWA 配置文件等
规则:
(cf.client.bot) or (http.request.uri.path in {"/rss.xml" "/sitemap.xml" "/robots.txt" "/favicon.ico" "/favicon.svg" "/favicon-96x96.png" "/apple-touch-icon.png" "/site.webmanifest" "/web-app-manifest-192x192.png" "/web-app-manifest-512x512.png"})
动作:
✅ Skip
要跳过的 WAF 组件
所有其余自定义规则
建议:
这条非常重要,否则容易误伤 Google/Bing 收录。且文件路径需自行修改。
3️⃣ 拦截|ASN 与已知恶意扫描器 UA
目的:直接阻断明显攻击工具
匹配 UA 包括:
masscannmapzgrabWPScansqlmap- 等扫描器关键词
匹配 ASN 包括:
- 恶意 ASN
- 中国国内云服务商 ASN(如阿里云,腾讯云,华为云等)
规则:
(http.user_agent eq "") or (http.user_agent eq "undefined") or (http.user_agent contains "masscan") or (http.user_agent contains "nmap") or (http.user_agent contains "zmap") or (http.user_agent contains "zgrab") or (http.user_agent contains "WPScan") or (http.user_agent contains "sqlmap") or (http.user_agent contains "fimap") or (http.user_agent contains "Acunetix") or (http.user_agent contains "FHscan") or (http.user_agent contains "Gscan") or (http.user_agent contains "Researchscan") or (http.user_agent contains "Wprecon") or (http.user_agent contains "BackDoorBot") or (http.user_agent contains "Zeus") or (ip.src.asnum in {210644 216246 211522 214351 213194 214196 44477 215789 214943 48589 202685 57523 136897 45104 45103 45102 37963 59055 59054 59053 59052 59051 59028 269939 206798 45090 132203 132591 133478 131444 63727 63655 61348 134963 34947 55990 141180 139144 139124 137876 140723 136907 211914 149167 206204 200756 398324 14618 10912 24940 14061 16276 36352 53667 60781 5065 6207 35624 43444 198571 33993 209847 35478 58854 138915 140666 265443})
动作:
⛔ Block(阻止)
建议:
这条也非常重要,否则容易误伤百度收录,因为这条规则屏蔽了百度的 ASN,不过我想各位mjj的内容百度也过不了申😂还有屏蔽了国内几乎所有大厂的 ASN,如果你需要可以自行去掉。
4️⃣ 验证|灰色 UA 与自动化行为
目的:对疑似自动化访问进行 JS Challenge
匹配 UA 示例:
javapythonlibwwwPyCurlPHPCrawlwrkhey
规则:
(http.user_agent contains "java")
or (http.user_agent contains "python")
or (http.user_agent contains "libwww")
or (http.user_agent contains "libweb")
or (http.user_agent contains "PyCurl")
or (http.user_agent contains "PHPCrawl")
or (http.user_agent contains "wrk")
or (http.user_agent contains "hey/")
or (http.user_agent contains "apache")
or (http.user_agent contains "scanbot")
动作:
⚠️ JS Challenge(JS 质询)
说明:
比直接 Block 更温和,可以减少误杀。
5️⃣ 验证|高风险国家访问
目的:对高风险地区流量进行托管质询
规则:
- 国家/地区属于 RU、UA 等
规则:
(ip.src.country eq "RU") or (ip.src.country eq "UA") or (ip.src.country eq "T1")
动作:
⚠️ Managed Challenge(托管质询)
📊运行效果(24h 数据)
从规则触发情况来看:
- 管理员白名单放行:1.34k
- SEO 静态资源放行:192
- 扫描器拦截:22
- 灰度验证暂未触发
- 高风险国家验证暂未触发
说明目前攻击主要集中在扫描器探测阶段。
⚡总结建议
这套规则适合大多数站点直接套用:
✅ 不误伤自己
✅ 不影响 SEO
✅ 扫描器直接拦截
✅ 灰色流量用 Challenge 缓冲
✅ 高风险地区加一道门槛
好东西bd
一直用的这套
https://github.com/sefinek/Cloudflare-WAF-Expressions
收藏了 赞
我都不建站
收藏学习
好东西
收藏学习!强!
还可以添加一个防止CC的
赞
收藏