Roommate’s Zip encrypted file exploration, Python solves the Zip encrypted file exploration secret!

I found an encrypted zip package on my old computer at home. Because I forgot the password after too long, I vaguely remember that the password is 6 letters and numbers. I downloaded a lot of software to crack the password but it didn’t work. So I thought of using it myself. Python writes a script to brute force the password.

Python has a built-in module zipfile that can do this, test a wave, a test file, set the decompression password to 123.

Many people learn python and don't know where to start.
Many people learn python and after mastering the basic grammar, they don't know where to find cases to get started.
Many people who have done case studies do not know how to learn more advanced knowledge.
For these three types of people, I will provide you with a good learning platform, free to receive video tutorials, e-books, and course source code! ??¤
QQ group: 232030553

import zipfile  
# 创建文件句柄  
file = zipfile.ZipFile("测试.zip", 'r')  
# 提取压缩文件中的内容,注意密码必须是bytes格式,path表示提取到哪  
file.extractall(path='.', pwd='123'.encode('utf-8')) 

The running effect is shown in the figure below, and the extraction is successful.

Well, I started to crack the password of the old file. In order to improve the speed, I added the original code of multithreading:

import zipfile  
import itertools  
from concurrent.futures import ThreadPoolExecutor  
def extract(file, password):  
    if not flag: return  
    file.extractall(path='.', pwd=''.join(password).encode('utf-8'))  
def result(f):  
    exception = f.exception()  
    if not exception:  
        # 如果获取不到异常说明破解成功  
        print('密码为:', f.pwd)  
        global flag  
        flag = False  
if __name__ == '__main__':  
    # 创建一个标志用于判断密码是否破解成功  
    flag = True  
    # 创建一个线程池  
    pool = ThreadPoolExecutor(100)  
    nums = [str(i) for i in range(10)]  
    chrs = [chr(i) for i in range(65, 91)]  
    # 生成数字+字母的6位数密码  
    password_lst = itertools.permutations(nums + chrs, 6)  
    # 创建文件句柄  
    zfile = zipfile.ZipFile("加密文件.zip", 'r')  
    for pwd in password_lst:  
        if not flag: break  
        f = pool.submit(extract, zfile, pwd)  
        f.pwd = pwd  
        f.pool = pool  
        f.add_done_callback(result) 

There is a problem with this code, the memory burst after running for a while! Reason: ThreadPoolExecutor uses an unbounded queue by default. The speed of password attempts cannot keep up with the speed of password production, and production tasks will be added to the queue infinitely. Cause the memory to be full. The memory directly hit 95:

Then the program crashed:

Looking at the source code, I found that ThreadPoolExecutor uses an unbounded queue internally, so the memory is directly full, rewrite the _work_queue property in the ThreadPoolExecutor class, and change the unbounded queue to a bounded queue, so that there will be no memory full problem, see Code:

import queue  
from concurrent.futures import ThreadPoolExecutor  
class BoundedThreadPoolExecutor(ThreadPoolExecutor):  
    def __init__(self, max_workers=None, thread_name_prefix=''):  
        super().__init__(max_workers, thread_name_prefix)  
        self._work_queue = queue.Queue(self._max_workers * 2) # 设置队列大小 

Finally, the crack was successful, as shown in the figure below.

Guess you like

Origin blog.csdn.net/Python_sn/article/details/111551122