最近查看linux系统日志,发现有很多陌生的公网ip在不断对服务器进行暴力破解,企图猜到系统账户和密码,除了设置一个强壮的密码之外,可以使用denyhosts来限制这些ip的不友好访问。

    Denyhosts是一个用Python编写的脚本,它会分析sshd的日志文件,当发现重复的***时就会记录IP到/etc/hosts.deny文件,从而达到自动屏IP的功能。具体见原文档:
 
What is DenyHosts?
DenyHosts is a script intended to be run by Linux system administrators to help thwart SSH server attacks (also known as dictionary based attacks and brute force attacks).
If you've ever looked at your ssh log (/var/log/secure on Redhat, /var/log/auth.log on Mandrake, etc...) you may be alarmed to see how many hackers attempted to gain access to your server. Hopefully, none of them were successful (but then again, how would you know?). Wouldn't it be better to automatically prevent that attacker from continuing to gain entry into your system?
一、安装环境检查
tcp_wrappers检查:
命令:ldd /usr/sbin/sshd | grep libwrap.so.0
出现例如:libwrap.so.0 => /lib/libwrap.so.0 (0x00813000) 则表示支持 tcp_wrappers.
python检查:
命令:python -V出现例如:python 2.4.3安装及配置
#!/bin/bash
二、安装
1.  安装denyhosts:
这里我下的版本是DenyHosts-2.6.tar.gz。将文件放到/root/soft下。
安装步骤:
# cd /root/soft
# tar -zxvf DenyHosts-2.6.tar.gz
# cd DenyHosts-2.6
# python setup.py install
默认安装到/usr/share/denyhosts目录下。
2.  配置
首先将denyhosts注册到服务。
在/usr/share/denyhosts下,
# cp daemon-control-dist daemon-control //提出运行脚本,daemon-control文件
# chown root daemon-control
# chmod 700 daemon-control
进入etc/init.d
   # cd /etc/init.d
# ln -s /usr/share/denyhosts/daemon-control denyhost  //将脚本文件静态链接到init.d下,并命名为denyhost
# chkconfig –add denyhost //注册为denyhost服务
# chkconfig –level 2345 denyhosts on //让它自启动 ,这一步也可以在/etc/rc.local下添加/usr/share/denyhosts/daemon-control start这一行让他自启动。

 
然后修改配置文件:
 
# cd /usr/share/denyhosts/
# cp denyhosts.cfg-dist denyhosts.cfg
# vi denyhosts.cfg
具体修改如下:
vi /etc/denyhosts.cfg
 
SECURE_LOG = /var/log/secure
#ssh日志文件,它是根据这个文件来判断的。
 
HOSTS_DENY = /etc/hosts.deny
#控制用户登陆的文件
 
PURGE_DENY = 5m
#过多久后清除已经禁止的
# ‘m’ = minutes
# ‘h’ = hours
# ‘d’ = days
# ‘w’ = weeks
# ‘y’ = years
 
BLOCK_SERVICE = sshd
#禁止的服务名
 
DENY_THRESHOLD_INVALID = 1
#允许无效用户失败的次数
 
DENY_THRESHOLD_VALID = 10
#允许普通用户登陆失败的次数
 
DENY_THRESHOLD_ROOT = 5
#允许root登陆失败的次数
 
HOSTNAME_LOOKUP=NO
#是否做域名反解
 
ADMIN_EMAIL =你的邮箱地址
#管理员邮件地址,它会给管理员发邮件
 
DAEMON_LOG = /var/log/denyhosts
#DenyHosts日志文件存放的路径,默认自己的日志文件
      
正常情况下,这么安装便可以运行启动了:service denyhost start
/etc/init.d/denyhosts restart (services denyhosts restart)
在/etc/hosts.deny里面有被禁止的ip地址。 
三、错误排除
在输入运行命令时,有错误提示打不开denyhosts.py文件,没有这个目录。如:
#service denyhost start
starting DenyHosts:  /usr/bin/env python /usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg
python: can't open file '/usr/bin/denyhosts.py': [Errno 2] No such file or directory
经过查找发现denyhosts.py在目录/usr/local/bin/目录下,于是修改daemon-control文件
#vi daemon-control
DENYHOSTS_BIN  = "/usr/bin/denyhosts.py"
DENYHOSTS_LOCK = "/var/lock/subsys/denyhosts"
DENYHOSTS_CFG  = "/usr/share/denyhosts/denyhosts.cfg"
 
将第一行修改为DENYHOSTS_BIN  = "/usr/local/bin/denyhosts.py"
在运行还会提示错误:导入Python版本错误的提示。如:
Traceback (most recent call last):
File "/usr/local/bin/denyhosts.py", line 5, in
import DenyHosts.python_version
ImportError: No module named DenyHosts.python_version
      到这里错误很明了了,经过查询发现版本不对会导致这个问题。
 
      分析后发现原因在此:以前本机已经有一个python2.4的版本,使用rpm安装的,默认的路径是/usr/lib/python2.4,因为要升级python到2.5,也没有对卸载这个2.4的版本,使用编译安装的python2.5,安装路径并没有配置,这默认安装到/usr/local/lib/python2.5这个路径,而目前激活的python环境是2.5的,可能因为denyhosts安装时会根据环境查找安装,因此会在/usr/local/lib/python2.5/site-packages路径下安装Denyhosts这个文件夹。当运行denyhosts时,脚本会指定使用的是/usr/lib/python*这个路径的python里(暂时没找到脚本哪个地方指定),因此它无法定位python的版本,会出现这个错误。
      最快速的解决方法是把/usr/local/lib/python2.5/site-packages路径下的Denyhosts文件夹整个拷贝到2.4的安装目录下即可。
    进入/usr/local/lib/python2.5/site-packages/目录
#cd /usr/local/lib/python2.5/site-packages/
#cp –rp Denyhosts /usr/lib/python2.4/ site-packages/
这样之后便可以启动Denyhosts了。
参考:The root cause is that python was upgraded from 2.4 to 2.5. So now denyhosts (and other python scripts) are working from a different directory. Why does this make a difference? The DenyHosts python scripts are located under /usr/lib/python2.4/site-packages/DenyHosts which, unless the script specifies that it is using the old 2.4 environment (DenyHosts doesn’t) then it will look in the new environment location, /usr/lib/python2.5/site-packages/DenyHosts, which of course, doesn’t exist. The quick fix is to cp over the DenyHost directory to the python 2.5 tree.