限制访问频率

为了防止恶意刷取图片请求,可以设置WAF的速率限制规则。

表达式如下,请求次数根据需求设置,动作选择Block(直接阻止) 或 JS Challenge(JS 验证,防止机器人)

1
(ends_with(http.request.uri.path, ".jpg")) or (ends_with(http.request.uri.path, ".png")) or (ends_with(http.request.uri.path, ".webp")) 

防盗链

防盗链的目的是允许特定的网站引用,防止其他网站盗取图片。

设置WAF的自定义规则,表达式如下,动作选择阻止。

1
(not http.referer contains "suzi.xyz" and http.host eq "pic.987545.xyz")

使用下面命令进行测试,这是没有设置防盗链之前的,可以直接下载,状态代码200,加了之后在测试就是403

curl -I "https://pic.937564.xyz/bb46021399c5ababd6b23c39332a3abe.jpg"

设置防盗链之后,使用下面命令才能下载成功,状态代码200

curl -H "Referer: https://suzi.xyz" -I "https://pic.987545.xyz/bb46021399c5ababd6b23c39332a3abe.jpg"

限制非浏览器访问

防止一些爬虫,机器人,python脚本等非正常访问,可以使用WAF的User-Agent进行限制。

1. 常见浏览器 User-Agent(允许)

这些是主流浏览器的 User-Agent,可以允许它们访问 pic.987545.xyz

浏览器 User-Agent 关键字
Google Chrome "Mozilla/5.0 (Windows NT" "Chrome/"
Mozilla Firefox "Mozilla/5.0 (Windows NT" "Firefox/"
Microsoft Edge "Mozilla/5.0 (Windows NT" "Edg/"
Safari (Mac & iOS) "Mozilla/5.0 (Macintosh" "Safari/"
Opera "Opera/" "OPR/"
Brave 浏览器 "Chrome/" "Brave"

Cloudflare WAF 规则(仅允许浏览器访问)

1
not (http.user_agent contains "Mozilla" or http.user_agent contains "Chrome" or http.user_agent contains "Safari" or http.user_agent contains "Firefox" or http.user_agent contains "Edg" or http.user_agent contains "Opera")
  • 这条规则会阻止所有非浏览器访问,如 curlwget 等工具。

2. 机器人和爬虫(可阻止)

这些是常见的 爬虫/机器人 User-Agent,可以阻止它们访问 pic.937564.xyz

爬虫/机器人 User-Agent 关键字
Googlebot "Googlebot"
Bingbot "bingbot"
Baidu Spider "Baiduspider"
Yandex Bot "YandexBot"
Sogou Spider "Sogou"
DuckDuckGo Bot "DuckDuckBot"
Ahrefs Bot "AhrefsBot"
MJ12 Bot "MJ12bot"
Semrush Bot "SemrushBot"
SEMrush Bot "SEMrushBot"
PetalBot (华为搜索) "PetalBot"
Python Requests "python-requests"
Scrapy 爬虫 "Scrapy"
Java 爬虫 "Java/"
Go 爬虫 "Go-http-client"
wget "wget"
curl "curl"

Cloudflare WAF 规则(阻止爬虫)

1
http.user_agent contains "bot" or http.user_agent contains "spider" or http.user_agent contains "AhrefsBot" or http.user_agent contains "Scrapy" or http.user_agent contains "Java/" or http.user_agent contains "Go-http-client" or http.user_agent contains "curl" or http.user_agent contains "wget" or http.user_agent contains "python-requests"
  • 这条规则会阻止爬虫、爬取工具、脚本等非正常用户访问

3. 非浏览器工具(应阻止)

这些是自动化工具、漏洞扫描工具或黑客常用工具的 User-Agent,建议拦截。

工具/软件 User-Agent 关键字
curl 命令行 "curl"
wget 命令行 "wget"
Python requests "python-requests"
Scrapy "Scrapy"
Java 客户端 "Java/"
Go HTTP Client "Go-http-client"
Nmap Scanner "nmap"
ZGrab Scanner "ZGrab"
Nikto Scanner "Nikto"
sqlmap(SQL 注入工具) "sqlmap"

Cloudflare WAF 规则(阻止工具访问)

1
http.user_agent contains "curl" or http.user_agent contains "wget" or http.user_agent contains "python-requests" or http.user_agent contains "Java/" or http.user_agent contains "Go-http-client" or http.user_agent contains "Scrapy" or http.user_agent contains "sqlmap" or http.user_agent contains "nmap" or http.user_agent contains "ZGrab" or http.user_agent contains "Nikto"
  • 这条规则可以有效拦截爬取工具、漏洞扫描工具等非法请求。

4. 组合 WAF 规则

你可以在 Cloudflare WAF 里 同时应用多个规则,达到最佳效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
(
not (
http.user_agent contains "Mozilla" or
http.user_agent contains "Chrome" or
http.user_agent contains "Safari" or
http.user_agent contains "Firefox" or
http.user_agent contains "Edg" or
http.user_agent contains "Opera"
)
)
or (
http.user_agent contains "bot" or
http.user_agent contains "spider" or
http.user_agent contains "AhrefsBot" or
http.user_agent contains "Scrapy" or
http.user_agent contains "Java/" or
http.user_agent contains "Go-http-client" or
http.user_agent contains "curl" or
http.user_agent contains "wget" or
http.user_agent contains "python-requests"
)

效果

允许正常浏览器访问
🚫 阻止爬虫/机器人(Googlebot、Baiduspider 等)
🚫 阻止非浏览器工具(curl、wget、Python requests、sqlmap 等)

测试

设置好User-Agent后,使用下面命令就会出现403的错误

curl -H "Referer: [https://suzi.xyz"](https://suzi.xyz") -I "https://pic.987545.xyz/bb46021399c5ababd6b23c39332a3abe.jpg"

如果防盗链检查更严格,还需要模拟浏览器的 User-Agent。你可以加上 -A 选项伪装成常见的浏览器,这样状态就会是200了。

curl -H "Referer: [https://suzi.xyz"](https://suzi.xyz") -A "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" -I "https://pic.987545.xyz/bb46021399c5ababd6b23c39332a3abe.jpg"


结论

  • 如果你的 pic.987545.xyz 仅供 suzi.xyz 访问,你可以直接设置防盗链,不需要太严格的 User-Agent 限制。

  • 如果担心爬虫或恶意刷流量,可以结合 WAF 速率限制 + User-Agent 规则,提高安全性。

你可以在 Cloudflare WAF 中设置 挑战(JS Challenge) 而不是直接 Block,这样可以避免误伤一些合法用户。