1. The origin of this post

2. Run down the error code to find the root of the problem

I can find that he added multithreaded code in order to improve the speed, very clever oh:

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('Password:', 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)]
    password_lst = itertools.permutations(nums + chrs, 6)
    zfile = zipfile.ZipFile("Encrypted file.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)
Copy the code

Run with his code, obediently long dong ~ run for a while memory burst.

The reason is that ThreadPoolExecutor uses an unbounded queue by default. The speed of trying passwords cannot keep up with the speed of production passwords. Production tasks are added to the queue indefinitely, causing memory to fill up.

To show you, the memory went straight up to 95:

And cause the program to crash:

3. Find the root of the problem and treat it

The _work_queue attribute of ThreadPoolExecutor is changed to a bounded queue instead of an unbounded queue. This will not cause memory to overflow.

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) # set queue size
Copy the code

Finally, according to my advice to him, the perfect crack password success.

Click 💌💌 to pick up dry goods full

Thank you for watching, I continue to share dry goods content for you, your collection, comments, likes is my biggest support! If you need to get my previous study materials, you can send me a private message and tell me the specific materials you need.