网络编程
位置:首页>> 网络编程>> Python编程>> ​​​​​​​如何利用python破解zip加密文件

​​​​​​​如何利用python破解zip加密文件

作者:??蓝星部队????  发布时间:2022-11-27 17:51:30 

标签:python,破解,zip,加密,文件

前言:

日常工作中,会遇到一些加密的zip文件,但是因为某些原因或者时间过长,密码不知道了。但是zip文件中文件有很重要很必须。那么,我们试一试万能的Python,暴力破解密码。

一、破解zip加密文件的思路

  • 准备一个加密的zip文件。

  • zipfile模块可以解压zip文件。

解压时可以提供密码zfile.extractall("./", pwd=password.encode("utf8"))

  • itertools.permutations实现全字符的全排列。

通过函数itertools.permutations("abc", 3)实现全字符的全排列:abc/acb/bca/bac/cab/cba

二、实例代码演示

0、zip的压缩方式

本文介绍的zip文件知道密码一共是4位的,密码字符的范围是a-z1-0。并且不存在重复字符的,不会有“aabb”的密码。zip压缩时是选择了zip传统加密!

1、解压zip文件

导入zipfile模块,使用其中的extractall()函数。

import itertools
filename = "readme.zip"
# 创建一个解压的函数,入参为文件名和密码
# 并使用try-except,避免报错中断程序。
def uncompress(file_name, pass_word):
   try:
       with zipfile.ZipFile(file_name) as z_file:
           z_file.extractall("./", pwd=pass_word.encode("utf-8"))
       return True
   except:
       return False

2、实现密码字符的全排列

import zipfile
import itertools
filename = "readme.zip"
# 创建一个解压的函数,入参为文件名和密码
# 并使用try-except,避免报错中断程序。
def uncompress(file_name, pass_word):
   try:
       with zipfile.ZipFile(file_name) as z_file:
           z_file.extractall("./", pwd=pass_word.encode("utf-8"))
       return True
   except:
       return False
# chars是密码可能的字符集
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
for c in itertools.permutations(chars, 4):
   password = ''.join(c)
   print(password)
   result = uncompress(filename, password)
   if not result:
       print('解压失败。', password)
   else:
       print('解压成功。', password)
       break

文件压缩时,一些注意的事项: 

​​​​​​​如何利用python破解zip加密文件

三、密码是几位未知,也可以破解密码

查过一些资料,zip压缩文件密码最长为12位,在原来的程序上增加上一个for循环就可以实现破解密码了。

import zipfile
import itertools
filename = "readme.zip"
def uncompress(file_name, pass_word):
   try:
       with zipfile.ZipFile(file_name) as z_file:
           z_file.extractall("./", pwd=pass_word.encode("utf-8"))
       return True
   except:
       return False
chars = "abcdefghijklmnopqrstuvwxyz0123456789"
for i in range(12):
   for c in itertools.permutations(chars, i):
       password = ''.join(c)
       print(password)
       result = uncompress(filename, password)
       if not result:
           print('解压失败。', password)
       else:
           print('解压成功。', password)
           break

来源:https://juejin.cn/post/7091925727817236488

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com