我時常被客戶詢問的問題,帳號密碼被人使用暴力破解(brute force),好比說: SSH 登入、POP3登入…etc.,這該如何預防?
2. 安裝Fail2Ban套件
3. 設定Fail2Ban
4. 測試Fail2Ban是否生效
因為CentOS並沒有 fail2ban ,所以安裝EPEL的repository
# rpm -Uvh http://mirror01.idc.hinet.net/EPEL/6/i386/epel-release-6-8.noarch.rpm
2. 安裝Fail2Ban套件
# yum install fail2ban
3. 設定Fail2Ban (使用SSH & Apache測試)
根據原廠文件的解釋 :
先讀取jail.conf 再讀取jail.local
如果是自定義的服務,建議把設定檔寫入jail.local
# vi /etc/fail2ban/jail.local
Note SSH:
enable: true (開啟) false (關閉)
filter: sshd 指定使用 /etc/fail2ban/filter.d/sshd.conf
logpath: 所比對的log檔案
maxretry: 嘗試登入次數
bantime: 封鎖時間 (-1 為永久封鎖)
ignoreip: 忽略IP
action: 發生狀況時,所使用的選項(iptables) &觸發Port & 發信通知
# vi /etc/fail2ban/jail.conf
Note Apache:
enable: true (開啟) false (關閉)
filter: apache 指定使用/etc/fail2ban/filter.d/apache-auth.conf
logpath: 所比對的log檔案
maxretry: http GET or Post request 次數
findtime: 時間內檢查maxretry次數
ignoreip: 忽略IP
action: 發生狀況時,所使用的選項(iptables) &觸發Port & 發信通知
觀察/etc/fail2ban/filter.d/sshd.conf
#vi /etc/fail2ban/filter.d/sshd.conf
觀察/etc/fail2ban/filter.d/apache-auth.conf
#vi /etc/fail2ban/filter.d/apache-auth.conf
比對到的Log 應該是上面的藍色文字
啟動Fail2ban Service
# /etc/init.d/fail2ban start
設定開機自動啟動 (必須要在iptables 服務啟動之後)
# chkconfig fail2ban on
4. 測試Fail2Ban是否生效
使用192.168.10.168 登入測試,查看一下/var/log/secure
# tail /var/log/secure
May 27 17:11:04 mail sshd[18972]: Invalid user ANGEL from 192.168.10.168
May 27 17:11:04 mail sshd[18977]: input_userauth_request: invalid user ANGEL
May 27 17:11:05 mail sshd[18972]: pam_unix(sshd:auth): check pass; user unknown
May 27 17:11:05 mail sshd[18972]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.10.168
May 27 17:11:05 mail sshd[18972]: pam_succeed_if(sshd:auth): error retrieving information about user ANGEL
May 27 17:11:07 mail sshd[18972]: Failed password for invalid user ANGEL from 192.168.10.168 port 53265 ssh2
May 27 17:11:07 mail sshd[18972]: pam_unix(sshd:auth): check pass; user unknown
May 27 17:11:07 mail sshd[18972]: pam_succeed_if(sshd:auth): error retrieving information about user ANGEL
May 27 17:11:09 mail sshd[18972]: Failed password for invalid user ANGEL from 192.168.10.168 port 53265 ssh2
May 27 17:11:09 mail sshd[18972]: pam_unix(sshd:auth): check pass; user unknown
May 27 17:11:09 mail sshd[18972]: pam_succeed_if(sshd:auth): error retrieving information about user ANGEL
May 27 17:11:12 mail sshd[18972]: Failed password for invalid user ANGEL from 192.168.10.168 port 53265 ssh2
確認iptables 是否生效
# iptables -L -nv
查看fail2ban狀態方式
# fail2ban-client status ssh-iptables
Status for the jail: ssh-iptables
|- filter
| |- File list: /var/log/secure
| |- Currently failed: 1
| `- Total failed: 7
`- action
|- Currently banned: 1
| `- IP list: 192.168.10.168
`- Total banned: 2
看來的確是封鎖了192.168.10.168
Refer:
http://www.fail2ban.org/wiki/index.php/MANUAL_0_8
http://pulipuli.blogspot.tw/2011/07/centosfail2ban.html
http://cyrilwang.blogspot.tw/2012/04/fail2ban.html