zoukankan      html  css  js  c++  java
  • Linux 利用hosts.deny 防止暴力破解ssh

    一、ssh暴力破解

      利用专业的破解程序,配合密码字典、登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果。

    二、暴力破解演示

    2.1.基础环境:2台linux主机(centos 7系统)、Development Tools.

      主机ip:192.168.30.64 (服务器端)、192.168.30.63(客户端+ 暴力破解【Hydra】)

      在30.63上进行暴力破解30.64

    2.2 客户端上安装 破解程序 hydra。关于该程序的详情请去官网.

    安装该软件的依赖环境:

    [root@ceph-osd1 ~]# yum install openssl-devel pcre-devel ncpfs-devel postgresql-devel libssh-devel subversion-devel libncurses-devel -y

    下载hydra 软件(如果在linux 中无法直接wget下载的话,可以在浏览器(windows)中直接输入该链接去下载该软件)

    [root@ceph-osd1 ~]# wget https://www.thc.org/download.php?t=r&f=hydra-8.1.tar.gz

    解压、编译、安装(注意: 编译安装时需要注意看下有没有error类的错误,不然可能会导致hydra程序无法使用)

    [root@ceph-osd1 ~]# tar zxvf hydra-8.1.tar.gz
    [root@ceph-osd1 ~]# cd hydra-8.1
    [root@ceph-osd1 hydra-8.1]# ./configure
    [root@ceph-osd1 hydra-8.1]# make && make install

    正常安装的话,就可以使用了。

    2.3 hydra 常用命令详解

    [root@ceph-osd1 ~]# hydra
    Hydra v8.1 (c) 2014 by van Hauser/THC - Please do not use in military or secret service organizations, or for illegal purposes.
    
    Syntax: hydra [[[-l LOGIN|-L FILE] [-p PASS|-P FILE]] | [-C FILE]] [-e nsr] [-o FILE] [-t TASKS] [-M FILE [-T TASKS]] [-w TIME] [-W TIME] [-f] 
    [-s PORT] [-x MIN:MAX:CHARSET] [-SuvVd46] [service://server[:PORT][/OPT]] Options: -l LOGIN or -L FILE login with LOGIN name, or load several logins from FILE
     # -l 登陆用户名 或者 -L 登陆用户名列表文件
    -p PASS or -P FILE try password PASS, or load several passwords from FILE
     # -p 登陆密码 或者 -P 密码字典文件
    -C FILE colon separated "login:pass" format, instead of -L/-P options
    # 使用用户名:密码 组合的破解文件。
    -M FILE list of servers to attack, one entry per line, ':' to specify port
    # 指定目标列表文件一行一条记录
    -t TASKS run TASKS number of connects in parallel (per host, default: 16)
    # 同时运行的线程数目,默认是16个
    -U service module usage details
    # 服务模块使用详情
    -h more command line options (COMPLETE HELP)
    # 更多命令选项
    server the target: DNS, IP or
    192.168.0.0/24 (this OR the -M option)
    #支持扫描 域名、ip、以及网段
    service the service to crack (see below
    for supported protocols)
    # 破解扫描的协议
    OPT some service modules support additional input (
    -U for module help)
    # 更多扩展选项参看 -U 选项 Supported services: asterisk cisco cisco
    -enable cvs ftp ftps http[s]-{head|get} http[s]-{get|post}-form http-proxy http-proxy-urlenum icq
    imap[s] irc ldap2[s] ldap3[-{cram|digest}md5][s] mssql mysql(v4) nntp oracle-listener oracle-sid pcanywhere pcnfs pop3[s] postgres rdp redis
    rexec rlogin rsh s7-300 sip smb smtp[s] smtp-enum snmp socks5 ssh sshkey svn teamspeak telnet[s] vmauthd vnc xmpp Hydra is a tool to guess/crack valid login/password pairs. Licensed under AGPL v3.0. The newest version is always available at http://www.thc.org/thc-hydra Don't use in military or secret service organizations, or for illegal purposes. Example: hydra -l user -P passlist.txt ftp://192.168.0.1
    #示例: 用user 用户,加上 passlist.txt密码字典,尝试破解 192.168.0.1 ftp服务器

    2.4 测试破解 (在192.168.30.63上操作)

    创建一个目录,用来存放用户文件以及密码字典,并创建用users.txt、passwd.txt 文件

    [root@ceph-osd1 ssh-test]# pwd
    /root/ssh-test
    [root@ceph-osd1 ssh-test]# cat users.txt 
    root
    mysql
    ftp
    apache
    rsync
    tt
    admin
    [root@ceph-osd1 ssh-test]# cat passwd.txt 
    123456
    123
    admin
    123456789
    helloworld

    运行一下命令进行破解,可以看到倒数第二行,提示已经找到1个有效的密码。倒数第三行,便是有效的用户名和密码(如果在命令里加上-vV 选项的话,会输出更详细的破解运行的信息)

    [root@ceph-osd1 ssh-test]# hydra -L users.txt -P passwd.txt ssh://192.168.30.64
    Hydra v8.1 (c) 2014 by van Hauser/THC - Please do not use in military or secret service organizations, or for illegal purposes.
    
    Hydra (http://www.thc.org/thc-hydra) starting at 2016-05-03 19:04:33
    [WARNING] Many SSH configurations limit the number of parallel tasks, it is recommended to reduce the tasks: use -t 4
    [DATA] max 16 tasks per 1 server, overall 64 tasks, 35 login tries (l:7/p:5), ~0 tries per task
    [DATA] attacking service ssh on port 22
    [22][ssh] host: 192.168.30.64   login: root   password: 123456
    1 of 1 target successfully completed, 1 valid password found
    Hydra (http://www.thc.org/thc-hydra) finished at 2016-05-03 19:04:39

    运行-o 选项可以将得到的有效用户名和密码保存到文件中,如下:

    [root@ceph-osd1 ssh-test]# hydra -L users.txt -P passwd.txt -vV ssh://192.168.30.64 -o valid-info.txt
    [root@ceph-osd1 ssh-test]# cat valid-info.txt 
    # Hydra v8.1 run at 2016-05-03 19:08:14 on 192.168.30.64 ssh (hydra -L users.txt -P passwd.txt -vV -o valid-info.txt ssh://192.168.30.64)
    [22][ssh] host: 192.168.30.64   login: root   password: 123456

    以上就是暴力破解的过程,另外hydra 程序支持更多的协议,如ftp、web用户名登陆、cisco、pop3、rdp、telnet...... 等等等等。有兴趣的可以谷歌搜索下用法。不要用来做坏事啊,防患于未然。

    三、如何防止自己的服务器被ssh暴力破解呢?

    3.1 这里利用到了linux 系统的日志,不知道大家发现没,每次我们登陆服务器时,如果有登陆认证失败的情况,会在服务器的/var/log/secure文件中记录日志。错误日志如下:

    [root@test deny]# tail -3f /var/log/secure
    May  3 19:14:49 test sshd[23060]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.30.63  user=root
    May  3 19:14:49 test sshd[23060]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
    May  3 19:14:51 test sshd[23060]: Failed password for root from 192.168.30.63 port 50704 ssh2

    通过上面的日志我们可以看出,是30.63这台主机ssh登陆失败了,那假如它登陆失败次数过多后,我们能否禁止它登陆呢?

    3.2 这里用到了linux 的hosts.deny(利用tcp_wrappers)文件。参考文章:我本善良

    hosts.deny介绍:一般來說linux的密碼我們會用shadow來保護。電子郵件大概有人也會說使用PGP,但是一般的網路連線呢?可能會有人舉手回答說防火牆,那防火牆跟tcp_wrappers有什麼關係呢?筆者個人認為,如果說Firewall是第一道防線的話,第二道防線大概就是tcp_wrappers了,我們可以利用不同的防護程式來增加防護的能力之外,並且還能增加被駭客破台的困難度。設計一個完善的防火牆規則本來就不是一件很容易的事情,但是我們能透過簡單的程式化困難為容易,讓新手更能快樂的接觸美麗的Linux新世界。

    3.3 利用脚本配合任务计划去定时检测 /var/log/secure 文件,当发现有大量失败日志后,就将该ip追加到hosts.deny中,以达到防止暴力破解的目的。(在192.168.30.64 服务器上操作)

    脚本内容:

    [root@test deny]# pwd
    /root/deny
    [root@test deny]# vim autoDeny.sh
    #!/bin/bash
    
    LIST=""
    
    #过滤出协议,尝试连接主机的ip
    LIST=$(cat /var/log/secure | grep "authentication failure" | awk '{print$14}' | sed -e 's/rhost=//g' -e 's/ /_/g' | uniq)
    
    #Trusted Hosts
    excludeList=( "192.168.30.55" )
    
    function chkExcludeList()
    {
    for j in "${excludeList[@]}"; do
        if [[ "$1" == $j ]]; then
            return 10
        fi
    done
    return 11
    }
    
    #检查并追加到hosts.deny文件中
    for i in $LIST; do
        chkExcludeList "$i"
            if [ $? != "10" ]; then
                if [ "$(grep $i /etc/hosts.deny)" = "" ]; then
                    echo "ALL: $i : DENY" >> /etc/hosts.deny
                fi
            fi
    done
    View Code

    任务计划:(每分钟运行下该程序)

    [root@test deny]# crontab -l
    */1 * * * * /usr/bin/sh /root/deny/autoDeny.sh

    重启crond服务

    [root@test deny]# systemctl restart crond

    我们先查看下/etc/hosts.deny文件,默认里面的内容应该是空的,如下

    [root@test deny]# tail -2f /etc/hosts.deny
    #        See 'man tcpd' for information on tcp_wrappers
    #

    3.4 在客户端上进行破解。(192.168.30.63上操作)

    第一次运行破解命令

    第二次在继续操作时出现连接端口失败的提示(已经被服务器加入deny拒绝了。)

    可以去服务器上去检查下hosts.deny文件内容。看下有没有把客户端的ip 追加到里面

     说明该脚本运行正常,可以拒绝那些想暴力破解我们服务器的ip。

    四、下面讲下如何利用DenyHosts 软件更智能的保护linux 系统,官网

    4.1 介绍:摘自wiki 

      DenyHosts是针对SSH服务器的一个基于日志的入侵预防安全工具,是用Python编写的。其通过监测身份验证登录日志中失败的登录尝试,屏蔽这些登录者的IP地址,从而预防对SSH服务器的暴力破解。DenyHosts是由Phil Schwartz开发的,他也是Kodos正则表达式调试器的开发者。

      原理:DenyHosts通过监测身份验证登录日志的末尾,来获取近期失败的登录尝试信息。DenyHosts记录了有关登录者IP地址的信息,并将失败的登录尝试次数与用户指定的阈值进行比较。如果失败的登录尝试次数太多,DenyHosts假定发生了字典式攻击,并通过将IP地址添加到服务器上的/etc/hosts.deny,屏蔽相关IP,防止其进一步的攻击。DenyHosts 2.0及以上的版本支持集中同步,从而可以阻止攻击过多台电脑的惯犯。 从多台计算机,DenyHosts 2.0 及以上支持集中同步,所以重复的罪犯将被阻止。denyhosts.net网站从运行了该软件的计算机上收集统计信息。

    4.2 该程序依然安装在服务器上,在192.168.30.64上操作

    下载解压安装:由于是python 程序,需要使用python 安装,并且有python 环境,现在的linux 系统默认有python环境,如果没有的话,需要yum install -y python

    [root@test ]# wget http://sourceforge.net/projects/denyhosts/files/denyhosts/2.6/DenyHosts-2.6.tar.gz
    [root@test ]# tar -zxvf DenyHosts-2.6.tar.gz
    [root@test DenyHosts-2.6]# cd DenyHosts-2.6
    [root@test DenyHosts-2.6]# python setup.py install
    [root@test DenyHosts-2.6]# cd /usr/share/denyhosts/

    看下当前目录中的文件

    [root@test denyhosts]# ls
    CHANGELOG.txt  daemon-control-dist  data  denyhosts.cfg-dist  LICENSE.txt  plugins  README.txt  scripts  setup.py

    daemon-control-dist 文件就是 该程序的启动停止脚本,是用python语言编写的。我们把它加入到开机自启。

    [root@test denyhosts]# cp /usr/share/denyhosts/daemon-control-dist /etc/init.d/denyhosts
    [root@test denyhosts]# chkconfig denyhosts on
    将命令加入到系统命令里,方便使用
    [root@test denyhosts]# cd /bin
    [root@test bin]# ln -s /usr/share/denyhosts/daemon-control-dist denyhosts
    [root@test bin]# cd ~
    测试下:
    [root@test ~]# denyhosts
    Usage: /usr/bin/denyhosts {start [args...] | stop | restart [args...] | status | debug | condrestart [args...] }
    
    For a list of valid 'args' refer to:
    $ denyhosts.py --help

    启动命令搞定后,我们编辑下denyhosts的配置文件。文件存放在 /usr/share/denyhosts/denyhosts.cfg.内容如下:

    [root@test ~]# vim /usr/share/denyhosts/denyhosts.cfg
    #ssh的日志文件
    SECURE_LOG = /var/log/secure
    #把检测到的ip写到hosts.deny文件
    HOSTS_DENY = /etc/hosts.deny
    #多久清除已经禁止的ip,如1h 代表1小时;w周,d天,s秒,m分钟
    PURGE_DENY = 1h
    #服务名称
    BLOCK_SERVICE  = sshd
    
    #无效用户名尝试的次数
    DENY_THRESHOLD_INVALID = 1
    #有效的用户名尝试的次数 (不包括root)
    DENY_THRESHOLD_VALID = 8
    # 允许root 用户尝试的次数
    DENY_THRESHOLD_ROOT = 3
    
    DENY_THRESHOLD_RESTRICTED = 1
    #记录拒绝的host 到 该目录中
    WORK_DIR = /usr/share/denyhosts/data
    #开启可疑的主机报告
    SUSPICIOUS_LOGIN_REPORT_ALLOWED_HOSTS=YES
    
    #开启域名解析
    HOSTNAME_LOOKUP=YES
    #软件的lock文件
    LOCK_FILE = /var/lock/subsys/denyhosts
    #下面是设置邮局报警,邮件接收拒绝的主机
    ADMIN_EMAIL = 
    SMTP_HOST = localhost
    SMTP_PORT = 25
    SMTP_FROM = DenyHosts <nobody@localhost>
    SMTP_SUBJECT = DenyHosts Report
    #有效用户登录失败计数归零的时间
    AGE_RESET_VALID=5d
    #root用户登录失败计数归零的时间
    AGE_RESET_ROOT=25d
    #普通用户的失败登录计数重置为0的时间
    AGE_RESET_RESTRICTED=25d
    #无效用户登录失败计数归零的时间
    AGE_RESET_INVALID=10d
    
    #denyhosts程序的日志文件
    DAEMON_LOG = /var/log/denyhosts
     
    DAEMON_SLEEP = 30s
    DAEMON_PURGE = 1h
    View Code

    编辑配置完成后,就可以启动服务器了,直接运行denyhosts start 或者停止denyhosts stop 等。或者直接输入denyhosts查看帮助

    [root@test ~]# denyhosts start
    starting DenyHosts:    /usr/bin/env python /usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg

    检查下服务日志,程序进程pid 3704

    [root@test ~]# tail -f /var/log/denyhosts 
    2016-05-04 17:39:28,300 - denyhosts   : INFO     launching DenyHosts daemon (version 2.6)...
    2016-05-04 17:39:28,305 - denyhosts   : INFO     DenyHosts daemon is now running, pid: 3704
    2016-05-04 17:39:28,305 - denyhosts   : INFO     send daemon process a TERM signal to terminate cleanly
    2016-05-04 17:39:28,305 - denyhosts   : INFO       eg.  kill -TERM 3704
    2016-05-04 17:39:28,306 - denyhosts   : INFO     monitoring log: /var/log/secure
    2016-05-04 17:39:28,306 - denyhosts   : INFO     sync_time: 3600
    2016-05-04 17:39:28,306 - denyhosts   : INFO     daemon_purge:      3600
    2016-05-04 17:39:28,306 - denyhosts   : INFO     daemon_sleep:      30
    2016-05-04 17:39:28,306 - denyhosts   : INFO     purge_sleep_ratio: 120
    2016-05-04 17:39:28,306 - denyhosts   : INFO     denyhosts synchronization disabled

    为了下面的验证,我们先停止服务,然后加上debug选项以调试模式启动 denyhosts 服务,如下:

    [root@test ~]# denyhosts start
    starting DenyHosts:    /usr/bin/env python /usr/bin/denyhosts.py --daemon --config=/usr/share/denyhosts/denyhosts.cfg
    [root@test ~]# denyhosts debug
    sent DenyHosts SIGUSR1

    4.3 验证,在客户端上进行操作(192.168.30.63上):

    4.4 服务器上的denyhosts 日志文件,以及hosts.deny文件已经记录了,如下


    hosts.deny文件

    4.5 另外我也尝试了用无效的用户名尝试登陆,可以先另外找台客户端,直接使用不存在的用户去尝试登陆,便会在第一次登陆后就会被加入到hosts.deny文件中。软件还有其它功能待学习。如有疑问,可以随时留言交流哈。下班了。(•ω•`)o

    五、注意事项

    1.上述的实验中,可能会存在很快的被扫描到账号和密码,没有起到防止暴力的作用,那是因为我的密码文件已经在里面了,而且用户名及密码就几个。但是在真实环境中,黑客不可能那么快就扫描到,除非你的密码弱爆了。。。

    2.上述实验中的软件来自于互联网。

    本文章属于原创,如有转载请注明出处。

  • 相关阅读:
    第五章4
    第五章3
    第五章2
    第五章1
    第四章14
    第四章13
    第四章12
    第四章11
    第五章例5-6
    第五章例5-4
  • 原文地址:https://www.cnblogs.com/hanyifeng/p/5456317.html
Copyright © 2011-2022 走看看