1、簡介
CrowdSec?是一款現代化、開源、基于行為的入侵防御系統(IDS/IPS),專為保護服務器、服務、容器、云原生應用而設計。它通過分析日志檢測可疑行為,并可基于社區協作共享惡意 IP 黑名單,從而實現分布式防御。
其理念源于 Fail2Ban,但 CrowdSec 更現代、更可擴展,具備強大的可視化和自動化能力。
項目地址:https://github.com/crowdsecurity/crowdsec?
2、主要特點
特性 | 描述 |
---|---|
開源免費 | 完全開源,MIT 許可,無強制商業組件 |
模塊化架構 | 解耦式架構,支持插件化擴展(例如不同的日志源、解析器、響應器) |
日志行為分析 | 基于日志行為檢測異常,如 SSH 暴力破解、Web 攻擊等 |
社區威脅共享 | 通過 CrowdSec 社區共享惡意 IP 列表(CTI:Cyber Threat Intelligence) |
多平臺支持 | 支持 Linux、BSD、Docker、Kubernetes 等環境 |
多語言通知與響應 | 支持自動封禁、郵件、Slack、Webhook、Cloudflare 等響應方式 |
可視化 Dashboard | 基于 Metabase 的 Web UI,提供可視化分析 |
3、應用場景
-
企業級服務器 SSH 暴力破解防護。
-
Web 應用防御(包括 Nginx、Apache、HAProxy 日志)。
-
Kubernetes 集群安全審計與防護。
-
公有云/私有云中的入侵檢測與響應。
-
與防火墻、WAF 聯動實現動態封禁。
-
DevOps 安全加固及日志威脅可視化。
4、主要組件
-
CrowdSec 代理(Agent):分析 Nginx 和系統日志,檢測可疑行為。
-
本地 API(LAPI):連接代理與防護組件。
-
防護組件(Bouncer):阻止惡意流量,支持 Nginx、防火墻等多種方式。
-
命令行工具(cscli):管理配置和決策。
5、工具對比
以下是 CrowdSec 與 Fail2Ban 和 Suricata 在保護 Nginx 部署的 Cilikube 項目時的對比:
特性/工具 | CrowdSec | Fail2Ban | Suricata |
---|---|---|---|
開源性 | 開源 | 開源 | 開源 |
威脅情報 | 社區共享,實時更新惡意 IP | 本地規則,無共享 | 依賴規則集,免費規則延遲 30 天 |
Web 防護 | 原生支持 Nginx,防護 SQL 注入、XSS 等 | 需手動配置正則規則 | 依賴流量分析,需額外配置 |
檢測方式 | 日志分析,適合 Nginx | 日志正則匹配 | 網絡流量分析 |
防護方式 | Nginx 集成、防火墻等多層次 | 僅防火墻規則 | 依賴防火墻或專用設備 |
易用性 | CLI 和 Web 儀表盤,易于配置 | 配置簡單,無 Web 界面 | 配置復雜,需專業知識 |
性能影響 | 輕量,適合 Ubuntu | 輕量 | 流量分析,資源占用高 |
Fail2Ban 的差異
-
Web 防護:CrowdSec 提供原生 Nginx 防護組件,可直接處理 HTTP 攻擊(如針對 Cilikube 的路徑遍歷);Fail2Ban 需手動編寫正則規則,配置還是比較復雜。
-
威脅情報:CrowdSec 的社區共享機制能快速識別全球攻擊者,Fail2Ban 僅依賴本地日志。
-
擴展性:CrowdSec 的 Hub 提供豐富的 Web 攻擊場景,Fail2Ban 擴展性有限。
Suricata 的差異
-
資源占用:CrowdSec 基于日志分析,適合資源有限的 Ubuntu 服務器;Suricata 需分析網絡流量,消耗較高。
-
Nginx 集成:CrowdSec 提供開箱即用的防護組件,Suricata 需復雜配置。
-
實時性:CrowdSec 實時共享威脅情報,Suricata 免費規則延遲 30 天。
6、安裝部署(centos7)
curl -s https://packagecloud.io/install/repositories/crowdsec/crowdsec/script.rpm.sh | bash
yum? update? -y
yum install crowdsec -y?###安裝完成后服務會自啟動
systemctl status crowdsec
7、查看防護狀態
列出已安裝的防護場景
####默認會安裝ssh防爆破規則cscli collections list
────────────────────────────────────────────────────────────────────────────────COLLECTIONS
────────────────────────────────────────────────────────────────────────────────Name 📦 Status Version Local Path
────────────────────────────────────────────────────────────────────────────────crowdsecurity/sshd ?? enabled 0.7 /etc/crowdsec/collections/sshd.yaml
────────────────────────────────────────────────────────────────────────────────cscli scenarios list
────────────────────────────────────────────────────────────────────────────────────────────────────────SCENARIOS
────────────────────────────────────────────────────────────────────────────────────────────────────────Name 📦 Status Version Local Path
────────────────────────────────────────────────────────────────────────────────────────────────────────crowdsecurity/ssh-bf ?? enabled 0.3 /etc/crowdsec/scenarios/ssh-bf.yaml crowdsecurity/ssh-cve-2024-6387 ?? enabled 0.2 /etc/crowdsec/scenarios/ssh-cve-2024-6387.yaml crowdsecurity/ssh-generic-test ?? enabled 0.2 /etc/crowdsec/scenarios/ssh-generic-test.yaml crowdsecurity/ssh-refused-conn ?? enabled 0.1 /etc/crowdsec/scenarios/ssh-refused-conn.yaml crowdsecurity/ssh-slow-bf ?? enabled 0.4 /etc/crowdsec/scenarios/ssh-slow-bf.yaml
────────────────────────────────────────────────────────────────────────────────────────────────────────###查看封禁ip
cscli decisions list
8、測試ssh防爆破封禁
采集原理:ssh爆破日志-----/var/log/secure----syslog去采集-----ssh-bf.yaml去正則匹配日志分析、符合條件觸發---alerts報警-----封禁
1)、采集日志是否安裝
cscli parsers list
───────────────────────────────────────────────────────────────────────────────────────────────────PARSERS
───────────────────────────────────────────────────────────────────────────────────────────────────Name 📦 Status Version Local Path
───────────────────────────────────────────────────────────────────────────────────────────────────crowdsecurity/sshd-logs ?? enabled 3.0 /etc/crowdsec/parsers/s01-parse/sshd-logs.yaml crowdsecurity/whitelists ?? enabled 0.3 /etc/crowdsec/parsers/s02-enrich/whitelists.yaml
───────────────────────────────────────────────────────────────────────────────────────────────────
安裝syslog
root@test:~# cscli parsers install crowdsecurity/syslog-logs
Action plan:
? enableparsers: crowdsecurity/syslog-logsenabling parsers:crowdsecurity/syslog-logsRun 'sudo systemctl reload crowdsec' for the new configuration to be effective.
root@test:~# systemctl restart crowdsec
root@test:~# cscli parsers list
────────────────────────────────────────────────────────────────────────────────────────────────────PARSERS
────────────────────────────────────────────────────────────────────────────────────────────────────Name 📦 Status Version Local Path
────────────────────────────────────────────────────────────────────────────────────────────────────crowdsecurity/sshd-logs ?? enabled 3.0 /etc/crowdsec/parsers/s01-parse/sshd-logs.yaml crowdsecurity/syslog-logs ?? enabled 0.8 /etc/crowdsec/parsers/s00-raw/syslog-logs.yaml crowdsecurity/whitelists ?? enabled 0.3 /etc/crowdsec/parsers/s02-enrich/whitelists.yaml
────────────────────────────────────────────────────────────────────────────────────────────────────###安裝防火墻方式攔截
yum install -y crowdsec-firewall-bouncer-iptables
2)、采集配置,默認已經配置好了,不用修改
root@test:/etc/crowdsec# cat acquis.yaml
#Generated acquisition file - wizard.sh (service: sshd) / files : /var/log/secure
filenames:- /var/log/secure
labels:type: syslog
---
#Generated acquisition file - wizard.sh (service: linux) / files : /var/log/messages
filenames:- /var/log/messages
labels:type: syslog
---
3)、修改日志格式,系統產生的格式一定要符合CrowdSec采集規則、否則無法命中
####主要是為了讓/var/log/secure里面記錄的規則符合CrowdSec采集的匹配vim /etc/rsyslog.conf ###注釋這一行
#$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat加入這三行
$LocalHostName localhost
$template MyTraditionalFormat,"%timegenerated% %hostname% %syslogtag% %msg%\n"
$ActionFileDefaultTemplate MyTraditionalFormatsystemctl restart rsyslog#####默認白名單規則里面加入了私網,為了測試注銷掉和內網相同的網段vim /etc/crowdsec/parsers/s02-enrich/whitelists.yaml name: crowdsecurity/whitelists
description: "Whitelist events from private ipv4 addresses"
whitelist:reason: "private ipv4/ipv6 ip/ranges"ip:- "::1"cidr:
# - "127.0.0.0/8"
# - "192.168.0.0/16"- "10.0.0.0/8"- "172.16.0.0/12"# expression:# - "'foo.com' in evt.Meta.source_ip.reverse" systemctl restart crowdsec
4)、測試
####原始狀態,主要看Lines parsed現在是0,就代表沒有讀到任何日志匹配項
root@test:/etc/crowdsec# cscli metrics
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Acquisition Metrics │
├────────────────────────┬────────────┬──────────────┬────────────────┬────────────────────────┬───────────────────┤
│ Source │ Lines read │ Lines parsed │ Lines unparsed │ Lines poured to bucket │ Lines whitelisted │
├────────────────────────┼────────────┼──────────────┼────────────────┼────────────────────────┼───────────────────┤
│ file:/var/log/messages │ 21 │ - │ 21 │ - │ - │
│ file:/var/log/secure │ 2 │ - │ 2 │ - │ - │
╰────────────────────────┴────────────┴──────────────┴────────────────┴────────────────────────┴───────────────────╯
╭─────────────────────────────────────────────╮
│ Local API Decisions │
├────────────────┬────────┬────────┬───────┤
│ Reason │ Origin │ Action │ Count │
├────────────────┼────────┼────────┼───────┤
│ generic:scan │ CAPI │ ban │ 3499 │
│ ssh:bruteforce │ CAPI │ ban │ 10266 │
│ ssh:exploit │ CAPI │ ban │ 1230 │
╰────────────────┴────────┴────────┴───────╯
╭──────────────────────────────────────╮
│ Local API Metrics │
├────────────────────┬────────┬──────┤
│ Route │ Method │ Hits │
├────────────────────┼────────┼──────┤
│ /v1/heartbeat │ GET │ 25 │
│ /v1/usage-metrics │ POST │ 1 │
│ /v1/watchers/login │ POST │ 1 │
╰────────────────────┴────────┴──────╯
╭─────────────────────────────────────────────────────────────────────────────────────╮
│ Local API Machines Metrics │
├──────────────────────────────────────────────────┬───────────────┬────────┬──────┤
│ Machine │ Route │ Method │ Hits │
├──────────────────────────────────────────────────┼───────────────┼────────┼──────┤
│ dd2bb318ae379c408892924592b37d97AJnzSsojiaCqXNKQ │ /v1/heartbeat │ GET │ 25 │
╰──────────────────────────────────────────────────┴───────────────┴────────┴──────╯
╭───────────────────────────────────────────────────────────────╮
│ Parser Metrics │
├─────────────────────────────────┬──────┬────────┬──────────┤
│ Parsers │ Hits │ Parsed │ Unparsed │
├─────────────────────────────────┼──────┼────────┼──────────┤
│ child-crowdsecurity/syslog-logs │ 34 │ 12 │ 22 │
│ crowdsecurity/syslog-logs │ 23 │ 12 │ 11 │
╰─────────────────────────────────┴──────┴────────┴──────────╯
#####封禁是沒有規則的
root@test:/etc/crowdsec# cscli decisions list
No active decisions
現在另開一臺機器進行爆破實驗,觸發規則后,就被拉入黑名單,無法發送數據了
for i in $(seq 1 10); do ssh invaliduser@ip -o PasswordAuthentication=yes -o StrictHostKeyChecking=no echo hi; done
### Lines parsed讀到數據,代表解析正常cscli metrics
╭───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Acquisition Metrics │
├────────────────────────┬────────────┬──────────────┬────────────────┬────────────────────────┬───────────────────┤
│ Source │ Lines read │ Lines parsed │ Lines unparsed │ Lines poured to bucket │ Lines whitelisted │
├────────────────────────┼────────────┼──────────────┼────────────────┼────────────────────────┼───────────────────┤
│ file:/var/log/messages │ 21 │ - │ 21 │ - │ - │
│ file:/var/log/secure │ 22 │ 14 │ 8 │ 32 │ - │
╰────────────────────────┴────────────┴──────────────┴────────────────┴────────────────────────┴───────────────────╯
╭────────────────────────────────────╮
│ Local API Alerts │
├───────────────────────────┬───────┤
│ Reason │ Count │
├───────────────────────────┼───────┤
│ crowdsecurity/ssh-bf │ 1 │
│ crowdsecurity/ssh-slow-bf │ 1 │
╰───────────────────────────┴───────╯
╭──────────────────────────────────────────────────────────╮
│ Local API Decisions │
├───────────────────────────┬──────────┬────────┬───────┤
│ Reason │ Origin │ Action │ Count │
├───────────────────────────┼──────────┼────────┼───────┤
│ crowdsecurity/ssh-bf │ crowdsec │ ban │ 1 │
│ crowdsecurity/ssh-slow-bf │ crowdsec │ ban │ 1 │
╰───────────────────────────┴──────────┴────────┴───────╯
╭────────────────────────────────────────╮
│ Local API Metrics │
├──────────────────────┬────────┬──────┤
│ Route │ Method │ Hits │
├──────────────────────┼────────┼──────┤
│ /v1/alerts │ DELETE │ 1 │
│ /v1/alerts │ GET │ 4 │
│ /v1/alerts │ POST │ 2 │
│ /v1/decisions │ DELETE │ 1 │
│ /v1/decisions/stream │ GET │ 30 │
│ /v1/heartbeat │ GET │ 9 │
│ /v1/usage-metrics │ POST │ 1 │
│ /v1/watchers/login │ POST │ 7 │
╰──────────────────────┴────────┴──────╯
╭──────────────────────────────────────────────────────────────────────────╮
│ Local API Bouncers Metrics │
├────────────────────────────────┬──────────────────────┬────────┬──────┤
│ Bouncer │ Route │ Method │ Hits │
├────────────────────────────────┼──────────────────────┼────────┼──────┤
│ cs-firewall-bouncer-1752116174 │ /v1/decisions/stream │ GET │ 30 │
╰────────────────────────────────┴──────────────────────┴────────┴──────╯
╭─────────────────────────────────────────────────────────────────────────────────────╮
│ Local API Machines Metrics │
├──────────────────────────────────────────────────┬───────────────┬────────┬──────┤
│ Machine │ Route │ Method │ Hits │
├──────────────────────────────────────────────────┼───────────────┼────────┼──────┤
│ dd2bb318ae379c408892924592b37d97AJnzSsojiaCqXNKQ │ /v1/alerts │ DELETE │ 1 │
│ dd2bb318ae379c408892924592b37d97AJnzSsojiaCqXNKQ │ /v1/alerts │ GET │ 4 │
│ dd2bb318ae379c408892924592b37d97AJnzSsojiaCqXNKQ │ /v1/alerts │ POST │ 2 │
│ dd2bb318ae379c408892924592b37d97AJnzSsojiaCqXNKQ │ /v1/decisions │ DELETE │ 1 │
│ dd2bb318ae379c408892924592b37d97AJnzSsojiaCqXNKQ │ /v1/heartbeat │ GET │ 9 │
╰──────────────────────────────────────────────────┴───────────────┴────────┴──────╯
╭───────────────────────────────────────────────────────────────╮
│ Parser Metrics │
├─────────────────────────────────┬──────┬────────┬──────────┤
│ Parsers │ Hits │ Parsed │ Unparsed │
├─────────────────────────────────┼──────┼────────┼──────────┤
│ child-crowdsecurity/sshd-logs │ 216 │ 14 │ 202 │
│ child-crowdsecurity/syslog-logs │ 43 │ 43 │ - │
│ crowdsecurity/sshd-logs │ 22 │ 14 │ 8 │
│ crowdsecurity/syslog-logs │ 43 │ 43 │ - │
│ crowdsecurity/whitelists │ 14 │ 14 │ - │
╰─────────────────────────────────┴──────┴────────┴──────────╯
╭────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ Scenario Metrics │
├─────────────────────────────────────┬───────────────┬───────────┬──────────────┬────────┬─────────┤
│ Scenario │ Current Count │ Overflows │ Instantiated │ Poured │ Expired │
├─────────────────────────────────────┼───────────────┼───────────┼──────────────┼────────┼─────────┤
│ crowdsecurity/ssh-bf │ - │ 2 │ 3 │ 14 │ 1 │
│ crowdsecurity/ssh-bf_user-enum │ - │ - │ 1 │ 2 │ 1 │
│ crowdsecurity/ssh-slow-bf │ 1 │ 1 │ 2 │ 14 │ - │
│ crowdsecurity/ssh-slow-bf_user-enum │ 1 │ - │ 1 │ 2 │ - │
╰─────────────────────────────────────┴───────────────┴───────────┴──────────────┴────────┴─────────╯
╭────────────────────────────────────────────────────────────────────────────────╮
│ Whitelist Metrics │
├──────────────────────────┬─────────────────────────────┬──────┬─────────────┤
│ Whitelist │ Reason │ Hits │ Whitelisted │
├──────────────────────────┼─────────────────────────────┼──────┼─────────────┤
│ crowdsecurity/whitelists │ private ipv4/ipv6 ip/ranges │ 14 │ - │
╰──────────────────────────┴─────────────────────────────┴──────┴─────────────╯#####觸發報警
root@test:/etc/crowdsec# cscli alerts list
╭────┬───────────────────┬───────────────────────────┬─────────┬────┬───────────┬─────────────────────────────────────────╮
│ ID │ value │ reason │ country │ as │ decisions │ created_at │
├────┼───────────────────┼───────────────────────────┼─────────┼────┼───────────┼─────────────────────────────────────────┤
│ 7 │ Ip:192.168.00.000 │ crowdsecurity/ssh-slow-bf │ │ │ ban:1 │ 2025-07-10 02:58:28.626116415 +0000 UTC │
│ 6 │ Ip:192.168.00.000 │ crowdsecurity/ssh-bf │ │ │ ban:1 │ 2025-07-10 02:58:28.625837032 +0000 UTC │
╰────┴───────────────────┴───────────────────────────┴─────────┴────┴───────────┴─────────────────────────────────────────╯#####封禁ip
root@test:/etc/crowdsec# cscli decisions list
╭───────┬──────────┬───────────────────┬───────────────────────────┬────────┬─────────┬────┬────────┬────────────┬──────────╮
│ ID │ Source │ Scope:Value │ Reason │ Action │ Country │ AS │ Events │ expiration │ Alert ID │
├───────┼──────────┼───────────────────┼───────────────────────────┼────────┼─────────┼────┼────────┼────────────┼──────────┤
│ 15006 │ crowdsec │ Ip:192.168.00.000 │ crowdsecurity/ssh-slow-bf │ ban │ │ │ 11 │ 3h56m39s │ 7 │
╰───────┴──────────┴───────────────────┴───────────────────────────┴────────┴─────────┴────┴────────┴────────────┴──────────╯
1 duplicated entries skipped####產生一條drop規則來拒絕爆破ip
root@test:/etc/crowdsec# iptables -nvL
Chain INPUT (policy ACCEPT 4474 packets, 465K bytes)pkts bytes target prot opt in out source destination 7223 741K CROWDSEC_CHAIN all -- * * 0.0.0.0/0 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 7530 packets, 1484K bytes)pkts bytes target prot opt in out source destination Chain CROWDSEC_CHAIN (1 references)pkts bytes target prot opt in out source destination 29 2680 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 match-set crowdsec-blacklists-0 src /* CrowdSec: crowdsec */
8、排錯,唯一的難點就是系統產生的日志,無法被CrowdSec適配,可以將config.yaml的日志級別調為debug模式,對應/var/log/crowdsec.log的日志去分析
可以使用這條命令去分析日志是否能被正確匹配
crowdsec -type syslog -dsn file:///var/log/secure -label service:sshd -no-api --debug