Skip to content
The RedTeam Monitor
Wednesday, October 21, 2026Your daily threat briefing239 reports
Malicious PyPI Packages: How Supply Chain Attacks Target Python
Malware & Ransomware

Malicious PyPI Packages: How Supply Chain Attacks Target Python

Attackers hijack popular package names to install backdoors that bypass traditional perimeter defenses and operate with full user privileges.

Quick answer

Malicious PyPI packages are legitimate-looking software libraries hosted on the Python Package Index that contain hidden malicious code. They infect systems when developers install them via standard commands, granting attackers access to credentials, data, or remote control capabilities without triggering standard network alerts.

The Broken Trust Model

Imagine you order a replacement part for your car from a trusted online store. You receive the box, it looks correct, but inside the part has been swapped for a device that records your driving habits. You installed it yourself because the box came from a reputable source. This is the core failure of software supply chains. You trust the repository, not the individual maintainer.

Python Package Index, commonly known as PyPI, is the default hub for Python libraries. It hosts millions of packages. Developers install them using the pip command. The system assumes that if a package is on PyPI, it is safe. This assumption is the vulnerability. Attackers exploit this trust by uploading code that looks benign but performs malicious actions during installation or execution.

AspectDetail
Attack VectorCompromised or impersonated packages on public repositories
Execution TriggerStandard installation commands or import statements
Primary GoalCredential theft, data exfiltration, or backdoor access
Detection DifficultyHigh; code runs with user privileges and may be obfuscated
Mitigation CoreHash pinning and dependency verification before installation

How the Infection Occurs

The attack relies on human error and repository policies. The most common method is typosquatting. An attacker registers a package name that is nearly identical to a popular library. If you type requests as requets, you might install the attacker’s version. The package structure mimics the real one. The metadata, description, and file names look correct.

Another method is package hijacking. Attackers find abandoned projects where the maintainer has stopped updating. They contact the maintainer, offer to take over, or simply wait for the account to be compromised. Once they have access, they push a new version with malicious code. You update your dependencies, and the new version installs automatically.

The danger lies in the installation process. When you run pip install, Python downloads the archive and executes setup scripts. These scripts run with your user privileges. If you are an administrator, the code runs with administrative privileges. It can modify system files, install keyloggers, or change environment variables.

Common Forms of Deception

Attackers use several techniques to hide their intent. Obfuscation is the most common. They encode the malicious string or use complex mathematical operations to generate the payload at runtime. This prevents simple text searches from finding the malicious command. The code looks like random noise or complex logic until it executes.

Another form is conditional execution. The package checks the environment before running the payload. It might look for specific IP addresses, hostnames, or environment variables. If the condition is not met, the package behaves normally. This allows the package to pass automated scans in development environments while activating only in production or on specific targets.

Some packages use legitimate libraries to mask their behavior. They might download additional code from a remote server during installation. This secondary payload is not in the initial package file. It arrives after the initial trust check passes. This technique makes forensic analysis difficult because the initial artifact appears clean.

Who Gets Hit

Any organization using Python is at risk. This includes data science teams, web developers, and DevOps engineers. The impact depends on the privileges of the user running the installation. A standard developer might lose their local credentials. A CI/CD pipeline running with high privileges might compromise the entire build infrastructure.

Cloud environments are particularly vulnerable. Many organizations use automated scripts to install dependencies on new virtual machines or containers. If the script pulls from PyPI without verification, a malicious package can infect every new instance. This spreads the compromise rapidly across the infrastructure.

Open-source projects are also targets. If a widely used library is compromised, every project that depends on it inherits the risk. This creates a ripple effect. You might not have installed the malicious package directly, but your project depends on a library that does. This is known as a transitive dependency attack.

What People Usually Get Wrong

Many teams believe that scanning the package file is enough. They run antivirus or static analysis tools on the downloaded archive. This misses conditional execution and runtime obfuscation. The code might look safe in isolation but becomes malicious when combined with the host environment.

