Embedded Malware in Coa
github.com
github.com
https://github.com/veged/coa/issues/99#issuecomment-96153687...
TLDR: The attacker injected an attack code as coa's `preinstall` script, which executes an obscurely-named file ("compile.bat"). This file is fully obfuscated, but what it does is basically to pull exploit DLLs from the attacker's server and install 'em.
I think the fortunate part of this accident is that the attacker failed to deploy the malware in his/her first attempt; v2.0.3 only contained the half of the changeset that the exploit needs to work (which accidentally broke tons of CI builds); So some developers could notice that something is wrong a bit early.
very nice use of substring, but a bit too linear... with some input redirects and nested spaced variables it would have become more robust and unpredictable, but i suppose nowadays batch chiselers are rare.
edit: by the way in the article is missing a -useless- decoded line (n.4)
The thing that should be causing concern is not so much these very loud obvious attacks, but how many better attacks that are harder to detect, are currently happening.
With 1.7M packages and an ecosystem that favours lots of 3rd party package usage, NPM is a large target. Whilst NPM isn't the only repository to have this kind of issue, it's definitely the largest attack surface.
Personal system-wide config:
npm/yarn config set ignore-scripts true -g
and add & commit a .npmrc/.yarnrc file with ignore-scripts true
Yes, this will cause headaches in some (increasingly rare) cases where some package actually needs those scripts. You can fix this with custom install scripts that take care of running install for those specific packages.And yes yes, as people love to point out, this isn't exactly a bulletproof solution either. The attacker could just put the malicious code inside the package's code and wait for it to be actually executed. But again and again, they don't, they choose to use the package's install scripts as the place to do their dirty work.
So in practice this policy would've alrady protected you from who knows how many of these attacks, and my guess is that it'll continue to do so.
$ grep postinstall node_modules/*/package.json
node_modules/esbuild/package.json: "postinstall": "node install.js"
$ cd node_modules/esbuild
$ npm run postinstallHere's what I use with yarn, install.sh:
#!/usr/bin/env bash
yarn
function run_install() {
local dependency="node_modules/$1"
if [[ -d "$dependency" ]]; then
yarn --cwd "$dependency" run postinstall
fi
}
run_install 'esbuild'
run_install 'other_package'
...I wish package.json had an option where I could explicitly mark which packages can run postinstall scripts.
Here's an RFC on exactly that: https://github.com/npm/rfcs/discussions/80
The behavior I described (where it just blanket ignores ALL scripts, even ones you try to run) existed on NPM 6 (which is what I was using until a few minutes ago). I couldn't find the exact version where this behavior was fixed.
https://github.com/advisories/GHSA-g2q5-5433-rhrf (" Embedded malware in rc" "critical severity")
Notable that both advisories link to the virustotal entry for the same file hash (same malware).
@dang Could the title be updated to include the names of other affected packages?
Let's assume that you have Deno compiler for other language. You run it through seemingly innocent deno run "https://..." --allow-write=. src/ (you use optional parameter to --allow-write, right?).
Unfortunately, webpage hosting script was compromised. Now our compiler can write to .git/hooks, .npmrc (npm can do arbitrary script execution in version 6 or lower, even on npm --version), .idea/ etc.
npm packages seem like a cardhouse.
I know that the node_modules folder is often times criticized for its sheer amount of 3rd party libraries. Is it because of JavaScripts "missing" standard library?
The amount of churn in JS ecosystem, security incidents like this and general crappiness of websites can be generally explained by how immature these hipsters are.
Perhaps because dependencies are more curated in PHP due to clusters of dominant frameworks, rather than a proliferation of smaller libraries.
Now PHP isn't hip anymore, node js is super popular hip thing, and every tom dick and harry from art school in US or 3rd tier engineering college in India will slap together three todo list applications on Resume and wants to call himself full stack developer. Internet is fast, hardware is fast, no one cares about pile of dependencies sitting beneath them.
Add to that resume driven development where every JS wants to write libraries and become github-famous. 30-line libraries will be considered a joke and it will be shameful to brag about such things in any other ecosystem.
Until composer, there wasn't much of a culture around creating packages full stop, apart from PEAR, which beginners didn't create packages for.
Also early this year there were news that git.php.net was compromised and a backdoor was introduced into PHP, but lucky enough the backdoor was catch before a production release.[2][3]
1: https://en.wikipedia.org/wiki/PHP#Security
2: https://arstechnica.com/gadgets/2021/03/hackers-backdoor-php...
The git.php.net server was also not compromised, as far as I know, from a PHP vulnerability that was active at the time.
[0] -> Error: Cannot find module '/Users/me/.npm/_npx/27078/lib/node_modules/@svgr/cli/node_modules/coa/compile.js'
What happened there was that he got the broken update, 2.0.3 which just referenced and used compile.js, but didn't include the file.
Then 2.0.4 came out which included compile.js and compile.bat.
Had he updated a couple of minutes later, this error would not have appeared. Not sure if /Users/ is a MacOS thing, but it is a Windows path structure, which might indicate that he was running this on Windows. And in that case he would have been compromised.
Going to the issue, it seems the `preinstall` field was changed to `start /B node compile.js & node compile.js",` which means this would only run on Windows machines, everyone else seems to be unaffected.
Here is how you can find out if you have the affected package on your machine/instance:
find ~/projects/ -name "*coa*" | xargs -I {} jq .version {}/package.json 2>/dev/null
Assumes you have `find`, `xargs` and `jq` installed, will print all versions of coa it can find. Seems any version above 2.0.3 is bad.Edit: is anyone sitting on the source for `compile.js` as mentioned? Would be interesting to see.
https://www.bleepingcomputer.com/news/security/popular-coa-n...
locate "/coa/package.json" | xargs -I {} jq .version {} 2>/dev/null
gci -r -dir | where name -eq coa | % { gc $_/package.json -EA si | ConvertFrom-Json | select version }[0] https://www.change.org/p/npm-please-secure-package-releasing
1. All accounts are MFA, no exceptions.
Only CI/CD usecases justify some laxity. Only push tokens should be non-MFA-able for CI/CD purposes, and they should only be usable for push. Tokens should only be obtainable with MFA.
Ideally these too would be part of a multi-step OAuth2 or OIDC refresh token to access token flow, so that any given access token is only used once and CI/CD jobs never get to see the refresh token.
And while we're on tokens: they should all expire, after a short period of time, without exception.
2. Signing packages is opt-out, not opt-in.
The two reasons package signing is relatively rare are that (1) in most ecosystems it is unwieldy, (2) not actually that effective at providing guarantees (self-signed certs are no better than a pinky promise).
But once these are solved, and I believe they can be, making signing opt-out means that we'd see signing rate percentages in the 90s, instead of languishing in single digits as they do today.
If you work on NPM and would like to swap notes, I would love to talk to you with my professional hat on -- email in profile.
Is it possible to check how many downloaded the compromised versions?
Having NPM just be a free-for-all is a ticking time bomb. It is only a matter of time before an event like this results in something very serious.
NpmJS echo system is cancer.
(Of course this malware was in a preinstall script, which should also be disabled... but any module you import in a node app can do bad things when you run your app, preinstall script or no.)
Would this mean any project using a package.lock/yarn.lock was 'safe' going through deploys? So only new installs and builds without lock files could have grabbed the higher version?
If so, I wonder if it's hard or impossible to swap a release version on NPM. Seems like that would hit a much wider audience before being detected.
May I suggest that a clearer phrasing would be ‘Malware embedded in Coa’? Or is ‘embedded malware’ a somewhat confusing term-of-art in the cybersec community?
What is missing is more automated and recurrent checks in all the packages and downstream dependencies.
https://my.diffend.io/npm/coa/2.0.3/2.0.4/
In 2021, why on earth does such a change not trigger a review before release?
My point is that: Npm is auditable, trackable. I'm not challenging the bug itself, neither the security issue..
Then I'd guess that Microsoft has enough information with NPM's history to train an AI. Specially the modifications made in these versions could easily trigger suspicious activity.
Did you look at the diffs?
Also, three years of inactivity and then a sudden upload should easily trigger a manual review, even if it is by automatically opening an issue with a review request on that project's GitHub page.
However, that is dependent on first finding and flagging issues, which is exactly what this post is about.
Additionally, an app's vendored dependencies don't automatically update until you choose to update them. And when you update a vendored dependency, you have a chance of finding malware when you review the diff before committing the update. The downside is that vendored dependencies introduce friction in updating your dependencies (I've experimented with extracting tarballs, git subtree, and git subrepo, and generally avoid submodules, and they're all somewhat awkward), and don't automatically receive patch/security updates like distribution libraries do.
I'm more familiar with crates.io and PyPI than npm, but I think they all work similarly (unvetted package repository allowing libraries to depend on other libraries, and transitive dependencies get pulled in when you install the library, and can be updated without the library author's knowledge). The primary difference IMO is the degree that packages use transitive dependencies vs. building/copying the subset of code they need (the average Python app uses dozens of dependencies, Rust apps use hundreds, Node apps use thousands).
My wish is that for each language, a team of Linux distribution maintainers or community members would "sign off on" a set of trusted packages, each of which depends on only trusted packages. And when a trusted package gets updated, the new version isn't marked as trusted until the maintainer verifies the diff isn't malicious. Then the language package manager would add a mode where apps or libraries can choose to only depend on trusted packages (which only depend on trusted packages, etc.).
(In the Rust world, where the compiler and package manager are developed by the same non-commercial organization, I hope the Rust organization will create the team handling "trusted packages"; sadly they seem opposed to it because they consider it favoritism, but IMO it's a good thing for Arch to package wget and curl, but not "joe's 1337 haxxor tool". In the Node world, V8 and node.js and npm are different groups/companies I think, so I don't know which organization is community-led and neutral enough to make a trustworthy host for this effort.)
Crev takes the "web of trust" approach, where everyone can choose who to trust. However, it doesn't ship a "deny untrusted dependencies" mode as part of Cargo. And as with PGP webs of trust, I haven't met any library developers in person, don't know which developers sign off on packages, and would prefer having semi/centralized institutions (like Linux distributions) who stake their reputation on the non-maliciousness of packages they ship, and allow different people to agree on which packages to trust. Having institutions reduces the responsibility placed on the individual app developer. Right now, you need to trust an distro to take responsibility for an OS, and a language team to take responsibility for a compiler. With crev, you'd have to also search for trustworthy people who've reviewed enough package that you can feasibly build apps using only those dependencies, instead of having the distro or language team provide that service out of the box.
Maybe I'm just too lazy to find trustworthy people. But currently there are not enough Crev reviews at all. Searching through https://web.crev.dev/rust-reviews/crates/, there's no mention of gtk and pipewire, and rusqlite and sqlite are reviewed but not libsqlite3-sys. And if I were to start writing reviews, would people trust me?
My hope is that Linux distributions will enforce a policy of only packaging applications where all dependencies are trusted packages (on top of reviewing the app's own codebase as usual). As difficult as this may sound, this is already the norm for C/C++ applications, where it's easy to find either distribution-packaged or self-contained libraries, and there are organized teams of maintainers selecting trustworthy packages, auditing and packaging libraries based on user demand, and telling audio libraries that require 70 packages to bind to PipeWire to clean up their act. Unfortunately, in languages where package managers and communities have embraced unvetted transitive dependencies, it's difficult to even find libraries with small or trusted dependency trees. This normalization of deviance has seriously harmed our ability to understand and/or trust the code making up our apps, and it will take changes in our package managers and community norms to restore trust.
My point is that: Npm is auditable, trackable.
I'm not challenging the bug itself, neither the security issue..
One thing NPM does (and I believe Python too) is to allow install scripts -- this has been a reliable vector for attackers to steal credentials. Not every package repository system has that.
https://www.bleepingcomputer.com/news/security/popular-coa-n...
>"Based on our analysis and information seen thus far, the malware is likely the Danabot password-stealing Trojan for Windows."
There's signal if there's detections
There's signal if something is old and there's no detections
There's no signal if something is recent with no detections