Python applications frequently depend on third-party packages to provide functionality without requiring developers to implement every component independently. These packages may be obtained from public repositories and incorporated into applications through dependency-management tools.
However, third-party packages can introduce software supply-chain security risks when their source code contains intentionally malicious functionality. An attacker may attempt to introduce malicious code into a package so that applications become exposed when the package is trusted, installed, or used.
Malicious-code injection can involve suspicious functions, unauthorized data access, unexpected command execution, or other code patterns intended to compromise a system. Such code may be embedded within otherwise legitimate-looking package source files, making manual inspection difficult when packages contain numerous files or complex functionality.
Traditional security tools can identify certain known vulnerabilities and suspicious patterns. However, identifying malicious code in previously unseen package samples may require additional analysis of the source code and its context.
In this use case, CodeBERT is used as the AI model for malicious-code classification. An existing public software-package security dataset is used as the primary data source for model training and evaluation. The dataset is inspected to identify suitable benign and malicious code samples, reliable classification labels, and source-code representations compatible with the proposed model.
The selected samples are preprocessed and used to fine-tune CodeBERT for binary classification. The trained model is evaluated against held-out records and subsequently tested using a controlled malicious-code injection scenario created within an isolated laboratory.
Ubuntu Linux provides the primary AI and package-analysis environment, while Kali Linux supports controlled security testing. The attack scenario uses a laboratory-only Python test package containing a harmless simulated malicious-code pattern. The code is inspected and classified without executing actual malware.
The model identifies potentially malicious code and flags suspicious package samples for further security review before installation. The classification result is compared with the known test condition and supporting source-code evidence to evaluate the detection workflow.
The complete workflow is:
Existing Public Software-Package Security Dataset
→
Dataset Suitability Assessment
→
Benign and Malicious Code Selection
→
Source-Code Preprocessing
→
CodeBERT Fine-Tuning
→
Held-Out Model Evaluation
→
Controlled Python Package Preparation
→
Simulated Malicious-Code Injection
→
Package Source-Code Inspection
→
CodeBERT Classification
→
Suspicious Code Identification
→
Risk Flagging
→
Security Review
→
Pre-Installation Approval Decision
→
Final Detection Validation