Fail2ban ป้องกัน Brute Force 2026

Fail2ban ป้องกัน Brute Force SSH และ Web Server 2026

fail2ban brute force protection 2026

ทุก server ที่มี public IP address ถูก brute force attack ตลอด 24 ชั่วโมงจากสถิติพบว่า server ใหม่ที่เปิด SSH port 22 จะถูก attack ภายใน 15 นาทีหลัง provision โดย bot ที่ scan IP range ทั้ง internet Fail2ban เป็นเครื่องมือที่ป้องกันการ attack เหล่านี้โดยอ่าน log file แล้ว ban IP ที่ login ผิดซ้ำๆอัตโนมัติ

วิดีโอประกอบการเรียนรู้ | YouTube @icafefx

บทความนี้ครอบคลุมทุกอย่างตั้งแต่ติดตั้ง Fail2ban ตั้ง jail สำหรับ SSH Nginx Apache WordPress เขียน custom filter สำหรับ application ของคุณเองตั้ง email notification monitoring และ best practices สำหรับ production server ทุกคำสั่งทดสอบบน Ubuntu 24.04 LTS ใช้ได้จริงสำหรับ SSH hardening เชิงลึกแนะนำอ่าน SSH Security Hardening ร่วมด้วยครับ

สารบัญ

1. Fail2ban ทำงานอย่างไร

Fail2ban ทำงานง่ายมากแต่ได้ผลดีเยี่ยมมี 3 component หลักได้แก่ Jail ที่กำหนดว่าจะ monitor log file ไหน ban กี่ครั้งนานเท่าไหร่ Filter ที่เป็น regex pattern สำหรับจับ failed login จาก log และ Action ที่กำหนดว่าจะทำอะไรเมื่อ ban เช่น block ด้วย iptables ส่ง email หรือ report ไป AbuseIPDB

เมื่อ Fail2ban ตรวจพบว่า IP หนึ่งๆมี failed login เกินจำนวน maxretry ภายในช่วงเวลา findtime จะ trigger action ที่กำหนดไว้ซึ่งปกติคือเพิ่ม rule ใน iptables หรือ nftables เพื่อ DROP traffic จาก IP นั้นตามระยะเวลา bantime เมื่อหมดเวลา ban จะ unban อัตโนมัติ

2. ติดตั้ง Fail2ban

# ติดตั้ง Fail2ban
sudo apt update
sudo apt install -y fail2ban

# ตรวจสอบ version
fail2ban-client --version
# Fail2Ban v1.0.2

# เปิด service
sudo systemctl enable --now fail2ban

# ตรวจสอบ status
sudo systemctl status fail2ban
# Active: active (running)

# ดู jail ที่ active
sudo fail2ban-client status
# Number of jail: 1
# Jail list: sshd

หลังติดตั้ง Fail2ban จะเปิด SSH jail ให้อัตโนมัติแต่ config default ค่อนข้างอ่อนต้องปรับเพิ่มเติม

3. Config พื้นฐาน jail.local

ห้ามแก้ไฟล์ jail.conf โดยตรงเพราะจะถูก overwrite เมื่ออัพเดทให้สร้าง jail.local แทน

# สร้าง jail.local
sudo nano /etc/fail2ban/jail.local

[DEFAULT]
# Ban นาน 1 ชั่วโมง
bantime = 3600

# ตรวจย้อนหลัง 10 นาที
findtime = 600

# ผิดได้ 3 ครั้ง
maxretry = 3

# IP ที่ไม่ ban (ใส่ IP ของตัวเอง!)
ignoreip = 127.0.0.1/8 ::1 YOUR_HOME_IP

# ใช้ systemd สำหรับอ่าน log
backend = systemd

# Action default — ban ด้วย iptables
banaction = iptables-multiport
banaction_allports = iptables-allports

# Email notification (optional)
destemail = admin@example.com
sender = fail2ban@example.com
action = %(action_mwl)s

4. ป้องกัน SSH Brute Force

# เพิ่มใน /etc/fail2ban/jail.local
[sshd]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600

# สำหรับ SSH ที่ใช้ port อื่น เช่น 99
[sshd]
enabled = true
port = 99
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 86400
findtime = 600
# Restart Fail2ban
sudo systemctl restart fail2ban

# ตรวจสอบ SSH jail
sudo fail2ban-client status sshd
# Status for the jail: sshd
# |- Filter
# | |- Currently failed: 2
# | |- Total failed: 45
# | `- File list: /var/log/auth.log
# `- Actions
# |- Currently banned: 3
# |- Total banned: 12
# `- Banned IP list: 192.168.1.100 10.0.0.50 203.0.113.15

