哈希函数(Hash Function,意译为 “散列函数”)是一种从任何一种数据中创建小的数字 “指纹” 的方法。散列函数把消息或数据计算成摘要 (Digest),使得数据量变小,将数据的格式固定下来。该函数将数据打乱混合,重新创建一个叫做哈希值(Hash value,又叫散列值)的指纹。

为什么叫 “Hash”?

麦当劳售卖薯饼 (Hash Brown) 早餐,即将一整个马铃薯削成丝状,再将丝状马铃薯重新油炸成一整个薯饼。由于本质上是将原先的一个整体打散再重新组合且过程不可逆,正好符合该类算法的设计思路,于是称该类算法为哈希 (Hash) 算法。

哈希算法如何保护密码?

用户注册时,密码先在前端进行哈希,哈希值传回后端数据库存储。用户登录时,输入的密码在前端进行哈希,然后传回后端进行比较。

有时,为了增强哈希算法的抗碰撞性,我们会给明文加上 “盐值”🧂。盐值会设置成一个随机的字符串,然后和明文进行拼接。由于加盐后的明文非常长,因此几乎不可能被碰撞。下面的 CTF 示例就有加盐的 MD5。

MD5 信息摘要算法 (MD5 Message-Digest Algorithm) 是 MD 系列算法的最常见版本。算法会生成一个 128 位的哈希值(长度为 32 的十六进制值)

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// : All variables are unsigned 32 bit and wrap modulo 2^32 when calculating
var int s[64], K[64]
var int i

// s specifies the per-round shift amounts
s[ 0..15] := { 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22 }
s[16..31] := { 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20 }
s[32..47] := { 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23 }
s[48..63] := { 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 }

// Use binary integer part of the sines of integers (Radians) as constants:
for i from 0 to 63 do
K[i] := floor(232 × abs(sin(i + 1)))
end for
// (Or just use the following precomputed table):
K[ 0.. 3] := { 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee }
K[ 4.. 7] := { 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501 }
K[ 8..11] := { 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be }
K[12..15] := { 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821 }
K[16..19] := { 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa }
K[20..23] := { 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8 }
K[24..27] := { 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed }
K[28..31] := { 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a }
K[32..35] := { 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c }
K[36..39] := { 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70 }
K[40..43] := { 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05 }
K[44..47] := { 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665 }
K[48..51] := { 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039 }
K[52..55] := { 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1 }
K[56..59] := { 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1 }
K[60..63] := { 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 }

// Initialize variables:
var int a0 := 0x67452301 // A
var int b0 := 0xefcdab89 // B
var int c0 := 0x98badcfe // C
var int d0 := 0x10325476 // D

// Pre-processing: adding a single 1 bit
append "1" bit to message<
// Notice: the input bytes are considered as bit strings,
// where the first bit is the most significant bit of the byte.[52]

// Pre-processing: padding with zeros
append "0" bit until message length in bits ≡ 448 (mod 512)

// Notice: the two padding steps above are implemented in a simpler way
// in implementations that only work with complete bytes: append 0x80
// and pad with 0x00 bytes so that the message length in bytes ≡ 56 (mod 64).

append original length in bits mod 264 to message

// Process the message in successive 512-bit chunks:
for each 512-bit chunk of padded message do
break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15
// Initialize hash value for this chunk:
var int A := a0
var int B := b0
var int C := c0
var int D := d0
// Main loop:
for i from 0 to 63 do
var int F, g
if 0 ≤ i ≤ 15 then
F := (B and C) or ((not B) and D)
g := i
else if 16 ≤ i ≤ 31 then
F := (D and B) or ((not D) and C)
g := (5×i + 1) mod 16
else if 32 ≤ i ≤ 47 then
F := B xor C xor D
g := (3×i + 5) mod 16
else if 48 ≤ i ≤ 63 then
F := C xor (B or (not D))
g := (7×i) mod 16
// Be wary of the below definitions of a,b,c,d
F := F + A + K[i] + M[g] // M[g] must be a 32-bit block
A := D
D := C
C := B
B := B + leftrotate(F, s[i])
end for
// Add this chunk's hash to result so far:
a0 := a0 + A
b0 := b0 + B
c0 := c0 + C
d0 := d0 + D
end for

var char digest[16] := a0 append b0 append c0 append d0 // (Output is in little-endian)

MD5 算法于 1993 年发现 “伪碰撞”,1996 年发现部分碰撞。2004 年,中国的王小云教授宣布发现了 MD5 的完整碰撞,该算法被证明不具有抗碰撞性。

如果自己写破解脚本,需要通过逆向知道原文的构成规律 —— 个人计算机大概率是无法承受完全穷举的工作量的,所以我们需要有目的、有限制地爆破 Hash。下面举几个例子:

这个比赛在 2025/7/12 进行。当时我们发现直接运行程序,不会输出 query: ...,这个 (0x686d4080 < *(longlong *)(unaff_RBP + 200)) && (*(longlong *)(unaff_RBP + 200) < 1752052052) 条件在控制。

向上查看发现 unaff_RBP + 200 实际上是从 FUN_140001493 中的 _time64 得到的 UNIX 时间戳。转换发现条件是本机 UTC 时间为 2025/7/9 00:00:00 - 2025/7/9 09:07:32 时,执行函数体并输出该时刻的查询字符串。此时断网并调整本机时间。