Privilage escalation with malicious .npmrc
The npm configuration file .npmrc can specify an onload-script that npm executes with the privileges of the user running npm. An attacker who can write to .npmrc (e.g., via a malicious repository or low-privilege process) can trick a victim into running npm with sudo, leading to arbitrary code execution as root.
- Triaged in 19 days
- Disclosed after 33 days
Hello.
I'm forwarding to you my conversation with npm staff regarding security issue. It allows to escalate to root privilages of victim using either:
a) basic social engineering - convincing victim to run npm in attacker-controlled folder (eg. repository), including such innocent ones like "npm help" or "npm whoami"
b) low-privilage process with access to writing files
I believe that impact of this bug can be high, if someone is able to hijack well-positioned tutorial.
Michał Wadas
---------- Forwarded message ----------
Jon Lamendola (npm)
May 22, 12:19 PDT
Hello Michal,
We're reviewing the impact of changing this behavior and still discussing internally how we might move forward. We understand that it's a risk, but it is also a feature that people use, so we need to fully understand the consequences of making major changes to it before we do. Unfortunately, this can take some time to analyze.
In the meantime, you can alias npm to something like npm --onload-script="" "$@" for a temporary workaround.
Thanks again for reporting this to us.
Michał Wadas
May 21, 07:05 PDT
Hi.
Is there any update on this?
Michał Wadas
Apr 26, 16:32 PDT
Just noticed - if attacker can control .npmrc (either by writing it from low-privilage script or tricking user into using sudo npm in infected folder), he can set user flag in .npmrc too.
Jon Lamendola (npm)
Apr 26, 11:36 PDT
Hello Michal,
Thanks for reporting this to us. I agree, this is a legitimate concern, and I will pass this on to the npm CLI team for discussion.
Michał Wadas
Apr 26, 09:54 PDT
Source of issue:
- onload-script is run with privilages of user running npm, in npm process.
- User can be unaware of .npmrc behaviour
I have pin-pointed it to line 236 in lib/npm.js file in master tree. Attack scenario:
- Attacker tricks victim into running "sudo npm" in folder (or descendant of folder) with malicious .npmrc
** This can be achieved in many ways - eg. by writing to $HOME/.npmrc from low-privilaged application or tricking victim to open infected directory
** Example: tutorial asks user to clone git repository, configure it and then run "sudo npm i -g eslint"
** Example 2: attacker publish malicious code to npm. Code writes to $HOME/.npmrc. Then, attacker can just wait for anyone running sudo npm.
- Then npm runs arbitrary Node.js script with arbitrary permissions
Proposed actions:
- Ignore onload-script when run as super user
- Ask for confirmation before running onload-script
- Run onload-script in separate process with lower privilages (it's already supported for other scripts in npm - https://docs.npmjs.com/misc/<wbr>scripts#user</wbr> )
These actions should limit scope of attack.
Quick survey in group of Polish programmer showed that around ~30% of npm users use sudo npm
All versions of npm between 3.10 and 6.0 are confirmed to be vulnerable.
Thanks for your attention,
Michał Wadas
Impact
Attacker can reliably run arbitrary code with user privilages if he is able to write to .npmrc.
If user use "sudo npm" in folder with malicious .npmrc, attacker can run arbitrary code with root privilages.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Impact
An attacker can reliably run arbitrary code with user privileges if they can write to .npmrc; if the user runs 'sudo npm' in a folder with a malicious .npmrc, the attacker can run arbitrary code with root privileges.
What the attacker needed
Attacker can write to .npmrc; victim runs npm with sudo in affected folder
The fix
The report suggests ignoring onload-script when run as super user, asking for confirmation before running onload-script, or running onload-script in a separate process with lower privileges; the text does not state that a fix was applied.
How it was found
- Attacker tricks victim into running 'sudo npm' in a folder (or descendant) with a malicious .npmrc
- This can be done by writing to $HOME/.npmrc from a low-privileged application or by tricking the victim into opening an infected directory
- Then npm runs an arbitrary Node.js script with arbitrary permissions
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 report has not been written yet. The Original tab links to the source.
When you run npm, it reads a settings file called .npmrc. That file can contain a setting that tells npm to run a script automatically. If an attacker can change that file, they can make npm run their own code. If you run npm with sudo (as an administrator), that code runs with full control of your computer.
What is Access control?
A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.