• 12th 六月 2008 - By Devil

    OKShield-128x128 前幾天無意間看到有網友用 Iptables 來做 ssh 的連線保護

    基本上是透過 Iptables recent 這個 module 來計算連線數量

    再透過 Iptables 來 Drop掉ip!

    不過效果我覺得並不是很滿意!

    在尋尋覓覓之下,就看到了 Fail2Ban 這隻小程式囉!

    安裝很簡單!設定也不難而且能設的 Service 還蠻多的喔!

    Fail2Ban 是透過 Iptables 與 tcpwrapper (( TCPWrapper 是一簡單易用的工具,主要的功能在紀錄並攔截由 inetd 管理的 TCP 服
    務。這程式主要是由 Wietse Venema 開發。 ))

    一步一步來吧!

    系統需求:CentOS 5、iptables、Python > 2.4 ( Fail2Ban 0.8 branch )

     

    Step 1

    沒有Python的朋友請使用:

    1
    
    yum install python python-devel gamin-python

    下載 Fail2Ban

    Cerebration下載DAG下載

    安裝 Fail2Ban

    1
    
    rpm -ivh fail2ban-0.8.1-1.el5.rf.noarch

    安裝完成!

     

    Step 2

    相關設定

    主設定檔位置在

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    /etc/fail2ban/jail.conf
     
    ignoreip = 192.168.1.0/24 ( 忽略不限制的ip、請依須求設定 )
     
    bantime  = 86400 ( 遭拒絕的ip被拒絕的時間長短 )
     
    findtime  = 600 ( 觸發的時間區間 )
     
    maxretry = 3 ( 在 findtime 時間內觸發的次數 )

    預設設定完成

     

    Step 3

    以SSH為例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    [ssh-iptables]
     
    enabled  = true ( 是否開啟 true or false )
    filter   = sshd
    action   = iptables[name=SSH, port=ssh, protocol=tcp] ( 名稱、port號、通訊協定 )
               sendmail[name=SSH, <a href="mailto:dest=devil3688@mamu.com.tw">dest=devil3688@mamu.com.tw</a>] ( 發出警告信 )
    logpath  = /var/log/secure ( log檔案位置 )
    bantime = -1 ( -1代表無限 )
    maxretry = 3 ( 觸發次數 )

    設定完成!請記得執行

    1
    
    service fail2ban restart

     

    各位可以透過觀察記錄檔 /var/log/fail2ban.log ,來觀察有無錯誤!

    或是觀察 Iptables 的規則

    1
    2
    
    <span style="font-family: Courier New;">
    iptables -L -n

    延伸參考

    http://www.uno-code.com/?q=node/94

    http://54061.blogspot.com/2007/11/spam-senders-part2.html

    http://www.fail2ban.org/wiki/index.php/HOWTOs

    http://www.ducea.com/2006/07/03/using-fail2ban-to-block-brute-force-attacks/

    http://cha.homeip.net/blog/archives/2007/06/_fail2ban_ip.html

    VN:F [1.8.0_1031]
    Rating: 5.0/5 (2 votes cast)
    保護你的伺服器 Fail2Ban5.052
    與大家分享
    • Print
    • Digg
    • del.icio.us
    • Facebook
    • Google Bookmarks
    • Hemidemi
    • MyShare
    • email
    • RSS
    • Twitter
    • Live
    • MySpace
  • Leave a Reply


Ad