Python脚本破解压缩文件口令

Python中操作zip压缩文件的模块是 zipfile 。

相关文章:Python中zipfile压缩文件模块的使用

我们破解压缩文件的口令也是用的暴力破解方法。我们提前准备好密码字典用来爆破,如果密码字典中存在密码,则会打印出该密码,否则提示密码字典中无密码。

main()函数用来打开密码字典 key.txt ,然后读取其中每一行的内容,调用Test()函数去一个个的试密码,如果密码错误,Test函数就会抛出异常,如果密码正确,则不会抛出异常。所以我们在main()函数中以是否接收到异常判断密码是否正确。如果没有接收到异常,说明密码正确!

以下代码是python2.7环境!

# -*- coding: utf-8 -*-
"""
Created on Thu Nov  1 09:00:26 2018
@author: 小谢
"""

import zipfile
import os
def Test(line):
    try:
        with zipfile.ZipFile("c://users//17250//desktop//test.zip","r") as f:
            f.extractall("c://users//17250//desktop//",pwd=line)  #利用密码字典中的密码解压缩
    except Exception as e:
        return e
    finally:
        f.close()
def main():
    try:
        with open("c://users//17250//desktop//key.txt") as file:
            lines=file.readlines()
            for line in lines:
                line=line.strip("\n")
                e=Test(line)
                if e:
                    pass
                else:
                    print("************压缩文件的密码是:%s"%line)
                    return line
    except Exception as e:
        print("异常对象的类型是:%s"%type(e))
        print("异常对象的内容是:%s"%e)
    finally:
        file.close()
if __name__=='__main__':
    re=main()
    if re:
        pass
    else:
        print("对不起,密码字典中未匹配到密码!")

     

猜你喜欢

转载自blog.csdn.net/qq_36119192/article/details/83615327