-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathchallenge.py
50 lines (40 loc) · 970 Bytes
/
challenge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/local/bin/python
import sys
import os
def wrapper(exit):
def hook(event, args):
for x in (
"import",
"spawn",
"process",
"os",
"sys",
"cpython",
"fork",
"open",
"interpreter",
"ctypes",
"compile",
"gc",
"__new__",
):
if x in (event + "".join(f"{x}" for x in args)).lower():
exit(0)
return hook
# read user input and disallow dangerous things
inp = input("Input: ")
try:
inp.encode("ascii")
except UnicodeEncodeError:
print("ASCII only pls!!!")
exit()
if "__" in inp:
print("No __ pls!!!")
exit()
# compile it into code object
code = compile(inp, "<usercode>", "exec")
# just be extra safe that no one can escape
sys.addaudithook(wrapper(os._exit))
# run user code in a sandbox
exec(code, {"__builtins__": None})
print("Bye")