Another common mistake is trusting the repository alone. PyPI has improved its review processes, but it cannot catch every malicious upload. The volume of packages is too high. Relying on the platform’s safety nets gives a false sense of security. You must verify the source yourself.

Teams also ignore the risk of transitive dependencies. They scan their direct dependencies but forget to check the libraries those dependencies rely on. A malicious package can hide deep in the dependency tree. It might be a small, obscure utility that no one notices. Until it executes, it remains hidden.

Reducing the Risk

Verification is the primary defense. You should pin your dependencies to specific versions and hashes. Instead of installing a package by name, you specify the exact file hash. This ensures that even if the package name is hijacked, you will not install the wrong version. The installation will fail if the hash does not match.

Use private package mirrors or proxies. These tools cache packages from PyPI and allow you to review them before they reach your internal networks. You can block known malicious packages or require approval for new ones. This adds a layer of control between the public repository and your systems.

Monitor your installation logs. Look for unusual network connections during the install process. If a package tries to reach an external server during installation, it is suspicious. Legitimate packages rarely need to make network calls during setup. Configure your network to block outbound connections from build agents unless explicitly allowed.

Beyond the Package

The threat extends beyond the code itself. Malicious packages can modify configuration files or environment variables. They can change where the system looks for libraries. This allows them to persist even if you remove the package. They might also install additional tools that evade standard security checks.

Consider the broader supply chain. If you use continuous integration tools, ensure they use verified sources. Integrate security checks into your build pipeline. Fail the build if any dependency is unverified. This stops the infection before it reaches production.

Infographic: Malicious PyPI Packages: How Supply Chain Attacks Target Python. Typosquatting and package hijacking are the primary methods used to distribute these threats. The installer runs with the same privileges as the user, often bypassing application whitelisting. Static analysis often fails b
Infographic: Malicious PyPI Packages: How Supply Chain Attacks Target Python. Free to share with a link to RedTeam Monitor.

The Long Game

Malicious packages are a persistent threat. Attackers constantly find new ways to bypass detection. They exploit new vulnerabilities in package managers and build tools. You must stay updated on these changes. Follow security advisories for the tools you use.

Collaboration is key. Share information about suspicious packages with the community. Report typosquatting attempts to the repository administrators. Help maintain the integrity of the ecosystem. Security is a shared responsibility.

Key takeaways

  • Typosquatting and package hijacking are the primary methods used to distribute these threats.
  • The installer runs with the same privileges as the user, often bypassing application whitelisting.
  • Static analysis often fails because the malicious payload executes only in specific environments.
Bottom line

Trusting public repositories implicitly exposes your systems to supply chain attacks that bypass traditional security controls. Verify every package with hash pinning and private mirrors before installation.

Frequently asked questions

How do I know if a package is typosquatting?

Check the spelling carefully against known popular libraries. Look at the maintainer’s history and the number of downloads. New packages with names similar to famous ones are often suspicious.

Can I use antivirus to detect these packages?

Antivirus may detect known malicious signatures, but it often misses novel or obfuscated code. It is not a reliable standalone defense against supply chain attacks.

What is hash pinning?

Hash pinning involves specifying the cryptographic hash of the package file in your requirements. This ensures you install exactly the file you expect, preventing substitution attacks.

Should I stop using PyPI?

No, but you should not use it directly in production. Use a private proxy or mirror that caches and verifies packages before they reach your internal systems.

How this guide was produced: written by the RedTeam Monitor editorial team with AI assistance, checked against the public references listed below, and reviewed when the facts change. See our editorial policy or report an error.

Further reading

  1. UK National Cyber Security Centre
  2. CISA: Stop Ransomware
  3. MITRE ATT&CK
malicious PyPI packagespython securitysupply chainpypi

Related stories

How PyPI Package Vulnerabilities Spread and How to Stop Them

Supply chain attacks hide inside trusted code dependencies, exploiting the trust you place in automated installers rather than your application logic directly.