zoukankan      html  css  js  c++  java
  • 用python暴力破解压缩文件并不是万能,至少这个场景我告诉你密码你用代码也破解不了

    看到论坛上各种贴子写用python进行暴力破解的文章,于是自己也想去尝试一下,不试不知道,一试吓一跳,真的就像那句有名的”python由入门到放弃“,把论坛上别人的脚本全部自己敲一遍,运行不报错,但也没有正常解压出来,然后就是全部拷下来运行,结果一样,不能正常解压。不知道在屏幕前的你看到我这篇文章有没有遇到同样问题,最后有没有解决掉。

    下面我也把我所遇到的问题代码贴出来及解决的办法,相信会对你有所帮助:

     1 # -*- coding:utf-8 -*-
     2 '''
     3 首先创建一个压缩包,压缩包密码是6位纯数字,该脚本的目的就是要正常解压出这个压缩包,我创建的压缩包解压密码为 000100,主要是能快速看到结果
     4 '''
     5 import zipfile
     6 
     7 class ForceUnlock():
     8 
     9     # 先生成6位数字的密码表并存入文件中
    10     def generate_password_table(self):
    11         with open("pwd_table.txt", 'w', encoding="utf-8") as f:
    12             for i in range(1000000):
    13                 pwd = str(i).zfill(6) + "
    "
    14                 f.write(pwd)
    15 
    16     def extract_file(self, zip_file, password):
    17         try:
    18             zip_file.extractall(pwd=bytes(password, "utf-8"))  # 注意这个密码是要以字节方式传入
    19             print("The extract password is:", password)
    20             return True
    21         except:
    22             print(("current {} password is not correct").format(password))
    23             return False  # 密码不对,继续破解
    24 
    25     def main(self):
    26         self.generate_password_table()
    27         zip_file = zipfile.ZipFile("strict_test.zip")
    28         with open("pwd_table.txt", 'r', encoding="utf-8") as f:
    29             pwd_list = f.readlines()
    30             for line in pwd_list:
    31                 pwd = line.strip("
    ")
    32                 if self.extract_file(zip_file, pwd):
    33                     break  # 密码正确后退出循环
    34                 else:
    35                     continue
    36 
    37 
    38 if __name__ == "__main__":
    39     test = ForceUnlock()
    40     test.main()

    运行了该代码无数次,都不能正常解压出来,于是单独把代码拧出来运行,尼玛竟然报密码不对

     1 import zipfile
     2 fn = zipfile.ZipFile("strict_test.zip")
     3 fn.extractall(pwd=b'100100')
     4 
     5 
     6 报错如下:
     7 Traceback (most recent call last):
     8   File "E:/05.Project/01.Python/Aziji-usefully/force_unlock_zipfile/d.py", line 10, in <module>
     9     fn.extractall(pwd=b'100100')
    10   File "C:Program FilesPython37libzipfile.py", line 1594, in extractall
    11     self._extract_member(zipinfo, path, pwd)
    12   File "C:Program FilesPython37libzipfile.py", line 1647, in _extract_member
    13     with self.open(member, pwd=pwd) as source, 
    14   File "C:Program FilesPython37libzipfile.py", line 1516, in open
    15     raise RuntimeError("Bad password for file %r" % name)
    16 RuntimeError: Bad password for file <ZipInfo filename='strict_test.txt' compress_type=deflate external_attr=0x20 file_size=9 compress_size=24>

    把这个错误在网上搜索了一翻,要么就是说密码那块输入的格式不对,要么就是编码用的不对,再找官方文档关于该extractall方法的使用介绍,也找不到任何能解决这个问题的方法。

    接下来对这个问题重新复盘一次:

    1、使用zipfile在文件加密再解密,能正常解压,说明加解密这它本身是没问题

    2、我是在window10 64bit上手动通过winrar创建的一个压缩包,并设置格式为zip,下面是创建步骤

     3、通过手动创建加密压缩包去用代码解压就不行,那么问题可能是下面的可能

      a),目前zipfile还不能很好的兼容windows端压缩解压方式,加解密方式不兼容

      b),暂时还没想到

    问题解决方法:

    有时候对一个问题过分的好奇,想着法都想把它搞定,尤其是搞定别人没有遇到过的问题,想想都高兴。于是重新把复盘里面步骤再试一次,在第二步创建压缩包时,在最后设置密码界面时好像跟在网上别人那看到的不一样,别人的都只有两个选项:显示密码和加密文件名,而我的却有三个,多了一个ZIP传统加密,难道多出来的这个选项就是为我这个问题而存在?想想都来劲,于是勾选上它再次运行代码,奇迹出来了,可以正常解压出来了,虽然问题是解决了,但这个选项是做啥用的,为什么会存在这个按钮,于是又去搜索了一翻,没搜索到关于这个ZIP传统加密有价值介绍,按目前我的测试来看,估计就是用来被破解的作用,哈哈。

  • 相关阅读:
    AtCoder Beginner Contest 167
    AtCoder Beginner Contest 166
    AtCoder Beginner Contest 165
    AtCoder Beginner Contest 164
    AtCoder Beginner Contest 163
    AtCoder Beginner Contest 162
    AtCoder Beginner Contest 161
    AtCoder Beginner Contest 160
    AtCoder Beginner Contest 159
    自定义Mybatis自动生成代码规则
  • 原文地址:https://www.cnblogs.com/aziji/p/12035085.html
Copyright © 2011-2022 走看看