🛡 Linux:Fail2Ban 進階自訂規則、Filter 解析與多服務防禦策略
Fail2Ban 是 Linux 系統最重要的主動式防護工具之一,透過分析日誌並自動封鎖惡意 IP,有效降低暴力破解與惡意存取的風險。本篇從基本原理、filter 自訂、jail 組合、防火牆整合到多服務保護策略完整解析。
📌 一、Fail2Ban 運作原理
Fail2Ban 的核心流程可分為 3 個部分:
- Filter:從日誌中找出惡意行為(如密碼錯誤、非法登入)
- Jail:定義封鎖條件,例如「5 分鐘內失敗 5 次」
- Action:觸發封鎖(如調用 iptables、nftables、firewalld)
只要 Filter 偵測到不合法的嘗試,就會觸發 jail,並把來源 IP 封鎖指定時間。
---📌 二、安裝 Fail2Ban
🟦 Debian / Ubuntu
sudo apt install -y fail2ban
sudo systemctl enable --now fail2ban
🟥 CentOS / Rocky / AlmaLinux
sudo yum install -y epel-release
sudo yum install -y fail2ban fail2ban-systemd
sudo systemctl enable --now fail2ban
查看運作狀態:
sudo systemctl status fail2ban
fail2ban-client status
---
📌 三、Fail2Ban 基本設定:jail.local
主要設定檔請勿直接修改 /etc/fail2ban/jail.conf,應建立 override:
sudo nano /etc/fail2ban/jail.local
範例:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
backend = systemd
ignoreip = 127.0.0.1/8 10.0.0.0/24
[sshd]
enabled = true
port = ssh
logpath = /var/log/auth.log
---
📌 四、Filter 深入解析(filter.d)
Filter 是 Fail2Ban 的核心,用 regex 從日誌找出惡意行為。
Filter 位置:
/etc/fail2ban/filter.d/
📝 範例:自訂 SSH 暴力破解 filter(擴充版)
sudo nano /etc/fail2ban/filter.d/ssh-bruteforce.conf
內容:
[Definition]
failregex = ^.*Failed password for .* from <HOST> port .* ssh2
^.*Invalid user .* from <HOST> port .* ssh2
ignoreregex =
🔍 regex 中的 <HOST> 由 Fail2Ban 自動替換成 IP 匹配區域。
測試 filter:
sudo fail2ban-regex /var/log/auth.log /etc/fail2ban/filter.d/ssh-bruteforce.conf
---
📌 五、Jail 進階設定(maxretry、banaction、port)
為自訂 filter 建立 jail:
sudo nano /etc/fail2ban/jail.d/ssh-bruteforce.local
內容:
[ssh-bruteforce]
enabled = true
filter = ssh-bruteforce
action = iptables-multiport
port = ssh
logpath = /var/log/auth.log
maxretry = 4
findtime = 5m
bantime = 12h
這個 jail 的策略是:「5 分鐘內錯 4 次 → 封 12 小時」。
---
📌 六、多服務整合:nginx / postfix / dovecot / proftpd
Fail2Ban 支援多數常見服務,只要啟用對應 jail 即可。
🟩 nginx 保護(最常被暴力掃描)
[nginx-http-auth]
enabled = true
filter = nginx-http-auth
port = http,https
logpath = /var/log/nginx/error.log
🟧 postfix(郵件暴破)
[postfix]
enabled = true
port = smtp,ssmtp
logpath = /var/log/maillog
🟦 dovecot(IMAP/POP 嘗試)
[dovecot]
enabled = true
logpath = /var/log/maillog
🟥 ProFTPD / vsftpd
[proftpd]
enabled = true
logpath = /var/log/proftpd/proftpd.log
---
📌 七、防火牆整合:iptables、nftables、firewalld
Fail2Ban 會自動呼叫系統的 firewall action,你也能手動指定。
| 防火牆 | Fail2Ban Action |
|---|---|
| iptables | iptables-multiport |
| nftables | nftables-multiport |
| firewalld | firewallcmd-multiport |
🟪 強制指定 nftables
[sshd]
enabled = true
banaction = nftables-multiport
---
📌 八、Fail2Ban 實用指令大全
查看所有 jail
sudo fail2ban-client status
查看某 jail
sudo fail2ban-client status sshd
封鎖 / 解鎖 IP
sudo fail2ban-client set sshd banip 1.2.3.4
sudo fail2ban-client set sshd unbanip 1.2.3.4
重新載入設定
sudo systemctl reload fail2ban
---
📌 九、最佳化建議(正式環境必做)
- 搭配 GeoIP 過濾,封鎖非台灣/非必要國家
- SSH 改非預設 22 port
- 啟用 fail2ban persistent log(保留封鎖紀錄)
- 與 Fail2Ban Cloud API 整合黑名單(選用)
- 定期檢查誤判 IP,加入 ignoreip 白名單
🔗 延伸閱讀
- 🔐 Linux 使用者與 ACL 高階權限管理
- 🛡 Linux 防火牆完整指南:iptables / nftables / firewalld
- 🛡 SELinux / AppArmor / Auditd / Fail2Ban 安全整合
- 🧱 Linux 伺服器從安裝到強化完整教學
— WWFandy・Linux Security 筆記
沒有留言: