Back to results

Python PYTHONPATH Modification During Package Installation

The following analytic detects modification of the PYTHONPATH environment variable in conjunction with a package installation process. Python looks up the `sys.path` variable, which is generated by combining user and…

Description

The following analytic detects modification of the PYTHONPATH environment variable in conjunction with a package installation process. Python looks up the `sys.path` variable, which is generated by combining user and site folders with `.pth` files and the value of the PYTHONPATH environment variable, to determine which directories to use for importing modules. If an adversary is able to control the value of PYTHONPATH, they can point it to an attacker-controlled directory and hijack imported packages, achieving user-level persistence across future Python invocations and new shell sessions. If confirmed malicious, this could result in arbitrary code execution every time Python is invoked by the affected user.

Detection logic

Detection requirements

Platform
LinuxmacOSWindows

The rule states no platform. This is derived from the 2 ATT&CK techniques it maps to.

Known benign triggers

  • Developers and legitimate installers may modify PYTHONPATH as part of normal environment configuration. Investigate the new value and parent process to determine legitimacy.

Detections can measure how the public catalogue is used — which detections people look for, and which pages bring them here. It sets a cookie that recognises this browser for 180 days. It is never linked to an account and never follows you to other sites. Privacy notice