1. 什么是DenyHosts

当我们的服务器对外提供服务的时候,难免的会受到其他用户的扫描和试图登录的操作,以侵入服务器,这样不仅会浪费系统的资源,有可能还会被其他用户进行N多次尝试后登录系统,对系统造成破坏影响业务系统的正常运行。DenyHosts是一个使用python编写的脚本文件,通过运行这个脚本(可以通过命令行运行、计划任务或者是作为服务运行)可以有效的阻止对SSH服务器的***。DenyHosts具有如下特性:

1. /var/log/secure日志文件进行分析,查找所有的登录尝试,并且过滤出失败和成功的尝试。

2.记录下所有失败的登录尝试的用户名和主机,如果超过阀值,则记录主机。

3.保持对每一个登录失败的用户(存在系统中或不存在系统中的用户)的跟踪

4.对每一个可疑的登录进行跟踪。(虽然登录成功,但是有很多次登录失败的记录)

5.将可疑地址的主机加入到/etc/hosts.deny文件中。


2. 安装DenyHosts

2.1 前提条件

1. 安装python,版本为2.3或以上

2. SSHD服务要开启tcp_wrappers的支持。判断一个服务是否支持tcp_wrappers,可以用如下方法:

1)由xinetd管理的服务都支持tcp_wrappers

2)使用ldd查看指令的动态链接库是否包含libwrap,如果有,则支持,如果没有,则不支持。

[root@server1 ~]#ldd $(which sshd) | grep libwrap

       libwrap.so.0 => /lib64/libwrap.so.0(0x00007ff039547000)

[root@server1 ~]#

3. tcp_wrappers所使用的文件是/etc/hosts.allow/etc/hosts.deny

2.2 下载并安装所需软件

测试环境系统为: RHEL6.3x64

wget http://sourceforge.net/projects/denyhosts/files/latest/download?source=files

官网:http://denyhosts.sourceforge.net

1. 安装python

yum install –ypython-*

查看python的版本:rpm –qi python

[root@server1 ~]#rpm -qi python | grep Version

Version     : 2.6.6                             Vendor: Red Hat,Inc.

[root@server1 ~]#

当前的python的版本为2.6.6

2. 解压DenyHosts

指令:

tar -zxvfDenyHosts-2.6.tar.gz

进入到该目录:

cd DenyHosts-2.6

在该目录下会有一个README.txt的文件,首先查看该文件的内容,根据提示进行安装。

3. 安装DenyHosts

执行如下指令安装DenyHosts

python2.6 setup.pyinstall

默认情况下该软件安装到/usr/share/denyhosts/目录下。

进入到该目录:

[root@server1denyhosts]# pwd

/usr/share/denyhosts

[root@server1denyhosts]# ll

total 84

-rw-r--r-- 1 rootroot 16549 Dec  8  2006 CHANGELOG.txt

-rwxr-xr-x 1 rootroot  4076 Apr 22  2006 daemon-control-dist

-rw-r--r-- 1 rootroot 20830 Aug 20  2006 denyhosts.cfg-dist

-rw-r--r-- 1 rootroot 18009 Dec 17  2005 LICENSE.txt

drwxr-xr-x 2 rootroot  4096 Feb 14 16:46 plugins

-rw-r--r-- 1 rootroot  3575 Feb  3  2006README.txt

drwxr-xr-x 2 rootroot  4096 Feb 14 16:46 scripts

-rw-r--r-- 1 rootroot  1522 Apr  5  2006setup.py

[root@server1denyhosts]#

其中denyhosts.cfg-dist是一个配置文件的示例。denyhosts在正常工作之前需要一个配置文件,配置文件名为denyhosts.cfg

2.3 配置DenyHosts

denyhosts.cfg-dist拷贝为denyhosts.cfg

指令:

[root@server1denyhosts]# cp denyhosts.cfg-dist denyhosts.cfg

[root@server1denyhosts]#

编辑该文件:

vim denyhosts.cfg

该文件中的一些参数含义如下:

SECURE_LOG:包含SSHD日志信息的文件。REDHAT默认为/var/log/secure。如果不确定是哪个文件,可以用grep "sshd:" /var/log/*查找。

HOSTS_DENY:限制主机不能够登录的hosts.deny文件的位置。/etc/hosts.deny

PURGE_DENY:拒绝时间,当一个ip地址被认定为恶意IP时,拒绝多长时间内不能够登陆。超过该时间,该条目将会从hosts.deny文件中清除。格式如下:

i[dhwmy]

Where 'i' is aninteger (eg. 7)   //i是整数

         'm' = minutes

          'h' = hours

          'd' = days

          'w' = weeks

          'y' = years

PURGE_THRESHOLD:定义一个主机被清除的次数。如果超过所定义的值,则该主机如果还违反策略的话,则不会从hosts.deny文件中清除。如果是0,则关闭该特性。

BLOCK_SERVICE:要限制的服务名称。

DENY_THRESHOLD_INVALID:无效用户(不存在/etc/passwd文件中的用户)登录尝试的最大次数,超过该值时,主机将会被限制。

DENY_THRESHOLD_VALID:除了root用户之外的有效用户,允许尝试登录的最大次数。

DENY_THRESHOLD_ROOTroot用户允许尝试登录的最大次数。

DENY_THRESHOLD_RESTRICTED:尝试登录的次数。

HOSTNAME_LOOKUP:是否根据IP地址查找主机名。

该文件该允许SYSLOG通知、SMTP邮件通知等功能。

2.4 DenyHosts作为一个服务运行

指令:

[root@server1denyhosts]# cp daemon-control-dist daemon-control

[root@server1denyhosts]#

编辑该文件:

仅仅需要编辑如下的信息:

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

#### Edit these tosuit your configuration ####

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


DENYHOSTS_BIN   = "/usr/bin/denyhosts.py"

DENYHOSTS_LOCK  = "/var/lock/subsys/denyhosts"

DENYHOSTS_CFG   ="/usr/share/denyhosts/denyhosts.cfg"

其他的不需要编辑。

修改daemon-control的权限700,拥有者为root

[root@server1denyhosts]# chown root daemon-control

[root@server1denyhosts]# chmod 700 daemon-control

[root@server1denyhosts]#

可以将该文件链接到/etc/init.d/目录下:

[root@server1share]# cd /etc/init.d/

[root@server1init.d]# ln -s /usr/share/denyhosts/daemon-control denyhosts

使用checkconfig –add添加该服务:

[root@server1init.d]# chkconfig --add denyhosts

[root@server1init.d]#

启动该服务:

/etc/init.d/denyhostsstart

[root@server1init.d]# /etc/init.d/denyhosts start

startingDenyHosts:    /usr/bin/env python/usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg

[root@server1init.d]#

3. 测试

客户端使用用户登录测试,并使用tail –f /etc/hosts.deny查看该文件的变化。

当尝试登录3次时就会就测试地址写入到该文件中去……

# DenyHosts: FriFeb 14 17:37:20 2014 | sshd: 172.17.100.1

sshd: 172.17.100.1