Cloudflare屏蔽机器人和漏洞利用的WAF规则

2024-09-14 438 0

用法

在Cloudflare WAF里添加自定义规则,点击Edit expressions,直接粘贴下面的规则。

1. 机器人屏蔽(直接block)

  • 阻止流行的不良用户代理
  • 按端口阻止连接(通常只有僵尸bot会这样做)
  • 阻止过时版本的 HTTP (1.0)
  • 阻止 cloudflare 标记的不良威胁
  • 阻止不良方法请求
  • 拦截可疑的 X-Forwarded-For
  • 阻止来自 Tor 网络的请求
  • 拦截 ASN 列表中最著名的代理搜刮网站
  • 阻止非标准 cookie

下面的代码块很长,注意全选:

V1

(http.request.version in {"HTTP/1.0"} and not cf.client.bot) or (http.user_agent eq "") or (http.user_agent eq " ") or (http.user_agent eq "-") or (http.user_agent eq "'") or (http.user_agent contains "/x/") or (http.user_agent contains "'XOR(") or (http.user_agent contains "ALittle") or (http.user_agent contains "got (") or (http.user_agent contains "quic-go-HTTP") or (http.user_agent contains "Go-http-client") or (http.user_agent contains "fasthttp") or (http.user_agent contains "python") or (http.user_agent contains "java") or (http.user_agent contains "PHP") or (http.user_agent contains "Nmap") or (http.user_agent contains "scrapy" and not cf.client.bot) or (http.user_agent contains "spider" and not cf.client.bot) or (http.user_agent contains "crawl" and not cf.client.bot) or (http.user_agent contains "bot" and not http.user_agent contains "bing" and not http.user_agent contains "google" and not http.user_agent contains "yandex" and not http.user_agent contains "duckduckgo" and not http.user_agent contains "facebook" and not http.user_agent contains "linkedIn" and not http.user_agent contains "twitter" and not http.user_agent contains "yahoo" and not cf.client.bot) or (cf.threat_score ge 20 and not cf.client.bot) or (http.request.method in {"PURGE" "PUT" "OPTIONS" "DELETE" "PATCH"}) or (http.x_forwarded_for contains "192.0.") or (http.x_forwarded_for contains ".0.0") or (ip.geoip.country in {"T1" "XX"} and not http.request.version in {"HTTP/2" "HTTP/3" "SPDY/3.1"} and not cf.client.bot) or (http.user_agent contains "lient" and http.user_agent contains "ttp") or (http.user_agent contains "libweb") or (http.user_agent contains "libwww") or (http.user_agent contains "wrk") or (http.user_agent contains "hey/") or (ip.geoip.asnum in {14061 60631 28438 60592 30823 4134 32505 27715 22773 131090 135905 55330 16629 4755 53363 34549 135330 47285 60798 207590 203087 198651 43289 14576 207319 201978 208425 201094 18978 52000 204601 199883 8220 36351 45011 8560 23969 45629 20207 6471 8075 45899 31400 208556 12271 7552 26496 21769 6876 45102 5617 199490 35816 131293 20860 31898 131428 8881 25429 29802 4788 3326 39284 13448 46484 174 577 29286 5056 9009 63949 212708 40788 12989 11351 11426 7029 42652 18403 54538 209 62044 3269 395003 8100 4190 12874 19740 197540 45458 136258 50837 51852 4826 195 49588 57613 34248 197099 29287 29066 30083 9534 42905 35804 45012 7303 25961 61317 5610 35320 262187 263693 20552 266706 49327 47232 32098 28429 3255 28431 14117 18734 24088 263196 41096 52228 8069 398101 28725 132196 61154 58199 6877 265537 32097 62240 3329 6830 133199 12334 270110 22884 54600 213375 206092 41009 213251 36444} and not http.request.version in {"HTTP/2" "HTTP/3" "SPDY/3.1"} and not cf.client.bot) or (http.host contains ":80") or (http.host contains ":443") or (http.cookie contains "cf_use_ob=" and not http.cookie contains "0" and not http.cookie contains "80" and not http.cookie contains "443" and not cf.client.bot)

V2

