linux 防 ssh 暴力破解→Denyhosts

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34461514/article/details/81082454

DenyHosts 是一个使用 Python2.3 编写的程序,它会分析 /var/log/secure 等日志文件。当发现同一 IP 在进行多次 SSH 密码尝试时就会记录 IP 到 /etc/hosts.deny 文件,从而达到自动屏蔽该 IP 的目的。

检查安装条件

  1. 先检查系统是否支持 tcp_wrappers

    [root@localhost ~]# ldd /usr/sbin/sshd | grep libwrap.so
    libwrap.so.0 => /lib64/libwrap.so.0 (0x00002ba28edcc000)

    如果没有任何显示,使用以下命令来解决

    [root@localhost ~]# yum install -y tcp_wrappers tcp_wrappers-libs
  2. 判断安装的 Python 版本

    [root@localhost ~]# python -V
    Python 2.7.4

    如果低于2.4,建议升级下

    [root@localhost ~]# yum update -y python

安装和配置 DenyHosts

  1. 安装 DenyHosts

    wget http://nchc.dl.sourceforge.net/project/denyhosts/denyhosts/2.6/DenyHosts-2.6.tar.gz
    tar xf DenyHosts-2.6.tar.gz
    cd DenyHosts-2.6
    python setup.py install

    以上命令的作用是:

    • 程序脚本自动安装到/usr/share/denyhosts
    • 库文件自动安装到/usr/lib/python2.3/site-packages/DenyHosts
    • denyhosts.py自动安装到/usr/bin
  2. 设置启动脚本

    cd /usr/share/denyhosts/
    grep -v "^#" denyhosts.cfg-dist > denyhosts.cfg
    cp daemon-control-dist denyhost
    chown root denyhost
    chmod 700 denyhost

    注意,上面第 3-5 句后的 denyhost 是自定义的文件名,不同的教程使用名字可能不同(笔者在此掉过坑)

    执行完以上命令后,修改 denyhosts.cfg 配置文件

    —————-denyhosts.cfg————————
    SECURE_LOG = /var/log/secure
    
    #ssh日志文件
    
    
    HOSTS_DENY = /etc/hosts.deny
    
    #将阻止IP写入到hosts.deny
    
    
    PURGE_DENY = 5m
    
    #过多久后清除已经禁止的,其中w代表周,d代表天,h代表小时,s代表秒,m代表分钟
    
    
    BLOCK_SERVICE = sshd
    
    #阻止服务名
    
    
    DENY_THRESHOLD_INVALID = 5
    
    #允许无效用户(在/etc/passwd未列出)登录失败次数,允许无效用户登录失败的次数.
    
    
    DENY_THRESHOLD_VALID = 5
    
    #允许普通用户登录失败的次数
    
    
    DENY_THRESHOLD_ROOT = 5
    
    #允许root登录失败的次数
    
    
    DENY_THRESHOLD_RESTRICTED = 1
    
    #设定 deny host 写入到该资料夹
    
    
    WORK_DIR = /usr/share/denyhosts/data
    
    #将deny的host或ip纪录到Work_dir中
    
    SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS = YES
    
    HOSTNAME_LOOKUP=YES
    
    #是否做域名反解
    
    
    LOCK_FILE = /var/lock/subsys/denyhosts
    
    #将DenyHOts启动的pid纪录到LOCK_FILE中,已确保服务正确启动,防止同时启动多个服务。
    
    
    ADMIN_EMAIL = [email protected]
    
    #设置管理员邮件地址
    
    
    SMTP_HOST = localhost
    SMTP_PORT = 25
    SMTP_FROM = DenyHosts <[email protected]>
    SMTP_SUBJECT = DenyHosts Report
    
    AGE_RESET_VALID=1d
    
    #有效用户登录失败计数归零的时间
    
    
    AGE_RESET_ROOT=1d
    
    #root用户登录失败计数归零的时间
    
    
    AGE_RESET_RESTRICTED=5d
    
    #用户的失败登录计数重置为0的时间(/usr/share/denyhosts/data/restricted-usernames)
    
    
    AGE_RESET_INVALID=10d
    
    #无效用户登录失败计数归零的时间
    
    
    DAEMON_LOG = /var/log/denyhosts
    
    #自己的日志文件
    
    —————-denyhosts.cfg————————
  3. 设置开机启动

    cd /etc/init.d
    ln -s /usr/share/denyhosts/denyhost dh
    chkconfig --add dh
    chkconfig dh on

    第 2 句的 denyhost 就是上面提到的自定义名字。 2-5 句中的 dh 也是自定义名字,千万不要同时看多个教程,否则可能会写岔了

启动

```linux
    service dh start
```

再次提示: dh 服务名是自定义的。

启动的时间可能有点长。

猜你喜欢

转载自blog.csdn.net/qq_34461514/article/details/81082454