利用 Python 破解 ZIP 压缩文件密码:基于字典与暴力破解的多线程实现——代码

rockyou.txt下载地址:
https://github.com/brannondorsey/naive-hashcat/releases/download/data/rockyou.txt 

"dictionary" 模式(字典攻击):使用一个预定义的密码列表(即“字典”)逐一尝试解压 ZIP 文件,直到找到正确密码或尝试完所有密码。
"brute" 模式(暴力破解):程序会根据指定的字符集(如数字、字母)和长度范围,生成所有可能的组合,并一一尝试。
程序会自动先字典后暴力。

请手动调整
MIN_LEN = 4
MAX_LEN = 6

以节省时间。

import pyzipper
import itertools
import string
import threading
from tqdm import tqdm
import os

# === 配置项 ===
THREADS = 8
ZIP_PATH = "2026考研数学660(数学一二三).zip"
MODE = "dictionary"  # "dictionary" or "brute"
WORDLIST_PATH = "rockyou.txt"
CHARSET = string.digits
MIN_LEN = 4
MAX_LEN = 6
LOG_FILE = "crack_result.txt"

# === 全局标志 ===
found_flag = threading.Event()

def try_password(zip_path, password):
    try:
        with pyzipper.AESZipFile(zip_path) as zf:
            zf.extractall(pwd=password.encode('utf-8'))
        if not found_flag.is_set():
            print(f"[✓] 破解成功!密码是:{password}")
            with open(LOG_FILE, 'w') as f:
                f.write(password)
            found_flag.set()
        return True
    except (RuntimeError, pyzipper.BadZipFile, Exception):
        return False

def dictionary_crack(zip_path):
    if not os.path.exists(WORDLIST_PATH):
        print(f"[!] 字典文件不存在:{WORDLIST_PATH},自动切换到暴力破解模式")
        brute_force_crack(zip_path)
        return
    with open(WORDLIST_PATH, 'r', errors='ignore') as f:
        passwords = [line.strip() for line in f if line.strip()]
    for pwd in tqdm(passwords, desc="字典破解中", unit="pwd"):
        if found_flag.is_set():
            return
        try_password(zip_path, pwd)

def brute_force_crack(zip_path):
    def worker(length_range):
        for length in length_range:
            for combo in itertools.product(CHARSET, repeat=length):
                if found_flag.is_set():
                    return
                pwd = ''.join(combo)
                try_password(zip_path, pwd)

    ranges = []
    step = (MAX_LEN - MIN_LEN + 1) // THREADS
    for i in range(THREADS):
        start = MIN_LEN + i * step
        end = MIN_LEN + (i + 1) * step - 1
        if i == THREADS - 1:
            end = MAX_LEN
        ranges.append(range(start, end + 1))

    threads = []
    for r in ranges:
        t = threading.Thread(target=worker, args=(r,))
        t.start()
        threads.append(t)

    for t in threads:
        t.join()

def crack_zip(zip_path):
    if not os.path.exists(zip_path):
        print(f"[✗] 文件不存在:{zip_path}")
        return

    print(f"[•] 正在破解:{zip_path}")
    print(f"[•] 模式:{MODE}")

    if MODE == "dictionary":
        dictionary_crack(zip_path)
    elif MODE == "brute":
        brute_force_crack(zip_path)
    else:
        print("[!] 错误:未知模式")

    if not found_flag.is_set():
        print("[×] 破解失败,密码未找到。")
    else:
        print(f"[✓] 密码已写入 {LOG_FILE}")

if __name__ == "__main__":
    crack_zip(ZIP_PATH)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脑子不好真君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值