針對暴力破解除了常規性的調整服務器的賬號密碼複雜度,以及調整默認端口等方式提高攻擊難度外,還可以通過開員工具fail2ban來進行設置防護規則來提高防護能力。
服務器基礎防護設置:
1.密碼滿足複雜性要求。
2.修改ssh程序默認的遠程端口。
3.修改默認管理員賬號:新建一個超級管理員賬號,並把toot改稱爲nologin。
Fail2ban的源碼安裝:
一、下載源碼包:wget https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.8.14
二、解壓源碼包:tar -xvf 0.8.14
三、安裝:cd fail2ban-0.8.14、python setup.py install
四、生成啓動腳本:cp files/redhat-initd /etc/init.d/fail2ban、chkconfig --add fail2ban
Fail2ban的配置文件:
/etc/fail2ban/action.d ##動作文件夾,內含默認文件。iptables及mail等動作配置。
/etc/fail2ban/fail2ban.conf ##定義了fail2ban日誌級別、日誌位置及sock文件位置。
/etc/fail2ban/filter.d ##條件文件夾,內含默認文件。過濾日誌關鍵內容設置。
/etc/fail2ban/jail.conf ##主要配置文件,模塊化。主要設置啓動ban動作的服務及動作閾值。
配置案例:ssh遠程登錄5分鐘內3次密碼驗證失敗,禁止用戶IP訪問主機1小時,1小時後限制自動解除。
/etc/fail2ban/jail.conf:
[DEFAULT]
ignoreip=127.0.0.1/8 ##忽略的IP列表(白名單)
bantime=3600 ##屏蔽時間,秒
findtime=300 ##這個時間段超過規定次數就會被ban掉
maxretry=3# #最大嘗試次數
backend=auto ##日誌修改檢測機制(gamin、polling和auto三種)
[ssh-iptables] ##針對各種服務的檢查配置,如設置bantime、findtime、maxretry和全局衝突,服務優先級大於全局
enabled=true ##是否激活此服務
filter=sshd ##過濾規則filter的名字,對應filter.d目錄下的sshd.conf
action=iptables[name=SSH,port=sh,protocol=tcp] ##動作的相關參數
sendmail-whois[name=SSH,[email protected],[email protected],sendername="Fail2ban"] ##郵件告警配置
logpath=/var/log/secure ##檢測的系統的登陸日誌文件
maxretry=3 ##最大嘗試次數
啓動和檢查Fail2ban:
service fail2ban restart
fail2ban-client status
查看效果:
iptables -L |tail -4 ##查看禁止的IP
Chain fail2ban-SSH (1 references)
target prot opt source destination
DROP all -- 192.168.2.5 anywhere
RETURN all -- anywhere anywhere
vi /var/lo/fail2ban.log ##查看日誌
2014-01-11 20:26:02,453 fail2ban.actions: WARNING [ssh-iptables] Ban 192.168.2.5
2014-01-11 20:26:20,923 fail2ban.actions: WARNING [ssh-iptables] 192.168.2.5 already banned
2014-01-11 20:26:42,946 fail2ban.actions: WARNING [ssh-iptables] 192.168.2.5 already banned
案例二:Nginx防護(規定時間內超過限定訪問次數的IP封鎖)
vim /etc/fail2ban/jail.conf
[nginx]
enabled=true
filter=nginx
action=iptables[name=nginx,port=http,protocol=tcp]
sendmail-whois[name=nginx,[email protected],[email protected],sendername="Fail2ban"]
logpath=/var/log/httpd/access_log ##檢測Nginx訪問日誌
bantime=86400
findtime=600
maxretry=1000
[Definition]
failregex = .-.-.*$ ##表示訪問IP,最簡單的正則匹配。沒有精確到精確地URL。
ignoreregex=
shell > fail2ban-regex /var/log/httpd/access_log /etc/fail2ban/filter.d/nginx.conf ##測試條件規則是否可用
shell > fail2ban-client status 可以看到兩個實例在監控
Status|- Number of jail: 2
`- Jail list: nginx, ssh-iptables
查看結果(可降低閾值自行測試):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