Join our Newsletter — 33% off our NHI Course
Home› Glossary› Cyber Security› .Pth File
Cyber Security

.Pth File

← Back to Glossary
By NHI Mgmt Group Updated October 31, 2026 Domain: Cyber Security

A .pth file is a Python startup file that can extend interpreter search paths and, when it contains import statements, execute code automatically. In supply-chain attacks, adversaries abuse this mechanism to run payloads at every Python startup, before the application imports anything. It turns package installation into a delayed execution trigger.

What a .pth file does

A .pth file is part of Python’s startup path handling, so it can change where the interpreter looks for imports before application code begins. That makes it more than a static configuration artifact, because a line in the file can alter interpreter behaviour at every startup and create an execution path that runs early and quietly.

In normal environments, this mechanism is used to extend search paths for legitimate packages. In hostile environments, the same startup hook becomes a persistence and execution primitive, especially when it is dropped during packaging, environment setup, or dependency installation. The security issue is not the file extension itself, but the trust placed in startup-time interpreter configuration.

Why .pth files are dangerous in supply chains

.pth files are attractive to attackers because they sit on a trusted path between installation and runtime. A malicious entry can survive routine application launches, execute before imports, and blend into ordinary package behaviour. That makes abuse difficult to spot if teams only review application source and do not inspect the installed environment.

The danger increases when build systems, virtual environments, or dependency workflows accept packages without verifying what they place on disk. A compromised package can seed startup code that triggers each time Python runs, turning a single installation event into repeated code execution across many hosts or environments.

For Python-specific hardening guidance, see the Python Security Guide, which covers startup hooks, trusted publishing, lockfiles, and package integrity controls.

How organisations expose themselves to .pth abuse

Exposure usually comes from incomplete inventory and weak software trust controls. If you do not know which virtual environments, deployment images, or Python distributions are present, you cannot reliably identify where .pth files may have been introduced. Internet-exposed build runners, developer workstations, and CI environments are especially sensitive because they often install dependencies automatically.

Vendor advisories, package metadata, and repository provenance should be checked whenever a Python dependency is introduced or updated. A .pth file is often discovered only after inspecting installed site-packages content, so static source review alone is not enough. Teams should also treat unexpected startup path changes as a sign of possible supply-chain compromise.

Detection, cleanup, and post-exploitation review

Detection starts with looking for unexpected .pth files, unusual lines inside them, and startup behaviour that occurs before an application’s own code path. File integrity checks, build-time inspection, and endpoint telemetry can all help reveal whether a startup hook was added where it should not have been. If the file contains import statements, treat that as code execution, not configuration.

After removal, review whether the environment also contains altered packages, dropped modules, altered wheels, or other persistence mechanisms. Because the abuse point is interpreter startup, a compromised environment may continue to be risky even after the .pth file is deleted if the surrounding package tree, cache, or build artefacts remain tainted. Resetting the environment is often safer than trying to surgically trust it again.

Why this matters for identity, secrets, and service accounts

Although .pth abuse is a Python startup problem, the practical damage often lands on credentials and runtime trust. A payload that runs before application logic can read environment variables, service tokens, API keys, cloud credentials, and other secret material that the process inherits at launch. It can also act inside automated jobs where elevated execution context is already present.

That is why teams should assume the blast radius may extend beyond a single application instance. A malicious startup hook can steal reusable secret material, hijack authenticated sessions, or pivot into connected services if the runtime has broad access. The security question is not only whether Python starts correctly, but what authority is available at the moment startup code executes.

Risk and Threat Considerations

.pth abuse is dangerous because it turns a legitimate interpreter feature into a hidden execution trigger that fires before the application can defend itself. The attacker’s goal is usually persistence, silent code execution, and access to whatever trust material the runtime already holds.

Failure mechanism: A malicious package introduces or modifies a .pth file so Python executes attacker-controlled code automatically during startup, before normal application controls or import checks run.

Impact: The payload can exfiltrate secrets, alter imports, implant persistence, or use the runtime’s existing permissions to reach internal services and downstream systems.

Standards & Framework Alignment

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

MITRE ATT&CK addresses the attack and risk surface, while CIS Controls v8, NIST SP 800-53 Rev 5 and OWASP ASVS set the governance and control requirements practitioners need to meet.

FrameworkControl / ReferenceRelevance
CIS Controls v8CIS-4 — Secure Configuration of Enterprise Assets and Software.pth abuse relies on insecure Python installation and startup configuration.
Recommendation — Harden Python environments and detect unexpected startup-path changes in software assets.
NIST SP 800-53 Rev 5CM-6 — Configuration SettingsPython startup files are configuration that can alter runtime behavior and must be controlled.
SI-7 — Software, Firmware, and Information IntegrityMalicious .pth content is a software integrity issue that can execute at startup.
Recommendation — Restrict and review Python startup configuration changes before deployment. Validate installed Python artifacts and flag tampered or unexpected package content.
OWASP ASVSV15 — Secure Coding and ArchitectureStartup hooks and import-time execution are architecture concerns that affect application trust boundaries.
Recommendation — Design Python applications to minimize trust in ambient startup behavior and installed code.
MITRE ATT&CKT1195 — Supply Chain CompromiseMalicious .pth files are a supply-chain execution mechanism introduced through dependencies or installations.
Recommendation — Hunt for tampering in dependency pipelines and installation artifacts that can plant startup code.

Practitioner Guidance

Why practitioners should care: Treat .pth files as executable startup content, not harmless packaging metadata. Any dependency workflow that can write into site-packages can potentially create an execution path at launch time, so package provenance and installation trust matter as much as source review.

What to watch for: Unexpected .pth files, startup code that appears outside the application repository, and environments where packages are installed automatically without integrity checks are all strong warning signs. When these conditions exist, review the installed environment as part of routine build and incident-response practice.

Practitioner takeaway: If you cannot explain why a .pth file exists, assume it deserves the same scrutiny as other executable startup mechanisms.

Free weekly newsletter

Subscribe to the NHI & AI Identity Journal

The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.

Bonus 33% off our NHI Course when you subscribe.

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