使用DenyHosts阻止SSH暴力破解

CentOS, DenyHosts, SSH

公司服务器每天都会有上千的SSH失败尝试记录,一直不停的扫描,这些人真够无 聊的,没事吃饱了撑着,尽找些软件在那里穷举扫描(幸好服务器密码设置的够长够复杂)。
要一条一条将这些IP阻止显然治标不治本,于是找了个DenyHosts软件来代替的偶的手工活,吼吼:)
DenyHosts是Python语言写的一个程序,它会分析SSHD的日志文件,当发现重复的攻击时就会记录IP到/etc/hosts.deny文件,从而达到自动屏蔽IP的功能。
DenyHosts官方网站为: http://denyhosts.sourceforge.net
以下是安装记录(以CentOS 4.4, DenyHosts 2.6 为例)

# cd /usr/local/src# wget http://jaist.dl.sourceforge.net/sourceforge/denyhosts/DenyHosts-2.6.tar.gz
# tar -zxvf DenyHosts-2.6.tar.gz
# cd DenyHosts-2.6
# python setup.py install
默认是安装到/usr/share/denyhosts/目录的。
# cd /usr/share/denyhosts/# cp denyhosts.cfg-dist denyhosts.cfg
# vi denyhosts.cfg
这里根据自己需要进行相应的配置
# cp daemon-control-dist daemon-control# vi daemon-control
DENYHOSTS_BIN   = "/usr/bin/denyhosts.py"
DENYHOSTS_LOCK = "/var/lock/subsys/denyhosts"
DENYHOSTS_CFG   = "/usr/share/denyhosts/denyhosts.cfg"
# chown root daemon-control
# chmod 700 daemon-control
完了之后执行daemon-contron start就可以了。
# ./daemon-control start
如果要使DenyHosts每次重起后自动启动还需做如下设置:
# cd /etc/init.d
# ln -s /usr/share/denyhosts/daemon-control denyhosts
# chkconfig --add denyhosts
# chkconfig --level 2345 denyhosts on
或者修改/etc/rc.local文件:
# vi /etc/rc.local
加入下面这条命令
/usr/share/denyhosts/daemon-control start
DenyHosts配置文件说明:
SECURE_LOG = /var/log/secure
#sshd日志文件,它是根据这个文件来判断的,不同的操作系统,文件名稍有不同。
HOSTS_DENY = /etc/hosts.deny
#控制用户登陆的文件
PURGE_DENY = 5m
#过多久后清除已经禁止的
BLOCK_SERVICE = sshd
#禁止的服务名
DENY_THRESHOLD_INVALID = 1
#允许无效用户失败的次数
DENY_THRESHOLD_VALID = 10
#允许普通用户登陆失败的次数
DENY_THRESHOLD_ROOT = 5
#允许root登陆失败的次数
HOSTNAME_LOOKUP=NO
#是否做域名反解
DAEMON_LOG = /var/log/denyhosts
#DenyHosts的日志文件

效果:

May 13 13:17:37 web sshd[3863]: Failed password for huzi from ::ffff:192.168.0.145 port 2743 ssh2
May 13 21:17:44 web sshd[3862]: Failed password for huzi from ::ffff:192.168.0.145 port 2743 ssh2
May 13 13:17:44 web sshd[3863]: Failed password for huzi from ::ffff:192.168.0.145 port 2743 ssh2
May 13 21:17:48 web sshd[3862]: Failed password for huzi from ::ffff:192.168.0.145 port 2743 ssh2
May 13 13:17:48 web sshd[3863]: Failed password for huzi from ::ffff:192.168.0.145 port 2743 ssh2
May 13 21:18:04 web sshd[3862]: Failed password for huzi from ::ffff:192.168.0.145 port 2743 ssh2
May 13 13:18:04 web sshd[3863]: Failed password for huzi from ::ffff:192.168.0.145 port 2743 ssh2
May 13 13:18:04 web sshd[3863]: Disconnecting: Too many authentication failures for huzi

你可能感兴趣的:(ssh,破解,休闲,denyhosts,暴力)