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

SHA-1字典攻击无法匹配大小写混合及字符变体字符串的解决方法求助

SHA-1字典攻击无法匹配大小写混合及字符变体字符串的解决方法求助

你遇到的这个问题非常典型——字典里存的是基础的小写单词,但目标哈希对应的是它的大小写混合变体或者leet字符替换变体(比如把o换成0),直接用原单词哈希对比肯定匹配不上。下面我给你两种针对性的解决思路,以及修改后的完整代码:

一、核心问题分析

你的原脚本只对字典里的原始小写单词计算哈希,但实际目标字符串可能是原单词的变形:

  • 大小写混合:比如joejoe → JoeJoe/jOeJoE
  • Leet字符替换:比如joejoe → j0ej0e/j3ej3e(用数字或特殊字符替换相似字母)

要匹配这些变体,我们需要为每个字典单词生成对应的变形,再逐一计算哈希对比。

二、修改后的完整代码

import hashlib
import itertools

# Prompt the user to enter the target SHA-1 hash
target_hash = input("Enter the target SHA-1 hash: ")

# Path to the dictionary file
dictionary_file = r'C:\Users\johnny\Documents\Security\src\wordlist.txt'

# 定义常见的leet字符替换规则(可根据需求增减)
leet_replacements = {
    'a': ['a', '4'],
    'e': ['e', '3'],
    'o': ['o', '0'],
    'l': ['l', '1'],
    's': ['s', '5'],
    't': ['t', '7'],
    'i': ['i', '1'],
    'g': ['g', '9']
}

def generate_case_variants(word):
    # 生成单词的所有大小写组合变体
    chars = [(c.lower(), c.upper()) for c in word]
    return [''.join(variant) for variant in itertools.product(*chars)]

def generate_leet_variants(word):
    # 生成单词的所有leet字符替换变体
    chars = []
    for c in word.lower():
        # 如果字符在替换规则里,就取所有可能的替换值,否则保留原字符
        chars.append(leet_replacements.get(c, [c]))
    return [''.join(variant) for variant in itertools.product(*chars)]

def dictionary_attack(target_hash, dictionary_file):
    with open(dictionary_file, 'r') as file:
        for line in file:
            base_word = line.strip()
            if not base_word:
                continue
            
            # 生成所有大小写变体
            case_variants = generate_case_variants(base_word)
            # 生成所有leet变体,再为每个leet变体生成大小写变体
            leet_variants = generate_leet_variants(base_word)
            all_variants = case_variants + [var for leet_word in leet_variants for var in generate_case_variants(leet_word)]
            
            # 去重,避免重复计算相同字符串的哈希,提升效率
            unique_variants = list(set(all_variants))
            
            for variant in unique_variants:
                variant_hash = hashlib.sha1(variant.encode()).hexdigest()
                if variant_hash == target_hash:
                    print(f"Found the original string: {variant}")
                    return variant
    print("Could not find the original string in the dictionary.")
    return None

# Run the attack
original_input = dictionary_attack(target_hash, dictionary_file)

代码细节说明

  1. 大小写变体生成:generate_case_variants用itertools.product生成每个字母的大小写组合,比如joejoe会生成JoeJoe、JOEJOE、jOeJoE等所有可能的大小写形式。
  2. Leet变体生成:generate_leet_variants根据预设规则,生成所有字符替换版本,比如joejoe会生成j0ej0e、j3ej3e等。
  3. 变体合并与去重:把两类变体合并后去重,避免重复计算同一个字符串的哈希,节省时间。
  4. 哈希对比:对每个生成的变体计算SHA-1哈希,匹配到目标哈希就立即返回结果。

优化小建议

如果你的字典很大,生成所有变体可能会拖慢速度,可以优先尝试高频变体(比如全小写、全大写、首字母大写、单字符leet替换),而不是所有组合,这样能在保证效果的同时提升效率。

备注:内容来源于stack exchange,提问作者jr.

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 18:25:32