centos下fail2ban安裝與配置詳解整理

來源:互聯網
上載者:User

centos下fail2ban安裝與配置詳解整理
一、fail2ban簡介

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

二、簡單來介紹一下fail2ban的功能和特性

1、支援大量服務。如sshd,apache,qmail,proftpd,sasl等等
2、支援多種動作。如iptables,tcp-wrapper,shorewall(iptables第三方工具),mail notifications(郵件通知)等等。
3、在logpath選項中支援萬用字元
4、需要Gamin支援(註:Gamin是用於監視檔案和目錄是否更改的服務工具)
5、需要安裝python,iptables,tcp-wrapper,shorewall,Gamin。如果想要發郵件,那必需安裝postfix/sendmail

三、fail2ban安裝與配置操作執行個體

1:安裝epel更新源:http://fedoraproject.org/wiki/EPEL/zh-cn

# yum install shorewall gamin-python shorewall-shell shorewall-perl shorewall-common python-inotify python-ctypes fail2ban
or
# yum install gamin-python python-inotify python-ctypes
# wget http://dl.fedoraproject.org/pub/epel/6/i386/fail2ban-0.8.11-2.el6.noarch.rpm
# rpm -ivh fail2ban-0.8.11-2.el6.noarch.rpm
or
# yum install gamin-python python-inotify python-ctypes
# wget http://ftp.sjtu.edu.cn/fedora/epel//5/i386/fail2ban-0.8.4-29.el5.noarch.rpm
# rpm -ivh fail2ban-0.8.4-29.el5.noarch.rpm

2:源碼包安裝


# wget https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.9.0
# tar -xzvf fail2ban-0.9.0.tar.gz
# cd
/etc/fail2ban/action.d#動作檔案夾,內含預設檔案。iptables以及mail等動作配置/etc/fail2ban/fail2ban.conf#定義了fai2ban記錄層級、日誌位置及sock檔案位置/etc/fail2ban/filter.d#條件檔案夾,內含預設檔案。過濾日誌關鍵內容設定/etc/fail2ban/jail.conf#主要設定檔,模組化。主要設定啟用ban動作的服務及動作閥值/etc/rc.d/init.d/fail2ban#啟動指令檔3. vi /etc/fail2ban/fail2ban.conf[Definition]loglevel =3logtarget = SYSLOG#我們需要做的就是把這行改成/var/log/fail2ban.log,方便用來記錄日誌資訊socket =/var/run/fail2ban/fail2ban.sock4. vi /etc/fail2ban/jail.conf
[DEFAULT] #全域設定ignoreip = 127.0.0.1 #忽略的IP列表,不受設定限制bantime  = 600 #屏蔽時間,單位:秒findtime  = 600 #這個時間段內超過規定次數會被ban掉maxretry = 3 #最大嘗試次數backend = auto #日誌修改檢測機制(gamin、polling和auto這三種)[sshd] #單個服務檢查設定,如設定bantime、findtime、maxretry和全域衝突,服務優先順序大於全域設定。enabled  = true #是否啟用此項(true/false)filter   = sshd #過濾規則filter的名字,對應filter.d目錄下的sshd.confaction   = iptables[name=SSH, port=ssh, protocol=tcp]#動作的相關參數,對應action.d/iptables.conf檔案logpath  = /var/log/secure #檢測的記錄檔pathbantime  = 3600findtime  = 300maxretry = 3
servicefail2ban start 啟動服務
4.解除fail2ban綁定的IP查詢限制列表# iptables -L --line-numbersChainfail2ban-SSH(1references)num target prot opt source destination1 DROP all --118.152.158.61.ha.cncanywhere2 RETURNall --anywhere anywhere釋放保留#iptables -Dfail2ban-SSH1
我們主要編輯jail.conf這個設定檔,其他的不要去管它.

# vi /etc/fail2ban.conf

SSH防攻擊規則


[ssh-iptables]

enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
sendmail-whois[name=SSH, dest=root, sender=fail2ban@example.com, sendername="Fail2Ban"]
logpath = /var/log/secure
maxretry = 5

[ssh-ddos]
enabled = true
filter = sshd-ddos
action = iptables[name=ssh-ddos, port=ssh,sftp protocol=tcp,udp]
logpath = /var/log/messages
maxretry = 2

[osx-ssh-ipfw]

enabled = true
filter = sshd
action = osx-ipfw
logpath = /var/log/secure.log
maxretry = 5

[ssh-apf]

enabled = true
filter = sshd
action = apf[name=SSH]
logpath = /var/log/secure
maxretry = 5

[osx-ssh-afctl]

enabled = true
filter = sshd
action = osx-afctl[bantime=600]
logpath = /var/log/secure.log
maxretry = 5

[selinux-ssh]
enabled = true
filter = selinux-ssh
action = iptables[name=SELINUX-SSH, port=ssh, protocol=tcp]
logpath = /var/log/audit/audit.log
maxretry = 5


proftp防攻擊規則

[proftpd-iptables]

enabled = true
filter = proftpd
action = iptables[name=ProFTPD, port=ftp, protocol=tcp]
sendmail-whois[name=ProFTPD, dest=you@example.com]
logpath = /var/log/proftpd/proftpd.log
maxretry = 6


郵件防攻擊規則

[sasl-iptables]

