Hashcat是世界上最快的密码恢复工具,支持GPU加速和300+哈希算法。本文将深入讲解密码哈希原理、Hashcat核心功能、攻击模式、规则引擎,以及实战破解技巧。

一、Hashcat概述

1.1 项目信息

属性 说明
项目地址 https://github.com/hashcat/hashcat
开发语言 C / OpenCL
GitHub Stars 21k+
开源协议 MIT
最新版本 v6.2.6
支持平台 Windows / Linux / macOS

1.2 核心特性

1
2
3
4
5
6
7
8
9
10
11
12
13
┌─────────────────────────────────────────────────────────────┐
│ Hashcat 核心特性 │
├─────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ GPU加速 │ │ 300+算法 │ │ 多攻击模式 │ │
│ │ CUDA/OCL │ │ Hash Types │ │Attack Modes │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 规则引擎 │ │ 分布式破解 │ │ 会话恢复 │ │
│ │Rule Engine │ │ Distributed │ │ Session │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────────┘

1.3 支持的哈希类型

类别 哈希类型 Mode编号
原始哈希 MD5, SHA1, SHA256, SHA512 0, 100, 1400, 1700
加盐哈希 md5($salt.$pass), sha256($salt.$pass) 10, 1410
迭代哈希 PBKDF2, bcrypt, scrypt 12000, 3200, 8900
系统密码 Windows NTLM, Linux SHA512crypt 1000, 1800
网络协议 WPA/WPA2, Kerberos, NTLM 22000, 13100, 5600
应用密码 MySQL, PostgreSQL, Oracle 300, 12, 112
文档加密 Office, PDF, ZIP, 7z 9400-9800, 10500, 13600

二、密码哈希原理

2.1 哈希函数基础

常见哈希算法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import hashlib

# MD5 (不安全,已弃用)
md5_hash = hashlib.md5(b"password").hexdigest()
# 5f4dcc3b5aa765d61d8327deb882cf99

# SHA1 (不安全)
sha1_hash = hashlib.sha1(b"password").hexdigest()
# 5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8

# SHA256 (推荐)
sha256_hash = hashlib.sha256(b"password").hexdigest()
# 5e884898da28047d9169e1e5d5d5d5d5d5d5d5d5d5d5d5d5d5d5d5d5d5d5d5d5

# SHA512
sha512_hash = hashlib.sha512(b"password").hexdigest()

# 加盐哈希
import os
salt = os.urandom(16)
salted_hash = hashlib.sha256(salt + b"password").hexdigest()

2.2 密码存储演进

1
2
3
4
5
6
7
8
9
10
11
12
13
14
演进历程:

1. 明文存储 (极不安全)
password → "password"

2. 简单哈希 (不安全)
password → MD5("password") → "5f4dcc3b..."

3. 加盐哈希 (较安全)
password → MD5(salt + "password") → "a1b2c3d4..."

4. 慢哈希 (推荐)
password → bcrypt("password", cost=12) → "$2b$12$..."
password → Argon2("password") → "$argon2id$..."

2.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
# Windows NTLM (Mode 1000)
# 格式: 32位十六进制
aad3b435b51404eeaad3b435b51404ee:8846f7eaee8fb117ad06bdd830b7586c

# Linux SHA512crypt (Mode 1800)
# 格式: $6$salt$hash
$6$rounds=5000$saltsalt$hash...

# Linux SHA256crypt (Mode 7400)
$5$rounds=5000$saltsalt$hash...

# Linux MD5crypt (Mode 500)
$1$saltsalt$hash...

# bcrypt (Mode 3200)
$2a$10$N9qo8uLOickgx2ZMRZoMye...

# MySQL (Mode 300)
*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19

# PostgreSQL MD5 (Mode 12)
md5be86a79bf2043622d58d5453c47d4860

# Kerberos TGS-REP (Mode 13100)
$krb5tgs$23$*user$realm$spn*$hash...

# WPA/WPA2 PMKID (Mode 22000)
WPA*02*hash*mac_ap*mac_sta*essid***

三、Hashcat安装与配置

3.1 安装方式

安装Hashcat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Linux (Ubuntu/Debian)
sudo apt update
sudo apt install hashcat

# 或从源码编译
git clone https://github.com/hashcat/hashcat.git
cd hashcat
make
sudo make install

# macOS
brew install hashcat

# Windows
# 从 https://hashcat.net/hashcat/ 下载二进制包

# 验证安装
hashcat --version
hashcat -I # 显示可用设备

# 基准测试
hashcat -b # 所有算法
hashcat -b -m 1000 # 指定算法

3.2 GPU驱动配置

