Aleer
Aleer
发布于 2026-11-13 / 10 阅读
0
0

RE - 奶龙大帝

赛事:第三届 PCTF2026 新生赛 | 方向:Reverse

Flag:PCTF{Py_pY_u_Ar3_s0_B3@ut1fuI}

一、题目信息

项

内容

附件

main.exe(8,138,419 B,PyInstaller onefile)

考点

PyInstaller 解包、Python 字节码、C 扩展(pyd)与内嵌模块 stuct

环境

Python 3.13 + Windows x64

二、解题思路

  1. 先确认打包方式:main.exe 尾部有 MEI 标记、导入 python313.dll → PyInstaller onefile。

  2. 用 pyinstxtractor-ng 解包,得到 main.pyc 和 PYZ.pyz。

  3. 反汇编 main.pyc:只做三件事 —— 打印诗句、input() 读口令、调用 who_is_nailong.confirm(emperor)。

  4. pyd 只有 PyInit_who_is_nailong 一个导出,真正的校验逻辑不在 C 里;pyd 的模块属性里有 stuct,而 PYZ 里正好有 stuct.pyc。

  5. stuct.pyc 是一个「二叉树十六进制编码」模块:run_it() 的默认参数就是一段硬编码 hex,解码即 flag;answer(buf) 则用 encode(buf) == 硬编码 hex 做校验。

三、关键步骤

  1. 解包:

python pyinstxtractor-ng.py main.exe
# 得到 main.exe_extracted/,其中 main.pyc、PYZ.pyz
  1. 看 main.pyc(Python 3.13 的 dis):

IMPORT_NAME who_is_nailong
... print(诗句) ...
emperor = input("请问当时您的口令为:")
if who_is_nailong.confirm(emperor): print("极道万岁!")
  1. 看 pyd:导出表只有 PyInit_who_is_nailong;.rdata 有
    confirm(str buf: str) -> bool、this function from stuct imported by user。
    dir(who_is_nailong) 里有 stuct —— 逻辑被挪到了 stuct.pyc。

  2. 解码 stuct:run_it(encoded, depth=2, expected_len=30) 按平衡二叉树切分 hex,
    每片前 2 位转 int 后
    chr(val ^ depth*7),最后 2 位是 checksum。默认 encoded 就是 flag 的编码:

run_it("6e01080273080919426e08080b4457084c7808094471080045...", 2, 30)
# -> PCTF{Py_pY_u_Ar3_s0_B3@ut1fuI}
  1. 确认 pyd 只是桥接:把 stuct.answer 换成 lambda s: True 后,who_is_nailong.confirm("x") 立刻从 False 变 True —— confirm 内部就是调 stuct.answer()。
    而
    stuct.encode(flag) 正好等于 answer 里的硬编码常量 6167096748...。

  2. 验证:who_is_nailong.confirm(flag) 返回 True;用 3.13 跑 main.pyc 并输入 flag,输出「极道万岁!」。

四、拿到 Flag

PCTF{Py_pY_u_Ar3_s0_B3@ut1fuI}

五、Exp

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""who_is_nailong:还原 stuct.run_it 的二叉树 hex 编码

用法:python "Reverse - who_is_nailong - exp.py"
"""
ENC = ("6e01080273080919426e08080b4457084c7808094471080045"
       "08090c124a7708486108097a657208080b6e440843700809416e"
       "086e6808090c1220")
EXPECTED_LEN = 30
DEPTH = 2


def get_encoded_length(n, depth=2):
    """子树编码后的 hex 长度。"""
    if n <= 1:
        return 2
    mid = n // 2
    return get_encoded_length(mid, depth + 1) + get_encoded_length(n - mid, depth + 1) + 2


def run_it(encoded, depth=2, expected_len=None):
    """stuct.run_it 的等价实现:平衡二叉树 + 每层 XOR depth*7。"""
    if expected_len is not None and expected_len <= 1:
        if expected_len == 0:
            return ""
        return chr(int(encoded[:2], 16) ^ (depth * 7))

    body = encoded[:-2]          # 末 2 位是 checksum
    mid = expected_len // 2
    left_len, right_len = mid, expected_len - mid
    left_enc_len = get_encoded_length(left_len, depth + 1)
    right_enc_len = get_encoded_length(right_len, depth + 1)

    # depth 为偶数时右子树在前,奇数时左子树在前(编码时就交换过)
    if depth % 2 == 0:
        right_encoded, left_encoded = body[:right_enc_len], body[right_enc_len:]
    else:
        left_encoded, right_encoded = body[:left_enc_len], body[left_enc_len:]

    return run_it(left_encoded, depth + 1, left_len) + run_it(right_encoded, depth + 1, right_len)


def main():
    print("[*] encoded len:", len(ENC), "expected:", get_encoded_length(EXPECTED_LEN, DEPTH))
    flag = run_it(ENC, DEPTH, EXPECTED_LEN)
    print("[+] flag:", flag)


if __name__ == "__main__":
    main()


评论