Python 暴力破解 zip 文件

Python brute forcing zip files

我一直在研究 Python 中的一个程序,它可以破解加密的 zip 文件。问题是,我的部分程序关键功能不起作用。

我的程序是如何工作的...

User enters in zip file name.

User enters min password length.

User enters max password length.

Program will enter a loop, where it will gen a password that is within the min & max numbers.

Program will try to open the zip with the password.

Program will then print out the password if it was successful or not.

这是我的程序出错的最后 2 步。

程序不会在密码匹配时弯腰,而是会继续,然后尝试另一个密码。我认为发生这种情况是因为在尝试打开 zip 时发生错误。因此,即使密码匹配,它也会直接转到“except:”语句。

这是我的代码,它搞砸了 -

# Function which tries to open zip (The buggy function)
def extract(zip_name, password, number):
    print("\nAttempt", number)
    # Tries and opens the zip
    try:
        zip_name.extractall(pwd=password)
        print("Success: " + password)
        exit(0)
    except:
        print("Failed: " + password)

任何人都可以告诉我如何进行这项工作。谢谢

当您调用 exit(0) 时会发生什么?好吧,如果你阅读了 exit 内置的文档,那么你会看到它说:

when called, raises SystemExit with the specified exit code

所以它引发了一个异常,但是在你的程序中这是在 try: ... except: ... 中所以接下来发生的事情是异常被捕获并且程序打印 Failed 并继续。

这就是为什么人们经常给出“don't use a bare except:”建议的原因——你几乎不想抓住 SystemExit

相反,当密码不匹配时,您可以捕获 zipfile 引发的实际异常,这似乎是 RuntimeError。此外,如果您将 exit(0) 替换为 return True 之类的内容,并在程序的更高级别处理成功或失败,它将改进程序。