请求协助:在Windows用Python生成指定规则的8位Brute Force密码字典
生成符合要求的暴力破解密码列表方案
一、Python实现8位密码生成
直接全量生成后筛选会因字符集过大导致效率极低,推荐用固定必须字符位置的方式优化,既保证规则符合,又减少冗余计算:
高效生成代码
import itertools from string import digits, ascii_uppercase, ascii_lowercase # 基础字符集:数字+大小写字母 allowed_chars = digits + ascii_uppercase + ascii_lowercase # 必须包含的固定字符 required_fixed = ['4', '8'] # 必须二选一的字符 required_either = ['p', 'P'] def generate_valid_passwords(): # 遍历p/P两种情况 for p_char in required_either: # 从8个位置中选3个放置必须的三个字符 for pos_triple in itertools.combinations(range(8), 3): # 生成三个必须字符的所有排列(覆盖所有位置组合) for perm in itertools.permutations(required_fixed + [p_char]): # 剩余5个位置的索引 remaining_positions = [i for i in range(8) if i not in pos_triple] # 生成剩余5位的所有字符组合 for remaining_part in itertools.product(allowed_chars, repeat=5): password = [''] * 8 # 填充必须字符 for pos, char in zip(pos_triple, perm): password[pos] = char # 填充剩余字符 for pos, char in zip(remaining_positions, remaining_part): password[pos] = char yield ''.join(password) if __name__ == '__main__': # 写入文件避免内存溢出 with open('valid_passwords.txt', 'w', encoding='utf-8') as f: count = 0 for pwd in generate_valid_passwords(): f.write(pwd + '\n') count += 1 if count % 10000 == 0: print(f"已生成 {count} 个密码")
代码说明
- 先锁定必须字符的位置和排列,再填充剩余字符,比全量筛选效率高至少一个数量级
- 使用生成器逐一生成并写入文件,不会占用过多内存
二、Windows环境下的工具建议
1. Crunch(推荐)
轻量高效的密码列表生成工具,Windows可下载编译版直接使用,通过管道结合findstr筛选符合规则的密码:
# 生成8位数字+大小写字母密码,筛选包含4、8、不区分大小写包含p的内容 crunch 8 8 mixalpha-numeric | findstr "4" | findstr "8" | findstr /i "p" > passwords.txt
mixalpha-numeric是Crunch内置的数字+大小写字母字符集/i参数让findstr不区分大小写匹配p/P
2. John the Ripper
兼具破解与密码生成功能,结合系统命令筛选:
# 使用内置混合字符集生成8位密码,再筛选符合规则的内容 john --incremental:mixalpha-numeric --min-length=8 --max-length=8 --stdout | findstr "4" | findstr "8" | findstr /i "p" > passwords.txt
3. Cupp
基于Python的个性化密码生成工具,适合结合个人信息的场景,可修改配置文件强制加入4、8、p/P,生成固定8位长度的密码。
三、加入特殊字符的方法
核心是扩大字符集,按需调整筛选规则:
Python代码修改
- 扩展字符集加入常用特殊字符:
from string import punctuation # 自定义需要的特殊字符(避免罕见符号) allowed_special = '!@#$%^&*()_+-=' allowed_chars = digits + ascii_uppercase + ascii_lowercase + allowed_special
- 如果要求必须包含特殊字符,在筛选条件中添加:
# 原筛选条件基础上增加特殊字符检查 if all(c in password for c in required_fixed) and any(c in password for c in required_either) and any(c in password for c in allowed_special):
若只是可选加入,直接扩展字符集即可,无需修改筛选条件。
工具修改
Crunch
使用内置混合字符集(含特殊字符)或自定义字符集:
# 使用内置含特殊字符的集合并筛选 crunch 8 8 mixalpha-numeric-symbols | findstr "4" | findstr "8" | findstr /i "p" > passwords_with_special.txt # 自定义字符集示例 crunch 8 8 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*" | findstr "4" | findstr "8" | findstr /i "p" > passwords_custom.txt
John the Ripper
使用内置含特殊字符的字符集:
john --incremental:mixalpha-numeric-symbols --min-length=8 --max-length=8 --stdout | findstr "4" | findstr "8" | findstr /i "p" > passwords_with_special.txt
内容的提问来源于stack exchange,提问作者igcs Computer
相关产品推荐
相关产品推荐

