Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

5 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

๐Ÿ›ก๏ธ LLM-Security-Firewall

Architecture

๐Ÿšจ Semantic Prompt Injection Firewall for Large Language Models

Python XGBoost Streamlit FastAPI LLM Security License


๐Ÿ“Œ Overview

LLM-Security-Firewall is a lightweight semantic firewall designed to protect Large Language Models (LLMs) against:

  • ๐Ÿšซ Prompt Injection Attacks
  • ๐Ÿ”“ Jailbreak Attempts
  • ๐Ÿง  Role-play Exploits
  • โš ๏ธ Instruction Override Attacks
  • ๐Ÿ” System Prompt Leakage

The system introduces a Decoupled Security Architecture that intercepts and analyzes prompts before they ever reach the LLM.

Unlike traditional keyword filtering systems, this project uses:

  • ๐Ÿง  Semantic Vector Embeddings
  • โšก XGBoost Threat Classification
  • ๐Ÿ“Š Real-time Probability Analysis
  • ๐Ÿ›ก๏ธ Local Security Inference

This allows the firewall to detect malicious intent even when attacks are obfuscated or rephrased.


๐Ÿ—๏ธ System Architecture

Workflow

The firewall operates using a 4-Phase Security Pipeline:

Phase Description
๐Ÿ“ฅ Input & Ingestion User submits prompt through Streamlit UI
๐Ÿง  Semantic Analysis MiniLM converts prompt into embeddings
๐Ÿšฆ Logic Gate XGBoost classifies malicious probability
โ˜๏ธ Secure Resolution Only verified prompts reach the LLM

๐Ÿ” Threat Detection Logic

Decision Flow

The security core performs:

Prompt โ†’ Embedding โ†’ XGBoost โ†’ Threat Score โ†’ Decision

โš™๏ธ Detection Pipeline

Step 1 โ€” Semantic Vectorization

The prompt is transformed into a 384-dimensional semantic vector using:

all-MiniLM-L6-v2

Step 2 โ€” Classification

The embedding is analyzed using an XGBoost classifier.

Step 3 โ€” Security Decision

Threat Score Action
> 0.5 ๐Ÿšซ BLOCK Prompt
โ‰ค 0.5 โœ… Forward to LLM

๐Ÿ’ฌ User Interaction Flow

Chat Flow

The user experience is designed for:

  • โšก Real-time detection
  • ๐Ÿ“Š Instant feedback
  • ๐Ÿ›ก๏ธ Transparent security status
  • โ˜๏ธ Secure API routing

Malicious prompts are intercepted locally before any external transmission occurs.


โœจ Features

๐Ÿ›ก๏ธ Security

  • Prompt Injection Detection
  • Jailbreak Prevention
  • Semantic Threat Analysis
  • Probability-based Threat Scoring
  • Decoupled LLM Firewall

โšก Performance

  • Lightweight ML inference
  • Low latency (<50ms)
  • CPU-friendly deployment
  • Real-time classification

๐Ÿ“Š Interface

  • Interactive Streamlit Dashboard
  • Plotly Threat Meter
  • Session Threat Logs
  • Safe/Blocked Visual Alerts

โ˜๏ธ API Integration

  • OpenRouter Integration
  • Secure Request Routing
  • Conditional API Access
  • External LLM Protection Layer

๐Ÿง  Machine Learning Pipeline

Component Technology
Embedding Model Sentence Transformers
Semantic Encoder all-MiniLM-L6-v2
Classifier XGBoost
Frontend Streamlit
Backend FastAPI
Visualization Plotly
API Integration OpenRouter
Programming Language Python

๐Ÿ“ˆ Model Performance

Metric Score
๐ŸŽฏ Accuracy 85.15%
๐Ÿ“ˆ ROC-AUC 0.8989
โš–๏ธ Weighted F1-Score 83.85%
๐Ÿ›ก๏ธ Precision (Malicious) 86.20%
๐Ÿšจ Recall (Malicious) 81.50%

๐Ÿ“‚ Project Structure

LLM-Security-Firewall/
โ”‚
โ”œโ”€โ”€ streamlit_app.py
โ”œโ”€โ”€ api_server.py
โ”œโ”€โ”€ config.py
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ .env.example
โ”œโ”€โ”€ LICENSE
โ”‚
โ”œโ”€โ”€ models/
โ”‚   โ””โ”€โ”€ xgb_embed_model.json
โ”‚
โ”œโ”€โ”€ notebook/
โ”‚   โ””โ”€โ”€ PromptInjectionSecurityFilter.ipynb
โ”‚
โ”œโ”€โ”€ assets/
โ”‚   โ”œโ”€โ”€ architecture-overview.png
โ”‚   โ”œโ”€โ”€ technical-workflow.png
โ”‚   โ”œโ”€โ”€ security-decision-flow.png
โ”‚   โ””โ”€โ”€ chat-flow-diagram.png
โ”‚
โ””โ”€โ”€ docs/
    โ”œโ”€โ”€ Project_Report.pdf
    โ””โ”€โ”€ PSF_Presentation.pptx

๐Ÿš€ Installation

1๏ธโƒฃ Clone Repository

git clone https://github.com/YOUR_USERNAME/LLM-Security-Firewall.git

cd LLM-Security-Firewall

2๏ธโƒฃ Install Dependencies

pip install -r requirements.txt

3๏ธโƒฃ Configure Environment Variables

Create a .env file:

OPENROUTER_API_KEY=your_api_key_here

โ–ถ๏ธ Running the Application

๐Ÿ”ฅ Launch Streamlit Frontend

streamlit run streamlit_app.py

โšก Launch FastAPI Backend

uvicorn api_server:app --reload

๐Ÿ”„ Security Workflow

User Prompt
     โ†“
Semantic Vectorization
     โ†“
XGBoost Threat Classification
     โ†“
Threat Score Evaluation
     โ†“
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Malicious     โ”‚ Safe           โ”‚
โ”‚ Prompt        โ”‚ Prompt         โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ BLOCK         โ”‚ Forward to LLM โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“š Research Contributions

This project demonstrates that:

  • Lightweight machine learning models can effectively secure LLMs
  • Semantic embeddings outperform traditional keyword filters
  • Decoupled architectures reduce inference costs
  • Real-time LLM firewalls can operate on standard consumer hardware

๐Ÿ”ฎ Future Improvements

  • ๐Ÿง  FAISS vector database integration
  • ๐ŸŒ Multilingual attack detection
  • ๐Ÿ–ผ๏ธ Multimodal prompt injection defense
  • ๐Ÿ’ฌ Multi-turn conversational memory analysis
  • โšก Adaptive threshold optimization
  • โ˜๏ธ Cloud-native deployment support

๐Ÿ“„ Documentation

Detailed research and implementation documents are available inside:

docs/

Including:

  • ๐Ÿ“˜ Research Report
  • ๐Ÿ“Š Project Presentation
  • ๐Ÿงช Experimental Results
  • ๐Ÿง  ML Workflow Analysis

โญ Support

If you found this project useful:

  • โญ Star the repository
  • ๐Ÿด Fork the project
  • ๐Ÿ›ก๏ธ Contribute to LLM security research

๐Ÿ“œ License

This project is licensed under the MIT License.


๐Ÿ›ก๏ธ Secure the Future of AI

Building Defensive Intelligence for Large Language Models

About

Semantic Prompt Injection Firewall for Large Language Models using Sentence Transformers and XGBoost

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages