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

它主要通过监控日志文件(比如 /var/log/auth.log、/var/log/apache/access.log 等)来生效。一旦发现恶意攻击的登录请求,它会封锁对方的 IP 地址,使得对方无法再发起请求。Fail2ban在需要阻止某IP时,会自动在iptables里添加条目进行拦载。
Fail2Ban 可以防止有人反复尝试 SSH 密码登录,但是如果 SSH 采用的是密钥登录,禁止了密码登录,就不需要 Fail2Ban 来保护。
Fail2Ban 的安装命令:
# ubuntu & Debiansudo apt install fail2ban# Fedorasudo dnf install epel-releasesudo dnf install fail2ban# Centos & Red hatyum install fail2ban
systemctl status fail2ban.service
systemctl start fail2ban
systemctl restart fail2ban
systemctl enable fail2ban
fail2ban-client status
fail2ban-client status sshd
fail2ban-client banned
fail2ban-client set sshd unbanip 192.168.1.69
配置
修改配置以后,需要重新启动 fail2ban.service,让其生效。
主配置文件
Fail2Ban 主配置文件是在 /etc/fail2ban/fail2ban.conf
[Definition]logtarget = /var/log/fail2ban/fail2ban.log
封禁配置
Fail2Ban 封禁行为的配置文件是 /etc/fail2ban/jail.conf,也可以在目录 /etc/fail2ban/jail.d 里面,新建单独的子配置文件,比如 /etc/fail2ban/jail.d/sshd.local
配置文件里面,[DEFAULT]标题行表示对于所有封禁目标生效。举例来说,如果封禁时间修改为1天,/etc/fail2ban/jail.local 里面可以写成:
[DEFAULT]bantime = 1d
如果某人被封时,对站长发送邮件通知,可以如下设置。
[DEFAULT]destemail = yourname@example.comsender = yourname@example.com# to ban & send an e-mail with whois report to the destemail.action = %(action_mw)s# same as action_mw but also send relevant log lines#action = %(action_mwl)s
如果配置写在其他标题行下,就表示只对该封禁目标生效,比如写在[sshd]下面,就表示只对 sshd 生效。
默认情况下,Fail2Ban 对各种服务都是关闭的,如果要针对某一项服务开启,需要在配置文件里面声明。
[sshd]enabled = true
上面声明表示,Fail2Ban 对 sshd 开启。
下面是配置文件 jail.local 的配置项含义,所有配置项的格式都是key=value。
1) bantime
封禁的时间长度,单位 m 表示分钟,d 表示天,如果不写单位,则表示秒。Fail2Ban 默认封禁 10 分钟(10m 或 600)。
[DEFAULT]bantime = 10m
2) findtime
登录失败计算的时间长度,单位 m 表示分钟,d 表示天,如果不写单位,则表示秒。Fail2Ban 默认封禁 10 分钟内登录 5 次失败的客户端。
[DEFAULT]findtime = 10mmaxretry = 5
3) maxretry
尝试登录的最大失败次数。
4) destemail
接受通知的邮件地址。
[DEFAULT]destemail = root@localhostsender = root@<fq-hostname>mta = sendmail
5) sendername
通知邮件的“发件人”字段的值。
6) mta
发送邮件的邮件服务,默认是 sendmail。
7) action
封禁时采取的动作。
[DEFAULT]action = $(action_)s
上面的 action_ 是默认动作,表示拒绝封禁对象的流量,直到封禁期结束。
下面是 Fail2Ban 提供的一些其他动作。
# ban & send an e-mail with whois report to the destemail.action_mw = %(action_)s%(mta)s-whois[sender="%(sender)s", dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]# ban & send an e-mail with whois report and relevant log lines# to the destemail.action_mwl = %(action_)s%(mta)s-whois-lines[sender="%(sender)s", dest="%(destemail)s", logpath="%(logpath)s", chain="%(chain)s"]# See the IMPORTANT note in action.d/xarf-login-attack for when to use this action## ban & send a xarf e-mail to abuse contact of IP address and include relevant log lines# to the destemail.action_xarf = %(action_)sxarf-login-attack[service=%(__name__)s, sender="%(sender)s", logpath="%(logpath)s", port="%(port)s"]# ban IP on CloudFlare & send an e-mail with whois report and relevant log lines# to the destemail.action_cf_mwl = cloudflare[cfuser="%(cfemail)s", cftoken="%(cfapikey)s"]%(mta)s-whois-lines[sender="%(sender)s", dest="%(destemail)s", logpath="%(logpath)s", chain="%(chain)s"]8) ignoreipFail2Ban 可以忽视的可信 IP 地址。多个 IP 地址之间使用空格分隔。ignoreip = 127.0.0.1/8 192.168.1.10 192.168.1.20
9) port
指定要监控的端口。可以设为任何端口号或服务名称,比如 ssh、22、2200 等。
ssh 配置
下面是 sshd 的设置范例。
[sshd]enabled = trueport = sshfilter = sshdbanaction = iptablesbackend = systemdmaxretry = 5findtime = 1dbantime = 2wignoreip = 127.0.0.1/8
首先需要注意,为了让 Fail2Ban 能够完整发挥作用,最好在 /etc/ssh/sshd_config 里面设置 LogLevel VERBOSE,保证日志有足够的信息。
nginx配置
云服务器部署nginx作为web反向代理部署面对互联网时,经常会有大量的扫描尝试访问,后端的服务器会响应返回404,我们可以设置策略阻止这些扫描行为。
新建一个文件 /etc/fail2ban/filter.d/nginx-4xx.conf。键入以下内容:
[Definition]failregex = ^<HOST>.*"(GET|POST).*" (404|444|403|400) .*$ignoreregex =
表示过滤404,444,403,400等错误。然后在 /etc/fail2ban/jail.local添加一节:
[nginx-4xx]enabled = trueport = http,httpslogpath = %(nginx_access_log)sbantime = 7200findtime = 2maxretry = 2
自定义了该节的findtime和maxretry、bantime,表示2秒内有2次以上的404等响应,就认为该IP是在扫描,就阻止它2个小时(7200秒),当然这个时间参数要根据实际情况不断作相应调整。




