# YAML

## Exploitation

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

#### Python 2

```
import yaml

# Input can be whater text or a file with this content
yaml.load(input)
```

#### Python 3

In Python 3, the default loader changed to a safe Loader, and to exploit this vulnerability should be enable UnsafeLoader explicitly.&#x20;

```
import yaml

# Input can be whater text or a file with this content
yaml.load(input, Loader=yaml.UnsafeLoader)
```

Example payload to exploit this vulnerability in a file sample.yaml os a direct input if allowed.

```
!!python/object/apply:os.system ["cat /etc/passwd"]
```

## Fix

Fixing this vulnerability is relatively easy.

Replace the usage of `yaml.load()` function with `yaml.safe_load()`&#x20;

In Python 3, `yaml.load()` uses as default data Loader `FullLoader` which avoids code execution.


---

# 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/yaml.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.
