☛ 簡介
Fail2ban 是一套以 Python 語言所撰寫的 GPLv2 授權軟體,藉由分析系統紀錄檔,並透過設定過濾條件 (filter) 及動作 (action),當符合我們所設定的過濾條件時,將觸發相對動作來達到自動化反應的效果 ( 如封鎖來源 IP、寄信通知管理者、查詢來源 IP 資訊等 )。 因其架構相當彈性,我們可以針對自己的需求,設計不同的過濾條件與動作來達到伺服器防護的功能,或是及時的反應某些異常資訊。 常見應用有:
① 阻擋 SSH、FTP 多次嘗試錯誤連線。
② 阻擋特定的瀏覽器或網路爬蟲。
③ 提供管理者瞭解異常伺服器服務要求 ( 如 apache、bind、postfix、vsftpd、proftpd… )。
☛ 安裝
須安裝 epel 的軟體套件:
[root@localhost ~ ]# yum install epel-release
再安裝 Fail2Ban:
[root@localhost ~ ]# yum install fail2ban
☛ 設定
使用 gedit 編輯器建立 /etc/fail2ban/jail.local 檔案,來自訂 fail2ban 設定,設定內容如下:
[DEFAULT] # 禁止時間 (秒) bantime = 86400 # 禁止方式 banaction = iptables-multiport # 要監聽的服務 [postfix-sasl] enabled = true [dovecot] enabled = true
☛ 啟用
[root@localhost ~ ]# systemctl start fail2ban [root@localhost ~ ]# systemctl enable fail2ban
☛ 查看 Ban ( 禁止 ) 狀態
檢視 postfix-sasl 服務的狀態和被禁止的 IP:
[root@localhost ~ ]# fail2ban-client status postfix-sasl Status for the jail: postfix-sasl |- Filter | |- Currently failed: 7 | |- Total failed: 1580 | `- Journal matches: _SYSTEMD_UNIT=postfix.service `- Actions |- Currently banned: 2 |- Total banned: 86 `- Banned IP list: 181.214.206.170 181.214.206.195
檢視 iptables 服務,確實拒絕了上述被禁止的 IP:
[root@localhost ~ ]# iptables -L -n ... 以上省略 ... # 對應上述 jail.local 自訂的設定,所監聽的服務 Chain f2b-dovecot (1 references) target prot opt source destination RETURN all -- 0.0.0.0/0 0.0.0.0/0 Chain f2b-postfix-sasl (1 references) target prot opt source destination # 拒絕的 IP 規則 REJECT all -- 181.214.206.195 0.0.0.0/0 reject-with icmp-port-unreachable REJECT all -- 181.214.206.170 0.0.0.0/0 reject-with icmp-port-unreachable
☛ 查看 Log 檔
狀態說明 :
① Unban:之前被禁止的 IP,但禁止時間到了所以解除。
② Ban:被禁止的 IP。
③ Found:登入失敗的 IP。
[root@localhost ~ ]# cat /var/log/fail2ban.log 2018-06-20 13:57:47,106 fail2ban.actions [16338]: NOTICE [postfix-sasl] Unban 181.214.206.170 2018-06-20 13:59:58,379 fail2ban.actions [16338]: NOTICE [postfix-sasl] Ban 181.214.206.170 2018-06-20 14:00:02,993 fail2ban.filter [16338]: INFO [postfix-sasl] Found 193.15.218.238
☛ 攔截暴力入侵 WordPress
安裝好 Fail2ban 後,在 Fail2ban 的 filter 目錄建立 wordpress 的 filter, 以下會以 /etc/fail2ban/filter.d/wordpress.conf 作為例子:
[root@localhost ~ ]# gedit /etc/fail2ban/filter.d/wordpress.conf
加入以下內容:
# WordPress brute force auth filter: /etc/fail2ban/filter.d/wordpress.conf: # # Block IPs trying to auth wp wordpress # # [Definition] failregex = ^<HOST> .* "POST .*(wp-login.php|xmlrpc.php) ignoreregex =
然後開啟 Fail2ban 的設定檔 /etc/fail2ban/jail.conf,加入以下內容:
[wordpress] enabled = true port = http,https action = iptables-multiport[name=wordpress, port="http,https", protocol=tcp] filter = wordpress logpath = /var/log/httpd/access_log findtime = 1800 bandtime = 1800 maxretry = 10
上面的設定需要根據主機的環境設定:
logpath: 這是 Apache 紀錄檔的位置。
bantime: 封鎖 IP 的時間,單位是秒,上面例子是封鎖 IP 半小時。
maxretry: 最多登入次數,上面設定了在半小時內有 10 次登入便會被封鎖。
修改好 jail.conf 後, 重新啟動 Fail2ban 便會生效。
☛ 防止網頁被試探路徑與 Script
設定參考範例如下:
[apache-noscript] enabled = true port = http,https filter = apache-noscript action = iptables-multiport[name=apache-noscript, port="http,https"] ignoreip = 127.0.0.1/8 192.168.0.0/24 logpath = /var/log/httpd/error_log maxretry = 1 bantime = 36000