如何在 Centos 7 Linux 中安裝和配置 Denyhost

DenyHosts 是 Phil Schwartz 用 Python 語言開發的開源軟件。 它主要用於監控和分析 SSH 服務器日誌中的無效登錄嘗試、基於字典的攻擊和蠻力攻擊,通過將其添加到服務器上的 /etc/hosts.deny 文件來阻止原始 IP 地址,從而防止進行任何進一步此類登錄嘗試的 IP 地址。

由於它的簡單性和手動配置規則的能力,它被廣泛用作 Fail2ban 的替代品,後者使用和配置有點複雜。

在本教程中,我將向您展示如何在 CentOS 7 服務器上安裝和配置 DenyHosts。

另請閱讀:如何在 CentOS 7 上使用 Fail2ban/Client

先決條件

在繼續安裝之前,我們需要確保我們的系統滿足編譯和安裝應用程序的所有軟件要求。 並且它還應該配置一個靜態 IP 地址。 DenyHosts 軟件取決於 "ipaddr" 蟒蛇模塊。 第一步,通過發出以下命令更新您的系統存儲庫和軟件包,包括 python 模塊。

#yum update
#yum install python-ipaddr -y

1. 從 Epel 存儲庫安裝 DenyHosts

我們需要使用 Epel 存儲庫安裝此包,使用以下命令進行安裝。

#yum install epel-release
#yum install denyhosts -y

配置拒絕主機

安裝 Denyhosts 後,您需要將您的 IP 添加到 IP 允許列表中,以確保您自己的 IP 地址被列入白名單,這樣您就永遠不會被鎖定。

# cat /etc/hosts.allow
#
# hosts.allow This file contains access rules which are used to
# allow or deny connections to network services that
# either use the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
sshd: 37.217.142.41
sshd: 37.217.142.42
sshd: 37.217.142.43
sshd: 37.217.142.44

接下來,您需要將要阻止的所有必需 IP 地址列入黑名單。 我們可以確認我們的 IP 地址也不在黑名單中。

# cat /etc/hosts.deny
#
# hosts.deny This file contains access rules which are used to
# deny connections to network services that either use
# the tcp_wrappers library or that have been
# started through a tcp_wrappers-enabled xinetd.
#
# The rules in this file can also be set up in
# /etc/hosts.allow with a 'deny' option instead.
#
# See 'man 5 hosts_options' and 'man 5 hosts_access'
# for information on rule syntax.
# See 'man tcpd' for information on tcp_wrappers
#
sshd: 78.189.206.37
sshd: 121.14.27.58
sshd: 1.246.228.161
sshd: 103.89.89.47
sshd: 116.29.148.2
# DenyHosts: Tue Jan 9 10:16:15 2018 | sshd: 222.186.174.81
sshd: 222.186.174.81
# DenyHosts: Tue Jan 9 10:40:46 2018 | sshd: 217.61.20.181
sshd: 217.61.20.181
# DenyHosts: Tue Jan 9 13:15:53 2018 | sshd: 112.86.117.182
sshd: 112.86.117.182

啟用 DenyHosts 服務

根據需要進行配置後,我們可以使用以下命令啟用並啟動我們的 DenyHosts 服務:

# systemctl enable denyhosts
# systemctl start denyhosts

# systemctl status denyhosts
● denyhosts.service - SYSV: Activates/Deactivates the
Loaded: loaded (/etc/rc.d/init.d/denyhosts; bad; vendor preset: disabled)
Active: active (running) since Wed 2018-01-10 06:47:54 UTC; 3h 57min ago
Docs: man:systemd-sysv-generator(8)
Process: 30660 ExecStart=/etc/rc.d/init.d/denyhosts start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/denyhosts.service
└─30665 python /usr/bin/denyhosts.py --daemon --config=/etc/denyhosts.conf

Jan 10 06:47:54 li226-12.members.linode.com systemd[1]: Starting SYSV: Activates/Deactivates the...
Jan 10 06:47:54 li226-12.members.linode.com denyhosts[30660]: starting DenyHosts: /usr/bin/env python /usr/bin/denyhosts.py --daemon --config=/etc/denyhosts.conf
Jan 10 06:47:54 li226-12.members.linode.com systemd[1]: Started SYSV: Activates/Deactivates the.

在 DenyHosts 中配置電子郵件警報

我們可以通過更改 DenyHosts 配置文件來設置有關可疑登錄和受限主機的電子郵件警報。 您可以將您的電子郵件地址添加到變量 ADMIN_EMAIL 在配置文件中 /etc/denyhosts.conf 接收有關可疑登錄的電子郵件警報。 您可以將任意數量的電子郵件地址添加到變量中,只需確保使用逗號分隔即可。 在這裡,我添加了我的電子郵件地址 [email protected] 以提醒我任何攻擊。

對配置文件進行任何更改後,您需要重新啟動 DenyHosts 服務。

移除被禁止的 IP

您可以監控 denyhosts ssh 日誌以查看有多少攻擊者和黑客試圖訪問您的服務器。 您可以使用以下命令查看實時日誌。

# tail -f /var/log/secure
Jan 10 10:56:43 li226-12 sshd[2096]: refused connect from 222.186.43.6 (222.186.43.6)
Jan 10 10:57:08 li226-12 sshd[2102]: refused connect from 222.186.43.6 (222.186.43.6)
Jan 10 10:57:43 li226-12 sshd[2113]: refused connect from 222.186.43.6 (222.186.43.6)
Jan 10 10:59:17 li226-12 sshd[2133]: refused connect from 222.186.43.6 (222.186.43.6)

很明顯,我們的服務器正試圖從這些日誌中拒絕來自 IP 222.186.43.6 的連接。 讓我們看看從黑名單中刪除此 IP 的步驟。

