开源入侵防御系统——CrowdSec

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)

bash 复制代码
curl -s https://packagecloud.io/install/repositories/crowdsec/crowdsec/script.rpm.sh | bash
yum  update  -y
yum install crowdsec -y 

###安装完成后服务会自启动
systemctl status crowdsec

7、查看防护状态

列出已安装的防护场景
bash 复制代码
####默认会安装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)、采集日志是否安装

bash 复制代码
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

bash 复制代码
root@test:~# cscli parsers install crowdsecurity/syslog-logs
Action plan:
✅ enable
 parsers: crowdsecurity/syslog-logs

enabling parsers:crowdsecurity/syslog-logs

Run '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)、采集配置,默认已经配置好了,不用修改

bash 复制代码
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采集规则、否则无法命中

bash 复制代码
####主要是为了让/var/log/secure里面记录的规则符合CrowdSec采集的匹配

vim /etc/rsyslog.conf 

###注释这一行
#$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

加入这三行
$LocalHostName localhost
$template MyTraditionalFormat,"%timegenerated% %hostname% %syslogtag% %msg%\n"
$ActionFileDefaultTemplate  MyTraditionalFormat



systemctl 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)、测试

bash 复制代码
####原始状态,主要看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

现在另开一台机器进行爆破实验,触发规则后,就被拉入黑名单,无法发送数据了

bash 复制代码
for i in $(seq 1 10); do   ssh invaliduser@ip -o PasswordAuthentication=yes -o StrictHostKeyChecking=no echo hi; done
bash 复制代码
### 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

相关推荐
zskj_zhyl3 小时前
毫米波雷达守护银发安全:七彩喜跌倒检测仪重构居家养老防线
人工智能·安全·重构
ai小鬼头5 小时前
AIStarter新版重磅来袭!永久订阅限时福利抢先看
人工智能·开源·github
春哥的研究所6 小时前
可视化DIY小程序工具!开源拖拽式源码系统,自由搭建,完整的源代码包分享
小程序·开源·开源拖拽式源码系统·开源拖拽式源码·开源拖拽式系统
ajassi20007 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
说私域8 小时前
基于开源AI大模型AI智能名片S2B2C商城小程序源码的私域流量新生态构建
人工智能·开源
算家计算9 小时前
“28项评测23项SOTA——GLM-4.1V-9B-Thinking本地部署教程:10B级视觉语言模型的性能天花板!
人工智能·开源
饶了我吧,放了我吧9 小时前
计算机网络实验——无线局域网安全实验
计算机网络·安全·web安全
TracyCoder12310 小时前
Apache Shiro 框架详解
安全·apache·shiro·认证·登录