[论文解读] Leveraging Large Language Models to Detect npm Malicious Packages
本文提出 SocketAI Scanner,一种基于多阶段 LLM 的工作流,采用迭代自我改进和零样本角色扮演思维链(Chain-of-Thought)提示技术,以检测恶意 npm 包。该方法在使用 GPT-4 时实现了 99% 的精确率和 97% 的 F1 分数,显著优于静态分析方法,并通过可解释的、低误报率的结果大幅减少了人工审查负担。
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.
研究动机与目标
- 通过改进 npm 包中的自动化恶意软件检测,应对预计到 2025 年将影响 45% 组织的软件供应链攻击日益增长的威胁。
- 通过最小化误报率的自动化恶意软件检测,减轻安全分析师的审查负担。
- 评估大型语言模型(LLMs)是否能够有效检测 JavaScript 包中的恶意意图,超越传统静态分析方法。
- 为开源生态系统中的实际部署开发一种可扩展、可解释且低误分类率的检测工作流。
提出的方法
- 提出 SocketAI Scanner,一种基于 GPT-3 和 GPT-4 的多阶段恶意软件检测工作流,利用迭代自我改进和零样本角色扮演思维链(CoT)提示技术。
- 通过提示工程引导 LLM 进行结构化推理,提升输出的一致性,并减少恶意软件分类中的模糊性。
- 生成可机器读取的 JSON 报告,并使用自定义的容错解析 JSON 验证器处理 LLM 输出中的模式违规和语法错误。
- 使用 MalwareBench 数据集对 5,115 个 npm 包(2,180 个恶意,2,935 个中性)应用该工作流进行评估。
- 将基于 LLM 的检测方法与静态分析基线进行对比,评估其在精确率、召回率和 F1 分数上的表现。
- 通过迭代验证和输出净化,防范提示注入攻击和模型非确定性问题。
实验结果
研究问题
- RQ1RQ1:如何利用先进提示技术,借助 LLM 检测 JavaScript 包中的恶意软件?
- RQ2RQ2:与静态分析相比,LLM 在精确率、召回率和 F1 分数方面能实现怎样的检测性能?
- RQ3RQ3:LLM 如何处理复杂或混淆的恶意软件模式?其输出一致性和可靠性存在哪些局限?
主要发现
- GPT-4 在检测恶意 npm 包方面实现了 99% 的精确率和 97% 的 F1 分数,显著优于静态分析基线。
- GPT-3 实现了 91% 的精确率和 94% 的 F1 分数,提供了性能强劲且成本更低的替代方案。
- 与静态分析相比,基于 LLM 的方法降低了误分类率,在精确率(提升 25%以上)和 F1 分数(提升 15%以上)方面均有改善。
- SocketAI Scanner 在 MalwareBench 数据集中正确识别了 2,067 个恶意包中的 2,180 个,展示了其在该数据集上的强大检测能力。
- 尽管准确率很高,但模型非确定性、模式崩溃以及噪声或格式错误的 JSON 输出仍带来挑战,需依赖自定义解析。
- 本研究揭示了数据集偏差带来的风险,以及由于中性包中未检测到的恶意代码可能导致的假阴性问题,尤其在更大或更复杂的文件中更为显著。
阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。