递归和多线程demo


 

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 <zipfile> -d <passdwlen>")
    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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM