fail2ban能夠監視你的系統日誌,而後匹配日誌的錯誤信息(正則式匹配)執行相應的屏蔽動做(通常狀況下是調用防火牆屏蔽),如:當有人在試探你的SSH、SMTP、FTP密碼,只要達到你預設的次數,fail2ban就會調用防火牆屏蔽這個IP,並且能夠發送e-mail通知系統管理員,是一款很實用、很強大的軟件!php
必須的前端
python>2.4(官網推薦使用2.5版本以上,2.4版本有bug)python
可選的git
iptables、shorewall、tcp-wrapper,當發生暴力破解現象時,使用其中一種方式來屏蔽ipgithub
gmain,文件更改監視器正則表達式
sendmail、postfix,能夠及時發郵件報告狀況vim
安裝也很簡單!centos
下載fail2ban:https://github.com/fail2ban/fail2ban多線程
cd /opt/fail2ban/ setup.py install
安裝的默認路徑:/usr/share/fail2ban併發
執行腳本路徑:/usr/bin
配置文件路徑:/etc/fail2ban
安裝截圖:
1.配置/etc/init.d腳本
首先,咱們要建立一個服務腳本,便於管理fail2ban的啓動與關閉,包括它的開機啓動。
fail2ban安裝內自帶了幾個系統的模板文件,centos的文件爲:files/redhat-initd。
#將redhat-initd文件複製到/etc/init.d目錄下 cp redhat-initd /etc/init.d/fail2ban #更改權限 chmod 755 /etc/init.d/fail2ban #加入開機服務項 chkconfig --add fail2ban #開機啓動 chkconfig fail2ban on
2.配置fail2ban的logrotate
建立這個文件:/etc/logrotate.d/fail2ban
#內容爲: /var/log/fail2ban.log { weekly rotate 7 missingok compress postrotate /usr/bin/fail2ban-client reload 1>/dev/null || true endscript }
其中fail2ban-client的路徑值得注意,可使用這個命令查詢:
which fail2ban-client #或者 whereis fail2ban-client
首先來簡述下fail2ban是如何工做的,涉及到哪些東西。
fail2ban包含下面5個要素,理解這5個要素之間的關係,對於如何配置如何使用fail2ban是頗有幫助的。
前三個都是配置文件,在/etc/fail2ban目錄下;最後兩個是fail2ban的可執行文件。
Server
fail2ban有兩部分組成:fail2ban-client和fail2ban-server。server是多線程的並用來監聽unix socket的命令。server自己不加載任何配置。
所以,在啓動時,server處於沒有jail定義的默認狀態!須要client給予具體的配置。
fail2ban-server的啓動參數以下:
------------ -b start in background(後臺啓動) -f start in foreground(前臺啓動) -s socket path(套接字路徑) -x force execution of the server(強制執行server) -h, --help display this help message(顯示幫助信息) -V, --version print the version(打印版本) -----------
Client
fail2ban-client是fail2ban-server的前端程序,它鏈接server的socket併發送命令給server以配置和操做server。
client以命令行或交互模式(使用-i選項)讀取配置文件或僅僅發送一個單獨的命令給server。fail2ban-client也能夠啓動server。
fail2ban-client的啓動參數:
-c configuration directory(配置目錄) -s socket path(socket路徑) -d dump configuration. For debugging(打印出配置,用於調試) -i interactive mode(交互模式) -v increase verbosity(增長詳細描述) -q decrease verbosity(減小詳細描述) -x force execution of the server(強制執行server) -h, --help display this help message(顯示幫助) -V, --version print the version(打印版本)
配置目錄
目錄結構以下圖:
每一個.conf文件都會被名爲 .local的文件覆蓋。.conf首先被讀取,其次是.local。新的配置會覆蓋早先的。所以,.local 文件沒必要包含每一個相應於.conf中的選項,只是填寫你想要覆蓋的設置。
/etc/fail2ban下已經自帶了一些經常使用服務的filter,action腳本了。因此只要簡單的配置下便可使用。
jail.conf文件裏能夠配置多個須要檢測的服務,好比sshd,vsftpd等。每一段均可以指定如何過濾(filter),如何屏蔽(action)。
一般只需修改jail.conf文件,修改filter掃描的日誌路徑,修改action等。
而自帶的filter和action,通常均可以用了。但若是自定義filter,須要瞭解正則表達式相關的知識。
vim /etc/fail2ban/jail.conf #SSH的配置: [ssh-iptables] enabled = true filter = sshd action = iptables[name=SSH, port=ssh, protocol=tcp] sendmail-whois[name=SSH, dest=lijialong1314@foxmail.com, sender=root] logpath = /var/log/secure maxretry = 5 #Vsftpd的配置 [vsftpd-iptables] enabled = true filter = vsftpd action = iptables[name=VSFTPD, port=ftp, protocol=tcp] sendmail-whois[name=VSFTPD, dest=lijialong1314@foxmail.com] logpath = /var/log/message maxretry = 5 bantime = 1800
啓動/關閉/重啓fail2ban:service fail2ban {start|stop|status|restart}