(
    http.request.version in {"HTTP/1.0"} and not cf.client.bot
) or (
    http.user_agent in {"" " " "-" "'"}
) or (
    http.user_agent contains "/x/"
) or (
    http.user_agent contains "'XOR("
) or (
    http.user_agent contains "ALittle"
) or (
    http.user_agent contains "got ("
) or (
    http.user_agent contains "quic-go-HTTP"
) or (
    http.user_agent contains "Go-http-client"
) or (
    http.user_agent contains "fasthttp"
) or (
    http.user_agent contains "python"
) or (
    http.user_agent contains "java"
) or (
    http.user_agent contains "PHP"
) or (
    http.user_agent contains "Nmap"
) or (
    http.user_agent contains "scrapy" and not cf.client.bot
) or (
    http.user_agent contains "spider" and not cf.client.bot
) or (
    http.user_agent contains "crawl" and not cf.client.bot
) or (
    http.user_agent contains "bot" and 
    not (http.user_agent contains "bing" or 
         http.user_agent contains "google" or 
         http.user_agent contains "yandex" or 
         http.user_agent contains "duckduckgo" or 
         http.user_agent contains "facebook" or 
         http.user_agent contains "linkedin" or 
         http.user_agent contains "twitter" or 
         http.user_agent contains "yahoo") and 
    not cf.client.bot
) or (
    cf.threat_score ge 20 and not cf.client.bot
) or (
    not http.request.method in {"GET" "POST" "HEAD"}
) or (
    http.x_forwarded_for contains "192.0." or 
    http.x_forwarded_for contains ".0.0"
) or (
    ip.geoip.country in {"T1"} and 
    not http.request.version in {"HTTP/2" "HTTP/3" "SPDY/3.1"} and 
    not cf.client.bot
) or (
    http.user_agent contains "lient" and http.user_agent contains "ttp"
) or (
    http.user_agent contains "libweb"
) or (
    http.user_agent contains "libwww"
) or (
    http.user_agent contains "wrk"
) or (
    http.user_agent contains "hey/"
) or (
    ip.geoip.asnum in {
        14061 60631 28438 60592 30823 4134 32505 27715 22773 
        131090 135905 55330 16629 4755 53363 34549 135330 47285 
        60798 207590 203087 198651 43289 14576 207319 201978 
        208425 201094 18978 52000 204601 199883 8220 36351 45011 
        8560 23969 45629 20207 6471 8075 45899 31400 208556 
        12271 7552 26496 21769 6876 45102 5617 199490 35816 
        131293 20860 31898 131428 8881 25429 29802 4788 3326 
        39284 13448 46484 174 577 29286 5056 9009 63949 212708 
        40788 12989 11351 11426 7029 42652 18403 54538 209 
        62044 3269 395003 8100 4190 12874 19740 197540 45458 
        136258 50837 51852 4826 195 49588 57613 34248 197099 
        29287 29066 30083 9534 42905 35804 45012 7303 25961 
        61317 5610 35320 262187 263693 20552 266706 49327 
        47232 32098 28429 3255 28431 14117 18734 24088 263196 
        41096 52228 8069 398101 28725 132196 61154 58199 6877 
        265537 32097 62240 3329 6830 133199 12334 270110 
        22884 54600 213375 206092 41009 213251 36444
    } and 
    not http.request.version in {"HTTP/2" "HTTP/3" "SPDY/3.1"} and 
    not cf.client.bot
) or (
    http.cookie contains "cf_use_ob=" and 
    not (http.cookie contains "0" or 
         http.cookie contains "80" or 
         http.cookie contains "443") and 
    not cf.client.bot
)

触发上述规则后的动作请选:block

2. 漏洞利用修复屏蔽(直接block)

  • 阻止 SQL 漏洞利用
  • 阻止 XSS 漏洞利用
  • 阻止流行的 PHP 漏洞利用

V1

