簡介:nginx
Fail2ban 可以監控系統日誌,匹配日誌中的錯誤信息(使用正則表達式),執行相應的屏蔽動做(支持多種,通常爲調用 iptables ),是一款很實用、強大的軟件。正則表達式
如:攻擊者不斷嘗試窮舉 SSH 、SMTP 、FTP 密碼等,只要達到預設值,fail2ban 就會調用防火牆屏蔽此 IP ,而且能夠發送郵件通知系統管理員。shell
功能、特性:apache
一、支持大量服務:sshd 、apache 、qmail 等
二、支持多做動做:iptables 、tcp-wrapper 、shorewall 、mail notifications 等
三、logpath 選項中支持通配符
四、須要 Gamin 支持(Gamin 用於監控文件和目錄是否更改)
五、若是須要郵件通知,則系統事先要確保可以正常發送郵件vim
一、fail2ban 安裝session
shell > yum -y install epel-release shell > yum -y install fail2ban
二、fail2ban 結構app
/etc/fail2ban ## fail2ban 服務配置目錄
/etc/fail2ban/action.d ## iptables 、mail 等動做文件目錄
/etc/fail2ban/filter.d ## 條件匹配文件目錄,過濾日誌關鍵內容
/etc/fail2ban/jail.conf ## fail2ban 防禦配置文件
/etc/fail2ban/fail2ban.conf ## fail2ban 配置文件,定義日誌級別、日誌、sock 文件位置等dom
三、fail2ban.conf 配置ssh
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 文件存放位置,默認
四、jail.conf 防禦配置socket
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)狀態,不用理會。
五、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
## 測試成功 !
六、加入 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 。很強大 !!!