个人网站安全防护

前阵子提到给网站接入日志监控后,发现每天都有人恶意扫描,扫描后一大堆 404 的访问记录,浪费带宽不说,还影响我排错,网络世界很险恶,总有刁民想害朕。

以前我只是简单部署了个 fail2ban 防止 ssh 爆破,无奈又加上日志分析。

类似这样,每天能抓到 10 多个恶意 ip。

1root@tokyo:~# cat /etc/fail2ban/filter.d/caddy-json-scan.conf
2[Definition]
3# 匹配 status=404 且 uri 以 .php、.cgi、.pl 等结尾的请求
4# 提取 remote_addr 中的 IP(自动忽略端口)
5failregex = ^.*"remote_ip":"<HOST>".*"uri":"[^"]*\.(?i:php|cgi|pl|asp)[^"]*".*"status":404.*
6ignoreregex =

这样写正则表达式,碰到那些扫描压缩包的又不行了。比如很多攻击会扫描.zip/.tar/.gz 等,HEAD 请求,再写一次匹配规则很繁琐。

而且 fail2ban 内存占用较多,测试命中结果慢,只能寻找替代方案。

搜了下 CrowdSec 能担当起这个责任,它是一个开源的、基于社区协作的网络安全工具,用于检测和阻止恶意行为(如暴力破解、扫描、Web 攻击等)。其通过分析日志文件实时识别可疑 IP 地址,并可与全球用户共享威胁情报(在用户同意的前提下),从而形成一个“集体免疫”式的防御网络。解析 Caddy 日志、SSH/FTP 暴力破解防御等也不在话下,感觉很适合个人使用。

1. CrowdSec

安装使用很简单:

1curl -s https://install.crowdsec.net | sudo bash
2sudo apt update
3sudo apt list --upgradable
4sudo apt install crowdsec -y
5sudo apt install crowdsec-firewall-bouncer-nftables -y

其中的 crowdsec-firewall-bouncer-nftables 是 CrowdSec 官方提供的一个 bouncer(拦截器),用于将 CrowdSec 检测到的恶意 IP 地址自动添加到 Linux 的 nftables 防火墙规则中,从而实现网络层的实时封禁。

安装的过程中,CrowdSec 会自动识别你系统有哪些服务(比如 Postgres、sshd、Nginx 等),自动加载对应的防护模块,相比 fail2ban 手编正则还是方便不少。

也就是说,恶意 ip 被检测到后,访问请求包就被系统丢弃了,根本不会到达 Caddy 服务,恶意扫描记录会大幅减少。bouncer 也可以在应用层级来做,比如 Caddy 就有对应的拦截器:https://github.com/hslatman/caddy-crowdsec-bouncer, 意思就是在 webserver 层级进行拦截,相比系统拦截,能进行更多定制化操作。

我比较痛恨这些恶意扫描的,直接就系统级丢弃了。每个月主机厂商都会准时给我账单,开站不赚钱好心为人民服务还被攻击,换谁都不乐意。

看下面的输出,感受下这个世界的邪恶:

 1root@tokyo:~#  cscli decisions list
 2╭────────┬──────────┬────────────────────┬───────────────────────────────────┬────────┬─────────┬─────────────────────────────────────────────┬────────┬────────────┬──────────╮
 3│   ID   │  Source  │     Scope:Value    │               Reason              │ Action │ Country │                      AS                     │ Events │ expiration │ Alert ID │
 4├────────┼──────────┼────────────────────┼───────────────────────────────────┼────────┼─────────┼─────────────────────────────────────────────┼────────┼────────────┼──────────┤
 5300360 │ crowdsec │ Ip:101.47.161.84   │ crowdsecurity/ssh-slow-bf         │ ban    │ SG      │ 150436 Byteplus Pte. Ltd.                   │ 28     │ 3h53m54s   │ 549 6300359 │ crowdsec │ Ip:101.47.162.19   │ crowdsecurity/ssh-slow-bf         │ ban    │ SG      │ 150436 Byteplus Pte. Ltd.                   │ 27     │ 3h53m22s   │ 548 7300358 │ crowdsec │ Ip:221.14.65.92    │ crowdsecurity/http-probing        │ ban    │ CN      │ 4837 CHINA UNICOM China169 Backbone         │ 12     │ 3h49m40s   │ 546 8300357 │ crowdsec │ Ip:122.192.133.100 │ crowdsecurity/http-bad-user-agent │ ban    │ CN      │ 4837 CHINA UNICOM China169 Backbone         │ 2      │ 3h45m54s   │ 545 9300356 │ crowdsec │ Ip:198.98.57.141   │ crowdsecurity/ssh-slow-bf         │ ban    │ US      │ 53667 PONYNET                               │ 14     │ 3h35m44s   │ 54310285355 │ crowdsec │ Ip:45.78.194.178   │ crowdsecurity/ssh-slow-bf         │ ban    │ SG      │ 150436 Byteplus Pte. Ltd.                   │ 31     │ 3h3m16s    │ 53811285354 │ crowdsec │ Ip:92.118.39.76    │ crowdsecurity/ssh-slow-bf         │ ban    │ US      │ 47890 Unmanaged Ltd                         │ 19     │ 3h2m50s    │ 53712285353 │ crowdsec │ Ip:45.78.219.42    │ crowdsecurity/ssh-slow-bf         │ ban    │ SG      │ 150436 Byteplus Pte. Ltd.                   │ 24     │ 2h53m55s   │ 53513285352 │ crowdsec │ Ip:45.78.229.72    │ crowdsecurity/ssh-slow-bf         │ ban    │ SG      │ 150436 Byteplus Pte. Ltd.                   │ 16     │ 2h45m33s   │ 53414285351 │ crowdsec │ Ip:173.249.50.59   │ crowdsecurity/ssh-slow-bf         │ ban    │ FR      │ 51167 Contabo GmbH                          │ 15     │ 2h43m43s   │ 53315285350 │ crowdsec │ Ip:159.89.143.86   │ crowdsecurity/ssh-slow-bf         │ ban    │ US      │ 14061 DIGITALOCEAN-ASN                      │ 15     │ 2h43m12s   │ 53216285349 │ crowdsec │ Ip:23.236.169.182  │ crowdsecurity/ssh-slow-bf         │ ban    │ US      │ 55286 SERVER-MANIA                          │ 13     │ 2h40m12s   │ 53117285348 │ crowdsec │ Ip:103.175.206.22  │ crowdsecurity/ssh-slow-bf         │ ban    │ ID      │ 147124 PT Hostingan Awan Indonesia          │ 13     │ 2h40m0s    │ 53018285347 │ crowdsec │ Ip:163.61.198.41   │ crowdsecurity/http-probing        │ ban    │ SG      │ 9664 SOUTHEAST ASIA TELECOMSG PTE. LTD.     │ 13     │ 2h37m41s   │ 52919285346 │ crowdsec │ Ip:45.78.193.199   │ crowdsecurity/ssh-slow-bf         │ ban    │ SG      │ 150436 Byteplus Pte. Ltd.                   │ 27     │ 2h34m44s   │ 52820285345 │ crowdsec │ Ip:101.47.163.243  │ crowdsecurity/ssh-slow-bf         │ ban    │ SG      │ 150436 Byteplus Pte. Ltd.                   │ 21     │ 2h30m40s   │ 52721285344 │ crowdsec │ Ip:162.240.109.153 │ crowdsecurity/ssh-slow-bf         │ ban    │ US      │ 46606 UNIFIEDLAYER-AS-1                     │ 14     │ 2h22m55s   │ 52422285343 │ crowdsec │ Ip:181.167.144.229 │ crowdsecurity/ssh-slow-bf         │ ban    │ AR      │ 7303 Telecom Argentina S.A.                 │ 18     │ 2h22m28s   │ 52323285342 │ crowdsec │ Ip:95.90.13.168    │ crowdsecurity/ssh-slow-bf         │ ban    │ DE      │ 3209 Vodafone GmbH                          │ 12     │ 2h21m59s   │ 52224285341 │ crowdsec │ Ip:211.106.133.202 │ crowdsecurity/ssh-slow-bf         │ ban    │ KR      │ 4766 Korea Telecom                          │ 14     │ 2h21m32s   │ 52125285340 │ crowdsec │ Ip:69.63.203.162   │ crowdsecurity/http-probing        │ ban    │ US      │ 3257 GTT Communications Inc.                │ 11     │ 2h9m41s    │ 51926285339 │ crowdsec │ Ip:49.86.41.49     │ crowdsecurity/http-bad-user-agent │ ban    │ CN      │ 146966 China Telecom                        │ 2      │ 1h54m39s   │ 51827285338 │ crowdsec │ Ip:43.134.66.41    │ crowdsecurity/http-probing        │ ban    │ SG      │ 132203 Tencent Building, Kejizhongyi Avenue │ 11     │ 1h29m46s   │ 51528270337 │ crowdsec │ Ip:20.78.169.245   │ crowdsecurity/http-wordpress-scan │ ban    │ JP      │ 8075 MICROSOFT-CORP-MSN-AS-BLOCK            │ 4      │ 56m13s     │ 51329270334 │ crowdsec │ Ip:152.42.225.101  │ crowdsecurity/http-probing        │ ban    │ SG      │ 14061 DIGITALOCEAN-ASN                      │ 11     │ 21m40s     │ 50830╰────────┴──────────┴────────────────────┴───────────────────────────────────┴────────┴─────────┴─────────────────────────────────────────────┴────────┴────────────┴──────────╯

CrowdSec 有很丰富的小功能点,不一一列举。整体来说,适合个人网站使用,值得推荐。

2. Caddy 集成 Coraza WAF 和 rate_limit

大一点的公司,服务的最外层都有一层 WAF; rate_limit 可以流防 CC 攻击、防接口刷取、暴力破解等,咱也得安排上。

1CGO_ENABLED=1 \
2xcaddy build \
3  --with github.com/dunglas/caddy-cbrotli \
4	--with github.com/mholt/caddy-ratelimit \
5	--with github.com/corazawaf/coraza-caddy/v2

我是在电脑上编译好上述非标准模块后,直接上传到服务器,服务器配置太低,编不动且影响用户。caddy-cbrotli 是添加 br 支持,后面 2 个才是 ratelimit 和 WAF。

结果核对:

 1➜  ~ ./caddy list-modules | tail -n 10
 2
 3  Standard modules: 127
 4
 5http.encoders.br
 6http.handlers.rate_limit
 7http.handlers.waf
 8
 9  Non-standard modules: 3
10
11  Unknown modules: 0

WAF 配置片段示例:

 1# --- 1. Coraza WAF 核心防护 ---
 2# 针对静态站,我们主要拦截异常扫描和恶意 User-Agent
 3coraza_waf {
 4    directives `
 5        SecRuleEngine On
 6        SecRequestBodyAccess On
 7        SecDebugLogLevel 0
 8
 9        # 阻止常见的恶意扫描器
10        SecRule REQUEST_HEADERS:User-Agent "(sqlmap|grabber|cgiscan|nessus|nikto|dirbuster|pangolin)" \
11            "id:101,phase:1,deny,status:403,msg:'Malicious Scanner Detected'"
12
13        # 阻止尝试访问敏感文件路径
14        SecRule REQUEST_URI "\.(git|env|config|php|asp|aspx|jsp|cgi|exe)$" \
15            "id:102,phase:1,deny,status:404,msg:'Path Traversal Attempt'"
16    `
17}

测试核验正常,如下所示:

1➜  ~ curl -A "sqlmap" https://mephisto.cc
2Error: 403 Forbidden⏎
3➜  ~ curl -I  https://mephisto.cc/.env
4HTTP/2 404
5alt-svc: h3=":443"; ma=2592000

限流示例:

 1:80
 2
 3rate_limit {
 4	distributed
 5	zone static_example {
 6		match {
 7			method GET
 8		}
 9		key    static
10		events 100
11		window 1m
12	}
13	zone dynamic_example {
14		key    {remote_host}
15		events 2
16		window 5s
17	}
18	log_key
19}
20
21respond "I'm behind the rate limiter!"

限流的功能,我很早就加上去了,是真怕人刷接口。

还有些 RSS 软件,不光访问 RSS 源文件,还要高频抓取全文,违背常理,通过 UA 识别也一并处理掉了。

3. 总结

这样一套组合拳下来,对于个人服务器来说,基本能防住一些常规攻击,也没有什么额外成本,小站良心低调经营,为人民服务。无利可图,应该还不是专业团伙攻击目标。

最后修改于: Friday, January 30, 2026
欢迎关注微信公众号,留言交流;也欢迎使用我开发的微信小程序。

相关文章:

翻译: