如何使用Fail2ban保护CentOS 7上的SSH

时间:2020-03-05 15:26:42  来源:igfitidea点击:

Fail2ban是从暴力强制攻击保护服务器的最新安全工具。
它会自动保护服务器免受黑客的影响。
它通过通过日志文件监视并对违规操作进行造成的违规操作来工作,如重复的登录尝试。
该过程包括在防火墙链中添加新规则并发送电子邮件通知。
我们可以使用此简单文档轻松安装和配置Fail.Ban。

在进行安装之前,我们需要确保我们的系统符合编译和安装应用程序的所有软件要求。
而且它也应该配置静态IP地址。
在第一步,通过在CentOS上发出以下命令,更新系统存储库和软件包。

# yum update

在CentOS上安装Fail2Ban

要在CentOS 7服务器上安装Fail2Ban,我们将必须首先安装EPEL(Enterprise Linux的另外软件包)存储库。
Epel包含所有CentOS版本的其他包。
我们可以从root运行以下命令以安装此包。

# yum install epel-release -y
# yum install fail2ban fail2ban-systemd

Fail2Ban需要一些依赖项。
它将自动从存储库下载这些。

fail2ban-firewalld
fail2ban-sendmail
fail2ban-server
systemd-python

在CentOS上配置Fail2Ban的设置

Fail2Ban的默认配置文件位于/etc/fail2ban/jail.confall fail2ban的所有配置将在本地文件中完成。
我们需要将/etc/fail2ban/jail.conf复制到/etc/fail2ban/jail.local

# cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

复制后,我们将不得不使用"jail.local"配置文件"配置和自定义软件。
Jail.local文件覆盖了Jail.conf文件,用于使自定义配置更新安全。

默认jail.local中的主要配置

文件代码可以由许多代码组成,该代码被执行到白名单或者禁止一个或者多个IP地址,设置Bantime持续时间,故障尝试次数等来激活我们可以将启用状态从FALSE设置为TRUE。
我们需要知道配置文件中使用的一些术语。

  • Ignoreip:它可以是IP地址,CIDR掩码或者DNS主机。我们可以通过添加到此列表来仔细选择任何IP地址。可以使用空间分隔符定义几个地址。
  • Bantime:它是禁止主机的秒数。
  • speime:它是用于检查是否必须禁止主机的参数。当主机在其最后一个查找措施中生成maxRetry时,它将被禁止。
  • maxretry:它是用于设置主机的重试次数的限制的参数,超过此限制时,主机被禁止。

在CentOS 7服务器中,我们需要将jail.local的后端选项从自动到systemd更改。

# "backend" specifies the backend used to get files modification.
# Available options are "pyinotify", "gamin", "polling", "systemd" and "auto".
# This option can be overridden in each jail as well.
## pyinotify: requires pyinotify (a file alteration monitor) to be installed.
# If pyinotify is not installed, Fail2ban will use auto.
# gamin: requires Gamin (a file alteration monitor) to be installed.
# If Gamin is not installed, Fail2ban will use auto.
# polling: uses a polling algorithm which does not require external libraries.
# systemd: uses systemd python library to access the systemd journal.
# Specifying "logpath" is not valid for this backend.
# See "journalmatch" in the jails associated filter config
# auto: will try to use the following backends, in order:
# pyinotify, gamin, polling.
## Note: if systemd backend is chosen as the default but you enable a jail
# for which logs are present only in its own log files, specify some other
# backend for that jail (e.g. polling) and provide empty value for
# journalmatch. See https://github.com/fail2ban/fail2ban/issues/959#issuecomment-74901200
backend = systemd

配置Fail2Ban for SSH保护

默认情况下,在Centos 7中没有启用监狱。
要启用SSH保护,则需要取消下列以下行的以下行:

# JAILS
# SSH servers
#[sshd]
enabled = true
# To use more aggressive sshd filter (inclusive sshd-ddos failregex):
#filter = sshd-aggressive
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s

最启用的参数设置为true,为了提供保护,禁用保护,我们可以将其设置为false。
过滤器参数检查位于Path/etc/fail2ban/filter.d/sshd.conf中的Fail2Ban SSHD配置文件。

参数操作用于派生使用/etc/fail2ban/action.d/firewallcmd -ipset.conf可用的筛选器需要禁止的IP地址。

  • port:如果将端口移动到2222而不是默认值,则可以将此参数更改为诸如端口= 2222的新值。无需更改默认端口22的此参数。
  • logpath:它提供了存储日志文件的路径。此日志文件由Fail2Ban扫描。
  • maxretry:它用于为失败的登录条目设置最大限制。
  • Bantime:这用于设置需要禁止主机的秒数的持续时间。
  • filter位于/etc/fail2ban/filter.d中的文件名,其中包含用于正确解析日志文件的failregex信息。

启用Fail2Ban服务

我们需要确保启用并启动CentOS Firewalld服务以运行此软件。

# systemctl enable firewalld
Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service.
# systemctl start firewalld
# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2016-01-14 10:57:34 UTC; 9s ago
Docs: man:firewalld(1)
Main PID: 19493 (firewalld)
CGroup: /system.slice/firewalld.service
└─19493 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
Now we can enable and start our fail2ban service by executing these commands below:
#systemctl enable fail2ban
#systemctl start fail2ban

有关任何配置更改,请务必重新启动该服务以将这些更改添加到下面的命令:

#systemctl restart fail2ban

使用fail2ban客户端

Fail2Ban提供命令fail2ban-client,可用于从命令行运行Fail2Ban。
查看以下各种命令行选项:

fail2ban-client COMMAND
  • start:开始服务器和监狱
  • "reload":重新加载配置
  • Reload &lt;Jail>:重新加载Jail <Jail>
  • stop:停止所有监狱并终止服务器
  • status:获取服务器的当前状态
  • ping:测试服务器是否活着
  • "help":返回此输出
  • "version":返回服务器版本

例如,要检查FailE2Ban是否正在运行,如果启用SSHD监控,则运行:

# fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd

有关此命令的更多信息,我们可以运行man fall2ban-client

跟踪登录失败尝试

我们可以使用以下命令检查SSH日志,以检查是否有通过SSH端口登录到SECT的失败尝试。
我们可以获得类似于此输出的不同IP地址的root密码尝试列表。

Jan 14 05:08:58 li226-12 sshd[14786]: Failed password for root from 67.171.30.161 port 43374 ssh2
Jan 14 05:09:00 li226-12 sshd[14786]: Failed password for root from 67.171.30.161 port 43374 ssh2
Jan 14 05:09:02 li226-12 sshd[14786]: Failed password for root from 67.171.30.161 port 43374 ssh2
Jan 14 05:09:04 li226-12 sshd[14786]: Failed password for root from 67.171.30.161 port 43374 ssh2
Jan 14 05:09:06 li226-12 sshd[14786]: Failed password for root from 67.171.30.161 port 43374 ssh2
Jan 14 05:09:08 li226-12 sshd[14786]: Failed password for root from 67.171.30.161 port 43374 ssh2

我们可以查看禁止的IPS列表,该列表已达到以下命令已达到最大故障尝试次数:

#iptables -L -n

未接受IP地址

为了从禁止列表中删除IP地址,参数ipaddress设置为需要白名单的适当IP。
名称"sshd"是监狱的名称,在这种情况下,我们在上面配置的"SSHD"监狱。
我们可以为其运行以下命令。

#fail2ban-client set sshd unbanip IPADDR