สำหรับ SSH port ที่ไม่ใช่ 22 อย่าลืมเปลี่ยน port ใน jail config ด้วยไม่งั้น Fail2ban จะ ban ผิด port สำหรับ SSH hardening เพิ่มเติมเช่นปิด password authentication ใช้ key-based auth เปลี่ยน port แนะนำอ่าน SSH Security Hardening

5. ป้องกัน Nginx HTTP Auth

# เพิ่มใน /etc/fail2ban/jail.local
[nginx-http-auth]
enabled = true
port = http, https
filter = nginx-http-auth
logpath = /var/log/nginx/error.log
maxretry = 3
bantime = 3600

# ป้องกัน Nginx 404 scan (bot scan หา vulnerable paths)
[nginx-botsearch]
enabled = true
port = http, https
filter = nginx-botsearch
logpath = /var/log/nginx/access.log
maxretry = 5
bantime = 86400

# ป้องกัน request ที่ใหญ่เกินไป (buffer overflow attempt)
[nginx-limit-req]
enabled = true
port = http, https
filter = nginx-limit-req
logpath = /var/log/nginx/error.log
maxretry = 5
bantime = 3600

สำหรับ Nginx reverse proxy configuration แบบละเอียดอ่าน Nginx Reverse Proxy + Load Balancing

6. ป้องกัน WordPress Login

# สร้าง filter สำหรับ WordPress
sudo nano /etc/fail2ban/filter.d/wordpress-login.conf

[Definition]
failregex = ^<HOST> .* "POST /wp-login.php
 ^<HOST> .* "POST /xmlrpc.php
ignoreregex =

# เพิ่ม jail ใน jail.local
[wordpress-login]
enabled = true
port = http, https
filter = wordpress-login
logpath = /var/log/nginx/access.log
maxretry = 3
bantime = 86400
findtime = 300

WordPress เป็น target หลักของ brute force attack เพราะ /wp-login.php และ /xmlrpc.php เป็น endpoint ที่ bot ทุกตัวรู้จักการ block ด้วย Fail2ban ลดจำนวน attack ได้กว่า 99 เปอร์เซ็นต์

7. เขียน Custom Filter

สำหรับ application ที่ไม่มี filter สำเร็จรูปสามารถเขียน custom filter ได้ง่ายมาก

# ตัวอย่าง: ป้องกัน API brute force
# สมมติ log format: 2026-02-22 10:00:00 WARN: Failed API auth from 192.168.1.100

sudo nano /etc/fail2ban/filter.d/myapi-auth.conf

[Definition]
failregex = ^.*WARN: Failed API auth from <HOST>$
ignoreregex =
datepattern = ^%%Y-%%m-%%d %%H:%%M:%%S

# ทดสอบ filter กับ log file จริง
sudo fail2ban-regex /var/log/myapi/auth.log /etc/fail2ban/filter.d/myapi-auth.conf

# Results:
# Failregex: 45 total
# Ignoreregex: 0 total
# Date template hits: 45 hits

# เพิ่ม jail
[myapi-auth]
enabled = true
port = 8080
filter = myapi-auth
logpath = /var/log/myapi/auth.log
maxretry = 5
bantime = 1800

fail2ban-regex เป็นเครื่องมือที่มีประโยชน์มากสำหรับทดสอบ filter ก่อน deploy ต้องใช้ทุกครั้งก่อนเปิดใช้ jail ใหม่เพื่อให้แน่ใจว่า regex จับ pattern ได้ถูกต้อง

8. Email Notification

# ติดตั้ง mail tool
sudo apt install -y mailutils

# ตั้ง action ที่ส่ง email พร้อม whois และ log lines
# ใน jail.local [DEFAULT]
action = %(action_mwl)s

# action_mwl = ban + mail with whois + log lines
# action_mw = ban + mail with whois
# action_ = ban only (ไม่ส่ง mail)

สำหรับ production ที่โดน attack เยอะไม่แนะนำให้ส่ง email ทุก ban เพราะจะได้ email เป็นร้อยต่อวันใช้ action_ (ban only) แล้ว monitor ผ่าน fail2ban-client status แทนหรือส่ง metrics ไป Prometheus แล้วตั้ง alert ใน Grafana เฉพาะ spike ที่ผิดปกติ

9. ใช้ร่วมกับ UFW

# ตั้ง Fail2ban ให้ใช้ UFW แทน iptables
# ใน jail.local [DEFAULT]
banaction = ufw
banaction_allports = ufw

# เมื่อ ban IP จะเรียก: ufw insert 1 deny from IP
# เมื่อ unban: ufw delete deny from IP

# ตรวจ UFW rules หลัง ban
sudo ufw status numbered
# [ 1] Anywhere DENY IN 203.0.113.15
# [ 2] 22/tcp ALLOW IN Anywhere
# [ 3] 80/tcp ALLOW IN Anywhere

