了解 fail2ban 配置文件:

fail2ban 配置存在于 /etc/fail2ban/ 和 /etc/fail2ban/jail.d/ 中。

fail2ban 首先读取 *.conf 文件,然后读取 .local 文件。
因此,
.conf 文件中的所有设置都被 *.local 文件中的设置覆盖。

因此,最好的做法是创建一个自定义的 jail.local 文件,而不是编辑默认的 jail.conf 文件。

fail2ban 仅提供带有初始配置的单个配置文件 /etc/fail2ban/jail.conf。
此文件包含常见网络服务的示例监狱配置。
因此,我们可以简单地将所需部分复制到 jail.local 文件中,并启用 jail 来应用它。

将默认的 jail.conf 文件复制为 jail.local 。

[root@fail2ban-01 ~]# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

现在,我们可以自定义这个文件来为不同的 Linux 服务创建 fail2ban jail。
事实上,大多数与常见 Linux 服务相关的 fail2ban jail 都在此文件中预定义。
因此,我们只需要启用所需的fail2ban jail。

但是,在启用 fail2ban jail 之前,我们将在 DEFAULT 部分描述一些设置。
除非覆盖,否则这些设置全局适用于所有 fail2ban jail。

ignoreip 是 IP 地址、主机名或者 CIDR 掩码。
如果主机在此处列出,fail2ban 不会禁止该主机。

bantime 它是禁止主机的时间(以秒为单位)。

findtime 它是fail2ban 必须捕获maxretry 失败以禁止主机的时间跨度。

maxretry 它是主机被禁止之前的失败次数。

www. On IT Road .com

配置 fail2ban 以保护 Apache Web 服务器

有各种预定义的 fail2ban jail 可用于 Apache 服务。
我们可以按需启用它们中的每一个。

这次我们只启用了一个jail apache-auth 来演示fail2ban。

[root@fail2ban-01 ~]# sed -i "/^\[apache-auth\]/a\enabled=true" /etc/fail2ban/jail.local
[root@fail2ban-01 ~]# systemctl restart fail2ban.service

我们还在这台机器上配置了一个带有 HTTP 基本身份验证的 Apache 网站。

我们将使用这个网站来测试fail2ban。

使用客户端浏览器浏览 URL http://fail2ban-01.onitroad.com。

该网站将要求我们提供登录凭据。

使用错误凭据执行登录尝试 6 次,主机将在预定义的禁止时间内被 fail2ban 自动禁止。

我们可以按如下方式检查 apache-auth jail 的状态。

[root@fail2ban-01 ~]# fail2ban-client status apache-auth
Status for the jail: apache-auth
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     13
|  `- File list:        /var/log/httpd/error_log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   192.168.1.1

在 CentOS 7 上安装 fail2ban

fail2ban 可通过 EPEL(企业 Linux 的另外软件包)yum 存储库获得。
因此,我们必须安装 epel-release 才能访问 EPEL yum 存储库。

[root@fail2ban-01 ~]# yum install -y epel-release

为 yum 存储库构建缓存。

[root@fail2ban-01 ~]# yum makecache fast

我们需要安装以下 fail2ban 软件包。

fail2ban.noarch :守护程序禁止导致多个身份验证错误的主机
fail2ban-firewalld.noarch :防火墙支持 fail2ban
fail2ban-systemd.noarch :fail2ban 的 Systemd 日志配置

EPEL yum 存储库始终提供fail2ban 的稳定版本。
但是,我们可以从fail2ban 官方网站下载最新的实验版本。

[root@fail2ban-01 ~]# yum install -y fail2ban fail2ban-firewalld fail2ban-systemd

由于我们是在 CentOS 7 系统上安装 fail2ban,因此,还需要 fail2ban-firewalld 和 fail2ban-systemd 软件包分别与 Firewalld 和 Systemd 集成。

启用并启动 fail2ban.service 。

[root@fail2ban-01 ~]# systemctl enable fail2ban.service
Created symlink from /etc/systemd/system/multi-user.target.wants/fail2ban.service to /usr/lib/systemd/system/fail2ban.service.
[root@fail2ban-01 ~]# systemctl start fail2ban.service

配置 fail2ban 以保护 MariaDB 服务器

我们在极少数情况下将 MariaDB 服务端口暴露给网络。
但是,暴露 MariaDB 的默认端口也会使其面临各种威胁。

因此,在这种情况下,我们可以使用 fail2ban 来保护 MariaDB 免受暴力、字典、DOS 和 DDOS 攻击。

[root@fail2ban-01 ~]# sed -i "/^\[mysqld-auth\]/a\enabled=true" /etc/fail2ban/jail.local
[root@fail2ban-01 ~]# systemctl restart fail2ban.service

MariaDB 服务器默认日志级别为 1,当日志级别为 1 时,MariaDB 不会在日志文件中记录失败的登录尝试。

在这种情况下,fail2ban 不起作用,因为它在 MariaDB 日志文件中找不到任何登录失败。

因此,我们必须提高 MariaDB 服务器的日志级别,以便它可以在日志文件中记录失败的登录尝试。

使用以下命令提高 MariaDB 服务器的日志级别。

[root@fail2ban-01 ~]# sed -i "/^\[mysqld\]/a\log-warnings=2" /etc/my.cnf
[root@fail2ban-01 ~]# systemctl restart mariadb.service

从另一台主机使用错误的用户名/密码执行多次登录尝试。
由于可疑活动,fail2ban 将在预定义的禁止时间内禁止主机。

[root@fail2ban-tester ~]# mysql -u jackli -p12a -h 192.168.1.171
ERROR 1045 (28000): Access denied for user 'jackli'@'192.168.1.152' (using password: YES)
[root@fail2ban-tester ~]# mysql -u root -p12a -h 192.168.1.171
ERROR 1045 (28000): Access denied for user 'root'@'192.168.1.152' (using password: YES)
[root@fail2ban-tester ~]# mysql -u root -p12a -h 192.168.1.171
ERROR 1045 (28000): Access denied for user 'root'@'192.168.1.152' (using password: YES)
[root@fail2ban-tester ~]# mysql -u r1 -p12a -h 192.168.1.171
ERROR 1045 (28000): Access denied for user 'r1'@'192.168.1.152' (using password: YES)
[root@fail2ban-tester ~]# mysql -u r2 -p12a -h 192.168.1.171
ERROR 1045 (28000): Access denied for user 'r2'@'192.168.1.152' (using password: YES)
[root@fail2ban-tester ~]# mysql -u r4 -p12a -h 192.168.1.171
ERROR 1045 (28000): Access denied for user 'r4'@'192.168.1.152' (using password: YES)
[root@fail2ban-tester ~]# mysql -u r5 -p12a -h 192.168.1.171
ERROR 1045 (28000): Access denied for user 'r5'@'192.168.1.152' (using password: YES)
[root@fail2ban-tester ~]# mysql -u r7 -p12a -h 192.168.1.171
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.171' (111)

使用fail2ban-client 命令检查mysqld-jail 的状态。

[root@fail2ban-01 ~]# fail2ban-client status mysqld-auth
Status for the jail: mysqld-auth
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- File list:        /var/log/mariadb/mariadb.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   192.168.1.152

我们已经在 CentOS 7 上成功安装了 fail2ban 并配置了 fail2ban 来保护 ssh、Apache、Nginx 和 MariaDB 服务器免受暴力、字典、DOS 和 DDOS 攻击。

配置 fail2ban 以保护 nginx Web 服务器

就像 Apache 一样,我们也为 nginx 身份验证失败定义了一个 fail2ban jail。
因此,我们将启用并测试它。

我们已经配置了一个具有基本 http 身份验证的 nginx Web 服务器。
现在我们将它用于演示目的。

[root@fail2ban-01 ~]# sed -i "/^\[nginx-http-auth\]/a\enabled=true" /etc/fail2ban/jail.local
[root@fail2ban-01 ~]# systemctl restart fail2ban.service

使用客户端浏览器浏览 URL http://fail2ban-01.onitroad.com。

该网站将要求我们提供登录凭据。

使用错误凭据执行登录尝试 6 次,主机将在预定义的禁止时间内被 fail2ban 自动禁止。

[root@fail2ban-01 ~]# fail2ban-client status nginx-http-auth
Status for the jail: nginx-http-auth
|- Filter
|  |- Currently failed: 0
|  |- Total failed:     5
|  `- File list:        /var/log/nginx/error.log
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   192.168.1.1
安装 fail2ban 以保护 CentOS 7 服务器