enabled = true
filter = postfix-sasl
backend = polling
action = iptables[name=sasl, port=smtp, protocol=tcp]
sendmail-whois[name=sasl, dest=you@example.com]
logpath = /var/log/mail.log

[dovecot]

enabled = true
filter = dovecot
action = iptables-multiport[name=dovecot, port="pop3,pop3s,imap,imaps,submission,smtps,sieve", protocol=tcp]
logpath = /var/log/mail.log

[dovecot-auth]

enabled = true
filter = dovecot
action = iptables-multiport[name=dovecot-auth, port="pop3,pop3s,imap,imaps,submission,smtps,sieve", protocol=tcp]
logpath = /var/log/secure

[perdition]

enabled = true
filter = perdition
action = iptables-multiport[name=perdition,port="110,143,993,995"]
logpath = /var/log/maillog


[uwimap-auth]

enabled = true
filter = uwimap-auth
action = iptables-multiport[name=uwimap-auth,port="110,143,993,995"]
logpath = /var/log/maillog


apache防攻擊規則

[apache-tcpwrapper]

enabled = true
filter = apache-auth
action = hostsdeny
logpath = /var/log/httpd/error_log
maxretry = 6

[apache-badbots]

enabled = true
filter = apache-badbots
action = iptables-multiport[name=BadBots, port="http,https"]
sendmail-buffered[name=BadBots, lines=5, dest=you@example.com]
logpath = /var/log/httpd/access_log
bantime = 172800
maxretry = 1

[apache-shorewall]

enabled = true
filter = apache-noscript
action = shorewall
sendmail[name=Postfix, dest=you@example.com]
logpath = /var/log/httpd/error_log


nginx防攻擊規則

[nginx-http-auth]

enabled = true
filter = nginx-http-auth
action = iptables-multiport[name=nginx-http-auth,port="80,443"]
logpath = /var/log/nginx/error.log


lighttpd防規擊規則

[suhosin]

enabled = true
filter = suhosin
action = iptables-multiport[name=suhosin, port="http,https"]
# adapt the following two items as needed
logpath = /var/log/lighttpd/error.log
maxretry = 2

[lighttpd-auth]

enabled = true
filter = lighttpd-auth
action = iptables-multiport[name=lighttpd-auth, port="http,https"]
# adapt the following two items as needed
logpath = /var/log/lighttpd/error.log
maxretry = 2


vsftpd防攻擊規則

[vsftpd-notification]

enabled = true
filter = vsftpd
action = sendmail-whois[name=VSFTPD, dest=you@example.com]
logpath = /var/log/vsftpd.log
maxretry = 5
bantime = 1800

[vsftpd-iptables]

enabled = true
filter = vsftpd
action = iptables[name=VSFTPD, port=ftp, protocol=tcp]
sendmail-whois[name=VSFTPD, dest=you@example.com]
logpath = /var/log/vsftpd.log
maxretry = 5
bantime = 1800


pure-ftpd防攻擊規則

[pure-ftpd]
enabled = true
filter = pure-ftpd
action = iptables[name=pure-ftpd, port=ftp, protocol=tcp]
logpath = /var/log/pureftpd.log
maxretry = 2
bantime = 86400
mysql防攻擊規則

[mysqld-iptables]

enabled = true
filter = mysqld-auth
action = iptables[name=mysql, port=3306, protocol=tcp]
sendmail-whois[name=MySQL, dest=root, sender=fail2ban@example.com]
logpath = /var/log/mysqld.log
maxretry = 5


apache phpmyadmin防攻擊規則
[apache-phpmyadmin]
enabled = true
filter = apache-phpmyadmin
action = iptables[name=phpmyadmin, port=http,https protocol=tcp]
logpath = /var/log/httpd/error_log
maxretry = 3
# /etc/fail2ban/filter.d/apache-phpmyadmin.conf
將以下內容粘貼到apache-phpmyadmin.conf裡儲存即可以建立一個apache-phpmyadmin.conf檔案.

# Fail2Ban configuration file
#
# Bans bots scanning for non-existing phpMyAdmin installations on your webhost.
#
# Author: Gina Haeussge
#

[Definition]

docroot = /var/www
badadmin = PMA|phpmyadmin|myadmin|mysql|mysqladmin|sqladmin|mypma|admin|xampp|mysqldb|mydb|db|pmadb|phpmyadmin1|phpmyadmin2

# Option: failregex
# Notes.: Regexp to match often probed and not available phpmyadmin paths.
# Values: TEXT
#
failregex = [[]client []] File does not exist: %(docroot)s/(?:%(badadmin)s)

# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
# service fail2ban restart

寫在最後,在安裝完fail2ban後請立即重啟一下fail2ban,看是不是能正常啟動,因為在後邊我們配置完規則後如果發生無法啟動的問題我們可以進行排查.如果安裝完後以預設規則能夠正常啟動,而配置完規則後卻不能夠正常啟動,請先檢查一下你 /var/log/ 目錄下有沒有規則裡的 logpath= 後邊的檔案,或者這個檔案的路徑與規則裡的是不是一致. 如果不一致請在 logpath 項那裡修改你的路徑, 如果你的緩衝目錄裡沒有這個檔案,那麼請你將該配置項的 enabled 項目的值設定為 false. 然後再進行重啟fail2ban,這樣一般不會有什麼錯誤了.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.