.pth file import execution
During site initialization at interpreter startup
- Lives in
*.pth files on sys.path (commonly site-packages)- Attacker needs
- Filesystem write access, Supply-chain foothold
- Lifecycle
- InstallStartupyour codeModule loadRuntimeREPL
Runs while the interpreter boots, before your entrypoint.
How it runs
A .pth file in a site-packages directory can do more than list paths. Any line that starts with import gets executed during site setup. Your code doesn't have to import anything, and there's no opt-in.
echo 'import sys; print("You are hacked")' \
> /opt/homebrew/lib/python3.13/site-packages/demo.pth
python # prints before your own code runs
The sneaky part is that .pth files look mundane. They are supposed to hold path entries, so nobody audits them. A stealthier variant hides import lines among legitimate path entries in easy-install.pth. Search for the .pth extension and the first results are PyTorch model files, which helps the disguise.
The LiteLLM attack in 2026 used this exact technique. A compromised version dropped a .pth into site-packages that executed on every Python startup, with no user interaction required.
In the wild
LiteLLM 2026, a compromised package dropped a .pth that ran on every startup
Defend
Audit .pth files in site-packages; launch with python -S where feasible; install from hash-pinned wheels instead of sdists; monitor site-packages for writes.