赛事:第三届 PCTF2026 新生赛 | 方向:Reverse
Flag:PCTF{Py_pY_u_Ar3_s0_B3@ut1fuI}
一、题目信息
二、解题思路
先确认打包方式:
main.exe尾部有MEI标记、导入python313.dll→ PyInstaller onefile。用
pyinstxtractor-ng解包,得到main.pyc和PYZ.pyz。反汇编
main.pyc:只做三件事 —— 打印诗句、input()读口令、调用who_is_nailong.confirm(emperor)。pyd 只有
PyInit_who_is_nailong一个导出,真正的校验逻辑不在 C 里;pyd 的模块属性里有stuct,而PYZ里正好有stuct.pyc。stuct.pyc是一个「二叉树十六进制编码」模块:run_it()的默认参数就是一段硬编码 hex,解码即 flag;answer(buf)则用encode(buf) == 硬编码 hex做校验。
三、关键步骤
解包:
python pyinstxtractor-ng.py main.exe
# 得到 main.exe_extracted/,其中 main.pyc、PYZ.pyz看 main.pyc(Python 3.13 的
dis):
IMPORT_NAME who_is_nailong
... print(诗句) ...
emperor = input("请问当时您的口令为:")
if who_is_nailong.confirm(emperor): print("极道万岁!")看 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。解码 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}确认 pyd 只是桥接:把
stuct.answer换成lambda s: True后,who_is_nailong.confirm("x")立刻从False变True——confirm内部就是调stuct.answer()。
而stuct.encode(flag)正好等于answer里的硬编码常量6167096748...。验证:
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()