Fail2Ban 簡介與使用


目錄

1. Fail2Ban 簡介
2. Fail2Ban 安裝配置與日常維護
3. Fail2Ban 目錄結構
4. jail.conf 配置項說明
5. sshd.local 自定義配置項
6. mail-whois.conf 自定義動作

1. Fail2Ban 簡介

Fail2Ban 是一款入侵防御軟件,可以保護服務器免受暴力攻擊。 它是用 Python 編程語言編寫的。

Fail2Ban 基於auth 日志文件工作,默認情況下它會掃描所有 auth 日志文件,如 /var/log/auth.log、
/var/log/apache/access.log 等,並禁止帶有惡意標志的IP,比如密碼失敗太多,尋找漏洞等等標志。

通常,Fail2Ban 用於更新防火牆規則,用於在指定的時間內拒絕 IP 地址。 它也會發送郵件通知。

Fail2Ban 為各種服務提供了許多過濾器,如 ssh、apache、nginx、squid、named、mysql、nagios 等。

Fail2Ban 能夠降低錯誤認證嘗試的速度,但是它不能消除弱認證帶來的風險。

這只是服務器防止暴力攻擊的安全手段之一。
返回目錄

2. Fail2Ban 安裝配置與日常維護

# yum install fail2ban				        ## 安裝

以下配置實現:阻止 SSH 遠程暴力攻擊並通過 mail 通知管理員

# cat /etc/fail2ban/jail.conf				## 根據指引,修改配置應在 jail.d/ 下新建文件進行

# vim /etc/fail2ban/jail.d/sshd.local                   ## 修改配置

# cd /etc/fail2ban/jail.d
# mv 00-firewalld.conf 00-firewalld.conf.disabled       ## 禁用 firewalld,使用 iptables

# vim /etc/fail2ban/action.d/mail-whois.conf		## 定義 action

# fail2ban-client reload                                ## 讓配置生效 

日常維護:

# systemctl enable fail2ban.service			## 開機啟動
# systemctl start fail2ban.service			## 啟動服務

# cat /var/log/fail2ban.log				## 日志文件

# fail2ban-client status				## 查看 fail2ban 的運行狀態

# fail2ban-client status sshd				## 查看 jail 的詳細信息,可以看到被封的 ip

# fail2ban-client set sshd unbanip 123.123.123.2 	## 解封 ip

返回目錄

3. Fail2Ban 目錄結構

/etc/fail2ban/

├── action.d
│   ├── dummy.conf
│   ├── hostsdeny.conf
│   ├── iptables.conf
│   ├── mail-whois.conf      ## mail 動作配置
│   ├── mail.conf
│   └── shorewall.conf
├── fail2ban.conf
├── fail2ban.local
├── filter.d
│   ├── apache-auth.conf
│   ├── apache-noscript.conf
│   ├── couriersmtp.conf
│   ├── postfix.conf
│   ├── proftpd.conf
│   ├── qmail.conf
│   ├── sasl.conf
│   ├── sshd.conf            
│   └── vsftpd.conf
├── jail.conf
|── jail.local
|—— jail.d
└── └── sshd.local          ## SSH 相關配置

返回目錄

4. jail.conf 配置項說明

# cat /etc/fail2ban/jail.conf

  1 #
  2 # WARNING: heavily refactored in 0.9.0 release.  Please review and
  3 #          customize settings for your setup.
  4 #
  5 # Changes:  in most of the cases you should not modify this
  6 #           file, but provide customizations in jail.local file,
  7 #           or separate .conf files under jail.d/ directory, e.g.:
  8 #
  9 # HOW TO ACTIVATE JAILS:
 10 #
 11 # YOU SHOULD NOT MODIFY THIS FILE.
 12 #
 13 # It will probably be overwritten or improved in a distribution update.
 14 #
 15 # Provide customizations in a jail.local file or a jail.d/customisation.local.
 16 # For example to change the default bantime for all jails and to enable the
 17 # ssh-iptables jail the following (uncommented) would appear in the .local file.
 18 # See man 5 jail.conf for details.
 19 #
 20 # [DEFAULT]
 21 # bantime = 1h
 22 #
 23 # [sshd]
 24 # enabled = true
 25 #
 26 # See jail.conf(5) man page for more information

     [DEFAULT ]

     ignorecommand =					## 
     bantime = 10m					## 禁止時長,默認10分鍾
     findtime = 10m					## 執行操作的窗口時長,默認10分鍾
     maxretry =5					## 最大嘗試次數
     backend = auto					## 指定用於獲取文件修改的后端
     usedns = warn					## 
     logencoding = auto
     enabled = false					## jails 默認關閉,在自定義的 .local 中打開需要用到的項
     mode = normal					## 過濾器類型
     filter = %( name )s [mode=%(mode)s ]		## 定義過濾器
     destemail =root@localhost				## 通知將被發送到的電子郵件地址
     sender = root@					## 發件人姓名
     mta =sendmail					## 郵件傳輸代理(默認是 sendmail,可以改成 mail)
     protocol = tcp
     chain = <known/chain>
     port = 0:65535
     fail2ban_agent = Fail2Ban/%(fail2ban_version)s
     banaction = iptables-multiport			## 動作的捷徑,用於定義動作參數
     banaction_allports = iptables-allports
     action_abuseipdb =abuseipdb
     ......
     action = %(action_)s

返回目錄

5. sshd.local 自定義配置項

# vim /etc/fail2ban/jail.d/sshd.local

  3 [DEFAULT]
  4 
  5 ignoreip = 127.0.0.1/8             ## 忽略本地 IP
  6 
  7 bantime = 300                      ## IP 禁止訪問時間
  8 
  9 findtime = 60                      ## 密碼輸入時間限制
 10 
 11 maxretry = 5                       ## 最大允許試錯次數
 12 
 13 backend = auto
 14 
 15 destemail = 123456@qq.com          ## 郵件接收地址
 16 
 17 sender = 654321@163.com            ## 郵件發送地址(必需配置)
 18 
 19 mta = mail                         ## 采用 mail 郵件服務
 22 
 23 action = %(action_mw)s             ## 動作模式 action_mw
 24
 25 
 26 [sshd]
 27 
 28 enabled = true                     ## 開啟 SSH 保護
 29 
 30 port = 7777                        ## SSH 端口號

返回目錄

6. mail-whois.conf 自定義動作

# vim /etc/fail2ban/action.d/mail-whois.conf

  1 # Fail2Ban configuration file
  2 #
  3 # Author: xiaobo
  4 #
  5 #
  6
  7 [INCLUDES]
  8
  9 before = mail-whois-common.conf
 10
 11 [Definition]
 12
 13 # Option: actionban
 14 # Notes:  command executed when banning an IP. Take care that the 
 15 #         command is executed with Fail2Ban user rights.
 16 # Tags:   See jail.conf(5) man page
 17 # Values: CMD
 18 actionban = printf %%b "Hi:\n
 19             Subject: [Fail2Ban] <name>: banned <ip> from <fq-hostname>
 20             攻擊者IP:<ip>\n
                位置:`/usr/bin/curl -s http://www.cip.cc/<ip> | sed -n 2p | awk -F ': ' '{print $2}' `\n
 21             被攻擊機器名:`uname -n` \n
 22             攻擊次數:<failures> 次 \n
 27             Fail2Ban提醒\n\n "|/usr/bin/mail -s "title" <dest>
 28
 29 [Init]
 30
 31 name = default
 32
 33 dest = root

參考鏈接-Fail2Ban 官方用戶手冊

返回目錄


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM