A two-week-old campaign to steal developers' credentials using malicious code distributed through npm, the Node.js package management registry, has been halted with the removal of 39 malicious npm packages.

Developers regularly add these bundles of JavaScript code to Node.js applications to implement common functions, so they don't have to write the code themselves.

In a blog post published on Wednesday, CJ Silverio, CTO at npm, said that between July 19 and July 31, an account named hacktask conducted a typosquatting attack by publishing a series of packages with names that are similar to popular existing npm packages.

"In the past, it's been mostly accidental," Silverio said. "In a few cases we’ve seen deliberate typo-squatting by authors of libraries that compete with existing packages. This time, the package naming was both deliberate and malicious – the intent was to collect useful data from tricked users."

Oscar Bolmsten, a developer based in Sweden, spotted the malicious code in a package named crossenv, designed to dupe people searching for cross-env, a popular script for setting environmental variables.

"Because environmental variables are such common a way to hand credentials to software, it's a pretty good thing to go after," said Silverio in a phone interview with The Register.

Environmental variables are used for, among other things, storing the account names, passwords, tokens, and keys that provide access to applications, cloud services, and APIs.

In this case, the malicious code attempts to copy any environmental variables set on the victim's machine and to transmit them to an attacker-controlled server at npm.hacktask.net.

The JSON configuration file used by crossenv runs a script named package-setup.js that converts existing environmental variables into a string and then sends the data via POST request.

According to Silverio, "about 40" packages submitted by hacktask have been removed from npm. Lift Security, she said, scanned every npm package for the abusive package setup code but found no other instances of it.

Silverio expressed doubt that the attack worked very well. "Typo-squatting turns out not to be a very effective way to get malware into the registries," she said, noting that people tend to rely on searches or copying and pasting published code.

Among the 39 packages that npm has linked to hacktask, most had about 40 downloads each since mid-July, excluding the surge in curiosity-driven downloads once word of the malware got out. The malicious crossenv package had the most downloads, at 700. But most of these are believed to be automated downloads triggered by npm mirror servers.

Silverio estimates that only about 50 people downloaded the bad crossenv package during the exposure period. She said she's not aware of any developers who have reported account compromises as a result of this incident.

A search of GitHub repos turns up a handful of references to the malicious crossenv package.

The hacktask account has been banned but the person or persons behind the account have not been publicly identified.

Asked whether npm has put measures in place to prevent someone else from conducting a similar attack under a different account name, Silverio acknowledged that the attack probably wouldn't be caught immediately.

"Even if we can't catch it all at the moment of publication, we have a system that works pretty well," she said, praising the vigilance of the npm community.

Nonetheless, in her blog post, Silverio said npm is looking into ways to identify name similarities in packages, in order to prevent future typo-squatting. The company is also working with security firm Smyte to detect spam published to the registry – apparently, spammers publish packages hoping the README files will be indexed by search engines, in order to boost website search rankings.

In a 2016 presentation at Kiwicon about Node.js security, developer Jeff Andrews posed this question to himself: "I use Node.js/npm. How do I stay safe?" His answer: "You can't." ®

PS: Nikolai Tschacher wrote a thesis on typosquatting and programming language package managers in 2016.