Skip to content
RCE Write-up Intermediate

Squatting NPM for Remote Code Execution

The npm CLI tool npx installs and runs a package whose name matches a requested command, even if that command is normally provided by a different package's bin script. An attacker can register a package with the same name as a popular bin command, and users running npx for that command may install and execute the attacker's code.

alxndrsn.com

Published on alxndrsn.com

Similar to Alex Birsan's "dependency confusion" bug, npm's CLI tool npx allows for RCE via binary confusion.

The author, on alxndrsn.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the mismatch between a package's bin command names and the package name, combined with npx's behaviour of installing a package by the requested command name.

Impact

Remote code execution on developers' computers, CI environments, or production environments; the prompt may be skipped in CI or non-TTY environments, and npm's automated malware detection is mentioned as a defence.

What the attacker needed

Attacker registers a package matching a bin name; victim runs npx with that command name; prompt may be skipped in CI or non-TTY

How it was found

  1. Register an npm package whose name matches a bin script of a popular package.
  2. A user runs npx with that command name.
  3. npx prompts to install the package with that name (or skips the prompt in CI/non-TTY).
  4. The package's bin script is executed, running attacker-controlled code.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

When you run a command with npx, it looks for a package with that exact name if the command isn't already installed. An attacker can publish a package with the same name as a command from a popular package, so when someone runs npx for that command, they might download and run the attacker's code instead of the intended tool.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

Similar reports

View all

Same bug class and techniques, clearest first.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.