pickle – Pickle tools

pwnypack.pickle.pickle_invoke(func, *args, target=None, protocol=None)[source]

Create a byte sequence which when unpickled calls a callable with given arguments.

Note

The function has to be importable using the same name on the system that unpickles this invocation.

Parameters:
  • func (callable) – The function to call or class to instantiate.
  • args (tuple) – The arguments to call the callable with.
  • target – The internals description of the targeted python version. If this is None the specification of the currently running python version will be used.
  • protocol – The pickle protocol version to use (use None for default).
Returns:

The data that when unpickled calls func(*args).

Return type:

bytes

Example

>>> from pwny import *
>>> import pickle
>>> def hello(arg):
...     print('Hello, %s!' % arg)
...
>>> pickle.loads(pickle_invoke(hello, 'world'))
Hello, world!
pwnypack.pickle.pickle_func(func, *args, target=None, protocol=None, b64encode=None)[source]

Encode a function in such a way that when it’s unpickled, the function is reconstructed and called with the given arguments.

Note

Compatibility between python versions is not guaranteed. Depending on the target python version, the opcodes of the provided function are transcribed to try to maintain compatibility. If an opcode is emitted which is not supported by the target python version, a KeyError will be raised.

Constructs that are known to be problematic:

  • Python 2.6 and 2.7/3.0 use very different, incompatible opcodes for conditional jumps (if, while, etc). Serializing those is not always possible between python 2.6 and 2.7/3.0.
  • Exception handling uses different, incompatible opcodes between python 2 and 3.
  • Python 2 and python 3 handle nested functions very differently: the same opcode is used in a different way and leads to a crash. Avoid nesting functions if you want to pickle across python functions.
Parameters:
  • func (callable) – The function to serialize and call when unpickled.
  • args (tuple) – The arguments to call the callable with.
  • target – The internals description of the targeted python version. If this is None the specification of the currently running python version will be used.
  • protocol (int) – The pickle protocol version to use.
  • b64encode (bool) – Whether to base64 certain code object fields. Required when you prepare a pickle for python 3 on python 2. If it’s None it defaults to False unless pickling from python 2 to python 3.
Returns:

The data that when unpickled calls func(*args).

Return type:

bytes

Example

>>> from pwny import *
>>> import pickle
>>> def hello(arg):
...     print('Hello, %s!' % arg)
...
>>> p = pickle_func(hello, 'world')
>>> del hello
>>> pickle.loads(p)
Hello, world!