Zip imports (zipimport)
On import (if on sys.path) or on direct invocation (python archive.zip)
- Lives in
.zip / .pyz files on sys.path, or passed directly to the interpreter- Attacker needs
- Filesystem write access, Environment control
- Lifecycle
- InstallStartupyour codeModule loadRuntimeREPL
Runs on import or require, for every module the app loads.
How it runs
Python's import system doesn't stop at plain source files. The built-in zipimport hook treats a .zip on sys.path like a regular directory and imports modules from inside it. It's always active and needs no configuration.
The real danger is module shadowing. Pack a json.py into a zip, add it to PYTHONPATH, and every import json loads your version instead of the stdlib one:
python3 -c "import zipfile; z=zipfile.ZipFile('vendor-utils.zip','w'); z.write('json.py'); z.close()"
PYTHONPATH=vendor-utils.zip python3 -c "import json"
Zip files are opaque. You can't ls inside them without extra tools, and reviewers miss them easily. Pair this with .pth files or PYTHONPATH injection for a clean two-stage attack. One mechanism adds the zip to the path, and the zip contains the shadowed module.
Defend
Flag archives on sys.path in audits; unpack and inspect .zip/.pyz contents; combine with PYTHONPATH hygiene.