Join our Newsletter — 33% off our NHI Course
Home Glossary Cyber Security .pth Persistence
Cyber Security

.pth Persistence

← Back to Glossary
By NHI Mgmt Group Updated August 26, 2026 Domain: Cyber Security

A .pth persistence mechanism uses Python startup files to execute code whenever the interpreter launches. That makes it especially dangerous in developer tools, notebooks, tests, and automation, because the payload can survive package removal and continue running outside the normal import path.

Expanded Definition

.pth persistence refers to abuse of Python startup processing, where a malicious or unauthorized line in a .pth file is executed when the interpreter initializes. The mechanism is legitimate and documented, but the security concern is that it can be used to establish persistence without modifying the main application code or package metadata. In practice, the threat is strongest in environments where Python is used for notebooks, build scripts, test runners, or automation, because those contexts often trust the runtime more than the filesystem.

Unlike a normal import hook, .pth execution can run before the expected application entry point and can survive package uninstalls if the file remains in site-packages or another searched directory. That makes it a supply chain and host persistence issue at the same time. Guidance varies across vendors on how broadly to classify it, but the operational concern is consistent: an attacker or rogue dependency can reintroduce code at every startup. NIST control families around software integrity and change management are relevant here, especially NIST SP 800-53 Rev 5 Security and Privacy Controls. The most common misapplication is treating .pth files as harmless packaging metadata, which occurs when teams do not inspect startup paths during dependency review.

Examples and Use Cases

Implementing defensive monitoring for .pth persistence rigorously often introduces startup-path inspection overhead, requiring organisations to balance detection coverage against build and developer workflow friction.

  • A compromised internal package drops a .pth file into site-packages so a loader executes on every Python start, even after the package name is removed from requirements.
  • A test environment uses a shared virtual environment, and a leftover .pth entry silently runs during CI jobs, causing repeated execution that is mistaken for flaky automation.
  • An attacker with write access to a notebook server adds a .pth file to a user-writable interpreter path, ensuring code runs when analysts open a fresh kernel session.
  • A malicious post-install step writes to a Python startup directory, creating persistence that bypasses normal application import monitoring and is easier to miss than an explicit launcher script.
  • Security teams compare interpreter startup artifacts against trusted baselines and validate them with package provenance checks and file integrity monitoring, using guidance from sources such as NIST SP 800-53 Rev 5 Security and Privacy Controls.

Why It Matters for Security Teams

.pth persistence matters because it shifts the threat from visible application code into interpreter startup behavior, where many defenders are not looking. For security teams, the key risk is not only code execution, but also trust erosion across developer endpoints, shared workstations, data science platforms, and CI/CD runners. If the startup path is writable by untrusted users or unmanaged automation, then Python becomes a durable execution surface that can be abused for lateral movement, credential access, or repeated tampering with analysis outputs. This is especially relevant when environments handle secrets in notebooks, service accounts in scripts, or NHI-related automation that depends on Python packages and plugins. Control objectives in NIST SP 800-53 Rev 5 Security and Privacy Controls map well to this problem because they emphasize configuration control, integrity, and monitoring. Organisations typically encounter the impact only after a cleanup fails and the same payload reappears on the next interpreter launch, at which point .pth persistence becomes operationally unavoidable to address.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

NIST CSF 2.0 and NIST SP 800-53 Rev 5 set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
NIST CSF 2.0PR.IPPersistence in startup paths is a configuration and integrity issue addressed by protected processes.
NIST SP 800-53 Rev 5CM-2Security controls for configuration baselines apply directly to interpreter startup artifacts.

Track Python startup files in secure baselines and verify they cannot be altered without approval.

NHIMG Editorial Note
Reviewed and updated by the NHIMG editorial team on August 26, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org