开源入侵防御系统——CrowdSec
1、简介
CrowdSec 是一款现代化、开源、基于行为的入侵防御系统(IDS/IPS),专为保护服务器、服务、容器、云原生应用而设计。它通过分析日志检测可疑行为,并可基于社区协作共享恶意 IP 黑名单,从而实现分布式防御。
其理念源于 Fail2Ban,但 CrowdSec 更现代、更可扩展,具备强大的可视化和自动化能力。
项目地址:https://github.com/crowdsecurity/crowdsec
2、主要特点
3、应用场景
-
企业级服务器 SSH 暴力破解防护。
-
Web 应用防御(包括 Nginx、Apache、HAProxy 日志)。
-
Kubernetes 集群安全审计与防护。
-
公有云/私有云中的入侵检测与响应。
-
与防火墙、WAF 联动实现动态封禁。
-
DevOps 安全加固及日志威胁可视化。
4、主要组件
-
CrowdSec 代理(Agent):分析 Nginx 和系统日志,检测可疑行为。
-
本地 API(LAPI):连接代理与防护组件。
-
防护组件(Bouncer):阻止恶意流量,支持 Nginx、防火墙等多种方式。
-
命令行工具(cscli):管理配置和决策。
5、工具对比
以下是 CrowdSec 与 Fail2Ban 和 Suricata 在保护 Nginx 部署的 Cilikube 项目时的对比:
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 | bashyum update -yyum 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 ────────────────────────────────────────────────────────────────────────────────────────────────────────###查看封禁ipcscli 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-logsAction plan:✅ enable parsers: crowdsecurity/syslog-logsenabling parsers:crowdsecurity/syslog-logsRun \'sudo systemctl reload crowdsec\' for the new configuration to be effective.root@test:~# systemctl restart crowdsecroot@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/securefilenames: - /var/log/securelabels: type: syslog---#Generated acquisition file - wizard.sh (service: linux) / files : /var/log/messagesfilenames: - /var/log/messageslabels: 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/whitelistsdescription: \"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 listNo 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 │╰────┴───────────────────┴───────────────────────────┴─────────┴────┴───────────┴─────────────────────────────────────────╯#####封禁iproot@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规则来拒绝爆破iproot@test:/etc/crowdsec# iptables -nvLChain 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