picoCTF - RE - Transformation writeup

本文探讨了Python中将字符串转化为二进制文件的过程,并详细解析了解码步骤。通过使用chr()和ord()函数,将读取的二进制数据还原为原始字符串。强调了Unicode编码在文件存储中的应用,以及ASCII与Unicode的区别。同时提醒读者在处理文件时要注意字符编码的正确读写方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 一份 binary 文件
  • ''.join([chr((ord(flag[i]) << 8) + ord(flag[i + 1])) for i in range(0, len(flag), 2)])

上面是把 flag 转化为 binary 的过程,需要还原。

解题如下:

enc = open("enc", "r")
buf = enc.read()
for c in buf:
	print(chr(ord(c)>>8),end="")
	print(chr(ord(c)-(ord(c)>>8<<8)),end="")

要点:

  1. 擅用chrord函数。
    ord() in Python Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the argument is an 8-bit string. 即把 unicode 和 ascii 转化为数字。
    chr() return a string of one character whose ASCII code or unicode is the integer i.
  2. 字符编码和读取、写入文件。
    注意这道题的 flag 在编码之后,每个字符是16位,也就是得到的是 unicode 字符,然后写入文件。所以读取的时候也要按 str 读取,不能是以 bytes rb 读取。
    另外,ASCII 码对应的 bytes 和它本身是一样的,但 Unicode 不是。比如一个字符a,它存储到文件中,hexdump得到的结果是\x61,但是一个 Unicode 字符\u7069,它存储到文件中 bytes 内容为 \xe7\x81\xa9

关于 Unicode encoding 的内容可以参考 UTF-8 Wikipedia

First code pointLast code pointByte 1Byte 2Byte 3Byte 4
U+0000U+007F0xxxxxxx
U+0080U+07FF110xxxxx10xxxxxx
U+0800U+FFFF1110xxxx10xxxxxx10xxxxxx
U+10000U+10FFFF11110xxx10xxxxxx10xxxxxx10xxxxxx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值