【发布时间】: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