CoolFace
Modelpublic

sajan-sarker/Qwen3.5-4B-LoRA-GRPO-CyberSec-Reasoner

sourceHugging Faceupdated 5mo agoView on Hugging Face
2likes70downloads
Model Card

๐Ÿ” CyberSec-Reasoner (Qwen3.5-4B | QLoRA + GRPO Fine-tuned)

A Three-Stage Post-Trained Reasoning LLM (Supervised Reasoning + GRPO Fine-Tuned) for Cybersecurity Vulnerability Analysis, optimized for CVE โ†’ CWE mapping with structured outputs.

A three-stage Supervised Instruction-Reasoning + GRPO fine-tuned LLM for cybersecurity reasoning, vulnerability classification, and explainable threat analysis.

Model Details

Model Description

CyberSec-Reasoner is a domain-specialized LLM designed to analyze cybersecurity scenarios using structured Chain-of-Thought reasoning.

  • โ€”๐Ÿ” CVE โ†’ CWE vulnerability mapping
  • โ€”๐Ÿงฉ Attack scenario decomposition
  • โ€”๐Ÿ›ก๏ธ Security reasoning with \<think>...\</think> traces
  • โ€”๐Ÿ“š Explicit Chain-of-Thought (CoT) reasoning for interpretable security analysis

Additional Information

  • โ€”Developed by: Sajan Kumer Sarker
  • โ€”Model type: Fully fine-tuned causal language model (merged from QLoRA + GRPO)
  • โ€”Language(s) (NLP): English
  • โ€”License: Apache License 2.0 (inherited from base model Qwen/Qwen3.5-4B by Alibaba)
  • โ€”Finetuned from model: Qwen/Qwen3.5-4B by Alibaba
  • โ€”Modifications: This model is a fine-tuned and merged version using QLoRA and GRPO for cybersecurity reasoning tasks.

Uses

Direct Use

This model is intended for:

  • โ€”Cybersecurity vulnerability analysis
  • โ€”CVE โ†’ CWE classification
  • โ€”Security reasoning and explanation generation
  • โ€”Educational use in cybersecurity and AI reasoning

Out-of-Scope Use

  • โ€”Fully automated security decision-making without human validation
  • โ€”Real-time production defense systems without human validation
  • โ€”High-stakes environments requiring guaranteed correctness
  • โ€”Malicious use such as generating exploit strategies

Bias, Risks, and Limitations

  • โ€”Limited coverage of CVEโ€“CWE mappings
  • โ€”May produce incorrect or hallucinated CWE IDs
  • โ€”Performance relies more on reasoning than memorization
  • โ€”Dataset bias may influence predictions
  • โ€”Not all security domains are equally represented

Recommendations

  • โ€”Always verify outputs with authoritative security sources
  • โ€”Use alongside retrieval systems (RAG) for better accuracy
  • โ€”Do not rely on the model for critical security decisions
  • โ€”Combine with expert human analysis

How to Get Started with the Model

โš™๏ธ This repository provides a fully merged model. No additional adapter loading is required. Use the code below to get started with the model.

Prerequisites

bash
pip install --upgrade transformers==5.5.4

Option 1 โ€” Quick Start with Wrapper Class (Recommended)

python
# Download the inference wrapper from the HuggingFace repo
import subprocess
subprocess.run(["wget", "https://huggingface.co/sajan-sarker/Qwen3.5-4B-LoRA-GRPO-CyberSec-Reasoner/resolve/main/inference.py"])

from inference import CyberSecReasoner, CyberSecReasonerConfig

cfg = CyberSecReasonerConfig()
model = CyberSecReasoner(cfg)

cve_description = """Analyze the following CVE description and map it to the appropriate CWE.
Provide a brief justification for your choice. Ensure the last line of your response contains only the CWE ID.
CVE Description: Cross-site scripting (XSS) vulnerability in MyBulletinBoard (MyBB) allows remote
attackers to inject arbitrary web script or HTML via a signature containing a JavaScript URI in the
SRC attribute of an IMG element..."""

messages = [{"role": "user", "content": cve_description.strip()}]

completion = model.generate(messages)
reasoning, final_output = model.format_completions(completion)

print("=== Reasoning Trace ===")
print(reasoning)

print("\n=== Analysis & CWE Classification ===")
print(final_output)

Option 2 โ€” Direct Transformers Usage

python
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_name = "sajan-sarker/Qwen3.5-4B-LoRA-GRPO-CyberSec-Reasoner"

tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.bfloat16,
    device_map="auto",
    trust_remote_code=True,
)
model.eval()

cve_description = """Analyze the following CVE description and map it to the appropriate CWE.
Provide a brief justification for your choice. Ensure the last line of your response contains only the CWE ID.
CVE Description: <YOUR CVE DESCRIPTION HERE>"""

