1. 程式人生 > >fail2ban防止暴力破解

fail2ban防止暴力破解

破解 服務器 監控 系統資源 align 穩定版 www 啟動 onf

  暴力破解在公網上是很常見的,當我們在公網上的服務器被暴力破解的時候,我們就的采取相應的措施,今天我們來講一個開源軟件fail2ban,以防止暴力破解。

我們去官網下下載穩定版本的fail2ban安裝包。

fail2ban官網地址:http://www.fail2ban.org

環境,假設192.168.1.63是公網上的服務器,被別人暴力破解時怎麽辦呢。

題目:

實戰背景:

最近公網網站一直被別人暴力破解sshd服務密碼。雖然沒有成功,但會導致系統負載很高,原因是在暴力破解的時候,系統會不斷地認證用戶,從而增加了系統資源額外開銷,導致訪問公司網站速度很慢。

fail2ban可以監視你的系統日誌,然後匹配日誌的錯誤信息(正則式匹配)執行相應的屏蔽動作(一般情況下是防火墻),而且可以發送e-mail通知系統管理員,很好、很實用、很強大!

實驗環境:

服務端:192.168.1.63

客戶端:192.168.1.63

在服務端上傳安裝包(fail2ban)

技術分享

解壓:

技術分享

我們使用編譯安裝的方法安裝。

技術分享

當我們安裝一個軟件的時候,不知道怎麽做的情況下可以看下幫助文件README.md,這個文件裏面hi告訴我們安裝的方法。

技術分享

[[email protected] fail2ban-0.8.14]# python setup.py install

安裝完成後,看一下相關文件說明

[[email protected] fail2ban-0.8.14]# ll /etc/fail2ban/
total 40
drwxr-xr-x 2 root root 4096 Mar 19 21:57 action.d 動作文件夾
-rw-rw-r-- 1 root root 1525 Aug 20 2014 fail2ban.conf 定義了fai2ban日誌級別、日誌位置及sock文件位置


drwxr-xr-x 2 root root 4096 Mar 19 21:57 fail2ban.d
drwxr-xr-x 2 root root 4096 Mar 19 21:57 filter.d 條件文件夾
-rw-rw-r-- 1 root root 19316 Aug 20 2014 jail.conf 主要配置文件
drwxr-xr-x 2 root root 4096 Mar 19 21:57 jail.d

生成服務啟動腳本

[[email protected] fail2ban-0.8.14]# grep chkconfig ./* -R --color 快速找到開機啟動文件

./files/redhat-initd:# chkconfig: - 92 08

技術分享

[[email protected] fail2ban-0.8.14]# cp files/redhat-initd /etc/init.d/fail2ban

[[email protected] fail2ban-0.8.14]# chkconfig --add fail2ban 添加開機自啟動
[[email protected] fail2ban-0.8.14]# chkconfig --list fail2ban
fail2ban 0:off 1:off 2:off 3:on 4:on 5:on 6:off

應用實例:SSH遠程登錄,五分鐘之內密碼驗證3次失敗,禁止1小時之內改ip訪問。

在主配置文件裏面進行修改。

[[email protected] fail2ban]# vim /etc/fail2ban/jail.conf

為了看一下實驗效果,我們設置的是5分鐘之內驗證三次,失敗的話,禁止2分鐘,兩分鐘之後恢復

技術分享

重啟fail2ban服務

技術分享

現在進行測試(三次密碼錯誤之後就不能登錄了):

技術分享

在服務器查看:

技術分享

想要恢復時清空一下/var/log/secure文件,然後重啟fail2ban服務就可以了。

當ssh的默認端口修改的話,配置fail2ban來監控ssh需要修改配置文件。

[[email protected] fail2ban]# vim /etc/fail2ban/jail.conf

技術分享

[[email protected] fail2ban]# vim /etc/fail2ban/action.d/iptables.conf

技術分享

如有問題請及時聯系博主:

QQ:1127000483

條件文件夾,內含默認文件。過濾日誌關鍵內容設置

fail2ban防止暴力破解