zoukankan      html  css  js  c++  java
  • Python基础之破解加密压缩包

    在日常工作生活中,经常用到压缩文件,有些为了安全保密,还专门设置了密码,如果忘记密码要怎么破,这时暴力破解就派上了用场,本文以一个简单的小例子,简述如何通过Python中的zipfile模块进行破解,仅供学习分享使用,如有不足之处,还请指正。

    准备工作

    在本例中,首先准备一个带密码的zip压缩包,采用winrar进行压缩,如下所示:

    设置zip文件密码,如下所示:

    注意:一定要采用zip传统加密,否则python的zipfile模块将无法解压成功。

    破解步骤

    1. 下载密码字典

    本例采用密码字典+多线程方式进行破解,首先需要下载密码字典,密码字典包含常用的密码,有多个文件,所有需要采用多线程方式,以提高破解效率。密码字典格式如下:

    2. 导入模块文件

    需要导入zipfile模块,及多线程相关和文件目录相关模块,如下所示:

    1 import zipfile
    2 import time
    3 import threading
    4 import os.path
    5 import os

    3. 单个密码解压函数

    通过zipfile对象的extractall可以进行解压,解压成功,则停止;否则,继续。如下所示:

     1     def extract(self, file, password):
     2         try:
     3             self.threadLock.acquire()
     4             if self.is_running:
     5                 # password = str(password)
     6                 zfile = zipfile.ZipFile(file, mode='r')
     7                 zfile.extractall(path=".", pwd=password.encode(encoding='utf-8'))
     8                 print("the password is {}".format(password))
     9                 end_time = time.time()
    10                 print('the end time is {}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))))
    11                 print("spend time is {}".format(end_time - self.start_time))
    12                 # 成功解压其余线程终止
    13                 self.is_running = False
    14                 zfile.close()
    15         except Exception as e:
    16             print('尝试密码:{},不对'.format(password))
    17             # print(e)
    18         finally:
    19             self.threadLock.release()

    4. 遍历单个密码字典文件 

    遍历单个密码字典文件,并调用解压函数进行解压,如下所示:

     1     def single_pwd_file(self, root, pwd_file):
     2         """单个密码本破解"""
     3         file = os.path.abspath("django.zip")
     4         print(file)
     5         pwd_file = os.path.abspath(os.path.join(root, pwd_file))
     6         print("遍历{}文件".format(pwd_file))
     7         try:
     8             with open(pwd_file, mode='r', encoding='utf-8') as f:
     9                 pwd = f.readline()
    10                 while pwd:
    11                     if self.is_running:
    12                         self.extract(file, pwd.strip())
    13                     else:
    14                         break
    15                     pwd = f.readline()
    16         except Exception as e:
    17             pass

    5. 遍历所有密码字典文件

    遍历所有密码字典文件,每一个字典文件,采用一个线程,如下所示:

     1     def start(self):
     2         """通过密码本破解"""
     3         self.start_time = time.time()
     4         print('the start time is {}'.format(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))))
     5         root = r"wpa2pojiezidian"
     6         for root, dirs, files in os.walk(root, topdown=True):
     7             for pwd_file in files:
     8                 if pwd_file.endswith('.txt') or pwd_file.endswith('.TXT'):
     9                     if self.is_running:
    10                         t = threading.Thread(target=self.single_pwd_file, args=(root, pwd_file,))
    11                         t.start()
    12                         # t.join()
    13                     else:
    14                         break

    破解示例

    本例为了测试,采用的密码比较简单,所以破解比较快,如下所示:

    源码下载链接

    为何一定要勾选传统加密?

    默认情况下,WinRAR在CTR模式下使用AES-256加密ZIP存档。虽然AES-256比ZIP 2.0传统加密算法安全得多,但它可能与一些较旧的解压软件不兼容。如果需要与这些工具兼容,可以在密码对话框中启用“ZIP传统加密”选项,或在命令行模式下使用-mezl开关。              

    Python标准库中的zipfile模块仅支持CRC32加密的zip文件。

    一定能暴力破解吗?

    采用密码字典文件的方式进行破解,密码字典只是收录了常规的密码,如果加密密码正好不在密码字典文件中,则无法破解。暴力破解,通俗的讲就是逐个密码取尝试,有可能需要破解几天,甚至更长时间,才可能会成功。所以技术理论上可行,但实际上可行性并不高。

    备注

    苏幕遮·燎沉香

    【作者】周邦彦【朝代】宋

    燎沉香,消溽暑。鸟雀呼晴,侵晓窥檐语。叶上初阳干宿雨、水面清圆,一一风荷举。

    故乡遥,何日去。家住吴门,久作长安旅。五月渔郎相忆否。小楫轻舟,梦入芙蓉浦。

    意境唯美的古诗词,让人心醉


    作者:Alan.hsiang
    出处:http://www.cnblogs.com/hsiang/
    本文版权归作者和博客园共有,写文不易,支持原创,欢迎转载【点赞】,转载请保留此段声明,且在文章页面明显位置给出原文连接,谢谢。
    关注个人公众号,定时同步更新技术及职场文章

  • 相关阅读:
    ValueError: Layer conv2d_1 was called with an input that isn't a symbolic tensor. Received type: <class 'tuple'>. Full input: [(600, 600, 3)]. All inputs to the layer should be tensors.
    OSError: `pydot` failed to call GraphViz.Please install GraphViz (https://www.graphviz.org/) and ensure that its executables are in the $PATH.该错误的解决
    numpy操作1--任务一:单通道图像转三通道理解/任务二:按axis=0或axis=1或axis=2(轴)堆叠(stack)
    探究灰度图像对目标检测测试结果影响----RGB转灰度图像、灰度图像扩充成三通道
    git项目上传github
    jupyter中,ipynb文件转pdf文件
    面试-操作系统
    数据库-面试
    面试—计算机网络
    PE文件结构
  • 原文地址:https://www.cnblogs.com/hsiang/p/15354930.html
Copyright © 2011-2022 走看看