ติดตั้ง Suricata IDS/IPS บน VPS ตรวจจับการบุกรุก
Suricata คือ Intrusion Detection and Prevention System (IDS/IPS) โอเพนซอร์สที่พัฒนาโดย OISF (Open Information Security Foundation) — วิเคราะห์ network traffic แบบ real-time ตรวจจับ port scan, exploit attempt, malware C2 traffic, SQL injection ผ่าน HTTP และภัยคุกคามอีกหลายพันรูปแบบจากฐานข้อมูล Emerging Threats rules Suricata รองรับ multi-threading ทำงานได้เร็วแม้บน VPS core เดียว รองรับทั้ง IDS mode (แค่ log) และ IPS mode (block traffic ด้วย NFQueue) และยัง parse protocol หลายชนิดได้เอง เช่น HTTP, TLS/SSL, DNS, FTP, SMB โดยไม่ต้องพึ่งเครื่องมือแยก บทความนี้ครอบคลุมตั้งแต่ติดตั้งจนถึงดู alert จริง
IDS vs IPS — เลือกใช้อะไร?
| Mode | วิธีทำงาน | เหมาะกับ |
|---|---|---|
| IDS (IDPS) | Copy traffic ผ่าน af-packet หรือ pcap — ตรวจแต่ log ไม่ block | VPS production ที่ต้องการ visibility ก่อน เพื่อ tune rules |
| IPS (NFQueue) | ส่ง packet ผ่าน kernel queue — Suricata drop ได้จริง | VPS ที่ tune rules แล้ว ต้องการ active blocking |
แนะนำเริ่มด้วย IDS mode 1-2 สัปดาห์ก่อน ดู false positive แล้วค่อยเปิด IPS
ติดตั้ง Suricata บน Ubuntu/Debian
# เพิ่ม PPA (Ubuntu) — ได้เวอร์ชันใหม่กว่า default repo
sudo add-apt-repository ppa:oisf/suricata-stable -y
sudo apt update
sudo apt install suricata -y
# ตรวจสอบเวอร์ชัน
suricata --build-info | head -5
# เริ่ม service
sudo systemctl enable suricata
sudo systemctl start suricata
sudo systemctl status suricata
echo "deb http://deb.debian.org/debian bookworm-backports main" | sudo tee /etc/apt/sources.list.d/backports.list && sudo apt install -t bookworm-backports suricataค้นหา Network Interface
# ดู interface ที่ใช้งาน
ip a
# ดู interface ที่มี traffic จริง (ส่วนใหญ่คือ eth0 หรือ ens3)
ip route get 8.8.8.8 | grep dev
จดชื่อ interface ไว้ เช่น eth0 หรือ ens3 — จะใช้ในขั้นตอนต่อไป
ตั้งค่า suricata.yaml
sudo nano /etc/suricata/suricata.yaml
ค้นหาและแก้ค่าสำคัญ:
# 1. กำหนด HOME_NET ให้ตรงกับ subnet ของ VPS
vars:
address-groups:
HOME_NET: "[YOUR_VPS_IP/32]"
# ตัวอย่าง: "[203.0.113.50/32]"
# 2. กำหนด interface (ค้นหา af-packet)
af-packet:
- interface: eth0 # เปลี่ยนเป็น interface ของคุณ
cluster-id: 99
cluster-type: cluster_flow
defrag: yes
# 3. เปิด JSON log (eve-log)
outputs:
- eve-log:
enabled: yes
filetype: regular
filename: eve.json
types:
- alert:
payload: yes
payload-printable: yes
http-body: yes
- http:
extended: yes
- dns:
query: yes
answer: yes
- tls:
extended: yes
อัปเดต Rules ด้วย suricata-update
# อัปเดต Emerging Threats Open rules (ฟรี ไม่ต้องสมัคร)
sudo suricata-update
# ดูรายการ rule sources ที่มีให้
sudo suricata-update list-sources
# เปิดใช้ ET Pro (ถ้ามี subscription)
# sudo suricata-update enable-source et/pro --secret-code YOUR_CODE
# อัปเดต rules และ reload
sudo suricata-update
sudo suricatasc -c reload-rules
echo "0 3 * * * root suricata-update && suricatasc -c reload-rules" | sudo tee /etc/cron.d/suricata-updateรัน Suricata ใน IDS Mode
# ตรวจสอบ config ก่อนเริ่ม
sudo suricata -T -c /etc/suricata/suricata.yaml -v
# รัน Suricata ผ่าน systemd (IDS mode — af-packet)
sudo systemctl restart suricata
# หรือรันด้วยมือเพื่อ debug
sudo suricata -c /etc/suricata/suricata.yaml -i eth0
# ดู stats
sudo suricatasc -c dump-counters | grep -E "decoder|detect|alert"
เปลี่ยนเป็น IPS Mode ด้วย NFQueue
# ติดตั้ง iptables NFQueue support
sudo apt install libnetfilter-queue-dev -y
# ส่ง incoming traffic ไปที่ queue 0
sudo iptables -I INPUT -j NFQUEUE --queue-num 0 --queue-bypass
sudo iptables -I OUTPUT -j NFQUEUE --queue-num 0 --queue-bypass
sudo iptables -I FORWARD -j NFQUEUE --queue-num 0 --queue-bypass
# แก้ suricata.yaml เพื่อใช้ NFQueue
sudo nano /etc/suricata/suricata.yaml
# เปลี่ยน af-packet เป็น nfq:
# nfq:
# mode: accept
# fail-open: yes
# รัน Suricata ในโหมด IPS
sudo suricata -c /etc/suricata/suricata.yaml -q 0
# บันทึก iptables rules ให้คงอยู่หลัง reboot
sudo apt install iptables-persistent -y
sudo netfilter-persistent save
--queue-bypass เสมอใน NFQueue — ถ้า Suricata หยุดทำงาน traffic จะไหลผ่านได้ปกติ ไม่ทำให้ VPS disconnectดู Alert และ Log
# ดู alert แบบ real-time
sudo tail -f /var/log/suricata/eve.json | python3 -c "
import sys, json
for line in sys.stdin:
try:
e = json.loads(line)
if e.get('event_type') == 'alert':
print(f\"{e['timestamp']} [{e['alert']['severity']}] {e['alert']['signature']} {e.get('src_ip','')} -> {e.get('dest_ip','')}\")
except: pass
"
# ดู fast.log (อ่านง่ายกว่า)
sudo tail -f /var/log/suricata/fast.log
# นับ alert ทั้งหมดวันนี้
sudo jq -r 'select(.event_type=="alert") | .alert.signature' /var/log/suricata/eve.json | sort | uniq -c | sort -rn | head -20
ติดตั้ง Suricata-Update แบบ Custom Rule
# สร้าง custom rule ไฟล์
sudo nano /etc/suricata/rules/local.rules
# ตัวอย่าง rule: alert เมื่อมีคนพยายาม scan port 22 จากภายนอก
alert tcp any any -> $HOME_NET 22 (msg:"ET SCAN SSH Brute Force Attempt"; flags:S; threshold: type both, track by_src, count 5, seconds 60; classtype:attempted-admin; sid:9000001; rev:1;)
# ตัวอย่าง rule: detect SQL injection pattern ใน HTTP
alert http any any -> $HOME_NET any (msg:"SQL Injection Attempt"; content:"union select"; nocase; http.uri; classtype:web-application-attack; sid:9000002; rev:1;)
# เพิ่ม custom rule ใน suricata.yaml
sudo nano /etc/suricata/suricata.yaml
# ค้นหา rule-files: และเพิ่ม
# rule-files:
# - /etc/suricata/rules/local.rules
# Reload rules
sudo suricatasc -c reload-rules
ตรวจสอบว่า Suricata ทำงานถูกต้อง
# 1. ดู stats daemon
sudo suricatasc -c dump-counters | grep -E "uptime|alert|decoder"
# 2. ทดสอบ rule โดยส่ง test signature
curl -sA "BlackSun" http://localhost/ 2>/dev/null
# ดูว่ามี alert ใน fast.log ไหม
sudo grep "User-Agent" /var/log/suricata/fast.log | tail -5
# 3. ตรวจสอบ eve.json มีข้อมูลไหม
sudo jq '.event_type' /var/log/suricata/eve.json | sort | uniq -c
# 4. ดู interface statistics
sudo suricatasc -c iface-stat eth0
# 5. ตรวจสอบ Suricata process
ps aux | grep suricata
sudo systemctl status suricata --no-pager
sudo suricatasc -c iface-listแก้ปัญหาที่พบบ่อย
Error: "PCAP packet drop" หรือ alert count ต่ำผิดปกติ
เกิดจาก Suricata ไม่ทัน process packet เมื่อ traffic สูง — เพิ่ม worker thread ใน suricata.yaml:
# ใน suricata.yaml ค้นหา threading:
threading:
set-cpu-affinity: no
cpu-affinity:
- management-cpu-set:
cpu: [ 0 ]
- receive-cpu-set:
cpu: [ 0 ]
- worker-cpu-set:
cpu: [ "all" ]
mode: "balanced"
detect-thread-ratio: 1.5
# บน VPS 1 core ตั้งให้ใช้ af-packet workers เพิ่ม
af-packet:
- interface: eth0
threads: 2
cluster-id: 99
cluster-type: cluster_flow
Error: "suricata.service: Start request repeated too quickly"
Suricata crash เพราะ config ผิด ดู log ก่อน restart:
# ดู error message
sudo journalctl -u suricata -n 50 --no-pager
# ทดสอบ config syntax
sudo suricata -T -c /etc/suricata/suricata.yaml -v 2>&1 | grep -E "Error|Warning"
# ถ้ามี "Failed to open" — ตรวจ path ของ rules files
ls -la /var/lib/suricata/rules/
sudo suricata-update # ดาวน์โหลด rules ใหม่
Error: "NFQ queue 0: recv failed" เมื่อใช้ IPS mode
เกิดเพราะ iptables NFQueue rule ยังไม่ถูกตั้งค่า หรือ module kernel ไม่พร้อม:
# ตรวจ iptables rules
sudo iptables -L -n | grep NFQUEUE
# โหลด module kernel
sudo modprobe nfnetlink_queue
sudo modprobe nf_conntrack
# ตรวจว่า Suricata รันด้วย root
sudo suricata -c /etc/suricata/suricata.yaml -q 0 --user root
# ถ้ายังไม่ได้ ลอง fail-open mode
# suricata.yaml:
# nfq:
# fail-open: yes
False positive มากเกินไป — alert เต็ม log
บาง rule ไม่เหมาะกับ workload ของ VPS เช่น web server จะโดน ET SCAN rules บ่อย:
# suppress rule เฉพาะ SID โดยไม่ต้องลบ rule ทั้งหมด
sudo nano /etc/suricata/threshold.conf
# เพิ่ม suppress สำหรับ SID ที่ false positive
suppress gen_id 1, sig_id 2010935 # ตัวอย่าง SID
suppress gen_id 1, sig_id 2010935, track by_src, ip 192.168.1.0/24
# Reload
sudo suricatasc -c reload-rules
# หรือใช้ disable-conf
sudo nano /etc/suricata/disable.conf
# เพิ่ม:
# re:emerging-scan
Suricata ใช้ RAM มากเกินไปบน VPS เล็ก
ลด memory สำหรับ flow tracking และ defrag:
# แก้ใน suricata.yaml
flow:
memcap: 64mb # ลดจาก default 128mb
hash-size: 65536
prealloc: 10000
stream:
memcap: 64mb # ลดจาก default
defrag:
memcap: 32mb # ลดสำหรับ VPS RAM ต่ำ
# restart
sudo systemctl restart suricata
คำถามที่พบบ่อย
Suricata กินทรัพยากรเยอะไหม เหมาะกับ VPS ราคาถูกไหม?
IDS mode (af-packet) กิน CPU ประมาณ 5-15% บน VPS 1-2 vCore และ RAM ประมาณ 200-400 MB ขึ้นอยู่กับปริมาณ traffic และจำนวน rules ที่เปิดใช้ สำหรับ VPS ราคาถูก (1GB RAM) แนะนำเปิด rules เฉพาะ group ที่จำเป็นเช่น emerging-web-server และ emerging-exploit เท่านั้น โดยใช้คำสั่ง suricata-update disable-source ปิด rule set ที่ไม่เกี่ยวข้อง
Suricata กับ CrowdSec ต่างกันอย่างไร ใช้ร่วมกันได้ไหม?
Suricata คือ network-level IDS/IPS ที่วิเคราะห์ raw packet และ protocol payload เหมาะตรวจจับ exploit และ malware C2 traffic ในระดับลึก ส่วน CrowdSec เป็น log-based behavioral analysis ที่ใช้ crowdsourced threat intel เพื่อ block IP ที่มีพฤติกรรมน่าสงสัย ใช้ร่วมกันได้ดีมาก — Suricata ตรวจจับ network-level threat แล้ว alert ผ่าน eve.json ส่วน CrowdSec อ่าน fast.log ของ Suricata และ block IP อัตโนมัติ
ถ้า Suricata block traffic ผิด VPS จะ disconnect ไหม?
ถ้าใช้ IPS mode ด้วย NFQueue และไม่ใส่ --queue-bypass ใน iptables — มีโอกาส disconnect ถ้า Suricata crash หรือตั้งค่าผิด วิธีป้องกัน: (1) เสมอใช้ --queue-bypass ใน iptables rule, (2) ตั้ง fail-open: yes ใน suricata.yaml, (3) เริ่มจาก IDS mode ก่อนเสมอ ทดสอบ rules สัก 1-2 สัปดาห์ให้แน่ใจว่าไม่มี false positive กับ traffic ปกติของ VPS ก่อนเปิด IPS