Learning from Few Samples: A Novel Approach for High-Quality Malcode Generation

Haijian Ma, Daizong Liu, Xiaowen Cai, Pan Zhou, Yulai Xie · OpenReview

Abstract

Intrusion Detection Systems (IDS) play a crucial role in network security defense. However, a significant challenge for IDS in training detection models is the shortage of adequately labeled malicious samples. To address these issues, this paper introduces a novel semi-supervised framework GANGRL-LLM, which integrates Generative Adversarial Networks (GANs) with Large Language Models (LLMs) to enhance malicious code generation and SQL Injection (SQLi) detection capabilities in few-sample learning scenarios. Specifically, our framework adopts a collaborative training paradigm where: (1) the GAN-based discriminator improves malicious pattern recognition through adversarial learning with generated samples and limited real samples; and (2) the LLM-based generator refines the quality of malicious code synthesis using reward signals from the discriminator. The experimental results demonstrate that even with a limited number of labeled samples, our training framework is highly effective in enhancing both malicious code generation and detection capabilities. This dual enhancement capability offers a promising solution for developing adaptive defense systems capable of countering evolving cyber threats.

Introduction

Intrusion Detection Systems (IDS) serve as the primary line of defense for modern networks. However, their effectiveness is often hampered by a "data drought." To accurately identify threats, these systems require extensive datasets of malicious code, or "black samples." In practice, high-quality labeled samples are difficult to obtain because organizations are reluctant to share breach data due to legal and reputational risks. Furthermore, existing automated attack generators often produce simplistic or repetitive code that fails to challenge sophisticated modern IDS. While Large Language Models (LLMs) show promise in code generation, they often lack the domain-specific nuance required for complex malicious patterns when trained on limited data. This research addresses the challenge of generating diverse, high-quality malcode using only a small set of labeled examples.

Methodology: The GANGRL-LLM Framework

The researchers propose GANGRL-LLM, a semi-supervised framework that frames malcode generation as a collaborative adversarial process. The architecture consists of two primary components:

  1. The Generator: Utilizing the Qwen2.5Coder LLM, this component synthesizes malicious code snippets (such as SQL injection) based on natural language prompts.
  2. The Discriminator: A BERT-based model designed to classify code as benign, real malicious, or synthetic "fake" attempts.
The GANGRL-LLM framework architecture, illustrating the collaborative training between the LLM generator and the GAN-based discriminator.

The core innovation lies in the two-tiered GAN structure. The discriminator provides a reward signal to the generator through reinforcement learning (Policy Gradient), effectively acting as a coach. To stabilize training, the framework employs Adaptive Reward Weighting, which dynamically adjusts the influence of these rewards over time. Additionally, a "Code Word Vector Distribution Simulator" is integrated to help the discriminator capture the structural nuances of code beyond simple text classification.

Experimental Results and Analysis

The framework was evaluated primarily on SQL Injection (SQLi) and Cross-Site Scripting (XSS) datasets. The results indicate that GANGRL-LLM significantly outperforms standard fine-tuning approaches in both code quality and defensive utility.

Code Quality and Ablation Study

In evaluations using Qwen2.5Turbo as a judge, GANGRL-LLM achieved a quality score of 5.74/10, compared to 5.27 for a model fine-tuned on the same amount of data. An ablation study confirmed that the discriminator is the most critical component of the system.

Ablation study results highlighting the critical role of the discriminator ('No Disc') in maintaining high code quality scores.

As shown in the ablation results, removing the discriminator ("No Disc") led to the most substantial decrease in performance. This suggests that while an LLM can generate valid syntax, the adversarial feedback is necessary to produce the complex, devious patterns required for realistic attack simulation.

Defensive Enhancement

The generated samples proved highly effective for training downstream classifiers. For instance, an SVM-based detector's accuracy improved from 0.48 to 0.80 when its training set was augmented with GANGRL-LLM samples. Furthermore, 99.7% of the generated samples were validated as legitimate SQLi attacks by commercial-grade security tools, demonstrating their real-world fidelity.

Conclusion

GANGRL-LLM provides a robust solution for the data scarcity problem in cybersecurity. By combining the generative capabilities of LLMs with the adversarial rigor of GANs, the framework produces high-quality malicious code that enhances both offensive testing and defensive training. Future work will focus on refining the reward mechanism to reduce noise and expanding the framework's scope to cover a wider variety of cyber threats and programming languages.

Summary updated

BibTeX
@inproceedings{ma2025learning,
  author = {Haijian Ma and Daizong Liu and Xiaowen Cai and Pan Zhou and Yulai Xie},
  title = {{Learning from Few Samples: A Novel Approach for High-Quality Malcode Generation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2025},
}
Figure