T1546.018

Python Startup Hooks

Adversaries may establish persistence by adding malicious content to Python startup hooks. Python offers several mechanisms that execute code when the interpreter starts. The PYTHONSTARTUP environment variable points to a Python script that executes in interactive mode before the first prompt. The usercustomize.py and sitecustomize.py files execute automatically when Python starts for all users (sitecustomize.py) or the current user (usercustomize.py). The PYTHONPATH and sys.path list directories where Python searches for modules — adversaries can inject malicious modules here. Additionally, .pth files in site-packages directories can execute arbitrary Python code on interpreter startup via import statements.

Microsoft Sentinel / Defender
kusto
let PythonStartupFiles = dynamic([
    "sitecustomize.py",
    "usercustomize.py",
    "site.py",
    "sitecustomize.pyc"
  ]);
let PythonDirs = dynamic([
    "site-packages",
    "dist-packages",
    "lib\\python",
    "lib/python"
  ]);
let StartupFileWrites = DeviceFileEvents
| where Timestamp > ago(24h)
| where FileName in~ (PythonStartupFiles)
   or (FileName endswith ".pth" and FolderPath has_any (PythonDirs))
| where ActionType in ("FileCreated", "FileModified")
| extend IsSitePackages = FolderPath has_any (PythonDirs)
| extend IsSitecustomize = FileName in~ ("sitecustomize.py", "usercustomize.py")
| extend IsPthFile = FileName endswith ".pth"
| project FileTime=Timestamp, DeviceName, AccountName, ActionType, FileName, FolderPath,
         IsSitePackages, IsSitecustomize, IsPthFile,
         InitiatingProcessFileName, InitiatingProcessCommandLine;
let EnvVarPersistence = DeviceRegistryEvents
| where Timestamp > ago(24h)
| where RegistryValueName in~ ("PYTHONSTARTUP", "PYTHONPATH")
| where ActionType in ("RegistryValueSet", "RegistryKeyCreated")
| where RegistryKey has_any ("Environment", "System\\CurrentControlSet")
| project RegTime=Timestamp, DeviceName, AccountName, RegistryKey,
         RegistryValueName, RegistryValueData, InitiatingProcessFileName;
union (StartupFileWrites | extend EventType="FILE"),
      (EnvVarPersistence | extend EventType="ENV_VAR")
| sort by FileTime desc, RegTime desc
high severity medium confidence

Data Sources

File: File Creation File: File Modification Windows Registry: Registry Key Modification Microsoft Defender for Endpoint

Required Tables

DeviceFileEvents DeviceRegistryEvents

False Positives

  • Python package installation (pip, conda, poetry) that creates or modifies sitecustomize.py or .pth files as part of package setup
  • Python virtual environment creation (venv, virtualenv) that initializes site-packages with standard configuration files including .pth files
  • Development tools (IPython, Jupyter, pytest) that install startup hooks for their configuration
  • System Python configuration management by system administrators that modifies PYTHONPATH for shared Python installations

Unlock Pro Content

Get the full detection package for T1546.018 including response playbook, investigation guide, and atomic red team tests.

Response PlaybookInvestigation GuideHunting QueriesAtomic Red Team TestsTuning Guidance

Related Detections