如何在 Linux 上安装和配置 Fail2ban如何在 Linux 上安装和配置 Fail2ban如何在 Linux 上安装和配置 Fail2ban如何在 Linux 上安装和配置 Fail2ban
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

如何在 Linux 上安装和配置 Fail2ban

发表 admin at 2025年2月28日
类别
  • 未分类
标签

Fail2ban 是一款免费开源软件,我们可以使用它来缓解暴力破解和 DoS/DDoS 攻击:它会扫描日志文件以查找多次失败的身份验证尝试,并通过创建临时防火墙规则来禁止相关 IP 地址。

在本教程中,我们将了解如何在 Linux 上安装和配置fail2ban,并了解其用法背后的基本概念。

在本教程中,您将学习:

  • 如何在一些最常用的 Linux 发行版上安装 Fail2ban
  • 如何配置 Fail2ban
  • 如何使用fail2ban-client实用程序

安装

Fail2ban 可在所有最常用的 Linux 发行版的官方存储库中找到。要在 Debian 和基于 Debian 的发行版上安装它,我们可以使用以下命令:

$ sudo apt install fail2ban


在 Fedora 上,我们使用 dnf 包管理器执行安装:

$ sudo dnf install fail2ban

在我们可以在 RHEL 或其克隆之一(例如 Rocky Linux)上执行安装之前,我们需要添加 EPEL 存储库(Enterprise Linux 的额外软件包)作为软件源。对于 Rocky,这是一个非常简单的操作,因为当我们安装 epel-release 包时会自动配置存储库:

$ sudo dnf install epel-release

如果使用 RHEL 本身,可以从 EPEL 站点下载并安装该软件包。

为了确保fail2ban服务处于活动状态并在引导时启动,我们启动以下命令:

$ sudo systemctl enable --now fail2ban

Fail2ban 配置

Fail2ban 软件包安装后,其所有配置文件都可以在 /etc/fail2ban 目录下找到。我们应该避免修改安装过程中附带的文件(带有“.conf”扩展名的文件),而是将自定义配置放在带有“.local”扩展名的相应文件中。主要的fail2ban配置文件是/etc/fail2ban/fail2ban.conf。该文件包含通用设置,例如fail2ban 日志级别。我们将覆盖值放置在 /etc/fail2ban/fail2ban.local 文件中,如果该文件不存在,则应创建该文件。例如,要将日志级别从“INFO”(默认)更改为“DEBUG”,我们可以编写:

[DEFAULT]
loglevel = DEBUG


在使用 Fail2ban 时,我们必须处理三个主要“实体”:过滤器、操作和监狱。让我们来看看它们。

过滤器

Fail2ban 扫描日志文件并搜索失败的身份验证尝试。通过过滤器,我们基本上告诉它如何识别特定服务的日志文件中的身份验证尝试。可以在 /etc/fail2ban/filter.d 目录下找到现成的过滤器:

$ ls /etc/fail2ban/filter.d

3proxy.conf                domino-smtp.conf        mysqld-auth.conf        selinux-ssh.conf
apache-auth.conf           dovecot.conf            nagios.conf             sendmail-auth.conf
apache-badbots.conf        dropbear.conf           named-refused.conf      sendmail-reject.conf
apache-botsearch.conf      drupal-auth.conf        nginx-botsearch.conf    sieve.conf
apache-common.conf         ejabberd-auth.conf      nginx-http-auth.conf    slapd.conf
apache-fakegooglebot.conf  exim-common.conf        nginx-limit-req.conf    softethervpn.conf
apache-modsecurity.conf    exim.conf               nsd.conf                sogo-auth.conf
apache-nohome.conf         exim-spam.conf          openhab.conf            solid-pop3d.conf
apache-noscript.conf       freeswitch.conf         openwebmail.conf        squid.conf
apache-overflows.conf      froxlor-auth.conf       oracleims.conf          squirrelmail.conf
apache-pass.conf           gitlab.conf             pam-generic.conf        sshd.conf
apache-shellshock.conf     grafana.conf            perdition.conf          stunnel.conf
assp.conf                  groupoffice.conf        phpmyadmin-syslog.conf  suhosin.conf
asterisk.conf              gssftpd.conf            php-url-fopen.conf      tine20.conf
bitwarden.conf             guacamole.conf          portsentry.conf         traefik-auth.conf
botsearch-common.conf      haproxy-http-auth.conf  postfix.conf            uwimap-auth.conf
centreon.conf              horde.conf              proftpd.conf            vsftpd.conf
common.conf                ignorecommands          pure-ftpd.conf          webmin-auth.conf
counter-strike.conf        kerio.conf              qmail.conf              wuftpd.conf
courier-auth.conf          lighttpd-auth.conf      recidive.conf           xinetd-fail.conf
courier-smtp.conf          mongodb-auth.conf       roundcube-auth.conf     znc-adminlog.conf
cyrus-imap.conf            monit.conf              screensharingd.conf     zoneminder.conf
directadmin.conf           murmur.conf             selinux-common.conf

行动

Fail2ban 操作在 /etc/fail2ban/action.d 目录中定义。操作以用于执行禁令的软件命名。让我们看一个例子。 UFW(Uncomplicated Firewall)是一款易于使用的防火墙管理器;这是 /etc/fail2ban/action.d/ufw.conf 文件的内容:

