Cross-source coverage
T1546.018 / ATT&CK
Event Triggered Execution: Python Startup Hooks
2 rules across 1 source.
From MITRE ATT&CK 19.2
Adversaries may achieve persistence by leveraging Python’s startup mechanisms, including path configuration (.pth) files and the sitecustomize.py or usercustomize.py modules. These files are automatically processed during the initialization of the Python interpreter, allowing for the execution of arbitrary code whenever Python is invoked.
Path configuration files are designed to extend Python’s module search paths through the use of import statements. If a .pth file is placed in Python's site-packages or dist-packages directories, any lines beginning with import will be executed automatically on Python invocation. Similarly, if sitecustomize.py or usercustomize.py is present in the Python path, these files will be imported during interpreter startup, and any code they contain will be executed.
Adversaries may abuse these mechanisms to establish persistence on systems where Python is widely used (e.g., for automation or scripting in production environments).
- Tactics
- Persistence · Privilege Escalation
- Platforms
- Linux · macOS · Windows
- Telemetry
-
auditd:SYSCALLauditd:PATHauditd:CONFIG_CHANGENSM:Flow
How MITRE says to detect it DET0258
Linux Python Startup Hook Persistence via .pth and Customize Files (T1546.018)
Linux Analytic 0713
Defender observes unauthorized modification or creation of Python hook files such as `.pth`, `sitecustomize.py`, or `usercustomize.py` in Python `site-packages`, `dist-packages`, or user paths. This is often correlated with subsequent unexpected interpreter execution (e.g., python3 running without user interaction), changes in interpreter behavior (e.g., malicious imports), and outbound connections initiated from Python. Defender links write/modify actions on hook files with execve of python process and/or anomalous child process or network activity.
auditd:SYSCALLexecve: execve where exe=/usr/bin/python3 or similar interpreterauditd:PATHwrite or create events on *.pth, sitecustomize.py, usercustomize.py in site-packages or dist-packagesauditd:CONFIG_CHANGEchmod or chown of hook files indicating privilege escalation or execution permission changeNSM:Flowhttp::request: Outbound HTTP initiated by Python interpreter
elastic/detection-rules
2 rules| Detection | Severity | Format |
|---|---|---|
| Python Path File (pth) Creation | Low | Elastic TOML |
| Python Site or User Customize File Creation | Low | Elastic TOML |