messages = [{"role": "user", "content": cve_description.strip()}]
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

with torch.no_grad():
    outputs = model.generate(**inputs, max_new_tokens=1024, do_sample=False)

generated_tokens = outputs[0][inputs["input_ids"].shape[1]:]
print(tokenizer.decode(generated_tokens, skip_special_tokens=True))

Expected Response with option 1

=== Reasoning ===
<think>
Okay, let's tackle this CVE analysis. The problem is a cross-site scripting (XSS) vulnerability in MyBulletinBoard (MyBB). The description mentions that remote attackers can inject arbitrary web script or HTML through a signature with a JavaScript URI in the SRC attribute of an IMG element. The specific issue is using SGML numeric character references without a trailing semicolon.

First, I need to recall what XSS vulnerabilities ...
</think>

=== Analysis & CWE Classification ===
The CVE describes an XSS vulnerability caused by improper neutralization of user-controllable input (JavaScript URI in an IMG SRC attribute) embedded into HTML output. This directly aligns with ...

CWE-79

Training Details

Three-Stage Post-Training Pipeline

Training Workflow Diagram

Training Procedure

Stage 1 โ€” Knowledge-Based SFT

  • โ€”Goal: Teach foundational cybersecurity vocabulary and domain facts
  • โ€”Data: Merged dataset from Fenrir v2.0 + Cybersec-Reasoning-Merged
  • โ€”Method: SFT with full loss on prompt + assistant tokens (~96K samples, 3 epochs)

Stage 2 โ€” Cold-Start Reasoning SFT

  • โ€”Goal: Establish the structured <think>...</think> reasoning format
  • โ€”Data: ~1,000 samples from Primus-Reasoning, filtered for CVE-to-CWE mapping
  • โ€”Method: SFT with assistant-only loss to enforce the reasoning structure

Stage 3 โ€” GRPO Reinforcement Learning

  • โ€”Goal: Refine judgment and improve CWE prediction accuracy
  • โ€”Data: ~2,300 CVE-to-CWE samples from the Primus dataset
  • โ€”Method: GRPO with a composite reward function (see below)

Evaluation

Testing Data, Factors & Metrics

Testing Data
Factors
  • โ€”Vulnerability type diversity
  • โ€”Reasoning structure correctness
  • โ€”Domain-specific keyword coverage
Metrics
  • โ€”BLEU
  • โ€”ROUGE
  • โ€”Perplexity
  • โ€”CWE Accuracy (exact match) [Custom rule based metric]
  • โ€”Format Match (\<think> ... \</think> structure) [Custom rule based metric]
  • โ€”Keyword Match [Custom rule based metric]

Results

  • โ€”BLEU: 0.1098
  • โ€”ROUGE1: 0.3680
  • โ€”ROUGE2: 0.1528
  • โ€”ROUGE3: 0.1779
  • โ€”ROUGELsum: 0.2507
  • โ€”PERPLEXITY: 1.7989
  • โ€”CWE Accuracy: 33.90%
  • โ€”Format Match: 94.85%
  • โ€”Keyword Match: 97.85%

*Note: \ Due to limited coverage of CVEโ€“CWE knowledge and the relatively small 4B parameter size, the model may achieve lower exact-match accuracy for CWE classification. However, it is specifically optimized for structured reasoning on CVEโ€“CWE tasks. As a result, when combined with external knowledge sources (e.g., a CVEโ€“CWE database or RAG pipeline), the model is expected to produce more accurate mappings along with high-quality, interpretable reasoning.*

Summary

The model demonstrates strong reasoning capabilities but is limited by incomplete vulnerability knowledge coverage.

Model Examination

  • โ€”Structured reasoning trace analysis via \<think> blocks
  • โ€”Output post-processing for CWE extraction
  • โ€”Behavior shaped via reward-based RL (GRPO)

Environmental Impact

Carbon emissions can be estimated using the Machine Learning Impact calculator presented in Lacoste et al. (2019).

  • โ€”Hardware Type: AMD MI300X (192GB single GPU)
  • โ€”Hours used: ~90 Hours
  • โ€”Cloud Provider: AMD Developer Cloud
  • โ€”Compute Region: Atlanta [Datacenter 1 - ATL1]
  • โ€”Carbon Emitted: Not estimated

Hardware Requirements

  • โ€”GPU recommended (>=16GB VRAM)
  • โ€”CPU inference possible but slow
  • โ€”Model size: ~9 GB

Model Card Authors

Sajan Kumer Sarker

Model Card Contact

For questions or collaboration, open an issue on the Hugging Face repository.

Framework versions

  • โ€”transformers>=5.5.4