Papers
Topics
Authors
Recent
Search
2000 character limit reached

GANGRL-LLM: Adversarial Code & SQLi Detection

Updated 3 July 2026
  • GANGRL-LLM is a semi-supervised adversarial framework combining GANs and LLMs to generate high-fidelity malicious code and improve SQL injection detection.
  • It employs a tightly coupled generator–discriminator architecture with policy-gradient updates and feature-matching regularization to effectively utilize scarce labeled samples.
  • Experimental results demonstrate superior code synthesis and enhanced SQLi detection accuracy compared to baseline models in low-resource cybersecurity settings.

GANGRL-LLM is a semi-supervised framework that merges Generative Adversarial Networks (GANs) with LLMs to address the challenge of high-quality malicious code generation and SQL Injection (SQLi) detection when labeled samples are scarce. Designed as a collaborative adversarial system, GANGRL-LLM achieves both robust code synthesis and enhanced detection performance through a tightly coupled generator–discriminator architecture, reward shaping, and feature-matching regularization (Ma et al., 25 Aug 2025).

1. System Architecture

The architecture of GANGRL-LLM comprises two principal components: a pre-trained LLM-based generator and a GAN-style discriminator schema referred to as “GAN-BERT.” The generator GG utilizes Qwen2.5Coder—an LLM with 1.5 billion parameters—which, given a natural-language prompt xx, produces malicious code samples yPθ(yx)y \sim P_\theta(y|x). The discriminator DD has two submodules: a simulator SS, which is a multi-layer perceptron (MLP) mapping Gaussian noise zN(0,I)z \sim \mathcal{N}(0, I) into synthetic hidden-state vectors; and a classifier CC, comprising an MLP above a BERT encoder, which accepts either real (xrx_r) or simulated/generated (xsx_s) representations and yields a (k+1)(k+1)-way softmax across xx0 semantic classes and a “fake” class.

Training alternates between generator and discriminator updates: the generator optimizes a combination of maximum-likelihood (cross-entropy) on a small labeled set and policy-gradient updates incentivized by the discriminator’s “malicious” probability on newly generated samples; the discriminator performs both supervised classification and unsupervised adversarial learning on simulated/generator data.

2. Objective Functions

The loss structure integrates supervised, adversarial, and regularization terms for each module:

  • Classifier Loss xx1:

Combines supervised cross-entropy over true classes and unsupervised adversarial objectives distinguishing real data from simulated/fake inputs,

xx2

where xx3 and xx4 denote the real-code and simulator distributions, respectively.

  • Simulator Loss xx5:

Enforces adversarial indistinguishability and feature-matching,

xx6

where xx7 and xx8 are first-moment feature means.

  • Generator Loss xx9:

Jointly minimizes maximum likelihood and a policy-gradient reward reflecting discriminator signals,

yPθ(yx)y \sim P_\theta(y|x)0

with reward yPθ(yx)y \sim P_\theta(y|x)1 and a decaying mixing coefficient, yPθ(yx)y \sim P_\theta(y|x)2, where yPθ(yx)y \sim P_\theta(y|x)3, yPθ(yx)y \sim P_\theta(y|x)4 is the epoch, and yPθ(yx)y \sim P_\theta(y|x)5 the maximum epoch.

3. Training Algorithm

The end-to-end pipeline follows alternating updates across epochs and mini-batches:

  1. Batch Sampling: Draw labeled pairs yPθ(yx)y \sim P_\theta(y|x)6.
  2. Generator Update:
    • Autoregressively sample yPθ(yx)y \sim P_\theta(y|x)7.
    • Evaluate rewards yPθ(yx)y \sim P_\theta(y|x)8.
    • Compute MLE and reinforcement learning (policy-gradient) losses.
    • Update yPθ(yx)y \sim P_\theta(y|x)9's parameters via gradient descent with norm clipping.
  3. Discriminator Update:
    • Encode real and generated samples.
    • Update classifier DD0 using DD1.
    • Update simulator DD2 using DD3.
  4. Iterate for a fixed number of epochs (20).

