0%

base64隐写

这种base64隐写特征很明显,就是你会得到一大堆base64的字符串但却无法解码。写过这题就很简单,但base64的原理值得我们去研究一下。参考文章

生成正常的有等号的base64字符串

1
2
3
4
5
6
7
8
9
import random
import base64
import string
b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
with open('0.txt','wb+') as fp:
for i in range(100):
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 13))
base64str = base64.b64encode(ran_str)
fp.write(base64str + '\n')

加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding: cp936 -*-
import base64

flag = 'Tr0y{Base64isF4n}' # flag
bin_str = ''.join([bin(ord(c)).replace('0b', '').zfill(8) for c in flag])
base64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
with open('0.txt', 'rb') as f0, open('1.txt', 'wb') as f1: # '0.txt'是明文, '1.txt'用于存放隐写后的 base64
for line in f0.readlines():
rowstr = base64.b64encode(line.replace('\n', ''))
equalnum = rowstr.count('=')
if equalnum and len(bin_str):
offset = int('0b' + bin_str[:equalnum * 2], 2)
char = rowstr[len(rowstr) - equalnum - 1]
rowstr = rowstr.replace(char, base64chars[base64chars.index(char) + offset])
bin_str = bin_str[equalnum * 2:]
f1.write(rowstr + '\n')

解密

1
2
3
4
5
6
7
8
9
10
11
12
# -*- coding: cp936 -*-
b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
with open('1.txt', 'rb') as f:
bin_str = ''
for line in f.readlines():
stegb64 = ''.join(line.split())
rowb64 = ''.join(stegb64.decode('base64').encode('base64').split())
offset = abs(b64chars.index(stegb64.replace('=', '')[-1]) - b64chars.index(rowb64.replace('=', '')[-1]))
equalnum = stegb64.count('=') # no equalnum no offset
if equalnum:
bin_str += bin(offset)[2:].zfill(equalnum * 2)
print ''.join([chr(int(bin_str[i:i + 8], 2)) for i in xrange(0, len(bin_str), 8)]) # 8 位一组
1
2
github有个工具
https://github.com/jerrita/b64steg
-------------本文结束感谢您的阅读-------------