Base64编码和隐写术

0x01 Base64编码

0x01 简述

Base64编码是目前应用十分广泛的一种编码方式。其作用在于将非ASCII字符转换为ASCII字符进行传输,使得消息能顺利完整地从文本通道通过。

数据在传输过程中会经过许多各式各样的网关,而且不同地区的计算机使用的文本编码可能不同,其中含有的非ASCII字符可能会在传输途中发生错误,从而导致数据出错或丢失。比如电子邮件的附件中往往含有许多非ASCII字符,为了让这些数据安全地到达目的地,人们于是使用Base64对其进行编码。

0x02 原理

假设有这样一个字符串”El Psy Kongroo“,我们根据其对应的ASCII码转换为二进制数。

01000101 01101100 00100000 01010000 01110011 01111001 00100000 01001011 01101111 01101110 01100111 01110010 01101111 01101111

转换后的二进制数若不满8位则用0补满。将得到的各二进制数拼接起来,再每6位划为一组,若不满6位则用0补满。

于是得到

010001 010110 110000 100000 010100 000111 001101 111001 001000 000100 101101 101111 011011 100110 011101 110010 011011 110110 111100 000000(红色的0为补位用)

最后将每组二进制数分别转换为十进制数,并按照编码表转换为对应的ASCII字符。

那么这个字符串对应的Base64编码结果就是

RWwgUHN5IEtvbmdyb28=

Base64编码表
图片版原理

0x02 Base64隐写术

0x01 原理

我们知道,一段数据在进行Base64编码时,若最后的二进制数不满6位则需要用0补满,而在解码时这些用来填充空位的0会被舍弃,也就是说这些占位符并不会影响解码的结果。因此可以对其随意更改,解码的结果都是不变的。

再拿这个字符串为例子”El Psy Kongroo“,其二进制数最后两组补满后为111100 000000111100在编码表中对应的字符为 ’8‘000000对应的是 ’=‘ ,我们不能对000000进行修改,否则 ’=‘ 会变为其他字符。但我们可以对111100红色的部分进行修改,修改这个二进制数的最后两位只会让编码后的结果发生改变,而不会影响复原得到的原文。

例如,将111100改为111110,整段字符串的编码结果变成了“RWwgUHN5IEtvbmdyb2+=”,而对这段新得到的字符串进行Base64解码,得到的原文仍是”El Psy Kongroo“。

111101111111同理。

从上述操作我们可以得知,当一段Base64编码结果末尾有一个 ’=‘ 时,这段编码有2比特可以用来隐藏数据;当末尾有两个 ’=‘ 时,这段编码有4比特可以用来隐藏数据;而当末尾没有 ’=‘ 时,这段编码无法用来隐藏数据。

这里就是可以让我们隐藏数据的好地方。我们可以将一大段文字分段进行Base64编码,然后将想要隐藏的信息转换为比特流,按顺序藏在在每段末尾有 ’=‘ 的编码中。

当想要复原隐写的信息时,就需要将有 ’=‘ 的编码筛选出来并进行如下操作:如果是只有一个 ’=‘ 的编码,则提取最后一个非 ’=‘ 字符对应编码表的十进制数,将其转换为二进制并提取最后位比特;如果是有两个 ’=‘ 的编码,则提取最后一个非 ’=‘ 字符对应Base64编码表的十进制数,将其转换为二进制并提取最后位比特。最后,将提取出来的比特拼接,再每八位划为一组,转换为十进制数后按照ASCII码表还原出隐写的信息。

0x02 示例

本例题来自于攻防世界MISC009

题目给出一个base64.txt文件,其内容为

