[关闭]
@henri001 2017-07-11T02:40:57.000000Z 字数 1902 阅读 277

Centos安装denyhosts防止ssh暴力破解

安全


防止ssh暴力破解有很多方法,这里介绍安装denyhosts的方法。

环境检查

  1. ldd /usr/sbin/sshd|grep libwrap  //查看libwrap动态链接库文件。
  2. libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f4b2a1b9000)
  3. python -V             //查询版本为2.6.6
  4. Python 2.6.6

安装步骤

  1. cd download
  2. wget http://ncu.dl.sourceforge.net/sourceforge/denyhosts/DenyHosts-2.6.tar.gz
  3. tar -xzvf DenyHosts-2.6.tar.gz
  4. cd DenyHosts-2.6
  5. python setup.py install   //安装Denyhost
  6. cd /usr/share/denyhosts/ //切换目录进入/usr/share/denyhosts目录
  7. cp denyhosts.cfg-dist denyhosts.cfg //备份配置文件

DenyHosts参数配置

denyhosts.cfg并找到相应的项进行修改。

  1. SECURE_LOG = /var/log/secure ##ssh日志文件,是根据这个文件来判断的
  2. # format is: i[dhwmy]
  3. # Where i is an integer (eg. 7)
  4. # m = minutes
  5. # h = hours
  6. # d = days
  7. # w = weeks
  8. # y = years
  9. #
  10. # never purge:
  11. PURGE_DENY=50m ##过多久之后清除已禁止的
  12. HOSTS_DENY=/etc/hosts.deny ##控制用户登录的文件
  13. BLOCK_SERVICE=sshd ##禁止的服务名
  14. DENY_THRESHOLD_INVALID=3 ##允许无效用户登录失败的次数
  15. DENY_THRESHOLD_VALID=3 ##允许普通用户登录失败的次数
  16. DENY_THRESHOLD_ROOT=3 ##允许root用户登录失败的次数
  17. AGE_RESET_VALID=5d #用户的登录失败计数会在多久以后重置为0,(h表示小时,d表示天,m表示月,w表示周,y表示年)
  18. AGE_RESET_ROOT=25d
  19. AGE_RESET_RESTRICTED=25d
  20. AGE_RESET_INVALID=10d
  21. RESET_ON_SUCCESS = yes #如果一个ip登陆成功后,失败的登陆计数是否重置为0
  22. DAEMON_LOG = /var/log/denyhosts #自己的日志文件
  23. DAEMON_SLEEP = 30s #当以后台方式运行时,每读一次日志文件的时间间隔。
  24. DAEMON_PURGE = 50m #当以后台方式运行时,清除机制在 HOSTS_DENY 中终止旧条目的时间间隔,这个会影响PURGE_DENY的间隔。

DenyHosts启动文件配置

  1. cp daemon-control-dist daemon-control
  2. chown root daemon-control
  3. chmod 700 daemon-control
  4. ./daemon-control start ##启动DenyHosts
  5. ln -s /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts ##建立链接
  6. chkconfig --add denyhosts ##增加denyhosts服务进程
  7. chkconfig denyhosts on ##设置开机启动denyhosts
  8. chkconfig --list denyhosts ##查看是否生效

至此基本就好了,接下来就看看你的异常日志吧

  1. Oct 12 15:56:47 virCentos sshd[1935]: Failed password for root from 192.168.5.140 port 60436 ssh2
  2. Oct 12 15:56:52 virCentos sshd[1935]: Failed password for root from 192.168.5.140 port 60436 ssh2
  3. Oct 12 15:56:53 virCentos sshd[1936]: Received disconnect from 192.168.5.140: 0:
  4. Oct 12 15:56:53 virCentos sshd[1935]: PAM 4 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.5.140 user=root
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注