Using .npmrc to achieve remote code execution on npm users
A project-level .npmrc file can override the npm registry, so a malicious repository can point npm at an attacker-controlled registry and serve a modified dependency that executes code. An attacker could achieve remote code execution when a victim runs npm install in a cloned repository.
medium.com
Published on medium.com
What a miserable day to be wring about anything, But here we go again since this day has been really amazing and I couldn’t miss writing a blog about this
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 npm gives a project-local .npmrc precedence over the user's system-wide configuration, letting a cloned repository redirect package downloads to an attacker-controlled registry.
Impact
An attacker could execute arbitrary code on a victim's machine when the victim runs npm install in a malicious repository, even if the victim uses the --ignore-scripts flag.
What the attacker needed
Victim clones a malicious repository; victim runs npm install in that directory; attacker hosts a custom npm registry
The fix
GitHub responded that this is intentional design and working as expected, so no fix was applied; users should avoid running npm install in untrusted repositories.
How it was found
- Set up a self-hosted npm registry using verdaccio.
- Create a dummy package that depends on a real package such as abbrev.
- Publish a modified copy of that dependency to the custom registry with code that executes on import.
- Include a .npmrc in the project that points the registry at the attacker's server with auth tokens.
- Publish the project; when a victim runs npm install, the modified dependency is fetched and runs.
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 npm install, npm reads a settings file called .npmrc to decide where to download packages from. If a repository includes its own .npmrc, npm uses that file instead of your normal settings, so the repository can send your downloads to a server the attacker controls. The attacker then serves a tampered package that runs code on your machine.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.