csaw-ctf-2016-quals_sleeping-guard

k e y w o r d s : keywords: keywords: 文件头异或得到key

D e s c r i p t i o n Description Description

import base64
from twisted.internet import reactor, protocol
import os
 
PORT = 9013
 
import struct
def get_bytes_from_file(filename):
    return open(filename, "rb").read()
 
KEY = "[CENSORED]"
 
def length_encryption_key():
    return len(KEY)
 
def get_magic_png():
    image = get_bytes_from_file("./sleeping.png")
    encoded_string = base64.b64encode(image)
    key_len = length_encryption_key()
    print 'Sending magic....'
    if key_len != 12:
        return ''
    return encoded_string
 
class MyServer(protocol.Protocol):
    def connectionMade(self):
        resp = get_magic_png()
        self.transport.write(resp)
 
class MyServerFactory(protocol.Factory):
    protocol = MyServer
 
    factory = MyServerFactory()
    reactor.listenTCP(PORT, factory)
    reactor.run()

PS: xctf中没有这段代码描述(但是实际上flag也是可以做出来的)

只有真正的hacker才能看到这张图片

A n a l y s i s Analysis Analysis

nc得到一串base64字符

解码得到的是乱码

现在来看看题目给出的代码描述了什么过程

def get_magic_png():
    image = get_bytes_from_file("./sleeping.png")
    encoded_string = base64.b64encode(image)
    key_len = length_encryption_key()
    print 'Sending magic....'
    if key_len != 12:
        return ''
    return encoded_string

没有提及加密的过程而是直接将原图像的数据转换为base64写入了文件

但是这里又提到了key,那么一定是加密了但是加密过程没有写上代码

而且key_len==12

现在来排除可能的加密方式,首先需要这样的密钥的加密方式:在古典密码中,可能就是维吉尼亚密码;现代密码就是常见的分组密码AES,DES以及一些衍生的小众加密

排除一下,首先当我们把base64解码后得到字符不是分组加密得到的密文长度,所以排除

维吉尼亚密码加密的是字母而不是这里的乱码

这里我们得到的密文是几乎与图像长度等长的乱码,又需要key

很可能是异或

那么由于图像文件格式就只有几种,那么密文开头解密之后一定是独特的文件头格式

所以将已知的文件头格式与密文异或,查看结果是否为有意义的字符串,那么这就是key,而由于key_len==12,所以我们可以只截取 12 12 12位的密文与文件头格式进行异或

最后是png图片的文件头格式异或成功得到有意义的字符串

那么key=="WoAh_A_Key!?"

之后就扩充key到密文长度进行异或即可

S o l v i n g   c o d e Solving~code Solving code

import base64
from Crypto.Util.strxor import strxor
from tqdm import tqdm

f = open("cipher.txt","r")
cipher = f.read()
txt = base64.b64decode(cipher)
f.close()
# print(list(txt))
key = "WoAh_A_Key!?"
key = (key * (len(txt) // len(key) + 1))[:len(txt)]
result= []
ls1 = list(txt)
ls2 = list(map(ord,list(key)))
for i in tqdm(range(len(txt))):
    result.append(ls1[i] ^ ls2[i])
result = bytearray(result)

print(result)
f = open("plaint.png","wb")
f.write(result)

R e f e r e n c e Reference Reference

CSAW 2016]Sleep Guard Writeup – Megabeets

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

M3ng@L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值