10. Monitoring และ Commands

# ดู status ทุก jail
sudo fail2ban-client status

# ดู status jail เฉพาะ
sudo fail2ban-client status sshd

# ดู IP ที่โดน ban
sudo fail2ban-client get sshd banip

# Unban IP
sudo fail2ban-client set sshd unbanip 192.168.1.100

# Unban ทุก IP ทุก jail
sudo fail2ban-client unban --all

# Ban IP manual
sudo fail2ban-client set sshd banip 203.0.113.50

# ดู log ของ Fail2ban
sudo tail -f /var/log/fail2ban.log

# ดูสถิติ ban
sudo zgrep 'Ban' /var/log/fail2ban.log | wc -l

# ดู top 10 IP ที่โดน ban บ่อย
sudo zgrep 'Ban' /var/log/fail2ban.log | awk '{print $NF}' | sort | uniq -c | sort -rn | head -10

11. Incremental Banning

Fail2ban รองรับ incremental banning ที่เพิ่มระยะเวลา ban ทุกครั้งที่ IP เดิมถูก ban ซ้ำเช่นครั้งแรก ban 1 ชั่วโมงครั้งที่สอง 6 ชั่วโมงครั้งที่สาม 1 วันครั้งที่สี่ 1 สัปดาห์

# เพิ่มใน jail.local [DEFAULT]
bantime.increment = true
bantime.factor = 4
bantime.formula = ban.Time * math.exp(float(ban.Count+1)*banFactor)/math.exp(1*banFactor)
bantime.maxtime = 604800

# ครั้งที่ 1: ~1 ชั่วโมง
# ครั้งที่ 2: ~4 ชั่วโมง
# ครั้งที่ 3: ~16 ชั่วโมง
# ครั้งที่ 4: ~64 ชั่วโมง (cap ที่ 7 วัน)

Incremental banning เป็น feature ที่ทรงพลังมาก bot ที่กลับมา attack ซ้ำจะถูก ban นานขึ้นเรื่อยๆจนในที่สุดไม่คุ้มที่จะ attack ต่อ

Fail2ban ไม่ ban IP

# ตรวจว่า jail enabled จริง
sudo fail2ban-client status

# ตรวจว่า filter จับ log ได้
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/sshd.conf

# ตรวจว่า log path ถูกต้อง
ls -la /var/log/auth.log

# ตรวจว่า backend ถูก (systemd vs file)
# ถ้าใช้ systemd journald ต้องตั้ง backend = systemd

Ban ตัวเองผิด

# Unban ทันที
sudo fail2ban-client set sshd unbanip YOUR_IP

# เพิ่ม IP ใน ignoreip เพื่อป้องกัน
# ใน jail.local [DEFAULT]
ignoreip = 127.0.0.1/8 ::1 YOUR_HOME_IP YOUR_OFFICE_IP

Fail2ban ใช้ memory เยอะ

# ลด dbpurgeage (ลบ ban history เก่า)
# ใน jail.local [DEFAULT]
dbpurgeage = 86400 # เก็บ history แค่ 1 วัน

# หรือปิด database เลย (ไม่แนะนำ เพราะ incremental ban จะไม่ทำงาน)
dbfile = /dev/null

Fail2ban กับ Security Strategy ขององค์กร

Fail2ban เป็นเพียงชั้นหนึ่งของ defense in depth strategy ที่ดีไม่ควรพึ่ง Fail2ban อย่างเดียวต้องใช้ร่วมกับมาตรการอื่นด้วยชั้นแรกคือ firewall ที่เปิดเฉพาะ port ที่จำเป็นชั้นที่สองคือ SSH key authentication ที่ปิด password login ทั้งหมดชั้นที่สามคือ Fail2ban ที่ ban IP ที่พยายาม attack ชั้นที่สี่คือ VPN ที่จำกัดการเข้าถึง management port เฉพาะผ่าน VPN เท่านั้น

สำหรับ VPN แนะนำ WireGuard VPN หรือ OpenVPN ครับสำหรับ monitoring ที่ครอบคลุม server ทุกด้านดู Zabbix Monitoring เรื่อง automation ของ security configuration ใช้ Ansible deploy Fail2ban config ไปทุก server พร้อมกันได้

สำหรับผู้ที่สนใจด้านการลงทุนและ financial security แนะนำ iCafeForex.com สอน Forex ครบวงจรปกป้องพอร์ตลงทุนของคุณเหมือนที่ Fail2ban ปกป้อง server รับสัญญาณเทรดฟรีจาก XMSignal.com/th รีวิวอุปกรณ์ IT ที่ SiamLancard.com และ Siam2R.com