fail2ban 是一种入侵防御软件,可保护基于 Linux 的服务器免受暴力、DOS、DDOS 和字典攻击。

fail2ban 是作为一项服务实现的,它持续监控服务的日志文件,是否出现故障,然后禁止主机,在指定的禁止时间内导致多次身份验证失败。

fail2ban 使用 CentOS 7 防火墙规则来禁止主机。

由于其简单性和有效性,fail2ban 被认为是保护 Linux 服务免受 DOS、DDOS、字典和蛮力攻击的首选软件。

在本文中,我们将在 CentOS 7 上安装 fail2ban,然后配置 fail2ban 以保护 ssh、apache、nginx 和 mariadb 服务器免受暴力、字典、DDOS 和 DOS 攻击。

配置 fail2ban 以保护 SSH 服务

在 jail.local 中有许多用于 sshd 服务的预定义 fail2ban jail。
因此,我们仅启用其中之一,如下所示。

[root@fail2ban-01 ~]# sed -i "/^\[sshd\]/a\enabled=true" /etc/fail2ban/jail.local
[root@fail2ban-01 ~]# systemctl restart fail2ban.service

我们还重新启动了 fail2ban.service 以重新加载配置。

现在,尝试使用密码错误的 ssh 客户端连接到 fail2ban-01.onitroad.com。
尝试 5 次失败,主机将被 fail2ban 禁止。

[root@fail2ban-tester ~]# ssh root@192.168.1.171
The authenticity of host '192.168.1.171 (192.168.1.171)' can't be established.
ECDSA key fingerprint is SHA256:kzyCimDDwGPsfsuGXxdrcBqlxVQlU8FZTsYrwbPzZHM.
ECDSA key fingerprint is MD5:b4:3f:a2:86:30:7a:b7:d7:b3:b0:10:8f:a3:3e:8a:bc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.171' (ECDSA) to the list of known hosts.
root@192.168.1.171's password:
Permission denied, please try again.
root@192.168.1.171's password:
Permission denied, please try again.
root@192.168.1.171's password:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
[root@fail2ban-tester ~]# ssh root@192.168.1.171
root@192.168.1.171's password:
Permission denied, please try again.
root@192.168.1.171's password:

使用 fail2ban-client 命令检查 sshd jail 状态。

[root@fail2ban-01 ~]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed: 1
|  |- Total failed:     6
|  `- Journal matches:  _SYSTEMD_UNIT=sshd.service + _COMM=sshd
`- Actions
   |- Currently banned: 1
   |- Total banned:     1
   `- Banned IP list:   192.168.1.152

主机 192.168.1.152 已被 fail2ban 禁止,因为在预定义的禁止时间内多次验证失败。

在 jail.local 文件中有更多与 sshd 服务相关的预定义 fail2ban jail。
其中之一是 sshd-ddos ,它可用于保护 ssh 服务免受 DDOS(分布式拒绝服务)攻击。

日期:2020-09-17 00:11:52 来源:oir作者:oir