defsource_simple_check(source): """ 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
defwaff(): deff(): yield g.gi_frame.f_back g = f() frame = [x for x in g][0] runner_frame = frame.f_back.f_back.f_back str=runner_frame.f_globals["_"+"_bui"+"ltins_"+"_"].str code=runner_frame.f_code.co_consts for i instr(code): print(i,end=',') waff()