# Fail2Ban action configuration file for ufw
#
# You are required to run "ufw enable" before this will have any effect.
#
# The insert position should be appropriate to block the required traffic.
# A number after an allow rule to the application won't be of much use.

[Definition]

actionstart = 

actionstop = 

actioncheck = 

actionban = [ -n "<application>" ] && app="app <application>"
            ufw insert   from  to  $app

actionunban = [ -n "<application>" ] && app="app <application>"
              ufw delete  from  to  $app

[Init]
# Option: insertpos
# Notes.:  The position number in the firewall list to insert the block rule
insertpos = 1

# Option: blocktype
# Notes.: reject or deny
blocktype = reject

# Option: destination
# Notes.: The destination address to block in the ufw rule
destination = any

# Option: application
# Notes.: application from sudo ufw app list
application = 

动作由两个主要部分组成:“Definition”和“Init”。前者指定的命令在不同情况下执行:作为预备步骤 (actioncheck)、监狱启动时 (actionstart)、监狱停止时 (actionstop)、禁止 (actionban) 和取消禁止 (actionunban) IP 地址。



“Init”部分包含特定于操作的配置。例如,在我们上面报告的 ufw 操作中,您可以看到它包含有关规则列表中的防火墙规则位置 (insertpos=1) 以及要使用的块类型(拒绝与拒绝)的说明。

监狱

最后,我们有监狱。监狱基本上将过滤器与一个或多个操作相关联。 Fail2ban 的监狱主要配置文件是 /etc/fail2ban/jail.conf;嵌入式配置文件可以放置在 /etc/fail2ban/jail.d 目录中。

Jail 根据其使用的过滤器命名:例如,如果监狱名为“sshd”,则它与 /etc/fail2ban/filter.d/sshd.conf 过滤器关联,除非是通过“filter”选项明确指定。监狱的名称在方括号内指定。默认情况下,Debian 为 sshd 监狱提供了覆盖。它在 /etc/fail2ban/jail.d/defaults-debian.conf 文件中定义:

[sshd]
enabled = true

“sshd”监狱的默认参数位于主监狱配置文件中。 Debian 提供了此覆盖,并将“enabled”参数设置为“true”,只是为了确保监狱处于活动状态。以下是定义监狱时或在“默认”部分中可以使用的一些参数(对所有现有监狱有效):

Option Role Default value
filter Filter used by the jail The filter corresponding to the jail name under /etc/fail2ban/filter.d
logpath Specifies the path(s) of the logfiles to be monitored service-dependent
action Actions(s) to be used by the jail. Actions are named after the file in which they are defined, without the extension %(action)s – see below
ignoreip List of IP addresses to ignore None
bantime The ban duration expressed in seconds or with explicitly time suffixes 10m
findtime The interval of time during which the specified number of failed authentication attempts must occur for an IP to be banned 10m
maxretry The number of failures which must occur in the specified findtime to trigger a ban 5

如何定义默认操作

如果你看一下主监狱配置文件(/etc/fail2ban/jail.conf),在“default”部分,你可以看到该操作是按以下方式定义的(第268行) :

action = %(action_)s

在上面的定义中,_action 变量是“expanded”,其值被分配给“action”参数。 _action 变量本身在上面几行定义(Debian 上的第 212 行):

action_ = __ %(banaction)s[port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]

在此表达式中使用了一些其他变量:

  • banaction:这是“核心”禁止操作,默认设置为iptables-multiport
  • port:要禁止的端口 – 默认设置为 0:65535,在特定的监狱中被覆盖
  • 协议:防火墙规则中用于强制禁止的协议 - 默认为 tcp
  • chain:应在需要此参数的禁止操作中添加跳转的链

端口、协议和链变量用在方括号之间,并用逗号分隔。使用此语法,它们作为“参数”传递并替换操作定义中包含的相应占位符。这里,“action_”是可用的宏之一,它只是强制执行禁令。其他的定义如下。一些例子是:

  • action_mw – 强制执行禁令并向指定邮箱发送包含 whois 报告的电子邮件
  • action_mwl – 与上面相同,但包括相关的日志行

让我们被禁止吧!

让我们验证fail2ban是否正常工作并让它触发禁令。正如我们之前看到的,默认的 findtime 为 10 分钟,默认的 maxretry 值为 5:这意味着如果我们在 10 分钟内失败 5 次身份验证尝试,我们的 IP(在本例中为 192.168.122.1)将被禁止。



我尝试通过 SSH 连接到 IP 192.168.122.93 的主机,故意提供错误的密码。这会触发对远程主机的禁止。我们可以通过查看fail2ban日志来验证这一点:

$ sudo tail /var/log/fail2ban.log

相关行是:

2023-09-27 15:54:47,028 fail2ban.actions
[2829]: NOTICE [sshd] Ban 192.168.122.1

可以看到,192.168.122.1 IP已被禁止。检查所有活动禁令的更便捷方法是使用 fail2ban-client 实用程序。要获取被禁止的 IP 列表,我们将其与“banned”子命令一起使用:

$ fail2ban-client banned
[{'sshd': ['192.168.122.1']}]

要取消禁止 IP(从所有监狱),我们将其作为参数传递给 unban 子命令:

$ sudo fail2ban-client unban 192.168.122.1

fail2ban-client 实用程序还可用于控制服务器(启动、停止、重新加载)并执行一些运行时配置。

结论

©2015-2025 Norria support@alaica.com