LLM-Enhanced Hierarchical Heterogeneous Graph Representation Learning for Malicious Python Package Detection

2026-07-03Cryptography and Security

Cryptography and SecurityArtificial IntelligenceSoftware Engineering
AI summary

The authors address the problem of detecting harmful Python packages in open-source repositories. They create a new method that builds detailed graphs showing different parts of the code and how they connect, while using large language models (LLMs) to understand the roles of functions better. Their approach uses these graphs and LLM insights to more accurately spot malicious behavior and pinpoint suspicious functions without needing experts to look manually. Tests on real-world data show their method works better than previous solutions and also explains its decisions clearly.

Malicious Python packagesSoftware supply chainHierarchical heterogeneous graphLarge Language Models (LLMs)Graph neural networksFunction-level semantic rolesCode analysisMalicious behavior detectionFine-grained localizationOpen-source repositories
Authors
Hang Gao, Xiaoyu Chen, Baoquan Cui, Zhen Tang, Peng Qiao, Fengge Wu, Jian Zhang
Abstract
Malicious Python packages have become a major threat to software supply chain ecosystems due to the widespread adoption of open-source repositories such as PyPI. Existing learning-based detection methods struggle to capture the hierarchical organization and heterogeneous interactions among different program entities. Although Large Language Models (LLMs) have demonstrated strong capabilities in code understanding and semantic reasoning, they are rarely integrated with structural program representations for fine-grained malicious behavior analysis. In this paper, we propose an LLM-enhanced hierarchical heterogeneous graph representation learning framework for malicious Python package detection. The framework constructs a hierarchical heterogeneous code graph that explicitly models heterogeneous code entities and different types of structural dependencies. LLMs are further leveraged to infer function-level semantic roles, introducing an additional layer of semantic heterogeneity. Based on this graph, we develop a hierarchical heterogeneous graph neural network that performs type-aware message passing over different node and edge categories, effectively modeling malicious behavior propagation for accurate package-level classification. The framework also incorporates a function-level attribution mechanism which, combined with LLM reasoning, automatically identifies suspicious functions and localizes fine-grained malicious behaviors without human expert intervention. Extensive experiments on real-world datasets show that our framework consistently outperforms traditional machine learning methods, graph-based detectors, and state-of-the-art LLMs across packages with varying sizes and dependency complexities, while providing accurate, robust, and interpretable malicious behavior localization.