Python Cache Poisoning — Elevating Your Privileges with Malicious Bytecode
Python's bytecode cache can be poisoned by replacing a compiled .pyc file with a malicious one that matches the original's header fields, allowing arbitrary code execution when the module is imported. If a user can write to the __pycache__ folder and run Python with elevated privileges, this can lead to privilege escalation.
Published on python.plainenglish.io
Python cache poisoning is mechanically simple, and exploitable under specific — and unfortunately still common — misconfigurations. In certain situations, as shown by a recent HackTheBox challenge (which shall remain unnamed), it can be used to elevate a malicious user’s privileges and compromise a system.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the bytecode cache validation relies only on easily forgeable header fields (magic number, timestamp, size) and does not verify the integrity of the cached bytecode, allowing a malicious .pyc file to be accepted.
Impact
An attacker could execute arbitrary code with the privileges of the user running the Python script, potentially escalating to root if the script is run with sudo.
What the attacker needed
Write access to __pycache__ folder; ability to run Python script as privileged user
The fix
The text suggests omitting .pyc files by using the -B argument or the PYTHONDONTWRITEBYTECODE environment variable, and being careful about folder permissions and which users can run applications with elevated privileges.
How it was found
- Create a malicious Python module with the same size as the original source file.
- Compile the malicious module to bytecode using py_compile.
- Use touch -r to set the malicious source file's modification timestamp to match the original.
- Recompile the malicious module to update the bytecode header.
- Copy the malicious .pyc file over the original .pyc file in __pycache__.
- Run the main script to trigger execution of the malicious code.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
When Python runs a script, it saves a compiled version of imported modules in a folder called __pycache__ to speed up future runs. If an attacker can write to that folder, they can replace the compiled file with a malicious one that looks identical to Python, so the next time the script runs, the attacker's code executes. This is dangerous if the script is run with higher privileges, like root.
What is Misconfiguration?
Settings left unsafe: exposed panels, open storage buckets, debug modes, default credentials.