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

时间:2020-03-05 15:27:51  来源:igfitidea点击:

Denyhosts是Phil Schwartz的Python语言开发的开源软件。
它主要旨在监视和分析SSH服务器日志以获取无效的登录尝试,通过阻止始发IP地址,通过将始发IP地址添加到服务器上的/etc/hosts.deny文件,从而阻止无法进行任何此类登录尝试的IP地址。

由于其简单性和手动配置规则的能力,广泛用作Fail2Ban的替代方法,这有点复杂于使用和配置。

在本教程中,将介绍如何在CentOS 7服务器上安装和配置denyhosts。

准备工作

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

#yum update
#yum install python-ipaddr -y

1.从EPEL存储库安装DenyHosts

我们需要使用epel存储库安装此软件包,使用以下命令安装它。

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

配置Denyhosts.

一旦安装了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 2016 | sshd: 222.186.174.81
sshd: 222.186.174.81
# DenyHosts: Tue Jan 9 10:40:46 2016 | sshd: 217.61.20.181
sshd: 217.61.20.181
# DenyHosts: Tue Jan 9 13:15:53 2016 | 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 2016-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配置文件中更改设置关于可疑登录和受限主机的电子邮件警报。
我们可以将电子邮件地址添加到配置文件/etc/denyhosts.conf中的变量admin_email,以接收关于可疑登录的电子邮件警报。
我们可以将任意数量的电子邮件地址添加到变量上,只需确保使用逗号分隔它。
其中我添加了我的电子邮件地址[email protected]以提醒我任何攻击。

在对配置文件进行任何更改后,我们需要重新启动Denyhosts服务。

删除禁止的IPS.

我们可以监控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文件中检查此IP条目,并使用denyhosts创建的所有这些自定义拒绝文件。
我们可以从以下所有人中删除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 2016 | sshd: 222.186.43.6
sshd: 222.186.43.6

我删除了此条目并重新启动Denyhosts服务以确认其工作!

2.从GitHub repo和源分发安装

我们可以下载GitHub存储库或者下载最新的Denyhosts源分发并从这些源包编译它。

让我们看看如何从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.

安装后,可以复制在安装文件夹下创建的示例配置文件denyhosts.conf设置,该配置文件夹包含大多数可能设置的设置到/etc文件夹。
配置文件包含各种设置以及它们的描述,应快速配置Denyhosts。
编辑配置文件后,保存它。

# cp -rp denyhosts.conf /etc/

接下来,我们需要复制示例daemon-control.dist脚本,如守护程序 - 控制件,并根据配置文件位置修改推荐部分。

#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是使用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并在后台运行它。

自动启动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 2016-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)