如何在 RHEL 7.x/CentOS 7.x 上使用 Fail2Ban 加固 SSHD


在本文中,我們將學習如何在 CentOS 7 上安裝和配置 Fail2ban 以保護 SSH 連線。我們都知道,透過 SSH 連線到伺服器是一種安全的方式,而 SSH 為了正常工作,通常會暴露在網際網路上。這樣一來,就存在被攻擊的風險,如果我們檢視這些服務的日誌,經常會看到使用暴力破解攻擊進行的重複登入嘗試。

安裝 Fail2ban

由於 Fail2ban 不包含在 CentOS 官方倉庫中,我們需要更新並使用 EPEL 專案安裝軟體包,然後安裝 fail2ban 並使其在系統啟動時自動啟動。

# yum update
# yum install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: centos.excellmedia.net
* epel: kodeterbuka.beritagar.id
* extras: mirrors.nhanhoa.com
* updates: mirrors.nhanhoa.com
Resolving Dependencies
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
fail2ban noarch 0.9.3-1.el7 epel 9.7 k
Installing for dependencies:
ebtables x86_64 2.0.10-13.el7 base 122 k
fail2ban-firewalld noarch 0.9.3-1.el7 epel 9.9 k
fail2ban-sendmail noarch 0.9.3-1.el7 epel 13 k
fail2ban-server noarch 0.9.3-1.el7 epel 395 k
firewalld noarch 0.3.9-14.el7 base 476 k
ipset x86_64 6.19-4.el7 base 36 k
ipset-libs x86_64 6.19-4.el7 base 46 k
libselinux-python x86_64 2.2.2-6.el7 base 247 k
python-slip noarch 0.4.0-2.el7 base 30 k
python-slip-dbus noarch 0.4.0-2.el7 base 31 k
systemd-python x86_64 219-19.el7_2.11 updates 99 k
Updating for dependencies:
libgudev1 x86_64 219-19.el7_2.11 updates 66 k
systemd x86_64 219-19.el7_2.11 updates 5.1 M
systemd-libs x86_64 219-19.el7_2.11 updates 358 k
systemd-sysv x86_64 219-19.el7_2.11 updates 53 k
Transaction Summary
================================================================================
Install 1 Package (+11 Dependent packages)
Upgrade ( 4 Dependent packages)
Total download size: 7.1 M
Is this ok [y/d/N]: y
Downloading packages:
--> Running transaction check
Installed:
fail2ban.noarch 0:0.9.3-1.el7
Dependency Installed:
ebtables.x86_64 0:2.0.10-13.el7
fail2ban-firewalld.noarch 0:0.9.3-1.el7
fail2ban-sendmail.noarch 0:0.9.3-1.el7
fail2ban-server.noarch 0:0.9.3-1.el7
firewalld.noarch 0:0.3.9-14.el7
ipset.x86_64 0:6.19-4.el7
ipset-libs.x86_64 0:6.19-4.el7
libselinux-python.x86_64 0:2.2.2-6.el7
python-slip.noarch 0:0.4.0-2.el7
python-slip-dbus.noarch 0:0.4.0-2.el7
systemd-python.x86_64 0:219-19.el7_2.11
Dependency Updated:
libgudev1.x86_64 0:219-19.el7_2.11 systemd.x86_64 0:219-19.el7_2.11
systemd-libs.x86_64 0:219-19.el7_2.11 systemd-sysv.x86_64 0:219-19.el7_2.11
Complete!

現在,我們將啟動 fail2ban 服務並使其在系統啟動時自動啟動。

# systemctl start fail2ban
# systemctl enable fail2ban

配置 Fail2ban 本地設定

安裝 Fail2ban 後,我們需要根據我們的環境自定義配置檔案。Fail2ban 的所有配置檔案都位於 ** /etc/fail2ban ** 目錄下。我們將從 ** /etc/fail2ban/jail.conf ** 中的 jail.com 配置複製到 ** /etc/fail2ban/jail.local **,以下是複製配置的命令

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

現在,我們將根據需要自定義配置檔案。

# vi /etc/fail2ban/jail.local
[DEFAULT]
#
# WARNING: heavily refactored in 0.9.0 release. Please review and
# customize settings for your setup.
#
# Changes: in most of the cases you should not modify this
# file, but provide customizations in jail.local file,
# or separate .conf files under jail.d/ directory, e.g.:
#
# HOW TO ACTIVATE JAILS:
#
# YOU SHOULD NOT MODIFY THIS FILE.
#
# It will probably be overwritten or improved in a distribution update.
#
# Provide customizations in a jail.local file or a jail.d/customisation.local.
# For example to change the default bantime for all jails and to enable the
# ssh-iptables jail the following (uncommented) would appear in the .local file.
# See man 5 jail.conf for details.
#
# [DEFAULT]
# bantime = 3600
#
# [sshd]
# enabled = true
#
# See jail.conf(5) man page for more informationignoreip = 127.0.0.1/8
# External command that will take an tagged arguments to ignore, e.g. <ip>,
# and return true if the IP is to be ignored. False otherwise.
#
# ignorecommand = /path/to/command <ip>
ignorecommand =
# "bantime" is the number of seconds that a host is banned to do ssh .
bantime = 1200
# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 600
# "maxretry" is the number of failures before a host get banned.
maxretry = 5

