- import zipfile
- import optparse, datetime, os
- from threading import Thread
- global i, ab
- i = 0 #记录测试次数
- ab = '1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM' #密码字符组
- def extractFile(zFile,password):
- try:
- zFile.extractall(pwd = password.encode('ascii')) #zFile.extractall 的输入字符必须转码
- print("[+] Found password" + password + "\n")
- endtime = datetime.datetime.now()
- # print('end time:', endtime)
- print('spend time: %s 秒 \n\n 完成校验密码, 程序退出.' % ((endtime - starttime).seconds))
- os._exit(0)
- except Exception as e:
- global i
- print('错误密码: %s' % password)
- i += 1
- def can(contune=1, pd=''):
- contune -= 1
- for a in ab:
- if contune:
- can(contune, pd+a) #递归传参
- password = pd + a
- t = Thread(target=extractFile, args=(zFile, password)) #创建多线程对象
- t.start()
- t.join()
- def main():
- global zFile, starttime
- parse = optparse.OptionParser("useage%prog" + "-f -d")
- parse.add_option("-f",dest="zname",type="string",help="specify zip file")
- parse.add_option("-d",dest="passdwlen",type="int",help="specify password length")
- (options,args) = parse.parse_args()
- if (options.zname == None) | (options.passdwlen == None):
- print(parse.usage)
- else:
- starttime = datetime.datetime.now()
- # print('start time:%s' % starttime)
- zname = options.zname
- pdlen = options.passdwlen
- zFile = zipfile.ZipFile(zname)
- a = 1
- while a<pdlen:
- can(a)
- a += 1
- if __name__=='__main__':
- main()
- endtime = datetime.datetime.now()
- # print('end time:', endtime)
- print('spend time: %s 秒 \n\n 无正确密码, 程序退出.' % ((endtime - starttime).seconds))
threading 库的更多用法:
https://www.cnblogs.com/hiwuchong/p/8673183.html
optparse 模块的更多用法:
https://www.cnblogs.com/darkpig/p/5677153.html
来源: http://www.bubuko.com/infodetail-3305032.html