คู่มือนี้แนะนำวิธีตรวจจับและบล็อก IP ที่ทำ DoS/DDoS Attack ด้วย psad (Port Scan Attack Detector) ร่วมกับ iptables
⚠️ คำเตือน: psad เป็นเครื่องมือทรงพลัง การตั้งค่าผิดอาจบล็อก IP ของตัวเองหรือผู้ใช้งานจริง ให้ทดสอบบนระบบทดสอบก่อน และมี White-list พร้อม
ติดตั้ง psad
# Ubuntu/Debian
sudo apt install psad
# CentOS/RHEL/Fedora
sudo dnf install psad
# หรือ yum install psad
ตั้งค่า Syslog สำหรับ psad
# แก้ไข /etc/rsyslog.conf (หรือ /etc/syslog.conf ระบบเก่า)
# เพิ่มบรรทัด:
kern.info |/var/lib/psad/psadfifo
# รีสตาร์ท syslog
systemctl restart rsyslog
สร้าง White-list (Safe IP List)
# vim /etc/psad/auto_dl
# หรือสร้างไฟล์แยก
cat > /home/user/safeiplist.cfg << 'EOF'
127.0.0.0/24
192.168.0.0/24
10.0.0.0/8
203.0.113.50 # IP สำนักงาน
EOF
ตั้งค่า iptables Rules พื้นฐาน (Script)
#!/bin/bash
WORKDIR="/home/user"
INTERVAL="5"
HITCOUNT="5"
SAFEIPLIST="safeiplist.cfg"
cd $WORKDIR
# ล้างกฎเก่า
iptables -F
# โหลด White-list
if [ -f $SAFEIPLIST ]; then
IPS=$(grep -Ev "^#" $SAFEIPLIST)
for i in $IPS; do
iptables -A INPUT -s $i -j ACCEPT
done
fi
# ตรวจจับ Port Scan / Flood
iptables -A INPUT -m state --state NEW -m recent --set
iptables -A INPUT -m state --state NEW -m recent --update --seconds $INTERVAL --hitcount $HITCOUNT -j LOG --log-prefix "PSAD: "
# บันทึกกฎ
iptables-save > /etc/iptables/rules.v4
ตั้งค่า psad (/etc/psad/psad.conf)
EMAIL_ADDRESSES admin@yourdomain.com;
HOSTNAME yourserver.yourdomain.com;
HOME_NET NOT_USED; # หากมี NIC เดียว
IGNORE_PORTS udp/80, udp/8080; # ข้าม port ที่คาดว่าจะมี traffic มาก
ENABLE_AUTO_IDS Y; # บล็อกอัตโนมัติ
AUTO_IDS_DANGER_LEVEL 3; # ระดับอันตรายที่จะบล็อก (1-5)
AUTO_BLOCK_TIMEOUT 3600; # บล็อก 1 ชั่วโมง
เริ่มใช้งานและตรวจสอบ
# รีสตาร์ท psad
systemctl restart psad
# ดูสถานะ
psad -S
# ดูรายการ IP ที่ถูกบล็อก
psad --fw-list
# ยกเลิกบล็อก IP
psad -F
# หรือ
iptables -D INPUT -s 1.2.3.4 -j DROP
เครื่องมือเสริมสำหรับ DDoS Protection
- fail2ban - บล็อกจาก Log authentication failure (SSH, HTTP Auth)
- CrowdSec - Modern, Community-powered, Decentralized
- Cloudflare / AWS Shield - Layer 7 DDoS Protection (แนะนำสำหรับ Production)
- ModSecurity (WAF) - Application layer protection
psad ดีสำหรับ Port Scan Detection และ Network-level flooding แต่ DDoS จริง (Volumetric) ต้องใช้ Upstream/Cloud protection