【Python实战】---- 30行代码破解加密压缩包

1. 需求是这样的

老板发我一个加密压缩包,告诉我是6位数的数字密码,让我将压缩包解压!

2. 环境

  1. 命令解压工具 7-zip;7-zip下载
  2. python 自带的执行命令模块 subprocess;

3. 安装 7-zip 配置环境变量

  1. 安装 7-zip,找到 7-zip 的应用程序路径;
  2. 将该路径配置环境变量;参考菜鸟Windows 10 配置Java 环境变量
    在这里插入图片描述

4. 循环生成所有6位数的数字密码

for i in range(1000000):
    pwd = str(("%06d"%i))

5. 验证密码

  1. 组装验证密码的 7z 命令;
  2. 对密码进行验证,成功返回0;
  cmd = f'7z t -p{pwd} ./test.zip'
  status = subprocess.call(cmd)

6. 拿到正确密码解压

  1. 组装 7z 解压命令,将所有的解压文件输出到当前路径的all文件夹下;
  2. 执行解压命令;
	cmd = f'7z x -p{pwd} ./test.zip -y -aos -o"./all/"'
  	subprocess.call(cmd)

7. 全部代码

#!/usr/bin/env python
"""
@Author  :Rattenking
@Date    :2021/06/02 15:42
@CSDN	 :https://blog.csdn.net/m0_38082783
"""

import time
import subprocess

def get_current_time_stamp():
  times = time.time()
  time_stamp = int(round(times * 1000))
  return time_stamp

def verify_password(pwd):
  cmd = f'7z t -p{pwd} ./test.zip'
  status = subprocess.call(cmd)
  return status

def unzip_file_other_folder(pwd):
  print(f'正确的密码是:{pwd}')
  cmd = f'7z x -p{pwd} ./test.zip -y -aos -o"./all/"'
  subprocess.call(cmd)

def get_all_possible_password():
  for i in range(1000000):
    pwd = str(("%06d"%i))
    status = verify_password(pwd)
    if status == 0:
      unzip_file_other_folder(pwd)
      break

if __name__ == "__main__":
  start = get_current_time_stamp()
  get_all_possible_password()
  end = get_current_time_stamp()
  print(f"解压压缩包用时:{end - start}ms")

8. 执行结果

在这里插入图片描述
在这里插入图片描述

9. 总结

  1. 查找合适的命令解压工具;
  2. 熟悉解压工具命令;
  3. 执行命令,熟悉不同执行结果的返回状态。

Guess you like

Origin blog.csdn.net/m0_38082783/article/details/117334050