Abuse of PYTHONPATH for Python Module Hijacking and Persistence
Adversaries are modifying the PYTHONPATH environment variable during Python package installation to redirect module imports, enabling code execution and persistence whenever Python is invoked.
Adversaries are targeting Python environments by manipulating the PYTHONPATH environment variable, a mechanism used by the Python interpreter to locate modules for import. By altering this variable - often via registry modifications - an attacker can force Python to load libraries from an attacker-controlled directory instead of legitimate locations. This technique facilitates module hijacking, allowing the attacker to execute arbitrary code with the privileges of the user who invokes the Python interpreter. This activity is frequently observed in conjunction with the installation of malicious Python packages, serving as both a method for initial code execution and a persistent backdoor that activates automatically upon future Python sessions. Defenders should monitor registry modifications affecting environmental paths during package management workflows.
Attack Chain
- Attacker delivers a malicious payload or script to the target system.
- The target user or an automated process executes a Python package installation (e.g., via
pip). - The installer triggers a process that interacts with the Windows environment configuration.
- The malicious process or installer modifies the
HKCU\Environment\PYTHONPATHregistry key. - The attacker places a malicious Python module (e.g., a
.pyfile) in the directory specified by the modified PYTHONPATH. - The victim executes a standard Python application or script.
- The Python interpreter traverses the modified PYTHONPATH, prioritizes the malicious directory, and loads the attacker's module.
- Malicious code within the hijacked module executes, providing the attacker with sustained, user-level persistence.
Impact
Successful exploitation allows for arbitrary code execution and persistent access to the target host. Because the persistence mechanism relies on the standard Python import lookup process, the attacker's code runs every time the user invokes Python, potentially leading to credential theft, data exfiltration, or further lateral movement within the network.
Recommendation
- Enable Sysmon Event ID 1 (Process Creation) and Event ID 13 (Registry Value Set) to monitor for unauthorized changes to the
PYTHONPATHenvironment variable. - Deploy the provided Sigma rule to detect registry modifications to the
PYTHONPATHkey performed by processes associated with Python package installation. - Investigate any process modifying
HKCU\Environment\PYTHONPATHif it originates from an unexpected parent process or is not part of a known, approved development workflow.
Immediate actions
Enable Sysmon Event ID 13 monitoring for the Environment registry hive.
Threat Hunt
Search registry logs for any changes to PYTHONPATH occurring alongside pip process execution.
Data: Sysmon Event ID 1 (Process), Sysmon Event ID 13 (Registry)
Detection coverage 1
Detect PYTHONPATH Modification via Registry
mediumDetects modifications to the PYTHONPATH environment variable registry key, which may indicate an attempt to hijack Python module imports for persistence.
Detection queries are available on the platform. Get full rules →