[Paper Review] Leveraging Large Language Models to Detect npm Malicious Packages
This paper proposes SocketAI Scanner, a multi-stage LLM-based workflow using iterative self-refinement and zero-shot role-play Chain-of-Thought prompting to detect malicious npm packages. It achieves high precision (99%) and F1 (97%) using GPT-4, significantly outperforming static analysis and reducing manual review burden with explainable, low false-positive results.
Existing malicious code detection techniques demand the integration of multiple tools to detect different malware patterns, often suffering from high misclassification rates. Therefore, malicious code detection techniques could be enhanced by adopting advanced, more automated approaches to achieve high accuracy and a low misclassification rate. The goal of this study is to aid security analysts in detecting malicious packages by empirically studying the effectiveness of Large Language Models (LLMs) in detecting malicious code. We present SocketAI, a malicious code review workflow to detect malicious code. To evaluate the effectiveness of SocketAI, we leverage a benchmark dataset of 5,115 npm packages, of which 2,180 packages have malicious code. We conducted a baseline comparison of GPT-3 and GPT-4 models with the state-of-the-art CodeQL static analysis tool, using 39 custom CodeQL rules developed in prior research to detect malicious Javascript code. We also compare the effectiveness of static analysis as a pre-screener with SocketAI workflow, measuring the number of files that need to be analyzed. and the associated costs. Additionally, we performed a qualitative study to understand the types of malicious activities detected or missed by our workflow. Our baseline comparison demonstrates a 16% and 9% improvement over static analysis in precision and F1 scores, respectively. GPT-4 achieves higher accuracy with 99% precision and 97% F1 scores, while GPT-3 offers a more cost-effective balance at 91% precision and 94% F1 scores. Pre-screening files with a static analyzer reduces the number of files requiring LLM analysis by 77.9% and decreases costs by 60.9% for GPT-3 and 76.1% for GPT-4. Our qualitative analysis identified data theft, execution of arbitrary code, and suspicious domain categories as the top detected malicious packages.
Motivation & Objective
- Address the rising threat of software supply chain attacks, forecasted to affect 45% of organizations by 2025, by improving automated malware detection in npm packages.
- Reduce the manual review burden on security analysts by automating malware detection with minimal false positives.
- Evaluate whether Large Language Models (LLMs) can effectively detect malicious intent in JavaScript packages, going beyond traditional static analysis.
- Develop a scalable, explainable, and low-misclassification detection workflow for real-world deployment in open-source ecosystems.
Proposed method
- Propose SocketAI Scanner, a multi-stage malware detection workflow leveraging iterative self-refinement and zero-shot role-play Chain-of-Thought (CoT) prompting in GPT-3 and GPT-4.
- Use prompt engineering to guide LLMs through structured reasoning, improving output consistency and reducing ambiguity in malware classification.
- Generate machine-readable JSON reports with custom sloppy-parsing JSON validators to handle schema violations and syntax errors in LLM outputs.
- Apply the workflow to 5,115 npm packages (2,180 malicious, 2,935 neutral) using the MalwareBench dataset for evaluation.
- Compare LLM-based detection against a static analysis baseline to assess precision, recall, and F1 performance.
- Implement safeguards against prompt injection and model non-determinism through iterative validation and output sanitization.
Experimental results
Research questions
- RQ1RQ1: How can LLMs be leveraged to detect malware in JavaScript packages using advanced prompting techniques?
- RQ2RQ2: What performance can be achieved in malware detection using LLMs compared to static analysis, in terms of precision, recall, and F1 score?
- RQ3RQ3: How do LLMs handle complex or obfuscated malware patterns, and what are the limitations in output consistency and reliability?
Key findings
- GPT-4 achieved a precision of 99% and F1 score of 97% in detecting malicious npm packages, significantly outperforming the static analysis baseline.
- GPT-3 achieved a precision of 91% and F1 score of 94%, offering a cost-effective alternative with strong performance.
- The LLM-based approach reduced misclassification rates compared to static analysis, with improvements in precision (over 25%) and F1 (over 15%).
- SocketAI Scanner correctly identified 2,067 out of 2,180 malicious packages, demonstrating high detection capability on the MalwareBench dataset.
- Despite high accuracy, challenges remain with model non-determinism, mode collapse, and noisy or malformed JSON outputs requiring custom parsing.
- The study highlights risks from dataset bias and potential false negatives due to undetected malicious code in neutral packages, especially in larger or more complex files.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.