我怎么能杀多进程池后,我的拉链被成功提取?

问题描述 投票:0回答:1

我一直在写一个.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工作,另外,如果我在print()把它try/except后,将给予outside loop错误。

我没有看到this线程,但不知道这是否会为我的实例工作;因为我不知道我怎么能表达“密码被发现”,并通过event/Eventdef extract_zip(zip_filename, password)

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

python multiprocessing pool kill-process
1个回答
0
投票

这样做的一种方式是使用一个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()处理在没有密码的情况下找到。

© www.soinside.com 2019 - 2024. All rights reserved.