-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
executable file
·75 lines (71 loc) · 1.64 KB
/
util.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python
import dis, marshal, pickle, readline
import types
def pr_co(co):
for i in map(lambda i: i[3:], filter(lambda i: i.startswith("co_"), dir(co))):
j = getattr(co, "co_" + i)
t = type(j)
if t == bytes:
j = list(j)
elif i == "flags":
j = dis.pretty_flags(j)
print(f"{i}: ({t.__name__}) {j}")
print("""
Choose:
1) Print all bytecode instructions
2) Marshal any object
3) Pickle any object
4) Compile and disassemble code
5) Compile and disassemble a function
""")
inp = input("> ")
if inp == "1":
ls = []
for i in dis.opmap:
op = dis.opmap[i]
cur = f"{i} [{op:3}]"
ls.append(cur)
maxlen = max(map(len, ls))
for i in ls:
print(i.rjust(maxlen))
elif inp == "2":
c = eval(input(">>> "))
print(marshal.dumps(c).hex())
elif inp == "3":
c = eval(input(">>> "))
print(pickle.dumps(c).hex())
elif inp == "4":
print("enter \"::\" to stop")
ls = []
while 1:
c = input(">>> ")
if c == "::":
break
ls.append(c)
ls = "\n".join(ls)
c = compile(ls, "<asm>", "exec")
try:
dis.dis(c, show_caches = True)
except:
dis.dis(c)
pr_co(c)
elif inp == "5":
print("enter \"::\" to stop")
ls = []
while 1:
c = input(">>> " if ls else ">>> def func(")
if c == "::":
break
ls.append(c)
ls = "\n".join(ls)
ls = "def func("+ls
print(ls)
exec(ls)
c = func
try:
dis.dis(c, show_caches = True)
except:
dis.dis(c)
pr_co(c.__code__)
else:
print("Invalid input!")