Misc — Snake 题总结

记录从 JPG 中提取隐藏 ZIP、分析 Base64 提示并使用 Serpent 解密得到 flag 的过程。

  • #CTF
  • #Misc
  • #文件分析
  • #Serpent

一、题目整体思路

这道题的完整链条:

snake.jpg
   │
   ├── 普通 JPG 数据
   │
   └── 隐藏 ZIP
          │
          ├── key
          │     │
          │     └── Base64
          │             ↓
          │       一道英文问题
          │             ↓
          │         Anaconda
          │
          └── cipher
                │
                └── Serpent 解密
                        │
                        └── flag

核心考点:文件分析 → 文件偏移 → 提取隐藏文件 → Base64 → 根据提示得到 Key → Serpent 解密。

二、查看图片内容和分解有价值文件

1. 发现 JPG 中藏了东西

首先使用 binwalk 分析文件内部结构,寻找隐藏或嵌套的文件、压缩包和文件头:

binwalk snake.jpg

发现 key.zip 和 cipher.zip 这两个隐藏文件在图片内。

2. 使用 dd 提取隐藏 ZIP

dd if=snake.jpg of=key.zip bs=1 skip=278260

3. 查看并解压压缩包

unzip -l key.zip
unzip key.zip
unzip cipher.zip

也可以使用 -d 指定解压目录。

4. 分析 key

file key
cat key

file 显示它是 ASCII 文本,并使用 Windows 风格的 CRLF 换行。读取后得到一串类似 Base64 的数据,解码后得到:

What is Nicki Minaj's favorite song that refers to snakes?

答案为 anaconda,这就是后续解密使用的 key。

5. 分析 cipher

和上面的指令一样尝试读取,但不能正常得到有效内容。这时可以使用:

strings cipher
xxd -g 1 cipher

strings 用于从二进制文件中提取连续的可打印字符串,xxd -g 1 则按单字节查看原始十六进制数据。

得到密文:

dc44158cd6a283b543b412f716a7d1fdd210d8eb9e8937e23560f9ee240131bf1ce75cabb68ebfda83730c728dbc748d

三、根据题目确定加密方式并解密

根据题目提示确定加密方式为 Serpent,构造 Python 脚本进行解密:

from pyserpent import Serpent

hex_cipher = "dc44158cd6a283b543b412f716a7d1fdd210d8eb9e8937e23560f9ee240131bf1ce75cabb68ebfda83730c728dbc748d"
ciphertext = bytes.fromhex(hex_cipher)

key = b"anaconda"
while len(key) not in (16, 24, 32):
    key += b"\x00"

cipher = Serpent(key)
plaintext_bytes = cipher.decrypt(ciphertext)

print("解密原始字节:", plaintext_bytes)
print("解密文本内容:", plaintext_bytes.decode("utf-8", errors="ignore"))