无法使用Python代码暴力破解zip文件

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

由于某种原因,我的代码无法识别密码列表中的密码

import zipfile
import rarfile
from threading import Thread, Event

passwords = ["password", "123456", "qwerty", "letmein", "password123", "BFH"]

def extract_zip(zip_file, password, found):
    try:
        with zip_file.open(pwd=bytes(password, 'utf-8')) as zf:
            zf.extractall()
        if not found.is_set():
            print("[*] Password found:", password)
            found.set()
    except Exception as e:
        print("[-] Incorrect password:", password)

def extract_rar(rar_file, password, found):
    try:
        rar_file.extractall(pwd=password)
        if not found.is_set():
            print("[*] Password found:", password)
            found.set()
    except Exception as e:
        print("[-] Incorrect password:", password)

def crack_password(passwords, file_path, archive_type):
    found = Event()
    if archive_type == "zip":
        with zipfile.ZipFile(file_path) as zip_file:
            for password in passwords:
                if not found.is_set():
                    t = Thread(target=extract_zip, args=(zip_file, password, found))
                    t.start()
                else:
                    break
    elif archive_type == "rar":
        with rarfile.RarFile(file_path) as rar_file:
            for password in passwords:
                if not found.is_set():
                    t = Thread(target=extract_rar, args=(rar_file, password, found))
                    t.start()
                else:
                    break
    else:
        print("Invalid archive type.")

def main():
    file_path = input ("Enter the path to the archive file: ")
    archive_type = input("Enter the archive type (zip or rar): ").lower()
    crack_password(passwords, file_path, archive_type)

if __name__ == "__main__":
    main()

密码错误:密码 密码错误:qwerty 密码错误:letmein 密码错误:123456 密码错误:password123 密码错误:BFH 密码错误:123456789

进程已完成,退出代码为 0

python pycharm zip brute-force rar
1个回答
0
投票

您编写的代码旨在尝试密码列表中的每个密码,并打印一条消息,无论密码是否正确。如果列表中的密码均不与 zip 或 rar 文件的实际密码匹配,程序将无法识别正确的密码。

您看到的错误消息是根据您的代码所预期的。对于每个不正确的密码,代码都会打印“错误的密码:{password}”。如果正确的密码不在您的列表中,程序将为它尝试的每个密码打印“密码错误”。

如果您确定列表中包含正确的密码,但程序无法识别它,则密码应用于 zip 或 rar 文件的方式可能存在问题。您可以检查以下几件事:

编码:您使用 zip 文件的“utf-8”编码将密码转换为字节。确保您的 zip 文件的编码正确。 Rar 文件不会被编码为字节,请确保这是您的 rarfile 模块期望密码的正确方式。 密码格式:确保列表中的密码与实际密码完全匹配 - 密码区分大小写。 存档类型:检查运行程序时是否提供了正确的存档类型(zip 或 rar)。 模块兼容性:确保您使用的 zipfile 和 rarfile 模块与您尝试破解的存档文件的类型和版本兼容。

如果这些都不起作用,请尝试将密码列表全球化作为最后的手段。 如果您已检查所有这些内容但问题仍然存在,请提供更多详细信息或错误消息(如果有)。

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