English |
  • 美国VPS主机推荐
  • |
  • 代购服务
  • |
  • 10美元以下VPS
  • |
  • VPS新手指南/教程
  • |
  • 留言板
  • |
  • 关于
  • | 登录 |

    Linux VPS上DenyHosts阻止SSH暴力攻击

    2009年07月23日 下午 | 作者:VPS侦探

    现在的互联网非常不安全,很多人没事就拿一些扫描机扫描ssh端口,然后试图连接ssh端口进行暴力破解(穷举扫描),所以建议vps主机的空间,尽量设置复杂的ssh登录密码,虽然在前段时间曾经介绍过Linux VPS禁止某个IP访问使用hosts.deny禁止某些IP访问,但是功能方面欠缺,如:不能自动屏蔽,那么有什么更好的办法吗,就可以使用denyhosts这款软件了,它会分析/var/log/secure(redhat,Fedora Core)等日志文件,当发现同一IP在进行多次SSH密码尝试时就会记录IP到/etc/hosts.deny文件,从而达到自动屏蔽该IP的目的。

    DenyHosts官方网站为:http://denyhosts.sourceforge.net/

    LNMP一件安装包中自带该软件可以一键安装,命令:wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5/tools/ && ./denyhosts.sh 回车确认即可开始安装配置,不需要下面的步骤进行安装配置。(该tools目录下也有denyhosts相似的工具fail2ban的一键安装工具 ./fail2ban.sh 安装即可)

    1、下载DenyHosts 并解压

    # wget http://soft.vpser.net/security/denyhosts/DenyHosts-2.6.tar.gz
    # tar zxvf DenyHosts-2.6.tar.gz
    # cd DenyHosts-2.6

    2、安装、配置和启动

    安装前建议执行:echo "" > /var/log/secure && service rsyslog restart 清空以前的日志并重启一下rsyslog

    # python setup.py install
    因为DenyHosts是基于python的,所以要已安装python,大部分Linux发行版一般都有。默认是安装到/usr/share/denyhosts/目录的,进入相应的目录修改配置文件

    # cd /usr/share/denyhosts/
    # cp denyhosts.cfg-dist denyhosts.cfg
    # cp daemon-control-dist daemon-control

    默认的设置已经可以适合centos系统环境,你们可以使用vi命令查看一下denyhosts.cfg和daemon-control,里面有详细的解释
    接着使用下面命令启动denyhosts程序
    # chown root daemon-control
    # chmod 700 daemon-control
    # ./daemon-control start

    如果要使DenyHosts每次重起后自动启动还需做如下设置:
    # ln -sf /usr/share/denyhosts/daemon-control /etc/init.d/denyhosts
    # chkconfig --add denyhosts
    # chkconfig --level 2345 denyhosts on
    或者执行下面的命令加入开机启动,将会修改/etc/rc.local文件:
    # echo "/usr/share/denyhosts/daemon-control start" >> /etc/rc.local

    DenyHosts配置文件/usr/share/denyhosts/denyhosts.cfg说明:

    SECURE_LOG = /var/log/secure
    #sshd日志文件,它是根据这个文件来判断的,不同的操作系统,文件名稍有不同。

    HOSTS_DENY = /etc/hosts.deny
    #控制用户登陆的文件

    PURGE_DENY = 5m
    DAEMON_PURGE = 5m
    #过多久后清除已经禁止的IP,如5m(5分钟)、5h(5小时)、5d(5天)、5w(5周)、1y(一年)

    BLOCK_SERVICE  = sshd
    #禁止的服务名,可以只限制不允许访问ssh服务,也可以选择ALL

    DENY_THRESHOLD_INVALID = 5
    #允许无效用户失败的次数

    DENY_THRESHOLD_VALID = 10
    #允许普通用户登陆失败的次数

    DENY_THRESHOLD_ROOT = 5
    #允许root登陆失败的次数

    HOSTNAME_LOOKUP=NO
    #是否做域名反解

    DAEMON_LOG = /var/log/denyhosts

    为防止自己的IP被屏蔽,可以:echo "你的IP" >>  /usr/share/denyhosts/allowed-hosts 将你的IP加入白名单,再重启DenyHosts:/etc/init.d/denyhosts ,如果已经被封,需要先按下面的命令删除被封IP后再加白名单。

    如有IP被误封,可以执行下面的命令解封:wget http://soft.vpser.net/security/denyhosts/denyhosts_removeip.sh && bash denyhosts_removeip.sh 要解封的IP

    更多的说明请查看自带的README文本文件,好了以后维护VPS就会省一些心了,但是各位VPSer们注意了安全都是相对的哦,没有绝对安全,将密码设置的更Strong,并请定期或不定期的检查你的VPS主机,而且要定时备份你的数据哦。

    与DenyHosts类似的软件还有fail2ban功能上更多,还可以对ftp进行保护,自己可以搜索看一下。

    >>转载请注明出处:VPS侦探 本文链接地址:https://www.vpser.net/security/denyhosts.html
    VPS侦探推荐:
    遨游主机VultrLinode搬瓦工LOCVPSKVMLAHOSTKVMHostXen80VPS美国VPS主机,国内推荐腾讯云阿里云
    欢迎加入VPS侦探论坛交流:https://bbs.vpser.net

    发表评论

    *必填

    *必填 (不会被公开)

    评论(20条评论)

    1. VPS侦探说道:

      centos8 变动比较大,默认没python,可以执行:dnf install python2
      alternatives --set python /usr/bin/python2
      pip2 install ipaddr
      后再进行安装

    2. 大大熊说道:

      CentOS 8 安装 Denyhosts 出现如下错误,请问是哪里出问题?

      Press any key to start...or Press Ctrl+c to cancel

      Last metadata expiration check: 0:02:00 ago on Sun 22 Mar 2020 02:13:00 PM CST.
      No match for argument: python
      There are following alternatives for "python": python2, python36
      Package rsyslog-8.37.0-13.el8.x86_64 is already installed.
      No match for argument: python-ipaddr
      Error: Unable to find a match: python python-ipaddr
      Redirecting to /bin/systemctl restart rsyslog.service
      Downloading...
      denyhosts-3.1.tar.gz [found]
      ../include/main.sh: line 430: cd: /src: No such file or directory
      Uncompress denyhosts-3.1.tar.gz...
      cd denyhosts-3.1...
      Installing...
      ./denyhosts.sh: line 33: python: command not found
      Copy files...
      cp: cannot stat '/usr/bin/daemon-control-dist': No such file or directory
      chown: cannot access '/usr/bin/daemon-control': No such file or directory
      chmod: cannot access '/usr/bin/daemon-control': No such file or directory
      cp: cannot stat '/usr/bin/daemon-control': No such file or directory
      sed: can't read /etc/init.d/denyhosts: No such file or directory
      sed: can't read /etc/init.d/denyhosts: No such file or directory
      Add denyhosts service at system startup...
      error reading information on service denyhosts: No such file or directory
      error reading information on service denyhosts: No such file or directory
      Start DenyHosts...
      ./denyhosts.sh: line 82: /etc/init.d/denyhosts: No such file or directory

    3. VPS侦探说道:

      @xiao, 这个就是官网的方法用脚本来实现的,测试看是没问题,能正常移除ip。lnmp安装包tools目录下也有,你也可以直接用安装包里的试试

    4. xiao说道:

      楼主好,

      你文中提到的“wget http://soft.vpser.net/security/denyhosts/denyhosts_removeip.sh && bash denyhosts_removeip.sh 要解封的IP”解封ip的方法好像不行了,试过报错了。

      官方文档的方法需要手动删除多个文件中的ip记录,http://denyhosts.sourceforge.net/faq.html#3_19。