天天看點

fail2ban 保護linux安全(轉載)已用于生産環境

一、下載下傳安裝

#tar xvfj fail2ban-0.8.4.tar.bz2

#cd fail2ban-0.8.4

#python setup.py install

#cd files

# cp ./redhat-initd /etc/init.d/fail2ban

# chkconfig --add fail2ban

#service fail2ban start 注:如果重起iptables 記的一定還要重起fail2ban,不然他就不能生效,fail2ban的過濾表是在iptables 啟動後在加入的.

二、配置

預設fail2ban.conf裡面就三個參數,而且都有注釋。

#預設日志的級别

loglevel = 3

#日志的目的

logtarget = /var/log/fail2ban.log

#socket的位置

socket = /tmp/fail2ban.sock

jail.conf配置裡是fail2ban所保護的具體服務的配置,這裡以SSH來講。

在jail.conf裡有一個[DEFAULT]段,在這個段下的參數是全局參數,可以被其它段所覆寫。

#忽略IP,在這個清單裡的IP不會被屏蔽

ignoreip = 127.0.0.1 172.13.14.15

#屏蔽時間

bantime = 600

#發現時間,在此期間内重試超過規定次數,會激活fail2ban

findtime = 600

#嘗試次數

maxretry = 3

#日志修改檢測機制

backend = auto

[ssh-iptables]

#激活

enabled = true

#filter的名字,在filter.d目錄下

filter = sshd

#所采用的工作,按照名字可在action.d目錄下找到

action = iptables[name=SSH, port=ssh, protocol=tcp]

mail-whois[name=SSH, dest=root]

#目的分析日志

logpath = /var/log/secure

#覆寫全局重試次數

maxretry = 5

#覆寫全局屏蔽時間

bantime = 3600

對jail.conf進行一定的設定後,就可以使用fail2ban了。

啟動fail2ban

/etc/init.d/fail2ban start

啟動之後,隻要符合filter所定義的正則式規則的日志項出現,就會執行相應的action。由于0.8源碼樹采用客戶機/伺服器的模式,是以可以很友善的查詢fail2ban的執行情況。比方所,要查詢剛才定義的“ssh-iptables”段的情況,隻要執行

三、測試:

fail2ban-client status ssh-iptables

會列印出結果

Status for the jail: ssh-iptables

|- filter

| |- Currently failed: 0

| `- Total failed: 5

`- action

|- Currently banned: 1

| `- IP list: 192.168.210.21 

`- Total banned: 1

fail2ban-client也可以直接定義運作中的fail2ban參數

比如增加屏蔽時間為一天

fail2ban-client set ssh-iptables bantime 86400

重新讀入配置檔案

fail2ban-client reload

其它還有很多用法,可以不帶參數執行fail2ban-client檢視更多選項。

因為fail2ban的架構,是以可以執行修改filter或者action來滿足自己的特殊需要,比如我希望改變fail2ban預設的 iptables規則插入方式,那麼我就可以到action.d目錄下,找到希望修改的action,這裡的例子是iptables.conf

預設actionstart的iptables規則有一條是

iptables -I INPUT -p <protocol> --dport <port> -j fail2ban-<name>

這樣就把fail2ban的規則插到INPUT鍊的最前面,而我希望自己寫的一條iptables -A INPUT -p ALL -s 1.2.3.4/32 -j ACCEPT一直作為第一條規則進而使自己的IP作為信任IP不受防火牆後面規則的限制。那麼就要修改fail2ban的啟動規則,把上面那條改為

iptables -I INPUT 2 -p <protocol> --dport <port> -j fail2ban-<name>

這樣fail2ban就會把自己的規則作為INPUT鍊的第二條規則插入,而不影響第一條。

這裡隻是一個很簡單的例子,你可以根據自己的規則,對action做更多的修改。

而在filter.d目錄裡就是一些日志的正則式比對規則,系統自帶了一些常見軟體的比對,如 sshd,apache,postfix,vsftpd,pure-ftpd等等。來看看sshd的規則,就能了解這些filter應該怎麼寫,你就可以 用fail2ban來保護更多自己的服務。

sshd.conf的内容

[Definition]

failregex = Authentication failure for .* from <HOST>

Failed [-/\w]+ for .* from <HOST>

ROOT LOGIN REFUSED .* FROM <HOST>

[iI](?:llegal|nvalid) user .* from <HOST>

ignoreregex =

可以看到,每行一則正則式,對應各種錯誤認證,如果你的sshd版本錯誤認證日志項不太一樣,可以修改這裡的,或者加入更多。

完全設定完畢後,過了一段時間,檢視日志/var/log/fail2ban.log,嘿嘿~ :cool: 

2007-05-30 17:42:49,681 fail2ban.actions: WARNING [ssh-iptables] Ban 219.235.231.76

2007-05-30 17:48:00,823 fail2ban.actions: WARNING [ssh-iptables] Ban 60.191.63.180

2007-05-30 18:42:50,456 fail2ban.actions: WARNING [ssh-iptables] Unban 219.235.231.76

2007-05-30 18:48:01,424 fail2ban.actions: WARNING [ssh-iptables] Unban 60.191.63.180

2007-05-30 23:14:43,921 fail2ban.actions: WARNING [ssh-iptables] Ban 59.42.210.176

2007-05-31 00:14:44,797 fail2ban.actions: WARNING [ssh-iptables] Unban 59.42.210.176

2007-05-31 01:49:14,241 fail2ban.actions: WARNING [ssh-iptables] Ban 58.143.242.123

2007-05-31 02:49:15,236 fail2ban.actions: WARNING [ssh-iptables] Unban 58.143.242.123

2007-05-31 07:20:54,717 fail2ban.actions: WARNING [ssh-iptables] Ban 210.51.22.207

2007-05-31 08:20:55,297 fail2ban.actions: WARNING [ssh-iptables] Unban 210.51.22.207

本文轉自 liang3391 51CTO部落格,原文連結:http://blog.51cto.com/liang3391/536383

繼續閱讀