GPU配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# NVIDIA GPU (CUDA)
# 安装NVIDIA驱动和CUDA Toolkit
sudo apt install nvidia-driver-535
sudo apt install nvidia-cuda-toolkit

# 验证
nvidia-smi
hashcat -I

# AMD GPU (ROCm)
# 安装ROCm
sudo apt install rocm-opencl-runtime

# Intel GPU (OpenCL)
sudo apt install intel-opencl-icd

# 多GPU配置
hashcat -d 1,2,3 # 使用指定GPU
hashcat -d 1 -w 3 # 工作负载配置
# -w 1: 低 (桌面使用)
# -w 2: 默认
# -w 3: 高 (专用破解)
# -w 4: 极限 (可能导致系统无响应)

四、攻击模式详解

4.1 字典攻击 (Mode 0)

字典攻击
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 基础字典攻击
hashcat -m 0 -a 0 hash.txt wordlist.txt

# 使用多个字典
hashcat -m 0 -a 0 hash.txt dict1.txt dict2.txt dict3.txt

# 常用字典
# rockyou.txt - 1400万密码
# SecLists - 多种密码列表
# CrackStation - 15亿密码

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

# 字典攻击 + 规则
hashcat -m 0 -a 0 hash.txt wordlist.txt -r rules/best64.rule

# 显示破解结果
hashcat -m 0 hash.txt --show

# 输出到文件
hashcat -m 0 -a 0 hash.txt wordlist.txt -o cracked.txt

4.2 组合攻击 (Mode 1)

组合攻击
1
2
3
4
5
6
7
8
9
10
11
12
# 组合两个字典
# dict1: [pass, admin, user]
# dict2: [123, 456, 789]
# 结果: pass123, pass456, pass789, admin123, ...

hashcat -m 0 -a 1 hash.txt dict1.txt dict2.txt

# 添加规则
hashcat -m 0 -a 1 hash.txt dict1.txt dict2.txt -j '$@' -k '$!'
# -j: 应用于左侧字典
# -k: 应用于右侧字典
# 结果: pass@123!, admin@456!, ...

