# <frame at 0x0000017C309E7C40, file 'test', line 8, code waff> # <frame at 0x0000017C520AF560, file 'test', line 13, code <module>> # <frame at 0x0000017C521A0040, file 'C:\\Users\\20820\\PycharmProjects\\shizhan\\bb.py', line 19, code <module>> # this is flag
首先,生成器的栈帧是 g ,经过一次回溯 之后到了waff的栈帧,再回溯一次之后到了exec的栈帧,这个时候访问f_globals就到了exec再上一层 的作用域,也就是全局变量 。
另一种情况:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
s3cret="this is flag"
codes=''' def waff(): def f(): yield g.gi_frame.f_back q = (q.gi_frame.f_back.f_back.f_back.f_globals for _ in [1]) b = [*q][0]["s3cret"] return b b=waff() ''' locals={} code = compile(codes, "test", "exec") exec(code,locals) print(locals["b"])
""" Check the source with pure string in string, prevent dangerous strings :param source: source code :return: None """
from sys import exit from builtins importprint
try: source.encode("ascii") except UnicodeEncodeError: print("non-ascii is not permitted") exit()
for i in ["__", "getattr", "exit"]: if i in source.lower(): print(i) exit()
defblock_wrapper(): """ Check the run process with sys.audithook, no dangerous operations should be conduct :return: None """
defaudit(event, args):
from builtins importstr, print import os
for i in ["marshal", "__new__", "process", "os", "sys", "interpreter", "cpython", "open", "compile", "gc"]: if i in (event + "".join(str(s) for s in args)).lower(): print(i) os._exit(1) return audit
defsource_opcode_checker(code): """ Check the source in the bytecode aspect, no methods and globals should be load :param code: source code :return: None """
from dis import dis from builtins importstr from io import StringIO from sys import exit
opcodeIO = StringIO() dis(code, file=opcodeIO) opcode = opcodeIO.getvalue().split("\n") opcodeIO.close() for line in opcode: ifany(x instr(line) for x in ["LOAD_GLOBAL", "IMPORT_NAME", "LOAD_METHOD"]): ifany(x instr(line) for x in ["randint", "randrange", "print", "seed"]): break print("".join([x for x in ["LOAD_GLOBAL", "IMPORT_NAME", "LOAD_METHOD"] if x instr(line)])) exit()
if __name__ == "__main__":
from builtins importopen from sys import addaudithook from contextlib import redirect_stdout from random import randint, randrange, seed from io import StringIO from random import seed from time import time