搬瓦工 VPS CrowdSec 协作安全引擎部署教程

CrowdSec 是新一代的开源安全引擎,采用协作式防御理念。它分析服务器日志来检测恶意行为,并通过社区共享威胁情报,让所有参与者共同受益。与传统的 Fail2ban 相比,CrowdSec 不仅能检测暴力破解,还支持复杂的攻击场景识别,并且可以在检测到威胁后通过 Bouncer 组件自动封锁恶意 IP。本文将详细介绍如何在搬瓦工 VPS 上部署 CrowdSec。

一、CrowdSec 架构概述

CrowdSec 由三个核心组件组成:

  • Agent(代理):负责读取和解析日志,根据场景规则检测恶意行为。
  • LAPI(Local API):本地 API 服务,管理决策(封禁/验证码等)和与社区 API 的通信。
  • Bouncer(执行器):根据 LAPI 中的决策执行封锁操作,如 iptables 封禁、Nginx 拦截等。

二、安装 CrowdSec

2.1 使用官方安装脚本

curl -s https://install.crowdsec.net | bash

2.2 手动安装(Ubuntu/Debian)

curl -s https://packagecloud.io/install/repositories/crowdsec/crowdsec/script.deb.sh | bash
apt install crowdsec -y

2.3 手动安装(CentOS/RHEL)

curl -s https://packagecloud.io/install/repositories/crowdsec/crowdsec/script.rpm.sh | bash
yum install crowdsec -y

2.4 验证安装

systemctl status crowdsec
cscli version

三、配置日志解析

3.1 安装日志解析器(Parsers)

# 查看已安装的解析器
cscli parsers list

# 安装 SSH 日志解析器
cscli parsers install crowdsecurity/sshd-logs

# 安装 Nginx 日志解析器
cscli parsers install crowdsecurity/nginx-logs

# 安装 Linux 系统日志解析器
cscli parsers install crowdsecurity/syslog-logs

3.2 配置日志采集

编辑采集配置文件 /etc/crowdsec/acquis.yaml

filenames:
  - /var/log/auth.log
  - /var/log/syslog
labels:
  type: syslog
---
filenames:
  - /var/log/nginx/access.log
  - /var/log/nginx/error.log
labels:
  type: nginx
# 重载配置
systemctl reload crowdsec

四、安装场景规则

# 查看可用场景
cscli scenarios list -a

# 安装 SSH 暴力破解检测
cscli scenarios install crowdsecurity/ssh-bf

# 安装 SSH 慢速暴力破解检测
cscli scenarios install crowdsecurity/ssh-slow-bf

# 安装 HTTP 扫描检测
cscli scenarios install crowdsecurity/http-probing

# 安装 HTTP 爬虫检测
cscli scenarios install crowdsecurity/http-crawl-non_statics

# 安装 HTTP 暴力破解检测
cscli scenarios install crowdsecurity/http-bf-wordpress_bf

# 重载服务
systemctl reload crowdsec

五、安装 Bouncer

5.1 安装防火墙 Bouncer

# 安装 iptables/nftables Bouncer
apt install crowdsec-firewall-bouncer-iptables -y

# 或安装 nftables 版本
apt install crowdsec-firewall-bouncer-nftables -y

# 查看 Bouncer 状态
cscli bouncers list

5.2 安装 Nginx Bouncer

# 安装 Nginx Bouncer
apt install crowdsec-nginx-bouncer -y

# 查看配置
cat /etc/crowdsec/bouncers/crowdsec-nginx-bouncer.conf

六、管理与监控

# 查看当前封禁列表
cscli decisions list

# 手动封禁 IP
cscli decisions add --ip 1.2.3.4 --duration 24h --reason "manual ban"

# 手动解封 IP
cscli decisions delete --ip 1.2.3.4

# 查看检测到的告警
cscli alerts list

# 查看指标统计
cscli metrics

# 查看引擎运行状况
cscli hub list

七、社区威胁情报

# 注册 CrowdSec 控制台(免费)
# 访问 https://app.crowdsec.net 注册

# 将实例注册到控制台
cscli console enroll your_enrollment_key

# 验证注册
systemctl restart crowdsec

注册控制台后,你的 CrowdSec 实例会自动接收社区共享的恶意 IP 黑名单,并将自身检测到的恶意 IP 贡献给社区。

八、自定义白名单

# 创建白名单文件
cat > /etc/crowdsec/parsers/s02-enrich/mywhitelists.yaml <<'EOF'
name: crowdsecurity/mywhitelists
description: "My custom whitelist"
whitelist:
  reason: "My trusted IPs"
  ip:
    - "10.0.0.0/8"
    - "172.16.0.0/12"
    - "192.168.0.0/16"
    - "YOUR_HOME_IP"
EOF

systemctl reload crowdsec

九、CrowdSec 与 Docker 集成

cat > /opt/crowdsec/docker-compose.yml <<'EOF'
version: '3.8'

services:
  crowdsec:
    image: crowdsecurity/crowdsec:latest
    container_name: crowdsec
    restart: always
    environment:
      COLLECTIONS: "crowdsecurity/linux crowdsecurity/nginx"
      GID: "${GID-1000}"
    volumes:
      - ./config:/etc/crowdsec
      - ./data:/var/lib/crowdsec/data
      - /var/log:/var/log:ro
    ports:
      - "8080:8080"
EOF

总结

CrowdSec 是搬瓦工 VPS 上防御网络攻击的利器,协作式防御模式让每台服务器都能受益于全球社区的威胁情报。建议搭配 Wazuh 安全平台OSSEC 入侵检测 构建多层安全防线。选购搬瓦工 VPS 请参考 全部方案,购买时使用优惠码 NODESEEK2026 可享受 6.77% 的折扣,通过 bwh81.net 进入官网购买。

关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工的方案、优惠和教程。我们不销售主机,不提供技术服务。

新手必读
搬瓦工优惠码

NODESEEK2026(优惠 6.77%)

购买时填入即可抵扣。