神奇的 Base64 隐写
俄罗斯有个叫 Olympic_ctf 的 CTF,在 2014 年有道 misc 题是关于 Base64 的隐写题. 做完真是感觉无处不能用于隐写
题目
大意就是给了你一段字符串,让你找 flag.
这里放上字符串,便于读者实验
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109U3RlZ2Fub2dyYXBoeSBpcyB0aGUgYXJ0IGFuZCBzY2llbmNlIG9m
IHdyaXRpbmcgaGlkZGVuIG1lc3NhZ2VzIGluIHN1Y2ggYSB3YXkgdGhhdCBubyBvbmV=
LCBhcGFydCBmcm9tIHRoZSBzZW5kZXIgYW5kIGludGVuZGVkIHJlY2lwaWVudCwgc3VzcGU=
Y3RzIHRoZSBleGlzdGVuY2Ugb2YgdGhlIG1lc3M=
YWdlLCBhIGZvcm0gb2Ygc2VjdXJpdHkgdGhyb3VnaCBvYnNjdXJpdHkuIFS=
aGUgd29yZCBzdGVnYW5vZ3JhcGh5IGlzIG9mIEdyZWVrIG9yaWdpbiBhbmQgbWVhbnMgImNvbmNlYW==
bGVkIHdyaXRpbmciIGZyb20gdGhlIEdyZWVrIHdvcmRzIHN0ZWdhbm9zIG1lYW5pbmcgImNv
dmVyZWQgb3IgcHJvdGVjdGVkIiwgYW5kIGdyYXBoZWluIG1lYW5pbmcgInRvIHc=
cml0ZSIuIFRoZSBmaXJzdCByZWNvcmRlZCB1c2Ugb2YgdGhlIHRlcm0gd2FzIGluIDE0OTkgYnkgSm9o
YW5uZXMgVHJpdGhlbWl1cyBpbiBoaXMgU3RlZ2Fub2dyYXBoaWEsIGEgdHJlYV==
dGlzZSBvbiBjcnlwdG9ncmFwaHkgYW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uDQoNCi0tDQpodHRwOi8vZW4ud2lraXBlZGlhLm9yZw==
L3dpa2kvU3RlZ2Fub2dyYXBoeQ0K
复习一下 Base64 吧
BASE64 是一种编码方式,是一种可逆的编码方式.
编码后的数据是一个字符串,包含的字符为: A-Za-z0-9+/
共 64 个字符:26 + 26 + 10 + 1 + 1 = 64
其实是 65 个字符,=
是填充字符.
64 个字符需要 6 位二进制来表示,表示成数值为 0 ~ 63
。
这样,长度为 3 个字节的数据经过 Base64 编码后就变为 4 个字节
编码
比如,字符串 Tr0
经过 Base64 编码后变为 VHIw
上面说的字符串长度为 3 个字节的数据位数是 8x3=24
,可以精确地分成 6x4
.
如果字节数不是 3 的倍数,则位数就不是 6 的倍数,那么就不能精确地划分成 6 位的块.
此时,需在原数据二进制值后面添加零,使其字节数是 6 的倍数.
然后,在编码后的字符串后面添加 1 个或 2 个等号,表示所添加的零值字节数.
比如,字符串 Tr0y
经过 Base64 编码后变为 VHIweQ==
橙色底纹就是添加的 0
这是 Base64 编码的方式
解码
解码就是编码的逆过程。
- 把 Base64 字符串去掉等号,转为二进制数(
VHIweQ==
->VHIweQ
->010101000111001000110000011110010000
) - 从左到右,8 个位一组,多余位的扔掉,转为对应的 ASCII 码(
01010100 01110010 00110000 01111001 0000
-> 扔掉最后 4 位 ->01010100 01110010 00110000 01111001
->Tr0y
)
隐写原理
注意红色的 0,我们在解码的时候将其丢弃了,所以这里的值不会影响解码. 所以我们可以在这进行隐写。为什么等号的那部分 0 不能用于隐写?因为修改那里的二进制值会导致等号数量变化,解码的第 1 步会受影响。自然也就破坏了源字符串。而红色部分的 0 是作为最后一个字符二进制的组成部分,还原时只用到了最后一个字符二进制的前部分,后面的部分就不会影响还原。
唯一的影响就是最后一个字符会变化。如下图
如果你直接解密VHIweQ==
与VHIweR==
',得到的结果都是'Tr0y'。
当然,一行 base64 顶多能有 2 个等号,也就是有 2*2
位的可隐写位。所以我们得弄很多行,才能隐藏一个字符串,这也是为什么题目给了一大段 base64 的原因。接下来,把要隐藏的 flag 转为 8 位二进制,塞进去就行了。
加密
1 |
|
解密
1 |
|
Emmmmmm
很多国外的 CTF 出题思路异常开阔,值得好好学学。
最后附上这道题的答案吧
话说貌似 ZJPCCTF 也出过这种题。
来呀快活呀