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
任务计划:(每分钟运行下该程序)
[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
Linux 利用hosts.deny 防止暴力破解ssh(转)的更多相关文章
- Linux 利用hosts.deny 防止暴力破解ssh
一.ssh暴力破解 利用专业的破解程序,配合密码字典.登陆用户名,尝试登陆服务器,来进行破解密码,此方法,虽慢,但却很有效果. 二.暴力破解演示 2.1.基础环境:2台linux主机(centos 7 ...
- Python脚本暴力破解SSH口令以及构建僵尸网络(pxssh)
目录 暴力破解SSH口令 SSH远端执行命令 构建僵尸网络 环境:Kali Linux python 2.7.13 暴力破解SSH口令 Pxssh是pexpect库的ssh专用脚本,他能用预先写好的 ...
- centos 7 DenyHosts 安装 防暴力破解ssh登陆
为了减少软件扫描ssh登陆 还是用这个比较好点 默认端口号22 也要改 登陆密码也不要使用 弱口令 123456 这样的 Description DenyHosts is a python prog ...
- 防止WordPress利用xmlrpc.php进行暴力破解以及DDoS
早在2012 年 12 月 17 日一些采用 PHP 的知名博客程序 WordPress被曝光存在严重的漏洞,该漏洞覆盖WordPress 全部已发布的版本(包括WordPress 3.8.1).该漏 ...
- 忘记秘密利用python模拟登录暴力破解秘密
忘记秘密利用python模拟登录暴力破解秘密: #encoding=utf-8 import itertools import string import requests def gen_pwd_f ...
- 10小时之内,暴力破解SSH账号的IP
10小时之内,暴力破解SSH账号的IP,IP 地址数据来源于 ip138.com 182.18.76.246 北京市昌平区 北京亿安天下网络科技有限公司 联通 221.223.200.143 北京市 ...
- 使用Medusa美杜莎暴力破解SSH密码
使用Medusa美杜莎暴力破解SSH密码 1.Medusa简介 Medusa(美杜莎)是一个速度快,支持大规模并行,模块化的爆力破解工具.可以同时对多个主机,用户或密码执行强力测试.Medusa和hy ...
- hydra暴力破解ssh服务器密码
概述 我都没想到,第一次暴力破解服务器密码.竟然是对自己的单位服务器出手..囧,因为还没来得及找测试部要来服务器登录密码,测试部负责人已经下班走了.后来又联系不上,这要更新代码,怎么办..于是就对测试 ...
- 防御暴力破解SSH攻击
托管在IDC的机器我们通常都用SSH方式来远程管理.但是经常可以发现log-watch的日志中有大量试探登录的 信息,为了我们的主机安全,有必要想个方法来阻挡这些可恨的"HACKER&quo ...
随机推荐
- sql: PL/SQL proc
A PL/SQL block has the following structure: [DECLARE declaration_statements ] BEGIN executable_state ...
- 例解 autoconf 和 automake 生成 Makefile 文件
本文介绍了在 linux 系统中,通过 Gnu autoconf 和 automake 生成 Makefile 的方法.主要探讨了生成 Makefile 的来龙去脉及其机理,接着详细介绍了配置 Con ...
- Android Folding View(折叠视图、控件)
版本号:1.0 日期:2014.4.21 版权:© 2014 kince 转载注明出处 非常早之前看过有人求助以下这个效果是怎样实现的, 也就是側滑菜单的一个折叠效果,事实上关于这个效果的实现,谷 ...
- OCP-1Z0-051-题目解析-第33题
33. You want to create an ORD_DETAIL table to store details for an order placed having the following ...
- EXT2/EXT3文件系统(一)
整理自<鸟哥的Linux私房菜>,整理者:华科小涛http://www.cnblogs.com/hust-ghtao/ 1.文件系统概念引入 文件系统是一种存储和组织计算机数据的方法,它使 ...
- winform基础——数据访问及几个案例
数据访问分为三个部分:(1)创建链接(2)创建与执行命令(3)读取或准备相关数据 一,需要引用的命名空间 using data: using data.SqlClient; 二,创建与数据库的链接—— ...
- JQuery+AJax - 无刷新使用验证码
最终效果: 项目目录: Default.aspx前端代码: <%@ Page Language="C#" AutoEventWireup="true" C ...
- 网页插入QQ 无需加好友
<p>联系方式:1073351325<a href="tencent://message/?Menu=yes&uin=1073351325&Site=dsf ...
- win 8.1 安装 SQL server 遇到的各种问题
企业版 SQL Server ed2k://|file|cn_sql_server_2012_enterprise_edition_x86_x64_dvd_813295.iso|5054384128| ...
- 移动开发的框架(用Firepower,不用listview,超快) good
我是通过http传送xml后台是阿帕奇的http server,后台可以用delphi或php 都可以.用post 刚才试了试自带的TNetHttpClient,感觉还好,代码封装也不算深,收发数据也 ...