码迷,mamicode.com
首页 > 其他好文 > 详细

暴力穷举zip压缩文件的密码

时间:2017-05-27 22:31:42      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:word   display   etl   nop   margin   pfile   else   import   append   

  生成密码的方式类似与时钟,末尾遍历完了第k位所有的字符,就让第k位的前一位到下一位字符,第k位回到第0个字符。

  对python还不太熟悉,效率比较低,但是能破解简单的密码。

import zipfile
#密码可能有的字符
testSetstr = "w.abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789#$%&‘()*+,-/:;<=>?@[\]^_`{|}~"
# testSetstr = "0123456789"
#
testSetLen = len(testSetstr)    #字符中长度
maxtestLen = 5                  #要猜的密码的最大长度,时间耗时越久。
filename = test.zip           #要解压的文件名
filedir = data/               #解压路径
def testPassword(teststr):
    r = zipfile.is_zipfile(filename)
    if r:
        fz = zipfile.ZipFile(filename,r)
        try:
            for file in fz.namelist():
                fz.extract(file,filedir,pwd=str.encode(teststr))
                print("密码是",teststr)
                return True
        except:
            pass
        fz.close()
    else:
        print(不是zip文件,不能解压)
        return True
    return False
def test(n):
    alist = [0] * n
    while(alist[0] < testSetLen):
        testlist = []
        for i in range(n):
            testlist.append(testSetstr[alist[i]])
        teststr = "".join(testlist)
        if(testPassword(teststr)):
            return True
        alist[n - 1] += 1
        for i in range(n-1,0,-1):
            if(alist[i] > testSetLen - 1):
                alist[i] = 0
                alist[i - 1] += 1
                if(i - 1 == 0):
                    print("进度 ",100.0 * alist[i - 1] / testSetLen ,"%")
            else:
                break
    return False
def main():
    for i in range(maxtestLen):
        print("正在测试长度为",i + 1,"的密码。")
        if(test(i + 1)):
            return
main()

 

技术分享

 

暴力穷举zip压缩文件的密码

标签:word   display   etl   nop   margin   pfile   else   import   append   

原文地址:http://www.cnblogs.com/youmuchen/p/6914510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!