本文仅供合法授权的安全研究与学习使用,请勿用于非法用途。读者应确保行为符合当地法律法规。
前言 在渗透测试和红队评估中,密码爆破仍然是获取系统权限的重要手段之一。然而,面对现代密码策略(要求大小写混合、数字、特殊字符、最小长度等),传统的通用字典文件往往效率低下。社会工程学字典生成器通过收集目标人员的个人信息(姓名、生日、手机号、身份证号等),按照人类设置密码的习惯规律进行组合,生成高度针对性的密码字典,大幅提高爆破命中率。
本文基于一个完整的Python社会工程学字典生成器项目,深入解析其信息读取、组合生成、特殊字符插入等核心模块的实现原理,并探讨其在安全评估中的应用场景。
技术背景 社会工程学密码分析 人们在设置密码时普遍存在以下心理规律:
个人信息组合 :姓名拼音 + 生日、手机号后四位、身份证后六位等
特殊字符点缀 :在个人信息之间插入 @、#、! 等常见特殊符号
重复拼接 :同一信息重复两次(如 czxczx)或与自身变体组合
数字补齐 :短密码后补随机数字以满足长度要求
研究表明,超过60%的个人密码可以通过其公开可获取的个人信息组合推导出来。
笛卡尔积与排列组合 字典生成的数学基础是排列组合。Python的 itertools 模块提供了强大的组合工具:
itertools.product:笛卡尔积,用于生成数字全组合
itertools.permutations:排列,用于生成信息的所有排列方式
例如,3位数字的所有组合可以通过 itertools.product(string.digits, repeat=3) 生成,共1000种可能。
实现思路 整体架构分为四个模块:
1 2 3 4 5 6 7 8 9 10 信息读取模块(read_info_list) ↓ 读取 info.txt 中的个人信息字段 数字列表生成模块(create_number_list) ↓ 生成3位数字全组合(用于密码补齐) 特殊字符列表生成模块(create_special_list) ↓ 获取所有ASCII特殊字符 密码组合生成模块(generate_password_combinations) ↓ 多维度组合:个人信息 + 数字补齐 + 双信息拼接 + 特殊字符穿插 输出模块 ↓ 写入 dict.txt 字典文件
核心设计思想是通过多维度组合策略 覆盖目标可能的密码设置习惯。
核心代码解析 1. 信息读取模块 从 info.txt 文件中读取目标的个人信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import itertoolsimport stringimport loggingimport osimport argparselogging.basicConfig(level=logging.INFO, format ='%(asctime)s - %(levelname)s - %(message)s' ) def read_info_list (info_file="info.txt" ): """ 读取个人信息文件info.txt,提取所有个人信息字段 文件格式:字段名:字段值(如 姓名全拼:chanzixuan) """ info_list = [] if not os.path.exists(info_file): logging.error(f"文件 {info_file} 不存在" ) return info_list try : with open (info_file, "r" , encoding="utf-8" ) as info: lines = info.readlines() for line in lines: parts = line.strip().split(":" ) if len (parts) == 2 : info_list.append(parts[1 ]) else : logging.warning(f"格式错误的行: {line.strip()} " ) except Exception as e: logging.error(f"读取个人信息文件时发生错误: {e} " ) return info_list
info.txt 文件示例:
1 2 3 4 5 6 7 8 9 姓名全拼:chanzixuan 姓名简拼:czx 手机号码:19071966450 生日:20101003 社交帐号:45484895 身份证号:650101198406021987 有意义的字符或数字:807512 子女生日:20450404 子女手机号:17711907865
设计要点 :使用冒号分隔字段名和字段值,便于扩展和维护。日志记录确保格式错误能被发现。
2. 辅助列表生成模块 1 2 3 4 5 6 7 8 9 10 11 12 13 14 def create_number_list (): """ 生成所有可能的三位数字组合(000-999) 用于密码长度不足时的数字补齐 """ numbers_list = ['' .join(p) for p in itertools.product(string.digits, repeat=3 )] return numbers_list def create_special_list (): """ 生成所有ASCII特殊字符列表 string.punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' """ return list (string.punctuation)
3. 密码组合生成模块(核心) 这是整个工具的核心,采用三种组合策略:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 def generate_password_combinations (infolist, specal_list, password_length ): """ 生成密码的所有可能组合 策略1:单信息 + 数字补齐 策略2:双信息拼接 策略3:双信息 + 特殊字符穿插 """ combinations = set () for a in infolist: if len (a) >= password_length: combinations.add(a) else : need_words = password_length - len (a) for b in itertools.permutations(string.digits, need_words): combinations.add(a + '' .join(b)) for a in infolist: for c in infolist: combined = a + c if len (combined) >= password_length: combinations.add(combined) for a in infolist: for d in infolist: for e in specal_list: combined1 = a + d + e combined2 = e + d + a combined3 = a + e + d if len (combined1) >= password_length: combinations.add(combined1) if len (combined2) >= password_length: combinations.add(combined2) if len (combined3) >= password_length: combinations.add(combined3) return combinations
三种策略的设计逻辑:
策略1 :模拟”姓名+数字”或”生日+数字”的密码习惯。例如 czx 不足4位,则生成 czx001、czx002…等
策略2 :模拟”姓名+生日”、”手机号+姓名”等双信息组合。例如 chanzixuan20101003、19071966450czx
策略3 :模拟含特殊字符的密码。三种穿插方式覆盖了符号在头部、中间、尾部的所有位置
4. 主函数与命令行接口 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 def combination (dict_file="dict.txt" , info_file="info.txt" , password_length=4 ): """ 生成密码组合并写入字典文件 """ infolist = read_info_list(info_file) specal_list = create_special_list() if not infolist: logging.warning("个人信息列表为空,无法生成密码组合" ) return combinations = generate_password_combinations(infolist, specal_list, password_length) with open (dict_file, "w" , encoding="utf-8" ) as df: for password in combinations: df.write(password + '\n' ) logging.info(f"生成的密码组合已写入文件 {dict_file} " ) parser = argparse.ArgumentParser(description="生成密码字典" ) parser.add_argument("--dict_file" , type =str , default="dict.txt" , help ="密码字典文件路径" ) parser.add_argument("--info_file" , type =str , default="info.txt" , help ="个人信息文件路径" ) parser.add_argument("--password_length" , type =int , default=4 , help ="密码最小长度" ) args = parser.parse_args() combination(dict_file=args.dict_file, info_file=args.info_file, password_length=args.password_length)
使用方法与运行效果 准备信息文件 创建 info.txt,填入目标人员的个人信息:
1 2 3 4 5 6 7 姓名全拼:zhangsan 姓名简拼:zs 手机号码:13800138000 生日:19900101 社交帐号:12345678 身份证号:110101199001011234 有意义的字符或数字:5201314
运行生成器 1 2 3 4 5 python dictsociety.py python dictsociety.py --dict_file custom_dict.txt --info_file target.txt --password_length 6
输出效果 生成的 dict.txt 中包含大量针对性密码组合,以下是真实输出的部分内容:
1 2 3 4 5 6 7 8 9 10 11 12 chanzixuanczx chanzixuan19071966450 19071966450czx 20101003chanzixuan 650101198406021987807512 czx@19071966450 #2045040419071966450 807512~17711907865 chanzixuan650101198406021987 czxczx 1907196645020101003 ...
可以看到,生成的密码涵盖了:
纯信息拼接(chanzixuanczx)
信息+数字(chanzixuan19071966450)
含特殊字符(czx@19071966450)
信息重复(czxczx)
多信息组合(650101198406021987807512)
配合爆破工具 生成的字典可直接用于 Hydra、Burp Suite 等爆破工具:
1 2 3 4 5 hydra -l username -P dict.txt ssh://target.com
防御对策
密码策略强化 :强制要求密码不得包含个人信息(姓名、生日、手机号等),可通过PAM模块或AD组策略实现密码组成检查
密码长度要求 :建议最低12位,并要求至少包含大写、小写、数字、特殊字符四类中的三类。较长的密码能使排列组合空间呈指数增长,大幅降低字典攻击成功率
登录限制 :实施账户锁定策略(如5次失败后锁定15分钟)和登录频率限制,使字典爆破在时间上不可行
多因素认证(MFA) :即使密码被爆破成功,MFA可以作为第二道防线阻止未授权访问
密码泄露检测 :定期检查员工密码是否出现在已知泄露字典中(如Have I Been Pwned API)
安全意识培训 :教育员工避免使用个人信息组合作为密码,这是最根本的防御措施
蜜罐账号监控 :部署蜜罐账号,当检测到针对特定账号的字典攻击时自动告警
总结 本文解析了一个基于个人信息的社会工程学字典生成器,其核心是通过多维度组合策略(单信息补齐、双信息拼接、特殊字符穿插)覆盖目标可能的密码设置习惯。工具使用了 itertools 进行排列组合计算,通过 set 自动去重,并支持命令行参数自定义配置。
需要强调的是,字典生成器的效果高度依赖于信息收集的完整性。在真实红队评估中,信息收集阶段应尽可能多地获取目标的公开信息(社交媒体、公开数据库、企业信息等),这些信息越完整,字典的命中率越高。
从防御角度看,抵御社会工程学字典攻击的关键在于避免使用个人信息作为密码组成部分,并配合MFA、登录限制等纵深防御手段。后续文章将继续探讨身份证信息工具和手机号归属地查询等社工辅助工具的实现。