(http.request.uri.query contains ")/*") or (http.request.uri.query contains ")--") or (http.request.uri.query contains "benchmark(") or (http.request.uri.query contains "'0:0:20'") or (http.request.uri.query contains "MD5(") or (http.request.uri.query contains "%20waitfor%20delay%20") or (http.request.uri.query contains "%22") or (http.request.uri.query contains "%20/*") or (http.request.uri.query contains "%20--") or (http.request.uri.query contains "%20%23") or (http.request.uri.query contains ")%23") or (http.request.uri.query contains "script>") or (http.request.uri.query contains "%40") or (http.request.uri.query contains "%00") or (http.request.uri.query contains "

V2

(http.request.uri.query contains ")/*") or (http.request.uri.query contains ")--") or (http.request.uri.query contains "benchmark(") or (http.request.uri.query contains "'0:0:20'") or (http.request.uri.query contains "MD5(") or (http.request.uri.query contains "%20waitfor%20delay%20") or (http.request.uri.query contains "%22") or (http.request.uri.query contains "%20/*") or (http.request.uri.query contains "%20--") or (http.request.uri.query contains "%20%23") or (http.request.uri.query contains ")%23") or (http.request.uri.query contains "script>") or (http.request.uri.query contains "%40") or (http.request.uri.query contains "%00") or (http.request.uri.query contains "

触发上述规则后的动作请选:block

3. 可选,可疑的methods(强制验证码 不要直接block)

(http.user_agent eq "109e15941c57") or (http.user_agent eq "d1b2df322c91") or (http.request.uri.query eq "--+") or (http.user_agent eq "84bd2cfee733") or (http.request.uri.query eq "d=1") or (http.user_agent eq "Mozilla/5.0 (compatible; AhrefsBot/7.0; +http://ahrefs.com/robot/)") or (http.request.uri.query eq "daksldlkdsadas=1") or (http.request.full_uri contains "\\x03\\x00\\x00/*\\xE0\\x00\\x00\\x00\\x00\\x00Cookie: mstshash=Administr") or (http.request.full_uri contains "\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00") or (http.request.full_uri contains "T\\x83\\xF8\\xCCu\\x18\\xA8\\xABw*w\\xF5j\\x91\\xE4[") or (http.request.full_uri contains "-\\x11\\xBERB#:\\xE4.\\xC6\\xFFHA\\x1A\\x03\\xD7") or (http.request.full_uri contains "MGLNDD_") or (http.request.full_uri contains "\\x03\\x00\\x00\\x13\\x0E\\xE0\\x00\\x00\\x00\\x00\\x00\\x01\\x00\\x08\\x00\\x03\\x00\\x00\\x00") or (http.request.full_uri contains "fI4y") or (http.request.full_uri contains "o\\xFA\\xC0\\xBE\\xB8\\xC0\\xA4\\xC9\\x89\\xA2\\xC2\\x8F\\x83\\xAF\\x91\\x97\\xBE\\xCD\\xB9\\xCF\\xAC\\x9B\\xB0\\xAB\\xA0\\xB6\\xB1\\xAA\\x9D\\x9C\\x9F\\x96\\x8D\\x93\\xCE\\xB4\\xB3\\xB5\\x98\\xCD\\xA6\\xFA\\xFA\\xFA\\xFA\\x12\\xFD\\xD8\\xF8\\xFA\\xFA\\xC2\\xFA\\xFA\\xFA\\xFA\\x1Af\\xEC\\xF9\\xFA\\xFA\\xFA\\xFA\\xFB\\xE5q\\xF2\\xFA\\xFA\\xFA\\xFA\\xFA\\xFA\\xF9wh\\x97ui\\xBA\\xEA=E\\xF0\\x1B/\\xA7XJ\\xF11Y\\x0B\\xBF\\xB1K\\x1F\\x00\\xFA\\xF8\\xAF5Y\\xDB\\xA1\\xA2 \\xE00\\xCC\\xBAU]\\xBCrhc@[\\x9Cz\\xEA\\x8AJ|\\x8F\\xEF\\x86V\\x11\\xDC\\xBB\\x5C\\xF8T\\xF3=\\x9B\\xAF\\x11\\xBD8\\x96\\xAD\\xE7e~ov\\xCC\\xB6\\xCA\\xDE\\xB78\\xDC\\xD88w9\\x91\\x8C\\xD1\\xDE/\\x98\\xCA\\x8D%\\xDC\\x85+sb\\xAE\\xE5&\\xCA\\x08\\x06\\xFF\\x9Ev\\xA5\\x96\\xED\\x0F\\xBC\\xEA2\\xFA\\x1F7\\x03\\xC9g\\x83)TF$H\\xA8\\xD2\\xA24\\x91\\x80\\xABg\\x0CF+\\xBFx*w\\x19\\x01\\x0E\\xFF\\xCF\\x1B\\xA8\\x9AJrF.\\x0B\\x9D\\x84\\xF2\\xEE\\x80Y\\x18\\xD4\\x12\\xFE\\x14\\x89\\x9B\\x8C\\x9AL6\\x17\\x09\\xF25\\x5C\\xEDb\\x02\\x89\\xCD\\xA7|\\xC9zL\\x97\\x81\\x92\\x96\\xA3\\xC4g\\xB4(\\xE3k\\x82Gk\\xC1\\x90B\\xE6][\\xE1\\x02\\x9B\\x86?Tua\\x1C\\xE0\\xFC\\x9F\\x8D\\xEB\\x01\\xAB\\xC0\\xE5\\xD6\\x98\\xD5\\xE0

触发上述规则后的动作请选:interactive challenge

4. 可选,可疑的网络和客户端(强制验证码 不要直接block)

  • 检查过期的 HTTP 版本(1.1、1.2)
  • 检查允许大量恶意流量的国家/地区
  • 检查 cloudflare 标记的不良威胁
  • 检查不安全请求(非 SSL 请求)
  • 检查来源不明的请求(无引用源)

V1

(http.request.version in {"HTTP/1.1" "HTTP/1.2"} and not http.request.version in {"HTTP/2" "HTTP/3" "SPDY/3.1"} and not ip.geoip.asnum in {13238 15169 8075 32934} and not cf.client.bot) or (cf.threat_score ge 10 and not cf.client.bot) or (not ssl) or (ip.geoip.continent in {"AF" "AS" "AN" "EU" "NA" "OC" "SA" "RU" "MD" "BY" "UA"} and not ip.geoip.asnum in {13238 15169 8075 47541 32934} and not cf.client.bot) or (http.referer eq "" and not cf.client.bot)

V2

(
    http.request.version in {"HTTP/1.1" "HTTP/1.2"} and 
    not http.request.version in {"HTTP/2" "HTTP/3" "SPDY/3.1"} and 
    not ip.geoip.asnum in {13238 15169 8075 32934} and 
    not cf.client.bot
) or (
    cf.threat_score ge 10 and not cf.client.bot
) or (
    not ssl
) or (
    ip.geoip.continent in {"AF" "AS" "AN" "EU" "NA" "OC" "SA" "RU" "MD" "BY" "UA"} and 
    not ip.geoip.asnum in {13238 15169 8075 47541 32934} and 
    not cf.client.bot
) or (
    http.referer eq "" and not cf.client.bot
)

触发上述规则后的动作请选:interactive challenge

cloudflare-rules基础上做了一些改进。

8G Firewall

另外推荐8G Firewall(和上面的CF没有关系)适合WP站,但别的站也可以用。

说明 https://perishablepress.com/8g-firewall
下载 https://perishablepress.com/sdc_download/20218/?key=yss7r75q6b2jddp9806qf4edn2dq8g

把这些内容添加到 .htaccess 里就行了。

@IPLark #20 发布于2024/7/3 08:24:27
首先感谢楼主分享,不得不说一下,互联网上针对Cloudflare WAF规则的公开分享属实不多,楼主分享的是我目前见过的算是最全面的规则集(之一)了。
xhj009:

大致看了下楼主发的几条WAF规则,由于内容稍微有点多,这次仅对规则1谈谈个人管见。

1.可能存在小概率的误封正常访客的风险。

举个简单例子,规则提到的

or (ip.geoip.country in {"T1" "XX"}

封禁TOR算是常规操作,但是封禁XX可能会误封一部分vpn用户。首先,我们要知道Cloudflare使用的是Maxmind的IP数据库,而Maxmind的数据库对IP的覆盖率其实并不是100%,在某些极少数情况下,访客的IP可能没被Maxmind数据库覆盖,这种情况下CF会把该IP列为XX(Unknown states, other entities or organizations)。举个简单例子,比如Opera内置VPN的IP(77.111.247.0/24)在Maxmind数据库就没有位置信息,启用规则1将导致这类正常用户被挡在防火墙外。。

2.部分规则不起作用。

如规则1中的

(http.host contains ":80") or (http.host contains ":443")

实际上不具备任何作用,该规则的本意可能是封禁诸如 example.com:80 和 example.com:443 之类的非正常请求,但是很可惜,用上述规则,无法匹配这类情况。

此外就是

(cf.threat_score ge 20 and not cf.client.bot)

threat score这个参数在现今已不具备太大意义,大部分IP的threat score都为0,为此CF推出了一套新的机制,可参见(https://community.cloudflare.com/t/cf-threat-score-being-deprecated/677049

3.部分规则可以精简合并。

比如

(http.request.method in {"PURGE" "PUT" "OPTIONS" "DELETE" "PATCH"})

不如

(not http.request.method in {"GET" "POST" "HEAD"} 

精简

4.部分规则有漏洞。

(http.user_agent contains "bot" and not http.user_agent contains "bing" and not http.user_agent contains "google" and not http.user_agent contains "yandex" and not http.user_agent contains "duckduckgo" and not http.user_agent contains "facebook" and not http.user_agent contains "linkedIn" and not http.user_agent contains "twitter" and not http.user_agent contains "yahoo" and not cf.client.bot)

假设伪造一个UA,UA值为

Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/W.X.Y.Z Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

则屏蔽规则将起不到预期作用,所以应该改变匹配机制。

总的来说楼主分享的规则具有一定参考意义和实际价值,再次感谢。 :xhj003:

相关文章

面向AI编程:使用Claude-3.7写一个文章归档页面
使用AI为博客新增网址导航功能
一键屏蔽垃圾爬虫,包括Censys、facebook、SemrushBot、GPTbot
WordPress 条件判断标签及用法大全
curl及wget命令使用教程
Linux实现免密登录以及密钥生成方法

发布评论