Detecting Malicious Packages in PyPI and NPM by Clustering Installation Scripts
- 23 October 2025
- journal article
- Published by Institute of Electrical and Electronics Engineers (IEEE) in IEEE Transactions on Software Engineering
- Vol. 52 (1) , 36-53
Abstract
Software repositories such as PyPI and npm are vital for software development but expose users to serious security risks from malicious packages. The malicious packages often execute their payloads immediately upon installation, leading to rapid system compromise. Existing detection methods are heavily dependent on difficult-to-obtain explicit knowledge, rendering them susceptible to overlooking emergent malicious packages. In this paper, we present a lightweight and effective method, namely EMPHunter, to detect malicious packages without requiring any explicit prior knowledge. EMPHunter is founded upon two fundamental and insightful observations. First, malicious packages are considerably rarer than benign ones, and second, the functionality of installation scripts for malicious packages diverges significantly from those of benign packages, with the latter frequently forming clusters. Consequently, EMPHunter utilizes the clustering technique to group the unique installation scripts of new-uploaded packages and identifies outliers as candidate malicious packages. It then ranks the outliers according to their deviate degrees and the distance between each of them and known malicious instances, effectively highlighting potential malicious packages. With EMPHunter, we successfully identified 122 previously unknown malicious packages from a pool of 267,009 newly-uploaded PyPI and npm packages, achieving an mAP (Mean Average Precision) of 0.813 and an exceptional recall of 0.992 when auditing the top-10 rankings. All detected packages have been officially confirmed as genuine malicious package by PyPI and npm. We assert that EMPHunter offers a valuable and advantageous supplement to existing detection tools, augmenting the arsenal of software supply chain security analysis.Keywords
Funding Information
- Strategic Priority Research Program of the Chinese Academy of Sciences (XDA0320401)
- National Natural Science Foundation of China (62202457)
- YuanTu Large Research Infrastructure
This publication has 27 references indexed in Scilit:
- A Needle is an Outlier in a Haystack: Hunting Malicious PyPI Packages with Code Clustering2023
- An Empirical Study of Malicious Code In PyPI Ecosystem2023
- SoK: Taxonomy of Attacks on Open-Source Software Supply Chains2023
- Bad Snakes: Understanding and Improving Python Package Index Malware Scanning2023
- CENTRIS: A Precise and Scalable Approach for Identifying Modified Open-Source Software Reuse2021
- Defending Against Package Typosquatting2020
- Typosquatting and Combosquatting Attacks on the Python Ecosystem2020
- Backstabber’s Knife Collection: A Review of Open Source Software Supply Chain Attacks2020
- Detecting Suspicious Package Updates2019
- VUDDY: A Scalable Approach for Vulnerable Code Clone Discovery2017