4.3 暴力破解 (Mode 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
# 掩码字符集
# ?l = 小写字母 (a-z)
# ?u = 大写字母 (A-Z)
# ?d = 数字 (0-9)
# ?s = 特殊字符 (!@#$%...)
# ?a = 所有可打印字符
# ?b = 所有字节 (0x00-0xff)

# 8位纯数字
hashcat -m 0 -a 3 hash.txt ?d?d?d?d?d?d?d?d

# 8位小写字母
hashcat -m 0 -a 3 hash.txt ?l?l?l?l?l?l?l?l

# 混合掩码 (首字母大写 + 6小写 + 1数字)
hashcat -m 0 -a 3 hash.txt ?u?l?l?l?l?l?l?d

# 自定义字符集
hashcat -m 0 -a 3 hash.txt -1 ?l?d ?1?1?1?1?1?1?1?1
# -1 定义字符集1为小写字母+数字

# 增量模式
hashcat -m 0 -a 3 hash.txt ?a?a?a?a?a?a --increment --increment-min 4
# 从4位开始,逐步增加到6位

# 常见密码模式
hashcat -m 0 -a 3 hash.txt ?u?l?l?l?l?l?d?d?d?s
# 首字母大写 + 5小写 + 3数字 + 特殊字符
# 例如: Password123!

4.4 混合攻击 (Mode 6/7)

混合攻击
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Mode 6: 字典 + 掩码
# 在字典词后追加掩码
hashcat -m 0 -a 6 hash.txt wordlist.txt ?d?d?d?d
# password → password0000, password0001, ...

# Mode 7: 掩码 + 字典
# 在字典词前添加掩码
hashcat -m 0 -a 7 hash.txt ?d?d?d?d wordlist.txt
# password → 0000password, 0001password, ...

# 实用示例
# 常见模式: 单词 + 年份
hashcat -m 0 -a 6 hash.txt wordlist.txt ?d?d?d?d
# password2024, admin2023, ...

# 常见模式: 单词 + 特殊字符 + 数字
hashcat -m 0 -a 6 hash.txt wordlist.txt ?s?d?d
# password!23, admin@99, ...

五、规则引擎

5.1 规则语法

规则语法
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
# 基础规则
: # 不做任何修改
l # 转小写
u # 转大写
c # 首字母大写
C # 首字母小写,其余大写
t # 切换大小写
r # 反转字符串
d # 复制 (password → passwordpassword)
f # 镜像 (password → passworddrowssap)

# 追加/前置
$X # 追加字符X (password → passwordX)
^X # 前置字符X (password → Xpassword)

# 删除
[ # 删除首字符
] # 删除尾字符
DN # 删除位置N的字符

# 插入
iNX # 在位置N插入字符X

# 替换
sXY # 将X替换为Y
@X # 删除所有X字符

# 截取
'N # 截取前N个字符
xNM # 从位置N提取M个字符

# 复制
pN # 复制N次
zN # 复制首字符N次
ZN # 复制尾字符N次

# 位操作
kN # 交换位置N和N+1的字符
*NM # 交换位置N和M的字符

5.2 规则示例

实用规则
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
# 创建规则文件 custom.rule
cat > custom.rule << 'EOF'
:
l
u
c
$1
$!
$123
$2024
^1
c $1
c $!
c $123
l $1
l $!
sa@
se3
si1
so0
sa@ se3
c $1 sa@
EOF

# 使用规则
hashcat -m 0 -a 0 hash.txt wordlist.txt -r custom.rule

# 内置规则文件
# rules/best64.rule - 最常用的64条规则
# rules/rockyou-30000.rule - 基于rockyou分析的规则
# rules/d3ad0ne.rule - 综合规则集
# rules/dive.rule - 深度规则集
# rules/toggles1.rule - 大小写切换

# 多规则文件
hashcat -m 0 -a 0 hash.txt wordlist.txt -r rules/best64.rule -r rules/toggles1.rule

# 生成规则
hashcat -m 0 -a 0 hash.txt wordlist.txt --generate-rules=1000

5.3 Leet Speak规则

Leet规则
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# leet.rule - Leet Speak转换
cat > leet.rule << 'EOF'
:
sa@
se3
si1
so0
ss$
st7
sa@ se3
sa@ se3 si1
sa@ se3 si1 so0
sa4
se3 sa4
sl1
sg9
EOF

# 使用
hashcat -m 0 -a 0 hash.txt wordlist.txt -r leet.rule
# password → p@ssword, p@ssw0rd, p455w0rd, ...

六、实战场景

6.1 Windows密码破解

Windows NTLM
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 从SAM提取哈希 (使用mimikatz或secretsdump)
# 格式: username:RID:LM_hash:NTLM_hash:::

# NTLM哈希破解 (Mode 1000)
hashcat -m 1000 -a 0 ntlm_hashes.txt rockyou.txt

# 带规则
hashcat -m 1000 -a 0 ntlm_hashes.txt rockyou.txt -r rules/best64.rule

# 掩码攻击 (常见Windows密码模式)
hashcat -m 1000 -a 3 ntlm_hashes.txt ?u?l?l?l?l?l?d?d?d?s

# NTLMv2 (Mode 5600)
# 格式: username::domain:challenge:response:blob
hashcat -m 5600 -a 0 ntlmv2_hashes.txt rockyou.txt

# 从NTDS.dit提取的哈希
secretsdump.py -ntds ntds.dit -system SYSTEM LOCAL -outputfile hashes
hashcat -m 1000 -a 0 hashes.ntds rockyou.txt

6.2 Linux密码破解

Linux Shadow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# /etc/shadow格式
# username:$id$salt$hash:...
# $1$ = MD5
# $5$ = SHA256
# $6$ = SHA512
# $y$ = yescrypt

# SHA512crypt (Mode 1800) - 最常见
hashcat -m 1800 -a 0 shadow_hashes.txt rockyou.txt

# SHA256crypt (Mode 7400)
hashcat -m 7400 -a 0 shadow_hashes.txt rockyou.txt

# MD5crypt (Mode 500)
hashcat -m 500 -a 0 shadow_hashes.txt rockyou.txt

# 提取哈希
sudo cat /etc/shadow | grep -v ':\*:' | grep -v ':!:' > shadow_hashes.txt

# 使用unshadow合并
unshadow /etc/passwd /etc/shadow > combined.txt

6.3 Kerberos票据破解

Kerberoasting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Kerberos TGS-REP (Mode 13100)
# 从Rubeus或Impacket获取

# Rubeus输出格式
# $krb5tgs$23$*user$realm$spn*$hash...

hashcat -m 13100 -a 0 kerberos_hashes.txt rockyou.txt

# 带规则
hashcat -m 13100 -a 0 kerberos_hashes.txt rockyou.txt -r rules/best64.rule

# AS-REP Roasting (Mode 18200)
# $krb5asrep$23$user@domain:hash...
hashcat -m 18200 -a 0 asrep_hashes.txt rockyou.txt

# 优化参数
hashcat -m 13100 -a 0 kerberos_hashes.txt rockyou.txt -O -w 3
# -O: 优化内核
# -w 3: 高工作负载

6.4 WiFi密码破解

WPA/WPA2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 捕获握手包 (使用aircrack-ng)
airodump-ng wlan0mon -c 6 --bssid XX:XX:XX:XX:XX:XX -w capture

# 转换为hashcat格式
# 方法1: 使用hcxpcapngtool
hcxpcapngtool -o hash.hc22000 capture.cap

# 方法2: 使用cap2hccapx (旧格式)
cap2hccapx capture.cap hash.hccapx

# WPA/WPA2 PMKID/EAPOL (Mode 22000) - 新格式
hashcat -m 22000 -a 0 hash.hc22000 rockyou.txt

# 旧格式 (Mode 2500)
hashcat -m 2500 -a 0 hash.hccapx rockyou.txt

# 8位数字密码 (常见WiFi密码)
hashcat -m 22000 -a 3 hash.hc22000 ?d?d?d?d?d?d?d?d

# 手机号码模式
hashcat -m 22000 -a 3 hash.hc22000 1?d?d?d?d?d?d?d?d?d?d

6.5 文档密码破解

Office/PDF/ZIP
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
# 提取哈希 (使用john的*2john工具)
office2john.py document.docx > office_hash.txt
pdf2john.py document.pdf > pdf_hash.txt
zip2john archive.zip > zip_hash.txt
rar2john archive.rar > rar_hash.txt

# Office 2013+ (Mode 9600)
hashcat -m 9600 -a 0 office_hash.txt rockyou.txt

# Office 2010 (Mode 9500)
hashcat -m 9500 -a 0 office_hash.txt rockyou.txt

# Office 2007 (Mode 9400)
hashcat -m 9400 -a 0 office_hash.txt rockyou.txt

# PDF 1.4-1.6 (Mode 10500)
hashcat -m 10500 -a 0 pdf_hash.txt rockyou.txt

# PDF 1.7 (Mode 10600/10700)
hashcat -m 10700 -a 0 pdf_hash.txt rockyou.txt

# ZIP (Mode 13600)
hashcat -m 13600 -a 0 zip_hash.txt rockyou.txt

# 7-Zip (Mode 11600)
hashcat -m 11600 -a 0 7z_hash.txt rockyou.txt

七、性能优化

7.1 优化参数

性能优化
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
# 工作负载配置
-w 1 # 低 (可同时使用桌面)
-w 2 # 默认
-w 3 # 高 (专用破解)
-w 4 # 极限

# 优化内核
-O # 启用优化内核 (限制密码长度)

# GPU温度控制
--gpu-temp-abort=90 # 90°C时中止
--gpu-temp-retain=80 # 保持80°C以下

# 多GPU
-d 1,2,3 # 使用指定GPU
-D 1 # 仅使用GPU (1=CPU, 2=GPU, 3=FPGA)

# 会话管理
--session=mysession # 命名会话
--restore # 恢复会话
--status # 显示状态
--status-timer=10 # 状态更新间隔

# 输出控制
-o cracked.txt # 输出文件
--outfile-format=2 # 输出格式 (2=hash:plain)
--show # 显示已破解
--left # 显示未破解

7.2 分布式破解

分布式配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 使用--skip和--limit分割任务
# 机器1
hashcat -m 0 -a 3 hash.txt ?a?a?a?a?a?a --skip 0 --limit 1000000

# 机器2
hashcat -m 0 -a 3 hash.txt ?a?a?a?a?a?a --skip 1000000 --limit 1000000

# 使用Hashtopolis (分布式框架)
# 服务端部署
git clone https://github.com/hashtopolis/server.git
# 配置Web界面和数据库

# 客户端
python3 hashtopolis.py --url https://server/api --token TOKEN

八、防御建议

8.1 密码存储最佳实践

推荐 不推荐
Argon2id MD5, SHA1
bcrypt (cost≥12) 简单SHA256
scrypt 无盐哈希
PBKDF2 (iterations≥100000) 自定义哈希

8.2 密码策略建议

1
2
3
4
5
6
7
□ 最小长度: 12字符以上
□ 复杂度: 混合大小写、数字、特殊字符
□ 禁止常见密码: 使用密码黑名单
□ 禁止个人信息: 用户名、生日等
□ 定期更换: 90天或发现泄露时
□ MFA: 启用多因素认证
□ 密码管理器: 使用随机生成的强密码

九、总结

Hashcat是密码安全评估的核心工具,掌握其使用对于渗透测试和安全审计至关重要。关键要点:

  1. 选择正确的哈希模式:使用hashcat --help | grep -i "hash_name"查找
  2. 合理的攻击策略:字典→规则→混合→暴力
  3. 优化性能:GPU加速、优化内核、合理的工作负载
  4. 规则是关键:好的规则可以大幅提升成功率
  5. 了解目标:根据目标环境选择合适的字典和规则

注意:本文内容仅供授权的安全测试和学习研究使用,未经授权破解他人密码属于违法行为。