ติดตั้ง CrowdSec บน VPS ระบบ IPS ทันสมัย
CrowdSec คือระบบ Intrusion Prevention System (IPS) ที่ทันสมัยกว่า Fail2ban — แทนที่จะแบน IP ด้วยการดู log ในเครื่องเอง CrowdSec แชร์ข้อมูลภัยคุกคาม crowdsourced จากทุก server ทั่วโลก ทำให้เจอภัยคุกคาม ใหม่ได้เร็วกว่า และป้องกันได้อยู่ก่อนที่ปัญหาจะเข้ามา ยิ่งไปกว่านั้น CrowdSec ใช้ machine learning ตรวจจับพฤติกรรมการโจมตีที่ซับซ้อน เช่น slowloris, WAF bypass, brute force SSH pattern หลากหลาย ทั้งยังเบากว่า Fail2ban (ประหยัด CPU/RAM ได้ 30%) และตั้ง bouncers (firewall integration) ได้ง่าย รองรับ Nginx, Apache, Docker, iptables, kernel filter บทความนี้สอนติดตั้ง CrowdSec + bouncers สำหรับ VPS ทั่วไป
ความแตกต่าง CrowdSec vs Fail2ban
- Threat Intelligence: CrowdSec แชร์ข้อมูล IP/pattern ภัยจากทุก server ตัวเอง ส่วน Fail2ban ต้อง subscribe feeds อื่น ซื้อ paid service
- Detection: CrowdSec ใช้ scenario (rule engine) + machine learning ตรวจจับการโจมตี pattern ที่ซับซ้อน Fail2ban แค่ regex log line
- Remediation: CrowdSec bouncers ทำงานที่ layer ต่างๆ (iptables/Nginx/Docker) ตัวเดียวแก้ทั้ง webserver + SSH + database Fail2ban ต้องตั้ง jail หลายชั้น
- Scalability: CrowdSec ออกแบบสำหรับ VPS หลายตัวที่ต้องทำงานปกติสง่า — บ่อน IP ที่ถูก ban โดยทั่วโลก ไม่ใช่แต่เพียง server เดียว
ติดตั้ง CrowdSec Agent
# ติดตั้ง repo (Ubuntu/Debian)
curl -s https://packagecloud.io/install/repositories/crowdsec/crowdsec/script.deb.sh | sudo bash
# ติดตั้ง CrowdSec
sudo apt update
sudo apt install crowdsec -y
# ตรวจสอบเวอร์ชัน
crowdsec --version
# เริ่มต้น CrowdSec
sudo systemctl restart crowdsec
sudo systemctl enable crowdsec
# ดู status
sudo systemctl status crowdsec
ตั้งค่า CrowdSec: ลบ log ที่ไม่ต้องสแกน
ค่า default CrowdSec สแกน log ทั้งหมด แต่บ่อยครั้งมี log เก่ากลึกมากหรือ healthcheck spam ให้ตั้ง log_level และลบ parser ที่ไม่ต้องการ:
sudo nano /etc/crowdsec/acquis.yaml
# Comment out parser ที่ไม่ต้องการ (โดยเติม # หน้า line)
# ตัวอย่าง: ลบ docker logs ถ้าไม่ใช้ docker
# - /var/log/auth.log
# - /var/log/syslog
# + /var/log/nginx/*.log # เพิ่มแค่ Nginx log
# + /var/log/mysql/error.log # เพิ่ม MySQL error
# ลบ crowdsec logs เองออกไป (ป้องกัน recursive detection)
# - /var/log/crowdsec/agent.log # comment this out
# Restart
sudo systemctl restart crowdsec
ติดตั้ง Bouncer สำหรับ Nginx
Bouncer คือตัวเชื่อมระหว่าง CrowdSec กับ firewall/webserver — เมื่อ CrowdSec พบการโจมตี bouncer จะ block IP ให้ทันที
# ติดตั้ง Nginx bouncer
sudo apt install crowdsec-nginx-bouncer -y
# เปิด Nginx module
sudo nano /etc/nginx/modules-enabled/50-mod-http-lua.conf
# ตรวจสอบว่ามี line นี้ (ถ้าไม่มีให้เพิ่ม):
load_module modules/ngx_http_lua_module.so;
# สร้าง bouncer config
sudo nano /etc/nginx/http.d/crowdsec.conf
# ใส่ config นี้:
lua_shared_dict crowdsec 10m;
init_by_lua_file /usr/local/lib/lua/crowdsec-bouncer/init.lua;
access_by_lua_file /usr/local/lib/lua/crowdsec-bouncer/access.lua;
# ใส่ config ข้างบน ของ server block แต่ละอันใน nginx.conf
# แล้ว test + reload
sudo nginx -t
sudo systemctl reload nginx
ngx_http_lua_module ถ้า error ให้ติดตั้ง libnginx-mod-http-lua แทนติดตั้ง Bouncer สำหรับ iptables (รองรับ SSH + ทั้ง VPS)
# ติดตั้ง iptables bouncer
sudo apt install crowdsec-firewall-bouncer -y
# เปิด config
sudo nano /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml
# ตั้ง mode เป็น nftables (ใหม่กว่า iptables legacy) หรือ iptables ขึ้นกับระบบ:
firewall_backend: nftables # หรือ iptables (ตรวจด้วย iptables --version)
dice_size: 10 # จำนวน IP per chain
dice_rolls: 1 # จำนวน chain
# ถ้า server ใช้ nftables ต้องสร้าง chain ก่อน
sudo nft add table inet crowdsec
sudo nft add chain inet crowdsec crowdsec-chain '{ type filter hook input priority filter; policy accept; }'
# ตั้ง bouncers ให้ใช้ (รองรับ nftables):
# bouncers:
# - firewall-bouncer
# Restart
sudo systemctl restart crowdsec-firewall-bouncer
sudo systemctl enable crowdsec-firewall-bouncer
ลง WordPress plugin เพื่อส่ง error log ไป CrowdSec
ถ้า VPS ใช้ WordPress ให้ติดตั้ง plugin CrowdSec plugin เพื่อจับ brute force login, SQL injection, XSS attempt:
# ข้างใน WordPress admin:
# Plugins → Add New → Search "CrowdSec"
# ติดตั้ง "CrowdSec WordPress Security" โดย CrowdSec
# หรือ download มือ:
cd /var/www/html/wp-content/plugins/
sudo wget https://downloads.wordpress.org/plugin/crowdsec.zip
sudo unzip crowdsec.zip
sudo chown -R www-data:www-data crowdsec/
# ใน WordPress admin: Plugins → Activate "CrowdSec"
# Settings → CrowdSec → ต่อ API token (ดูหัวข้อถัดไป)
ตั้ง API Token สำหรับเชื่อมกับ CrowdSec Console (Optional แต่แนะนำ)
ถ้าต้องการดูสถิติการโจมตี log decision ผ่าน web dashboard ต้องสร้าง API token:
# สร้าง API token
sudo cscli bouncers add my-vps-bouncer
# จะได้ token แบบนี้:
# Created with ID xxx, the bouncers can be viewed in the Dash
# ก็ทำได้ด้วยคำสั่งนี้ด้วย (ดูที่สร้างไว้)
sudo cscli bouncers list
# ลบ bouncer ที่ไม่ใช้
sudo cscli bouncers delete BOUNCER_ID
ตรวจสอบว่า CrowdSec ทำงานถูกต้อง
# ตรวจ status crowdsec
sudo crowdsec -c /etc/crowdsec/config.yaml -cli
# หรือใช้ cscli (tool บรรทัดคำสั่ง)
sudo cscli metrics
# ควรเห็น alert ที่พบมา
# ดู decisions (IP ที่ถูก ban)
sudo cscli decisions list
# ดู alerts
sudo cscli alerts list
# ดู bouncer ที่ attach
sudo cscli bouncers list
# ดู crowdsec log
sudo tail -f /var/log/crowdsec/agent.log
# ดู bouncer log (firewall-bouncer)
sudo tail -f /var/log/crowdsec/firewall-bouncer.log
sudo cscli simulation list — ลองเขียน test log ใน /tmp เพื่อทดสอบ parsing ก่อนแก้ปัญหาที่พบบ่อย
Error: "firewall backend not available"
ระบบไม่รองรับ nftables หรือ iptables ส่วน firewall-bouncer ต่อไม่ได้
# ตรวจว่าระบบใช้ nftables หรือ iptables
nftables --version # ถ้า error = ไม่มี nftables
iptables --version # ตรวจ iptables
# ถ้ามี iptables legacy ให้ลง
sudo apt install iptables
# เปลี่ยน backend ใน crowdsec-firewall-bouncer.yaml
firewall_backend: iptables
# ตรวจสอบว่ามี modules ที่ต้องใช้
sudo modprobe nf_conntrack
sudo modprobe nf_tables
# Restart bouncer
sudo systemctl restart crowdsec-firewall-bouncer
Error: "agent.log reports acquisition error" + "Cannot read log file"
CrowdSec agent ไม่มีสิทธิ์อ่าน log file
# ตรวจ permission log file (เช่น Nginx)
ls -la /var/log/nginx/*.log
# CrowdSec agent รัน as crowdsec user ต้องให้อ่านได้
sudo chown -R crowdsec:adm /var/log/nginx/
sudo chmod 640 /var/log/nginx/*.log
# เช่นเดียวกัน auth log
sudo chown -R crowdsec:adm /var/log/auth.log
sudo chmod 640 /var/log/auth.log
# ตรวจ syslog
sudo usermod -a -G adm crowdsec
# Restart
sudo systemctl restart crowdsec
Error: "no decision made" — CrowdSec พบการโจมตี แต่ไม่ ban IP
Scenario ไม่เปิด หรือ bouncer ไม่ attach ถูกต้อง
# ตรวจ scenario ที่เปิด
sudo cscli scenarios list
# เปิด scenario ที่ต้องการ (เช่น SSH brute force)
sudo cscli scenarios install crowdsecurity/ssh-bf
# หรือเปิด all default scenario
sudo cscli hub install
# ตรวจ bouncer มี attach scenario ไหม
sudo cscli bouncers list -o json | grep -i scenario
# ถ้า bouncer ไม่ attach scenario ให้ add ใหม่:
sudo cscli bouncers add my-bouncer --machine-name localhost
# Restart
sudo systemctl restart crowdsec
Error: "Nginx bouncer: Lua error" หลังติดตั้ง
Lua module ไม่ load ถูกต้องใน Nginx
# ตรวจว่า Nginx มี Lua module
nginx -V 2>&1 | grep -i lua
# ถ้าไม่มี ให้ลง
sudo apt install libnginx-mod-http-lua
# ตรวจ config Nginx
sudo nano /etc/nginx/nginx.conf
# ต้องมี: load_module modules/ngx_http_lua_module.so;
# Test config
sudo nginx -t
# ถ้า syntax error ให้ลบ comment ที่ conflict
# (บ่อยเกิดจาก multi load_module)
sudo systemctl restart nginx
sudo systemctl restart crowdsec
คำถามที่พบบ่อย
CrowdSec ใช้ Internet แบบไหน? ข้อมูล server ส่งไปไหน?
CrowdSec agent ส่ง aggregated threat data ไปยัง CrowdSec API (api.crowdsec.net) — ไม่ส่ง full log files เฉพาะ IP + attack pattern ที่พบเท่านั้น ส่วนตัวเลข attack metrics ส่งไปเพื่อสร้างความเป็นอันหนึ่งอันเดียวกันของฐานข้อมูลภัย เมื่อระบบอื่นพบการโจมตีจาก IP เดียวกัน agent ของคุณจะได้ decision ไม่ต้องรอ attack ตัวเองแล้วจึงบ่น สามารถตั้ง offline mode ได้ถ้าไม่ต้องการเชื่อมต่อ external (แต่ threat intel ก็จะอ่อนลง)
CrowdSec ป้องกัน DDoS ได้ไหม?
บางส่วน — CrowdSec จับ slowloris, connection exhaustion, rate-based attacks ได้ แต่ DDoS ระดับ layer 3 (packet flood) ต้องใช้ upstream protection (Cloudflare, AWS Shield, CAF) CrowdSec เหมาะสำหรับ application-level DDoS เช่น WordPress brute force POST requests repetitive ขอ assets นั่นแหละที่ CrowdSec จะบ่น block ได้
สามารถบ่น IP เองได้ไหม ไม่รอให้ crowdsourcing ส่งมา?
ได้ — สามารถ add manual decision ด้วยคำสั่ง sudo cscli decisions add -i 1.2.3.4 -d 24h -r "manual block" หรือสร้าง custom scenario ที่ดัดแปลงจาก default scenarios แล้ว tune threshold ให้หลวมขึ้น (ลด attack count แต่ละครั้ง) — ดูใน /etc/crowdsec/scenarios/ copy scenario เดิมแล้วแก้