配置檔案中使用的選項

**Ignoreip** - 此選項用於設定允許的 IP 地址,IP 地址列表應使用空格分隔,用於設定您的 IP 地址。

**Bantime** - 此選項設定主機被禁止訪問的持續時間(秒)。

**Findtime** - 此選項用於檢查被禁止的主機列表,它將檢查上次被禁止的時間。

**Maxretry** - 此選項用於設定主機嘗試登入的次數限制,超過限制則該主機將被禁止訪問。

新增配置檔案以保護 SSH

我們將使用以下命令建立一個新的 sshd 配置檔案,並在其中新增 fail2ban 配置。

# vi /etc/fail2ban/jail.d/sshd.local
[sshd]
enabled = true
port = ssh
#action = firewallcmd-ipset
logpath = %(sshd_log)s
maxretry = 25
bantime = 36000

配置檔案中使用的選項

**Enable** → 如果此選項設定為 true,則啟用保護,要停用則可以設定為 false。這將檢查 sshd 配置,位於 ** /etc/fail2ban/filters.d/sshd.conf ** 中。

**Action** → 此選項用於定義被禁止的 IP 地址,位於 ** /etc/fail2ban/action.d/firewallcmd–ipset.conf ** 中。

**Port** → 當我們更改 SSH 埠時使用此選項,SSH 的預設埠是 22,所以如果您沒有更改預設埠,則無需更改此選項。

**Logpath** → 此選項用於允許我們將 fail2ban 掃描的日誌儲存到指定位置。

**Maxretry** → 此選項用於設定允許的失敗登入的最大次數。

**Bantime** → 此選項用於設定主機被禁止的持續時間(秒)。

所有配置完成後,我們需要重新啟動 fail2ban 服務。

# systemctl restart fail2ban

檢查 Fail2ban 狀態

以下是用於檢查 fail2ban 狀態的命令

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

以下是檢查使用 SSH 埠登入伺服器的失敗嘗試次數的命令。

# cat /var/log/secure | grep ‘Failed password’
Jul 18 16:41:12 htf sshd[5487]: Failed password for root from 54.46.23.45 port 23421 ssh2
Jul 18 16:41:15 htf sshd[1254]: Failed password for root from 54.46.23.45 port 15286 ssh2
Jul 18 16:41:16 htf sshd[1254]: Failed password for root from 54.46.23.45 port 24157 ssh2
Jul 18 16:41:18 htf sshd[1254]: Failed password for root from 54.46.23.45 port 24057 ssh2
Jul 18 16:41:19 htf sshd[1254]: Failed password for root from 54.46.23.45 port 27286 ssh2
Jul 18 16:41:22 htf sshd[1254]: Failed password for root from 54.46.23.45 port 13486 ssh2

從 Fail2ban 中解除 IP 地址的禁止

如果要從禁止列表中刪除 IP 地址,則需要將 IPADDRESS 替換為需要允許的 IP 地址列表。“sshd” 是以下示例中使用的監獄名稱,以下是用於允許 IP 地址的命令。

通用語法

# fail2ban-client set sshd unbanip IPADDRESS
Example:
# fail2ban-client set sshd unbanip 192.168.1.22
# fail2ban-client set sshd unbanip 54.46.23.45

我們可以對自上次啟動以來 fail2ban 的日誌進行故障排除,以下是用查詢日誌的命令

# journalctl -b -u fail2ban
-- Logs begin at Mon 2016-07-18 08:20:57 EDT, end at Mon 2016-07-18 11:47:19 EDT
Jul 18 17:47:19 centos-linux-1.shared systemd[1]: Starting Fail2Ban Service...
Jul 18 17:47:19 centos-linux-1.shared fail2ban-client[2542]: 2016-07-18 11:47:19
Jul 18 17:47:19 centos-linux-1.shared fail2ban-client[2542]: 2016-07-18 11:47:19
Jul 18 17:47:19 centos-linux-1.shared systemd[1]: Started Fail2Ban Service.

透過以上配置和設定,我們現在能夠為服務配置基本的禁止策略,使用 Fail2ban 非常容易設定,我們還可以使用 Fail2ban 來保護任何型別的服務。

更新於: 2020-07-14

390 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.