hackergame2020-writeups icon indicating copy to clipboard operation
hackergame2020-writeups copied to clipboard

自复读的复读机-栈帧解法

Open zkonge opened this issue 4 years ago • 0 comments

看到题目一开始的想法是试着去找 input 读入变量的值。 但是把源码拖下来一看,exec(input()),~~这没法玩啊~~。

这行代码的字节码:

  1           0 LOAD_NAME                0 (exec)
              2 LOAD_NAME                1 (input)
              4 CALL_FUNCTION            0
              6 CALL_FUNCTION            1
              8 RETURN_VALUE

Python 内部完全没提供访问这个 input 输入临时字符串的功能。

str 类型不受 gc 跟踪,代表 gc.get_objects() 没法用,又因为这是个中间变量,也不存在于 globals,locals 里,代码对象也和这玩意没关系。

本来想着试试 ctypes 到处摸一摸,但在写的时候发现在 exec 中有更方便的解法。

exec 本身就是一个确定的代码对象,可以从栈帧上找到这个代码对象和它的常量等信息,稍微好做一点。

inner_code = b'''import inspect
import hashlib
d=inspect.currentframe().f_back.f_code.co_consts[0]
inp=f"exec(bytes.fromhex('{d}').decode())"
print(hashlib.sha256(inp.encode()).hexdigest(),end='')
'''.hex()

print(f'exec(bytes.fromhex(\'{inner_code}\').decode())')

(从大佬那学到了从mem里翻出输入字符串 大佬tql

zkonge avatar Nov 07 '20 16:11 zkonge