2014/11/27

fail2ban 0.8.1



最近玩 mailscanner 玩的不亦樂乎 (喂喂)

今早在看 mail.log 時,突然看到一個IP亂入
Nov 27 08:39:31 lnx02 postfix/smtpd[32455]: connect from unknown[113.111.198.92]
Nov 27 08:39:31 lnx02 postfix/smtpd[32455]: lost connection after AUTH from unknown[113.111.198.92]
Nov 27 08:39:31 lnx02 postfix/smtpd[32455]: disconnect from unknown[113.111.198.92]




上面LOG 的意思是,同一個IP登入 port 25,卻在送出 AUTH 指令後,立刻斷線
這個行為重覆了約20次左右,就不再出現了
直覺是有人用 DoS 的手法來亂 postfix

whois 的結果:
netname:        CHINANET-GD
descr:          CHINANET Guangdong province network
descr:          Data Communication Division
descr:          China Telecom
country:        CN
大陸 IP ~ 本公司和大陸基本是沒有往來的,這下可以放心的玩了(?)


本來可以用 iptables 去阻擋 IP

前一陣子在酷學園看到新玩具:fail2ban
大意是說,用行為偵測的方式,符合某些行為特徵,就呼叫 iptables 做指定的動作 (EX: DROP DISCARD )

我的系統是 debian 6.0 ,用 aptitude 看了一下,有 fail2ban 可以裝,所以安裝就不敘述了

接下來是針對 dns / postfix / ssh 去做監測

底下是舊版 (好像是 0.8.1 吧) 的設定


dns 的設定參考這一篇:
http://peterrabbit-home.blogspot.tw/2011/04/postfix-fail2banpart-1.html
由於 debian 預設是不 log bind9 ,因此
  1. 更改 named.options.local
    加入 logging 的設定 (參考 fail2ban/jail.conf named 的部份)
    # logging {
    #     channel security_file {
    #         file "/var/log/named/security.log" versions 3 size 30m;
    #         severity dynamic;
    #         print-time yes;
    #     };
    #     category security {
    #         security_file;
    #     };
    # };
    #
  2. 建立 /var/log/bind
    將 log 獨立
  3. 調整 fail2ban/jail.conf named 的區塊
    enabled  = true 
postfix 參考上面步驟3的設定,將監測 postfix 的功能打開
然後再額外調整 filter.d/postfix.conf ,設定偵測 log 的語法,下面有部份是 regex 的語法要注意:
failregex = (.*)lost connection after AUTH (.*)\[\]

postfix 的測試:
開兩個視窗,一個用來看 mail.log,一個用來模擬攻擊的行為


視窗二:
archer@lnx01:~$ telnet lnx02 25
Trying 192.168.0.2...
Connected to lnx02.
Escape character is '^]'.
220 lnx02.motorpro.com.tw ESMTP Postfix
auth
503 5.5.1 Error: authentication not enabled
^]
telnet> quit
Connection closed.

重覆三次之後,就連不上了
archer@lnx01:~$ telnet lnx02 25
Trying 192.168.0.2...
telnet: Unable to connect to remote host: Connection timed out


視窗一:
Nov 27 09:41:29 lnx02 postfix/smtpd[2784]: connect from lnx01[10.1.1.1]
Nov 27 09:41:32 lnx02 postfix/smtpd[2784]: lost connection after AUTH from lnx01[10.1.1.1]
Nov 27 09:41:32 lnx02 postfix/smtpd[2784]: disconnect from lnx01[10.1.1.1]
Nov 27 09:41:33 lnx02 postfix/smtpd[2784]: connect from lnx01[10.1.1.1]
Nov 27 09:41:35 lnx02 postfix/smtpd[2784]: lost connection after AUTH from lnx01[10.1.1.1]
Nov 27 09:41:35 lnx02 postfix/smtpd[2784]: disconnect from lnx01[10.1.1.1]
Nov 27 09:41:38 lnx02 postfix/smtpd[2784]: connect from lnx01[10.1.1.1]
Nov 27 09:41:40 lnx02 postfix/smtpd[2784]: lost connection after AUTH from lnx01[10.1.1.1]
Nov 27 09:41:40 lnx02 postfix/smtpd[2784]: disconnect from lnx01[10.1.1.1]

然後可以在 /var/log/fail2ban.log 裡面看到剛剛的動作已被偵測到,並自動阻檔2014-11-27 09:41:42,171 fail2ban.actions: WARNING [postfix] Ban 10.1.1.1
由於我的設定僅阻檔10min,時間到後會自動解除阻檔
2014-11-27 09:51:42,867 fail2ban.actions: WARNING [postfix] Unban 10.1.1.1

由此驗證上述設定已成功
這真是太棒了~ 完全不用自己去改 iptables 阻檔一堆 IP

沒有留言: