個人網站安全防護
前陣子提到給網站接入日誌監控後,發現每天都有人惡意掃描,掃描後一大堆 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├────────┼──────────┼────────────────────┼───────────────────────────────────┼────────┼─────────┼─────────────────────────────────────────────┼────────┼────────────┼──────────┤
5│ 300360 │ crowdsec │ Ip:101.47.161.84 │ crowdsecurity/ssh-slow-bf │ ban │ SG │ 150436 Byteplus Pte. Ltd. │ 28 │ 3h53m54s │ 549 │
6│ 300359 │ crowdsec │ Ip:101.47.162.19 │ crowdsecurity/ssh-slow-bf │ ban │ SG │ 150436 Byteplus Pte. Ltd. │ 27 │ 3h53m22s │ 548 │
7│ 300358 │ crowdsec │ Ip:221.14.65.92 │ crowdsecurity/http-probing │ ban │ CN │ 4837 CHINA UNICOM China169 Backbone │ 12 │ 3h49m40s │ 546 │
8│ 300357 │ crowdsec │ Ip:122.192.133.100 │ crowdsecurity/http-bad-user-agent │ ban │ CN │ 4837 CHINA UNICOM China169 Backbone │ 2 │ 3h45m54s │ 545 │
9│ 300356 │ crowdsec │ Ip:198.98.57.141 │ crowdsecurity/ssh-slow-bf │ ban │ US │ 53667 PONYNET │ 14 │ 3h35m44s │ 543 │
10│ 285355 │ crowdsec │ Ip:45.78.194.178 │ crowdsecurity/ssh-slow-bf │ ban │ SG │ 150436 Byteplus Pte. Ltd. │ 31 │ 3h3m16s │ 538 │
11│ 285354 │ crowdsec │ Ip:92.118.39.76 │ crowdsecurity/ssh-slow-bf │ ban │ US │ 47890 Unmanaged Ltd │ 19 │ 3h2m50s │ 537 │
12│ 285353 │ crowdsec │ Ip:45.78.219.42 │ crowdsecurity/ssh-slow-bf │ ban │ SG │ 150436 Byteplus Pte. Ltd. │ 24 │ 2h53m55s │ 535 │
13│ 285352 │ crowdsec │ Ip:45.78.229.72 │ crowdsecurity/ssh-slow-bf │ ban │ SG │ 150436 Byteplus Pte. Ltd. │ 16 │ 2h45m33s │ 534 │
14│ 285351 │ crowdsec │ Ip:173.249.50.59 │ crowdsecurity/ssh-slow-bf │ ban │ FR │ 51167 Contabo GmbH │ 15 │ 2h43m43s │ 533 │
15│ 285350 │ crowdsec │ Ip:159.89.143.86 │ crowdsecurity/ssh-slow-bf │ ban │ US │ 14061 DIGITALOCEAN-ASN │ 15 │ 2h43m12s │ 532 │
16│ 285349 │ crowdsec │ Ip:23.236.169.182 │ crowdsecurity/ssh-slow-bf │ ban │ US │ 55286 SERVER-MANIA │ 13 │ 2h40m12s │ 531 │
17│ 285348 │ crowdsec │ Ip:103.175.206.22 │ crowdsecurity/ssh-slow-bf │ ban │ ID │ 147124 PT Hostingan Awan Indonesia │ 13 │ 2h40m0s │ 530 │
18│ 285347 │ crowdsec │ Ip:163.61.198.41 │ crowdsecurity/http-probing │ ban │ SG │ 9664 SOUTHEAST ASIA TELECOMSG PTE. LTD. │ 13 │ 2h37m41s │ 529 │
19│ 285346 │ crowdsec │ Ip:45.78.193.199 │ crowdsecurity/ssh-slow-bf │ ban │ SG │ 150436 Byteplus Pte. Ltd. │ 27 │ 2h34m44s │ 528 │
20│ 285345 │ crowdsec │ Ip:101.47.163.243 │ crowdsecurity/ssh-slow-bf │ ban │ SG │ 150436 Byteplus Pte. Ltd. │ 21 │ 2h30m40s │ 527 │
21│ 285344 │ crowdsec │ Ip:162.240.109.153 │ crowdsecurity/ssh-slow-bf │ ban │ US │ 46606 UNIFIEDLAYER-AS-1 │ 14 │ 2h22m55s │ 524 │
22│ 285343 │ crowdsec │ Ip:181.167.144.229 │ crowdsecurity/ssh-slow-bf │ ban │ AR │ 7303 Telecom Argentina S.A. │ 18 │ 2h22m28s │ 523 │
23│ 285342 │ crowdsec │ Ip:95.90.13.168 │ crowdsecurity/ssh-slow-bf │ ban │ DE │ 3209 Vodafone GmbH │ 12 │ 2h21m59s │ 522 │
24│ 285341 │ crowdsec │ Ip:211.106.133.202 │ crowdsecurity/ssh-slow-bf │ ban │ KR │ 4766 Korea Telecom │ 14 │ 2h21m32s │ 521 │
25│ 285340 │ crowdsec │ Ip:69.63.203.162 │ crowdsecurity/http-probing │ ban │ US │ 3257 GTT Communications Inc. │ 11 │ 2h9m41s │ 519 │
26│ 285339 │ crowdsec │ Ip:49.86.41.49 │ crowdsecurity/http-bad-user-agent │ ban │ CN │ 146966 China Telecom │ 2 │ 1h54m39s │ 518 │
27│ 285338 │ crowdsec │ Ip:43.134.66.41 │ crowdsecurity/http-probing │ ban │ SG │ 132203 Tencent Building, Kejizhongyi Avenue │ 11 │ 1h29m46s │ 515 │
28│ 270337 │ crowdsec │ Ip:20.78.169.245 │ crowdsecurity/http-wordpress-scan │ ban │ JP │ 8075 MICROSOFT-CORP-MSN-AS-BLOCK │ 4 │ 56m13s │ 513 │
29│ 270334 │ crowdsec │ Ip:152.42.225.101 │ crowdsecurity/http-probing │ ban │ SG │ 14061 DIGITALOCEAN-ASN │ 11 │ 21m40s │ 508 │
30╰────────┴──────────┴────────────────────┴───────────────────────────────────┴────────┴─────────┴─────────────────────────────────────────────┴────────┴────────────┴──────────╯
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. 總結
這樣一套組合拳下來,對於個人服務器來說,基本能防住一些常規攻擊,也沒有什麼額外成本,小站良心低調經營,爲人民服務。無利可圖,應該還不是專業團伙攻擊目標。

版權申明:
- 未標註來源的內容皆為原創,未經授權請勿轉載(因轉載後排版往往錯亂、內容不可控、無法持續更新等);
- 非營利為目的,演繹本博客任何內容,請以'原文出處'或者'參考鏈接'等方式給出本站相關網頁地址(方便讀者)。
相關文章:
- Caddy2 Vue history 模式配置
- 使用goaccess即時分析Caddy日誌
- Arch 重裝記錄
- 打造自己的流媒體音樂服務
- MangoHud性能監控
- 獨立窗口管理器下無法錄屏問題處理
- Arch linux如何順暢連接藍牙設備
- 使用Git和Ansible管理配置文件
- Arch Linux SSL VPN 客戶端配置
- Arch linux下iNode客戶端的安裝和使用方法