U3RlZ2Fub2dyYXBoeSBpcyB0aGUgYXJ0IGFuZCBzY2llbmNlIG9m
IHdyaXRpbmcgaGlkZGVuIG1lc3NhZ2VzIGluIHN1Y2ggYSB3YXkgdGhhdCBubyBvbmV=
LCBhcGFydCBmcm9tIHRoZSBzZW5kZXIgYW5kIGludGVuZGVkIHJlY2lwaWVudCwgc3VzcGU=
Y3RzIHRoZSBleGlzdGVuY2Ugb2YgdGhlIG1lc3M=
YWdlLCBhIGZvcm0gb2Ygc2VjdXJpdHkgdGhyb3VnaCBvYnNjdXJpdHkuIFS=
aGUgd29yZCBzdGVnYW5vZ3JhcGh5IGlzIG9mIEdyZWVrIG9yaWdpbiBhbmQgbWVhbnMgImNvbmNlYW==
bGVkIHdyaXRpbmciIGZyb20gdGhlIEdyZWVrIHdvcmRzIHN0ZWdhbm9zIG1lYW5pbmcgImNv
dmVyZWQgb3IgcHJvdGVjdGVkIiwgYW5kIGdyYXBoZWluIG1lYW5pbmcgInRvIHc=
cml0ZSIuIFRoZSBmaXJzdCByZWNvcmRlZCB1c2Ugb2YgdGhlIHRlcm0gd2FzIGluIDE0OTkgYnkgSm9o
YW5uZXMgVHJpdGhlbWl1cyBpbiBoaXM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0IHBob3RvcyB0aGF0IHNob3dlZCB0aGVtIHNtaQ==
bGluZyBhbmQgY29tZm9ydGFibGUuDQoNCi0tDQpodHRwOi8vZW4ud2lraXBlZGlhLm9yZw==
L3dpa2kvU3RlZ2Fub2dyYXBoeQ0K

直接进行Base64解码得到的结果是这样一段文字

        Steganography is the art and science of writing hidden messages in such a way that no one, apart from the sender and intended recipient, suspects the existence of the message, a form of security through obscurity.The word steganography is of Greek origin and means "concealed writing" from the Greek words steganos meaning
"covered or protected", and graphein meaning "to write". The first recorded use of the term was in 1499 by Johannes Trithemius in his Steganographia, a treatise on
 cryptography and steganography disguised as a book on magic. Generally, messages will appear to be something else: images, articles, shopping lists, or some other covertext and, classically, the hidden message may be in invisible ink between the visible lines of a private letter.

按照上一节所阐述的原理,可以写出对应的隐写复原脚本

#将字符按照Base64编码表转换为十进制数
def b64ord(char):
    if char in 'abcdefghijklmnopqrstuvwxyz':
        return ord(char) - 71
    elif char in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
        return ord(char) - 65
    elif char in '0123456789':
        return ord(char) + 4
    elif char == '+':
        return 62
    elif char == '/':
        return 63
    else: return

with open('C:/Users/shenn/Desktop/base64.txt', 'r') as f:
    ostr = f.read()

info = ''
flag = ''
count = 0

for i in ostr:
    if i == '\n':
        count += 1
ostr = ostr.splitlines()

for i in range(0, count):
    tmp = ''

    if '==' in ostr[i]:
        code = b64ord(ostr[i][-3])
        tmp = str(bin(code))[-4::]
        #由于python会在二进制数前加上'0b',所以采取以下操作去掉
        if code <= 1:
            tmp = '000' + tmp[-1]
        elif 2<= code <= 3:
            tmp = '00' + tmp[-2::]
        elif 4<= code <= 7:
            tmp = '0' + tmp[-3::]

    elif '=' in ostr[i]:
        code = b64ord(ostr[i][-2])
        tmp = str(bin(code))[-2::]
        if code <= 1:
            tmp = '0' + tmp[-1]
    
    info += tmp

#将得到的结果拼接起来
for i in range(0, len(info), 8):
    flag += chr(int(info[i:i+8], 2))
print(flag)

脚本运行结果为:

Base_sixty_four_point_five

至此,flag已到手。

-EOF-
这篇文章的作者Shennoter祝你心情愉快ღゝ◡╹)ノ♡♪(^∇^)
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