簡介:
Fail2ban 能夠監控系統日志,匹配日志中的錯誤信息(使用正則表達式),執行相應的屏蔽動作(支持多種,一般為調用 iptables ),是一款很實用、強大的軟件。
如:攻擊者不斷嘗試窮舉 SSH 、SMTP 、FTP 密碼等,只要達到預設值,fail2ban 就會調用防火牆屏蔽此 IP ,並且可以發送郵件通知系統管理員。
功能、特性:
1、支持大量服務:sshd 、apache 、qmail 等
2、支持多作動作:iptables 、tcp-wrapper 、shorewall 、mail notifications 等
3、logpath 選項中支持通配符
4、需要 Gamin 支持(Gamin 用於監控文件和目錄是否更改)
5、如果需要郵件通知,則系統事先要確保能夠正常發送郵件
1、fail2ban 安裝
shell > yum -y install epel-release shell > yum -y install fail2ban
2、fail2ban 結構
/etc/fail2ban ## fail2ban 服務配置目錄
/etc/fail2ban/action.d ## iptables 、mail 等動作文件目錄
/etc/fail2ban/filter.d ## 條件匹配文件目錄,過濾日志關鍵內容
/etc/fail2ban/jail.conf ## fail2ban 防護配置文件
/etc/fail2ban/fail2ban.conf ## fail2ban 配置文件,定義日志級別、日志、sock 文件位置等
3、fail2ban.conf 配置
shell > grep -v ^# /etc/fail2ban/fail2ban.conf [Definition] loglevel = 3 ## 定義日志級別,默認 logtarget = /var/log/fail2ban.log ## 定義 fail2ban 日志文件 socket = /var/run/fail2ban/fail2ban.sock ## sock 文件存放位置,默認 pidfile = /var/run/fail2ban/fail2ban.pid ## pid 文件存放位置,默認
4、jail.conf 防護配置
shell > grep -v ^# /etc/fail2ban/jail.conf [DEFAULT] ## 全局設置,優先級最小 ignoreip = 127.0.0.1/8 ## 不受限制的 IP ,多組用空格分割 bantime = 600 ## 非法 IP 被屏蔽時間(秒),-1 代表永遠封鎖 findtime = 600 ## 設置多長時間(秒)內超過 maxretry 限制次數即被封鎖 maxretry = 3 ## 最大嘗試次數 backend = auto ## 日志修改檢測機制(gamin 、polling 、auto 三種) usedns = warn [ssh-iptables] ## 分類設置(基於 SSHD 服務的防護) enabled = true ## 是否開啟防護,false 為關閉 filter = sshd ## 過濾規則 filter 名稱,對應 filter.d 目錄下的 sshd.conf action = iptables[name=SSH, port=ssh, protocol=tcp] ## 動作參數 sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, sendername="Fail2Ban"] ## 郵件通知參數 ## 收件人地址 ## 發件人地址 logpath = /var/log/secure ## 檢測系統登陸日志文件 maxretry = 5 ## 最大嘗試次數
## 默認此配置文件中還有大量的服務防護配置,只不過默認都是關閉(false)狀態,不用理會。
5、fail2ban 啟動、測試 SSHD 防護
shell > service fail2ban start ## 如果重啟 iptables ,必須重啟 fail2ban shell > fail2ban-client status ## 可以看到有一個實例已經開始監控 Status |- Number of jail: 1 `- Jail list: ssh-iptables shell > iptables -nL ## iptables 也加入了一條規則 fail2ban-SSH tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
## 同時,管理員郵箱也收到一封郵件..
[Fail2Ban] SSH: started on localhost.localdomain 發件人:Fail2Ban 收件人:1355******* 時 間:2015-06-05 23:58:5 Hi, The jail SSH has been started successfully. Regards, Fail2Ban
## 這時客戶端嘗試登陸本機,故意輸入五次密碼,就會看到如下日志:
shell > tail -1 /var/log/fail2ban.log 2015-06-05 17:39:19,647 fail2ban.actions[1313]: WARNING [ssh-iptables] Ban 192.168.214.1
## 可以看到:192.168.214.1 被 Ban 掉了。
shell > cat /var/log/secure ## 系統登陸日志 Jun 5 17:39:01 localhost sshd[1341]: Failed password for root from 192.168.214.1 port 2444 ssh2 Jun 5 17:39:06 localhost sshd[1341]: Failed password for root from 192.168.214.1 port 2444 ssh2 Jun 5 17:39:11 localhost sshd[1341]: Failed password for root from 192.168.214.1 port 2444 ssh2 Jun 5 17:39:14 localhost sshd[1341]: Failed password for root from 192.168.214.1 port 2444 ssh2 Jun 5 17:39:18 localhost sshd[1341]: Failed password for root from 192.168.214.1 port 2444 ssh2 Jun 5 17:41:39 localhost login: pam_unix(login:session): session opened for user root by LOGIN(uid=0)
## 收到的郵件通知
[Fail2Ban] SSH: banned 192.168.214.1 from localhost.localdomain 發件人:Fail2Ban 收件人:1355******* 時 間:2015-06-06 00:05:45 Hi, The IP 192.168.214.1 has just been banned by Fail2Ban after 5 attempts against SSH. Here is more information about 192.168.214.1: missing whois program Regards, Fail2Ban
## 測試成功 !
6、加入 Nginx 防護( httpd 代替 )
## 目的是把規定時間內達到限定訪問次數的 IP 封鎖(例如,一分鍾內有幾百次請求)
shell > vim /etc/fail2ban/jail.conf [nginx] ## nginx 防護 enabled = true filter = nginx ## 訪問規則定義文件,位置在 /etc/fail2ban/filter.d/nginx.conf action = iptables[name=nginx, port=http, protocol=tcp] sendmail-whois[name=nginx, dest=1355*******@139.com, sender=fail2ban@aoath.com, sendername="Fail2Ban"] logpath = /var/log/httpd/access_log ## nginx 訪問日志 bantime = 86400 ## 符合規則的屏蔽一天,如果參數值與全局有沖突,優先級大於全局配置 findtime = 600 ## 10 分鍾內訪問超過 maxretry 次數的封鎖 IP maxretry = 1000 ## 最大嘗試次數 shell > vim /etc/fail2ban/filter.d/nginx.conf [Definition] failregex =<HOST>.*-.*-.*$ ## <HOST> 表示訪問 IP ,其余的其實是最簡單匹配了。因為這里沒有要匹配精確的 URL ,只是限制訪問次數 ignoreregex = shell > fail2ban-regex /var/log/httpd/access_log /etc/fail2ban/filter.d/nginx.conf ## 可以測試條件規則是否可用 shell > service fail2ban restart ## 重啟服務 shell > fail2ban-client status ## 可以看到有兩個實例在監控中 Status |- Number of jail: 2 `- Jail list: nginx, ssh-iptables
## 開始測試,通過腳本或者不管刷新頁面測試 Nginx 防護( 便於測試,可以將 maxretry 的值調為 10 )
shell > fail2ban-client status nginx ## 可以看到被 Ban 掉的 IP Status for the jail: nginx |- filter | |- File list: /var/log/httpd/access_log | |- Currently failed: 1 | `- Total failed: 39 `- action |- Currently banned: 1 | `- IP list: 192.168.214.1 `- Total banned: 1
## 同時也有對應的郵件通知
[Fail2Ban] nginx: banned 192.168.214.1 from localhost.localdomain 發件人:Fail2Ban 收件人:1355******* 時 間:2015-06-06 01:04:11 Hi, The IP 192.168.214.1 has just been banned by Fail2Ban after 20 attempts against nginx. Here is more information about 192.168.214.1: missing whois program Regards, Fail2Ban shell > tail -1 /var/log/fail2ban.log ## fail2ban 的日志信息 2015-06-05 19:04:11,705 fail2ban.actions[2592]: WARNING [nginx] Ban 192.168.214.1
## OK ,這就是 fail2ban 。很強大 !!!
