You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何快速暴力破解6位abc123格式的密码(含哈希校验)

3小写字母+3数字格式6位密码的暴力破解方案

需求说明

需要快速暴力破解格式为abc123(3个小写英文字母 + 3个阿拉伯数字)的6位密码,实现过程需包含密码哈希生成与哈希校验步骤,当前采用随机迭代+多线程方式实现。

现有实现代码

import string
from itertools import product
from time import time
import threading
import random

password = input("请输入你的3字母3数字格式密码:")
hashed = hash(password)
start = time()

def product_loop(hashing, generator):
    global stop_threads
    for p in generator:
        if stop_threads:
            break
        current_pwd = ''.join(p)
        if hash(current_pwd) == hashing:
            print('\n找到密码:', current_pwd)
            print('密码哈希值:', hash(current_pwd))
            stop_threads = True
            end = time()
            print('耗时:%.2f 秒' % (end - start))
            return current_pwd
    return False

def bruteforce(hashing, max_nchar=8):
    for l in range(6, max_nchar + 1):
        if stop_threads:
            break
        print("\t正在尝试%d位密码.." % l)
        # 随机打乱字符集顺序生成迭代器
        char_set = random.sample(string.ascii_lowercase, 26) + random.sample(string.digits, 10)
        generator = product(char_set, repeat=int(l))
        p = product_loop(hashing, generator)
        if p is not False:
            return p

if __name__ == "__main__":
    stop_threads = False
    # 创建8个线程并行执行暴力破解
    threads = []
    for _ in range(8):
        t = threading.Thread(target=bruteforce, args=(hashed,))
        threads.append(t)
        t.start()

现有实现的问题与优化建议

  • 字符集范围冗余:当前代码会尝试所有6-8位的混合字符组合,但需求仅针对3字母+3数字的6位密码,无需遍历其他长度和不符合格式的组合,可直接生成3个小写字母 + 3个数字的所有排列组合,减少无效计算。
  • 多线程资源浪费:多个线程在重复遍历相同的字符组合空间,导致大量重复计算,应将密码空间拆分后分配给不同线程,避免重复工作。
  • 哈希函数选择:Python内置的hash()函数会随解释器重启变化,不适合作为固定的密码哈希校验标准,建议使用SHA-256等标准哈希算法(如hashlib库实现)。

优化后的代码示例

import string
from itertools import product
from time import time
import threading
import hashlib

def get_hash(pwd):
    # 使用SHA-256生成固定哈希值
    return hashlib.sha256(pwd.encode()).hexdigest()

password = input("请输入你的3字母3数字格式密码:")
target_hash = get_hash(password)
start = time()
stop_threads = False

def crack_task(chunk):
    global stop_threads
    for letters_part, nums_part in chunk:
        if stop_threads:
            break
        current_pwd = ''.join(letters_part) + ''.join(nums_part)
        if get_hash(current_pwd) == target_hash:
            print('\n找到密码:', current_pwd)
            print('密码SHA-256哈希值:', get_hash(current_pwd))
            stop_threads = True
            end = time()
            print('耗时:%.2f 秒' % (end - start))
            return

def split_chunks(data, num_chunks):
    # 将密码空间拆分为多个块分配给线程
    chunk_size = len(data) // num_chunks
    chunks = []
    for i in range(num_chunks):
        start_idx = i * chunk_size
        end_idx = start_idx + chunk_size if i != num_chunks-1 else len(data)
        chunks.append(data[start_idx:end_idx])
    return chunks

if __name__ == "__main__":
    # 生成所有3小写字母+3数字的组合
    letters = list(product(string.ascii_lowercase, repeat=3))
    nums = list(product(string.digits, repeat=3))
    all_combinations = [(l, n) for l in letters for n in nums]
    
    # 拆分任务到8个线程
    chunks = split_chunks(all_combinations, 8)
    threads = []
    for chunk in chunks:
        t = threading.Thread(target=crack_task, args=(chunk,))
        threads.append(t)
        t.start()
    
    # 等待所有线程结束
    for t in threads:
        t.join()

内容的提问来源于stack exchange,提问作者blazerlazer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 21:01:07