Skip to content

1. Fail2ban介绍

Fail2Ban 是一个 Linux 系统的应用软件,用来防止系统入侵,主要是防止暴力破解系统密码。它是用 Python 开发的。

它主要通过监控日志文件(比如/var/log/auth.log/var/log/apache/access.log等)来生效。一旦发现恶意攻击的登录请求,它会封锁对方的 IP 地址,使得对方无法再发起请求。

Fail2Ban 可以防止有人反复尝试 SSH 密码登录,但是如果 SSH 采用的是密钥登录,禁止了密码登录,就不需要 Fail2Ban 来保护。

https://github.com/fail2ban/fail2ban

2. Faile2ban部署

2.1 安装

基于rocklinux8

bash
#centos8
dnf install epel-release -y 

#centos7
yum install epel-release -y

yum install install fail2ban iptables -y
#centos8
dnf install epel-release -y 

#centos7
yum install epel-release -y

yum install install fail2ban iptables -y

也可以通过源码进行安装

2.2 目录介绍

bash
 tree -L 1 /etc/fail2ban
/etc/fail2ban
├── action.d
├── fail2ban.conf
├── fail2ban.d
├── filter.d
├── jail.conf
├── jail.d
├── paths-common.conf
└── paths-fedora.conf
 tree -L 1 /etc/fail2ban
/etc/fail2ban
├── action.d
├── fail2ban.conf
├── fail2ban.d
├── filter.d
├── jail.conf
├── jail.d
├── paths-common.conf
└── paths-fedora.conf

2.3 配置

根据 fail2ban 官网 wiki 建议,在配置 fail2ban 的时候应该避免直接更改由 fail2ban 安装创建的.conf 文件(例如 fail2ban.confjail.conf),相反,应该创建扩展名为.local 的新文件(例如 jail.local)来进行自定义配置。.local 文件将覆盖.conf 文件相同部分的参数

  • 修改配置

vim /etc/fail2ban/jail.conf.local

bash
[DEFAULT]
# 忽略 IP,IP 白名单,这些 IP 永远不会被禁
ignoreip = 127.0.0.1/8
#IP 被封禁的时间,单位秒
bantime  = 600
#日志文件中,在 findtime 时间段内,ip 出现超过 maxretry 次数,就会封禁该 IP
findtime = 600
maxretry = 3
# "backend" 指获取日志文件的方法,分为"pyinotify", "gamin", "polling", "auto"四种
# auto 值得是哪种可用就用哪种,默认 polling 可用
backend = auto
# "usedns" specifies if jails should trust hostnames in logs,
usedns = warn
#接受邮件地址
destemail = xxxxxx@gmail.com
# Name of the sender for mta actions
sendername = Fail2Ban
#默认的动作执行行为,在 action.d 目录下有各种行为策略,默认是 iptables-multiport
banaction = iptables-multiport
# email action. Since 0.8.1 upstream fail2ban uses sendmail
mta = sendmail
# Default protocol
protocol = tcp
# Specify chain where jumps would need to be added in iptables-* actions
chain = INPUT
#定义各种行为参数
#只禁 IP
action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
#禁 IP+邮件通知
action_mw = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
%(mta)s-whois[name=%(__name__)s, dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s", sendername="%(sendername)s"]
# 禁 IP+邮件通知+报告相关日志
action_mwl = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
%(mta)s-whois-lines[name=%(__name__)s, dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s", sendername="%(sendername)s"]
# 选择行为
action = %(action_)s
#以下是各种应用监控日志的配置,以 ssh 日志为例说明一下配置原理
[ssh]
enabled = true
port = 22
filter = sshd
logpath = /var/log/secure
bantime  = 1mon
findtime  = 5m
maxretry = 3
action = %(action_mw)s
[DEFAULT]
# 忽略 IP,IP 白名单,这些 IP 永远不会被禁
ignoreip = 127.0.0.1/8
#IP 被封禁的时间,单位秒
bantime  = 600
#日志文件中,在 findtime 时间段内,ip 出现超过 maxretry 次数,就会封禁该 IP
findtime = 600
maxretry = 3
# "backend" 指获取日志文件的方法,分为"pyinotify", "gamin", "polling", "auto"四种
# auto 值得是哪种可用就用哪种,默认 polling 可用
backend = auto
# "usedns" specifies if jails should trust hostnames in logs,
usedns = warn
#接受邮件地址
destemail = xxxxxx@gmail.com
# Name of the sender for mta actions
sendername = Fail2Ban
#默认的动作执行行为,在 action.d 目录下有各种行为策略,默认是 iptables-multiport
banaction = iptables-multiport
# email action. Since 0.8.1 upstream fail2ban uses sendmail
mta = sendmail
# Default protocol
protocol = tcp
# Specify chain where jumps would need to be added in iptables-* actions
chain = INPUT
#定义各种行为参数
#只禁 IP
action_ = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
#禁 IP+邮件通知
action_mw = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
%(mta)s-whois[name=%(__name__)s, dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s", sendername="%(sendername)s"]
# 禁 IP+邮件通知+报告相关日志
action_mwl = %(banaction)s[name=%(__name__)s, port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
%(mta)s-whois-lines[name=%(__name__)s, dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s", sendername="%(sendername)s"]
# 选择行为
action = %(action_)s
#以下是各种应用监控日志的配置,以 ssh 日志为例说明一下配置原理
[ssh]
enabled = true
port = 22
filter = sshd
logpath = /var/log/secure
bantime  = 1mon
findtime  = 5m
maxretry = 3
action = %(action_mw)s

❌ 注意

参数说明:

ignoreip:配置忽略检测的 IP (段),如有多个用空格隔开

enabled:配置是否启用此 section 的的扫描监控

port:配置服务端口,如果 SSH 使用非默认端口 22,要修改为实际使用端口

filter:配置使用的匹配规则文件(位于 /etc/fail2ban/filter.d 目录中)

logpath:配置要扫描的日志文件路径

bantime:配置 IP 封禁的持续时间(秒或时间缩写格

式:years/months/weeks/days/hours/minutes/seconds)

findtime:配置从当前时间的多久之前开始计算失败次数(秒或时间缩写格式:years/months/weeks/days/hours/minutes/seconds)

maxretry:配置在 findtime 时间内发生多少次失败登录然后将 IP 封禁。

3. Faile2ban管理

3.1 常用命令

bash
#查看 fail2ban 状态
fail2ban-client status

#查看日志监控状态
fail2ban-client status name

#查看 iptables 禁 ip 情况
iptables -nvL

#手动解除限制(使用unban解除封锁的IP)
fail2ban-client unban 192.168.1.16
fail2ban-client unban --all  #解除所有
#查看 fail2ban 状态
fail2ban-client status

#查看日志监控状态
fail2ban-client status name

#查看 iptables 禁 ip 情况
iptables -nvL

#手动解除限制(使用unban解除封锁的IP)
fail2ban-client unban 192.168.1.16
fail2ban-client unban --all  #解除所有