我們需要在 /etc/hosts.deny 文件和所有使用 denyhosts 創建的自定義拒絕文件中檢查此 IP 條目。 我們可以從所有這些中刪除 IP 條目,如下所示:

# nano /etc/hosts.deny
# nano /var/lib/denyhosts/hosts
# nano /var/lib/denyhosts/hosts-restricted
# nano /var/lib/denyhosts/hosts-root
# nano /var/lib/denyhosts/hosts-valid
# nano /var/lib/denyhosts/users-hosts

我可以在我的 hosts.deny 文件中註意到這個條目。

# DenyHosts: Wed Jan 10 03:40:07 2018 | sshd: 222.186.43.6
sshd: 222.186.43.6

我刪除了這個條目並重新啟動了拒絕主機服務以確認它的工作!

2. 從 Github Repo 和 Source Distribution 安裝

我們可以下載 Github 存儲庫 或下載最新的 拒絕主機 源分發並從這些源包編譯它。 讓我們看看我是如何從 Github Repo 安裝 denyhosts 的。

#yum install git
# git clone https://github.com/denyhosts/denyhosts
Cloning into 'denyhosts'...
remote: Counting objects: 1353, done.
remote: Total 1353 (delta 0), reused 0 (delta 0), pack-reused 1353
Receiving objects: 100% (1353/1353), 263.93 KiB | 0 bytes/s, done.
Resolving deltas: 100% (899/899), done.

從 Github 下載文件後,我們需要安裝它。 您可以通過從 denyhosts 文件夾發出此命令來安裝它,如下所示:

#cd denyhosts
#python setup.py install

這會將 DenyHosts 模塊安裝到 python 的 site-packages 目錄中。

同樣,您可以從源發行版下載最新的 Denyhosts 並按照上述說明進行安裝。 在這兩種情況下,其餘的配置步驟是相同的。

配置拒絕主機

安裝完成後,複製示例配置文件即可 denyhosts.conf 在包含大多數可能的設置的安裝文件夾下創建 /etc folder. 配置文件包含各種設置及其說明,可幫助您快速配置 DenyHosts。 編輯完配置文件後,保存它。

# cp -rp denyhosts.conf /etc/

接下來,我們需要復製樣本 daemon-control.dist 這樣的腳本 daemon-control 並根據您的配置文件位置修改推薦部分。

#cp daemon-control-dist daemon-control
#chmod 700 daemon-control

現在我們可以編輯守護進程控製文件。 您應該只需要在頂部附近編輯此部分,如下所示。 我將此部分編輯為我的配置路徑。

############################################

############################################
#### 編輯這些以適合您的配置####
############################################

DENYHOSTS_BIN = “/usr/bin/denyhosts.py”
DENYHOSTS_LOCK = “/var/run/denyhosts.pid”
DENYHOSTS_CFG = “/etc/denyhosts.conf”

PYTHON_BIN = “/usr/bin/env python”

#######################################

這些默認值對於許多系統來說應該是合理的。 但我建議您自定義這些設置以匹配您的特定係統。 一旦您編輯了配置和守護程序控製文件,請確保將守護程序控制腳本的執行限制為 root。

手動啟動 DenyHosts

使用我們的守護程序控制腳本將 Denyhosts 設置為作為守護程序運行後,您只需從安裝文件夾運行此命令即可從那里手動啟動它。

# ./daemon-control start
starting DenyHosts: /usr/bin/env python /usr/bin/denyhosts.py --daemon --config=/etc/denyhosts.conf

可以參考守護進程日誌 (/var/log/denyhosts) 以確保 DenyHosts 成功運行。 你可以參考這個 常問問題 有關此軟件的任何問題和疑問的鏈接。

此外,您可以手動啟動 DenyHosts 是使用 Python 從命令行運行它,就像這裡提到的常見參數,如其二進製文件位置和配置文件。

# python /usr/bin/denyhosts.py --config /etc/denyhosts.conf --daemon
# ps aux | grep denyhosts
root 25650 0.0 1.0 216116 10740 ? S 09:44 0:00 python /usr/bin/denyhosts.py --config /etc/denyhosts.conf --daemon
root 25656 0.0 0.2 112668 2200 pts/0 S+ 09:44 0:00 grep --color=auto denyhosts

上面的命令啟動 DenyHosts 並在後台運行它。

自動啟動拒絕主機

我們可以設置 cronjobs 在啟動時自動啟動 Denyhosts。 或者我們可以創建一個符號鏈接 /etc/init.d 如下:

# cd /etc/init.d
# ln -s /root/denyhosts/daemon-control denyhosts
# ll | grep denyhosts
lrwxrwxrwx 1 root root 30 Jan 10 04:48 denyhosts -> /root/denyhosts/daemon-control

現在我們可以正常管理這個服務了 systemctl 命令。

# systemctl enable denyhosts
denyhosts.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig denyhosts on

# systemctl start denyhosts
# systemctl status denyhosts
● denyhosts.service - SYSV: Activates/Deactivates the
Loaded: loaded (/etc/rc.d/init.d/denyhosts; bad; vendor preset: disabled)
Active: active (exited) since Thu 2018-01-11 09:57:53 UTC; 2s ago
Docs: man:systemd-sysv-generator(8)
Process: 25876 ExecStart=/etc/rc.d/init.d/denyhosts start (code=exited, status=0/SUCCESS)

包起來

在本文中,我討論瞭如何在我們的 CentOS 7 服務器上安裝和配置 DenyHosts。 此工具可以輕鬆配置,並且還支持電子郵件、SMTP 和系統日誌通知。 該應用程序是防禦 SSH 服務器攻擊(如基於字典的攻擊和暴力攻擊)的絕佳解決方案。 我希望這篇文章對你有用。 請就此發表您的寶貴意見和建議。