脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - ​​​​​​​如何利用python破解zip加密文件

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

2023-02-14 11:06蓝星部队 Python

这篇文章主要介绍了​​​​​​​如何利用python破解zip加密文件,文章基于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()函数。

?
1
2
3
4
5
6
7
8
9
10
11
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、实现密码字符的全排列

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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循环就可以实现破解密码了。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

总结

此方法可以是实现破解zip文件的密码,python可以完成一些好玩的事情。

到此这篇关于如何利用python破解zip加密文件的文章就介绍到这了,更多相关python破解加密文件内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://juejin.cn/post/7091925727817236488

延伸 · 阅读

精彩推荐