Fail2ban คืออะไรทำงานอย่างไร

Fail2ban เป็น intrusion prevention tool ที่อ่าน log file ของ service ต่างๆแล้วตรวจจับ pattern ของการ login ผิดพลาดเมื่อพบ IP ที่ login ผิดเกินจำนวนครั้งที่กำหนดจะ ban IP นั้นอัตโนมัติด้วย iptables หรือ nftables

Fail2ban กับ UFW ใช้ร่วมกันได้ไหม

ได้ครับตั้ง banaction = ufw ใน jail.local เมื่อ ban IP จะเรียก ufw deny from IP แทน iptables โดยตรงทำงานร่วมกับ UFW rules ที่มีอยู่ได้ดี

ตั้ง maxretry และ bantime เท่าไหร่ดี

สำหรับ SSH แนะนำ maxretry 3 ถึง 5 ครั้ง bantime 1 ถึง 24 ชั่วโมง findtime 10 นาทีสำหรับ web application แนะนำ maxretry 5 ถึง 10 ครั้ง bantime 30 นาทีถึง 1 ชั่วโมงถ้าโดน attack หนักให้ตั้ง bantime เป็น -1 คือ ban ถาวร

Fail2ban รองรับ IPv6 หรือไม่

รองรับครับตั้งแต่ version 0.10 เป็นต้นมารองรับ IPv6 เต็มรูปแบบตรวจจับและ ban ทั้ง IPv4 และ IPv6 ได้อัตโนมัติ

ถ้า ban ตัวเองผิดจะ unban อย่างไร

ใช้คำสั่ง sudo fail2ban-client set sshd unbanip YOUR_IP หรือเข้าผ่าน console ของ hosting provider แล้วรัน fail2ban-client unban --all เพื่อ unban ทั้งหมดป้องกันโดยเพิ่ม IP ตัวเองใน ignoreip

ติดตั้ง Fail2ban วันนี้ใช้เวลาแค่ 10 นาทีแต่ป้องกัน server ของคุณจาก brute force attack ได้ตลอด 24 ชั่วโมงทุกวันไม่มีเหตุผลที่จะไม่ติดตั้งครับเพราะทุก server ที่มี public IP ถูก scan และ attack ตลอดเวลา

สรุป

Fail2ban เป็นเครื่องมือที่ผมติดตั้งเป็นอันดับแรกหลังจาก provision server ใหม่ทุกครั้งก่อน web server ก่อน application ก่อนทุกอย่างเพราะ brute force attack เริ่มภายในไม่กี่นาทีหลังเปิด SSH port การมี Fail2ban ตั้งแต่แรกช่วยป้องกันปัญหาตั้งแต่ต้นดีกว่ามาตามแก้ทีหลังครับ

Best Practices สำหรับ Fail2ban ในองค์กร

จากประสบการณ์ที่ deploy Fail2ban ให้หลายองค์กรผมมี best practices ที่อยากแนะนำข้อแรกคือตั้ง ignoreip ให้ครอบคลุม IP ทุกตัวที่ admin ใช้เข้าถึง server เพื่อป้องกันการ ban ตัวเองข้อที่สองคือเปิด incremental banning เพื่อให้ bot ที่กลับมา attack ซ้ำถูก ban นานขึ้นเรื่อยๆข้อที่สามคือตั้ง monitoring สำหรับ Fail2ban เองดูว่า ban กี่ IP ต่อวันถ้าจำนวนเพิ่มขึ้นผิดปกติอาจเป็น targeted attack ที่ต้องใช้มาตรการเพิ่มเติมข้อที่สี่คือ test filter ด้วย fail2ban-regex ทุกครั้งก่อนเปิดใช้ jail ใหม่เพื่อให้แน่ใจว่า regex จับ pattern ได้ถูกต้องครับ

Fail2ban เป็นเครื่องมือป้องกัน brute force ที่ทุก Linux server ต้องติดตั้งใช้เวลา setup แค่ 10 นาทีแต่ป้องกัน attack ได้กว่า 99 เปอร์เซ็นต์ด้วย custom filter ที่เขียนเองได้ Fail2ban ปกป้องได้ทุก service ที่มี log file จำไว้ว่า Fail2ban เป็นเพียงชั้นหนึ่งของ security ต้องใช้ร่วมกับ firewall SSH hardening VPN และ regular updates เพื่อ defense in depth ที่แข็งแกร่งที่สุดครับ

บทความแนะนำ

ปกป้องพอร์ตลงทุนของคุณเหมือน Fail2ban ปกป้อง server iCafeForex.com สอน Forex พร้อม EA Trading อัตโนมัติ

รับ สัญญาณเทรด Forex ฟรีจาก XMSignal