Fail2ban 是一套以 Python 語言所撰寫的 GPLv2 授權軟體,藉由分析系統紀錄檔,並透過設定過濾條件 (filter) 及動作 (action),當符合我們所設定的過濾條件時,將觸發相對動作來達到自動化反應的效果 (如封鎖來源 IP、寄信通知管理者、查詢來源 IP 資訊等)。因其架構相當彈性,我們可以針對自己的需求,設計不同的過濾條件與動作來達到伺服器防護的功能,或是及時的反應某些異常資訊。常見應用有:
常見的像是 SSH 服務,當使用者嘗試輸入帳號密碼進行登入時,如發生驗證錯誤,系統將紀錄事件於記錄檔中。藉由即時的分析系統紀錄檔,我們可以過濾出一些有用的資訊,再加以判斷此類事件是否對伺服器服務有害。
# yum install fail2ban
# vim /etc/yum.repos.d/atrpms.repo
[atrpms] name=Red Hat Enterprise Linux $releasever - $basearch - ATrpms baseurl=http://dl.atrpms.net/el$releasever-$basearch/atrpms/stable gpgkey=http://ATrpms.net/RPM-GPG-KEY.atrpms gpgcheck=1 enabled=1
# yum install fail2ban
# apt-get install fail2ban
Fail2ban 的設定檔主要有以下三個項目:
jail.(conf|local) filter.d/
我們以 Fedora 用戶為例,首先編輯主要設定檔 jail.(conf|local),並設定一些基本資訊。
為避免因為套件更新或升級,導致 *.conf 檔案異動,建議將使用者自訂部分寫在 *.local 檔案中。
# vim /etc/fail2ban/jail.conf (.local)
[DEFAULT] ignoreip = 127.0.0.1 bantime = 600 findtime = 600 maxretry = 3 backend = auto
ignoreip bantime maxretry findtime [ssh-iptables]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
sendmail-whois[name=SSH, dest=root, sender=fail2ban@myhost]
logpath = /var/log/secure
maxretry = 5
上述例子,我們啟用 ssh-iptables 這個 jail,分析 /var/log/secure 記錄檔,並使用 sshd 這個 filter 來過濾,當符合條件且達最大重試次數 5 次時,便執行 iptables 與 sendmail-whois 兩個 action。
/etc/fail2ban/filter.d/sshd.conf/etc/fail2ban/action.d/iptables.conf/etc/fail2ban/action.d/sendmail-whois.conf# service fail2ban start
# service fail2ban stop
# /etc/init.d/fail2ban start
# /etc/init.d/fail2ban stop
apache-notexist、apache-badbots、ssh-iptables 三個 jail 啟用中。# fail2ban-client status
Status |- Number of jail: 3 `- Jail list: apache-notexist, apache-badbots, ssh-iptables
apache-notexist 這個 jail,是分析 /var/log/httpd/error_log 記錄檔,累計有 7 次失敗,且已有 1 個 IP 被阻擋了。# fail2ban-client status apache-notexist
Status for the jail: apache-notexist |- filter | |- File list: /var/log/httpd/error_log | |- Currently failed: 1 | `- Total failed: 7 `- action |- Currently banned: 1 | `- IP list: 140.114.xxx.xxx `- Total banned: 1
欲定義過濾條件,可以編輯 /etc/fail2ban/filter.d/*.(conf|local) 目錄下的檔案,如「sshd.conf」,後續於 jail.conf 中使用該 filter 則名為 sshd。每個 filter 設定檔中可分為以下幾個主要部分:
INCLUDES .local 檔案。before after Definition
定義動作內容,可以編輯 /etc/fail2ban/action.d/*.(conf|local) 目錄下的檔案,如「iptables.conf」,後續於 jail.conf 中使用該 action 則名為 iptables。每個 action 設定檔中可分為以下幾個主要部分:
INCLUDES .local 檔案。Init Definitionactionstart:定義當 Fail2ban 啟動時,所要執行的指令,如初使化設定。actionstop:定義當 Fail2ban 停止時,所要執行的指令。actionban:定義要阻擋某個 IP 時,所要執行的指令。actionunban:定義要取消阻擋某個 IP 時,所要執行的指令。apache-notexist.conf) 以及在 jail.conf 中的實際設定,如有過多嘗試不存在的檔案,則有較大可能為攻擊前的猜測。[Definition] # Option: failregex # Notes.: regex to match the password failure messages in the logfile. The # host must be matched by a group named "host". The tag "<HOST>" can # be used for standard IP/hostname matching and is only an alias for # (?:::f{4,6}:)?(?P<host>\S+) # Values: TEXT # failregex = [[]client <HOST>[]] (File does not exist): .* # Option: ignoreregex # Notes.: regex to ignore. If this regex matches, the line is ignored. # Values: TEXT # ignoreregex =
[apache-notexist] enabled = true filter = apache-notexist action = iptables[name=HTTP, port=http, protocol=tcp] sendmail-whois[name=HTTP, dest=root, sender=fail2ban@localhost] logpath = /var/log/httpd/*error_log maxretry = 3 bantime = 600