Adaptive reward decay ensures initial exploration with GAN feedback and eventual stabilization around the labeled data likelihood.

4. Few-Sample Learning and Regularization Techniques

GANGRL-LLM adopts several mechanisms to address data scarcity:

  • Semi-supervised Adversarial Expansion: Generator and simulator outputs serve as pseudo-labeled data to augment the effective sample size without increasing manual annotation costs.
  • Contrastive Feature-Matching Regularization: Minimizing DD4 ensures generated/simulated activations remain close to real data in feature space, counteracting mode collapse and semantic drift.
  • Cross-Entropy Anchor: Inclusion of an explicit supervised term on a small labeled set stabilizes early training dynamics and encourages correct prompt-code alignment.
  • Adaptive Reward Decay: The GAN feedback (policy-gradient) term is weighted less over time, avoiding over-dependence on discriminator signals and promoting convergence on true data distribution.

5. Experimental Protocol and Results

a) Malicious Code Generation

Experiments evaluate GANGRL-LLM on SQLi code synthesis using 100 prompts constructed from the Kaggle SQL Injection dataset. Output is scored (1–10) by Qwen2.5Turbo along four axes: prompt adherence, complexity, exploit effectiveness, and correctness. GANGRL-LLM outperforms the base and fine-tuned Qwen2.5Coder, with improvements increasing alongside the number of labeled samples available.

Number of labeled samples Model Score
none Qwen2.5Coder 5.58
GANGRL-LLM 5.64↑
1,000 Qwen2.5Coder 5.28
GANGRL-LLM 5.74↑
2,000 Qwen2.5Coder 6.35
GANGRL-LLM 6.40↑

Ablation studies confirm that removing core components—discriminator, simulator, reward mixing, and adversarial/feature-matching losses—results in degraded performance (scores decrease to 5.27–5.62).

b) SQLi Detection

GANGRL-LLM-generated samples are used to augment training data for downstream SQLi detection models (CNN, Naïve Bayes, SVM, KNN, Decision Tree). Performance metrics include accuracy, precision, recall, and F1. For instance, CNN accuracy rises from 0.904 to 0.920 with the addition of 2,000 generated samples. Comparative benchmarks place the GANGRL-LLM discriminator at 99.9% recall, exceeding Gamma-TF-IDF (99.2%), EP-CNN (98.0%), ASTNN (99.2%), Trident (99.4%), and others. Transferability tests demonstrate consistent performance gains with both Qwen2.5Coder and Llama3.2 across SQLi and XSS datasets.

6. Key Hyperparameters and System Configuration

  • Hardware: Three NVIDIA RTX A5000 (24GB), Intel Xeon Platinum 8222L, Ubuntu 22.04, Python 3.11.10, PyTorch 2.5.1 with CUDA 12.1.
  • Learning rates: Both generator and discriminator set to DD5.
  • Batch size: 64; epochs: 20; gradient-clip norm: 1.0.
  • Reward-mixing: Initial DD6, decay base DD7.
  • Model components: Generator is Qwen2.5Coder (1.5B parameters); discriminator backbone is BERT (uncased_L-12_H-768_A-12) with dual MLPs for simulation and classification.

7. Significance and Implications

GANGRL-LLM demonstrates that tightly linking a GAN-style adversarial discriminator with an LLM code generator using policy-gradient reward shaping, feature-matching regularization, and semi-supervised losses can produce high-fidelity malicious code while significantly boosting downstream detection accuracy—even under extreme sample scarcity. This framework constitutes a practical approach for developing adaptive, high-precision IDS pipelines. A plausible implication is that similar adversarial-LLM couplings, incorporating regularization and semi-supervision, may be broadly effective in other low-resource cybersecurity and generative code synthesis settings (Ma et al., 25 Aug 2025).

Definition Search Book Streamline Icon: https://streamlinehq.com
References (1)

Topic to Video (Beta)

No one has generated a video about this topic yet.

Whiteboard

No one has generated a whiteboard explanation for this topic yet.

Follow Topic

Get notified by email when new papers are published related to GANGRL-LLM.