Abstract
With the growing popularity of modularity in software development comes the rise of package managers and language ecosystems. Among them, npm stands out as the most extensive package manager, hosting more than 2 million third-party open-source packages that greatly simplify the process of building code. However, this openness also brings security risks, as evidenced by numerous package poisoning incidents. In this paper, we synchronize a local package cache containing more than 3.4 million packages in near real-time to give us access to more package code details. Further, we perform manual inspection and API call sequence analysis on packages collected from public datasets and security reports to build a hierarchical classification framework and behavioral knowledge base covering different sensitive behaviors. In addition, we propose the DONAPI, an automatic malicious npm packages detector that combines static and dynamic analysis. It makes preliminary judgments on the degree of maliciousness of packages by code reconstruction techniques and static analysis, extracts dynamic API call sequences to confirm and identify obfuscated content that static analysis can not handle alone, and finally tags malicious software packages based on the constructed behavior knowledge base. To date, we have identified and manually confirmed 325 malicious samples and discovered 2 unusual API calls and 246 API call sequences that have not appeared in known samples.
Introduction
The npm registry is the backbone of modern JavaScript development, hosting over 2 million packages. However, its openness makes it a prime target for "software supply chain attacks," such as typosquatting (e.g., crossenv vs. cross-env) and dependency confusion. Existing security tools often fall into two traps: static analysis is easily bypassed by obfuscation, and metadata analysis is too shallow to catch sophisticated poisoning where a legitimate maintainer's account is hijacked. DONAPI addresses these challenges by focusing on the "smoking gun" of malware: its behavior, specifically the sequence of sensitive API calls (e.g., reading a file then sending it over the network).
System Architecture
DONAPI employs a multi-stage pipeline to analyze packages, combining static code reconstruction with dynamic sandbox execution to defeat obfuscation tactics.
The process begins with the Code Dependencies Reconstructor, which uses Abstract Syntax Trees (ASTs) to merge internal dependencies and scripts into a single file, simulating the package's actual execution flow. Simultaneously, the Malicious Shell Command Detector parses package.json scripts and .sh files to catch "install-time" attacks. An Obfuscated Code Detector then flags code likely attempting to hide malicious logic. Finally, suspicious packages are executed in a Docker sandbox where the Dynamic Behavior Extractor intercepts 132 different APIs to capture the exact order of system interactions.
Behavioral Knowledge Mapping
The core of DONAPI is its hierarchical classifier, which maps captured API sequences to a structured knowledge base of known attack behaviors.
This framework organizes detection into three layers:
- APIs: 806 sensitive atomic operations (e.g.,
fsPromises.rmdir). - Behavior Types: 40 types of sensitive behavior (e.g.,
FILE_DELETE,FILE_MODIFY). - Malicious Categories: 5 broad categories (e.g., Sensitive Information Theft, Reverse Shell, Malicious Software Import).
By focusing on sequences rather than isolated calls, DONAPI can distinguish between legitimate use and malicious intent. For example, calling http.get to download a script followed by fs.writeFile and child_process.exec is identified as a classic "Malicious Software Import" pattern.
Experimental Results
The researchers evaluated DONAPI using a massive local replicate of over 3.4 million npm packages. On a curated test dataset of 1,159 malicious and 3,000 benign samples, the system achieved 98.88% precision and 91.63% recall (F1-score of 0.93), significantly outperforming existing tools like Google’s GUARDDOG.
In real-world deployment, DONAPI identified 325 new malicious samples and discovered 246 previously unseen API call sequences. The system is highly efficient, processing approximately 1.29 million lines of code every 10 minutes, sufficient to keep pace with the daily churn of the npm registry (~17,000 updates/day).
Conclusion
DONAPI demonstrates that behavior-aware detection is a powerful defense against software supply chain attacks. By merging static and dynamic analysis and mapping actions to a behavioral knowledge base, it effectively identifies both known and novel malicious patterns. While attackers may attempt evasion through environment-sensitive triggers or sleep functions, the system's ability to track API sequences provides a robust foundation for future security enhancements. Future work will focus on refining the sandbox to better emulate production environments and expanding shell command support.
Summary updated