【问题标题】:How can I kill a multiprocess pool after my zip is successfully extracted?成功解压缩 zip 后,如何终止多进程池?
【发布时间】:2019-02-05 22:29:37
【问题描述】:

我已经编写了一个 .zip 蛮力破解程序,但我相信它会继续/尝试仍然找到密码,即使它已找到(密码存储在由 Program.py -z zipname.zip -f filename.txt 调用的 .txt 中

一旦找到密码并停止池,我不确定如何停止程序。主要是因为我使用的是multiprocessing.Pool。我的代码如下:

import argparse
import multiprocessing
import zipfile

parser = argparse.ArgumentParser(description="Unzips a password protected .zip", usage="Program.py -z zip.zip -f file.txt")
# Creates -z arg
parser.add_argument("-z", "--zip", metavar="", required=True, help="Location and the name of the .zip file.")
# Creates -f arg
parser.add_argument("-f", "--file", metavar="", required=True, help="Location and the name of the file.txt.")
args = parser.parse_args()


def extract_zip(zip_filename, password):
    try:
        with zipfile.ZipFile(zip_filename, 'r') as zip_file:
            zip_file.extractall('Extracted', pwd=password)
            print(f"[+] Password for the .zip: {password.decode('utf-8')} \n")
    except:
        # If a password fails, it moves to the next password without notifying the user. If all passwords fail, it will print nothing in the command prompt.
        pass


def main(zip, file):
    if (zip == None) | (file == None):
        # If the args are not used, it displays how to use them to the user.
        print(parser.usage)
        exit(0)
    # Opens the word list/password list/dictionary in "read binary" mode.
    txt_file = open(file, "rb")
    # Allows 8 instances of Python to be ran simultaneously.
    with multiprocessing.Pool(8) as pool:
        # "starmap" expands the tuples as 2 separate arguments to fit "extract_zip"
        pool.starmap(extract_zip, [(zip, line.strip()) for line in txt_file])


if __name__ == '__main__':
    main(args.zip, args.file)

现在file.txt 通常有数百行到数千行(我的意思是数千行,比如 300k 或 1500k)。任何地方的密码;从第一行到最后一行。一旦找到密码,我就不确定如何实施/放置这个“handbreak”。我想过使用break,但这似乎不正确,因为我正在使用multiprocessing.Pool,而且如果我将它放在try/except 中的print() 之后,它会给出outside loop 错误。

我确实看到了this 线程,但不确定这是否适用于我的实例;因为我不确定如何表达“找到密码”并将event/Event 传递给def extract_zip(zip_filename, password)

任何帮助或指导将不胜感激!

【问题讨论】:

  • 你试过pool.terminate()吗?
  • 我该如何实现?在print() 之后?但是我相信我必须以某种方式将pool 传递给def extract_zip,对吧?

标签: python multiprocessing pool kill-process


【解决方案1】:

一种方法是使用Queue(或其他方式在进程之间发出信号)。

给我们Queue,在您的main 中添加几行以创建Queue,然后再创建Pool

m = multiprocessing.Manager()
q = m.Queue()

这为您提供了一个可共享的Queue,它可以传递给池进程。将q 添加到传递给extract_zip() 方法的参数中。然后修改您的extract_zip() 方法以使用q

def extract_zip(zip_filename, password, que):
    try:
        with zipfile.ZipFile(zip_filename, 'r') as zip_file:
            zip_file.extractall('Extracted', pwd=password)
            print(f"[+] Password for the .zip: {password.decode('utf-8')} \n")
            que.put('Done')    # signal success
    except:
        # If a password fails, it moves to the next password without notifying the user. If all passwords fail, it will print nothing in the command prompt.
        pass

您需要使用async 变体StarMap。所以你的main 方法看起来像:

def main(zip, file):
    if (zip == None) | (file == None):
        # If the args are not used, it displays how to use them to the user.
        print(parser.usage)
        exit(0)
    # Opens the word list/password list/dictionary in "read binary" mode.
    txt_file = open(file, "rb")

    # create a Queue
    m = multiprocessing.Manager()
    q = m.Queue()

    # Allows 8 instances of Python to be ran simultaneously.
    with multiprocessing.Pool(8) as pool:
        # "starmap" expands the tuples as 2 separate arguments to fit "extract_zip"
        pool.starmap_async(extract_zip, [(zip, line.strip(), q) for line in txt_file])
        pool.close()
        q.get(True)    # wait for a process to signal success
        pool.terminate()    # terminate the pool
        pool.join()

您可以将timeout 添加到q.get() 以处理找不到密码的情况。

【讨论】:

  • 我明白了。据说一旦找到密码,这就会“杀死”一切,对吧?因此不再尝试解压缩 zip。
  • 根据documentation, terminate() “在没有完成未完成工作的情况下立即停止工作进程。”但是,当前正在进行的解压缩尝试可能会完成。将chunksize 设置为1 可能会限制terminate() 之后的额外工作。
猜你喜欢
  • 2021-12-29
  • 2013-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-05
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
相关资源
最近更新 更多