Production-ready RAG security testing and defense framework
Project description
CorpusGuard
Production-ready RAG security testing and defense framework
Built on published research: Poisoning the Compliance Mind: Adversarial Memory Injection Attacks on RAG-Based AML Agents โ SSRN 6734225
What CorpusGuard does
CorpusGuard automatically red teams any RAG-based AI system for corpus poisoning vulnerabilities and deploys the Memory Hygiene Layer (MHL) defense to protect it.
One command. Full security assessment. CISO-ready PDF report.
๐ See the output without running anything: docs/sample_report.pdf โ a representative CISO assessment report.
# Option 1 โ Docker Compose (full stack)
docker compose up # older Docker: docker-compose up
# Dashboard: http://localhost:3000
# API: http://localhost:8000/docs
# Option 2 โ Run demo directly
python examples/bank_compliance_demo.py
# Option 3 โ install from source (not yet published to PyPI)
pip install -e .
corpusguard scan --target my_rag_config.yaml
The attack it finds
An adversary injects strategically crafted documents into a RAG system's vector database through legitimate ingestion pathways. The AI agent's accuracy collapses from 91.9% to 1.4% โ with zero anomalous log signatures. The system appears fully operational while making catastrophically wrong decisions.
| Finding | Result |
|---|---|
| Baseline AML agent F1 | 0.919 |
| After QTPI attack (ฮฒ=50 docs) | 0.014 โ 98.4% degradation |
| Log anomalies generated | Zero |
| Existing framework coverage | None โ not in NIST AI RMF, OWASP LLM Top 10, or MITRE ATLAS |
The defense it deploys
| MHL Component | Function | Result |
|---|---|---|
| CPT โ Cryptographic Provenance Tracking | SHA-256 hashing + source trust scoring | 100% detection, 0% false quarantine |
| FCS โ Factual Consistency Scoring | Cross-encoder scoring against anchor corpus | 22.6ms p99 latency on CPU |
| SRAD โ Statistical Retrieval Anomaly Detection | KS-test on 500-query sliding window | Zero-config anomaly detection |
Quickstart
Docker Compose โ one command
git clone https://github.com/OndiekiFrank/CorpusGuard.git
cd CorpusGuard
docker compose up
Opens:
- React Dashboard:
http://localhost:3000 - FastAPI Swagger UI:
http://localhost:8000/docs - PDF report auto-saved to
./reports/
CLI (install from source)
CorpusGuard is not yet published to PyPI โ install the CLI from a clone:
git clone https://github.com/OndiekiFrank/CorpusGuard.git
cd CorpusGuard
pip install -e .
corpusguard scan --target config/my_rag.yaml
corpusguard attack --vector qtpi --budget 50
corpusguard defend --mode full
corpusguard report --format pdf --output ./ciso_report.pdf
Bank compliance demo
python examples/bank_compliance_demo.py
Runs a complete 5-phase assessment โ attack campaign, MHL defense, OWASP scorecard, real-time monitor, CISO PDF report.
Architecture
CorpusGuard/
โโโ frontend/ โ React + Vite professional dark UI (port 3000)
โ โโโ src/components/
โ โโโ TabView.jsx โ 6 tabs: Attack, OWASP, MHL, Campaign, Monitor, Threat Intel
โ โโโ MonitorTab.jsx โ Real-time corpus monitoring with live chart
โ โโโ CampaignRunner.jsx โ 3-phase visual stepper
โ โโโ PDFReport.jsx โ One-click PDF download
โโโ corpusguard/
โ โโโ api/main.py โ FastAPI REST gateway โ 7 endpoints (port 8000)
โ โโโ attacks/
โ โ โโโ qtpi.py โ Query-Time Prompt Injection attack
โ โ โโโ campaign.py โ Multi-phase attack campaigns
โ โโโ defense/
โ โ โโโ mhl.py โ Memory Hygiene Layer (main)
โ โ โโโ cpt.py โ Cryptographic Provenance Tracking
โ โ โโโ fcs.py โ Factual Consistency Scoring
โ โ โโโ srad.py โ Statistical Retrieval Anomaly Detection
โ โโโ scanner/
โ โ โโโ owasp.py โ OWASP LLM Top 10 compliance scorecard
โ โ โโโ monitor.py โ Real-time monitoring daemon
โ โโโ dashboard/
โ โโโ app.py โ Streamlit dashboard (alternative UI)
โ โโโ report_generator.py โ CISO-ready PDF report generator
โโโ examples/
โ โโโ bank_compliance_demo.py โ Full end-to-end bank assessment demo
โโโ tests/ โ pytest suite passing (Python 3.9, 3.10, 3.11)
โโโ docker-compose.yml โ Full stack: API + React UI + Redis
โโโ Dockerfile โ API container (lean runtime deps)
โโโ Dockerfile.dashboard โ Streamlit container
โโโ .github/workflows/
โโโ ci.yml โ GitHub Actions CI/CD
โโโ security-gate.yml โ EU AI Act compliance gate
REST API
Full Swagger UI at http://localhost:8000/docs
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/health |
Health check |
| POST | /api/v1/scan |
Submit RAG system for scanning |
| GET | /api/v1/scan/{id} |
Get scan results |
| GET | /api/v1/owasp/{id} |
OWASP LLM Top 10 scorecard |
| GET | /api/v1/report/{id} |
Download PDF report |
| POST | /api/v1/defend |
Deploy MHL defense |
| GET | /api/v1/scans |
List all scans |
React Dashboard โ 6 Tabs
| Tab | What it shows |
|---|---|
| Attack results | Live F1 degradation chart, attack metrics, verdict |
| OWASP LLM Top 10 | Full compliance scorecard with severity badges |
| MHL defense | Module status with live pulse indicators |
| Campaign | 3-phase visual stepper โ Recon โ Exploit โ Persist |
| Monitor | Real-time document feed, corpus health chart, live alerts |
| Threat intel | Documented attack patterns with actor, target, mitigation |
OWASP LLM Top 10 Coverage
| ID | Vulnerability | Status |
|---|---|---|
| LLM01 | Prompt Injection | TESTED โ CRITICAL |
| LLM03 | Training Data Poisoning | TESTED โ PARTIAL |
| LLM05 | Supply Chain Vulnerabilities | TESTED โ FAILED |
| LLM06 | Sensitive Information Disclosure | TESTED โ PARTIAL |
| LLM09 | Overreliance | TESTED โ PARTIAL |
| LLM02, 04, 07, 08, 10 | Various | Planned v0.3 |
Regulatory Coverage
| Framework | CorpusGuard Mitigation |
|---|---|
| NIST AI RMF | First reference implementation for RAG corpus security |
| OWASP LLM Top 10 | QTPI formalises new LLM01 sub-category |
| MITRE ATLAS | New corpus-level attack pattern documented |
| FINRA Rule 3110 | CPT provides cryptographic audit trail |
| FinCEN 2024 | CPT + SRAD provide auditable controls |
| FATF 2025 | CPT detects source-level anomalies |
| EU AI Act Aug 2026 | CI/CD security gate โ automated red-teaming built-in |
CI/CD Security Gate
- name: CorpusGuard Security Gate
uses: OndiekiFrank/corpusguard-action@v1
with:
target: ${{ secrets.RAG_ENDPOINT }}
fail-on-risk-score: 70
Blocks deployment if RAG system scores above 70/100. EU AI Act compliant.
Test Results
pytest โ attack, defense, API, CLI, and version-consistency tests
CI matrix: Python 3.9 / 3.10 / 3.11 (.github/workflows/ci.yml)
Research
Poisoning the Compliance Mind: Adversarial Memory Injection Attacks on RAG-Based AML Agents Frankline Ondieki Ombachi ยท SSRN 6734225 ยท 2026 Under peer review โ ACM ICAIF 2026, Milan, Italy BSides Toronto 2026 talk submitted โ October 3โ4
@article{ondieki2026poisoning,
title = {Poisoning the Compliance Mind: Adversarial Memory Injection Attacks on RAG-Based AML Agents},
author = {Ondieki Ombachi, Frankline},
year = {2026},
url = {https://papers.ssrn.com/sol3/papers.cfm?abstract_id=6734225},
note = {Under peer review at ACM ICAIF 2026}
}
Disclaimer
For academic reproducibility and defensive security research only. Do not use against systems you do not own or have explicit permission to test.
Author
Frankline Ondieki Ombachi โ AI Security Researcher ยท Nairobi, Kenya
๐ง ondiekifrank021@gmail.com ๐ LinkedIn ๐ SSRN 6734225 ๐ฌ ORCID: 0009-0008-3483-0025
If your organisation runs RAG-based AI systems โ run CorpusGuard before someone else does.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file corpusguard-0.1.0.tar.gz.
File metadata
- Download URL: corpusguard-0.1.0.tar.gz
- Upload date:
- Size: 41.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38a7a4704b488e8a5485191644c9136842ebca85951d9949edd1f13ab4c9e49e
|
|
| MD5 |
9f1f33b01ea509dddfd20dd4d68cf0e2
|
|
| BLAKE2b-256 |
34d03dea5c46bd1df429e18c3fd292e81293f5ca7bcda320e32611f0f8f253c9
|
File details
Details for the file corpusguard-0.1.0-py3-none-any.whl.
File metadata
- Download URL: corpusguard-0.1.0-py3-none-any.whl
- Upload date:
- Size: 41.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a57137366a7af6e6b14f6ec62bbc13ed4342efb844eb0b0d254c237aa4b2b296
|
|
| MD5 |
dd2348a3430693d81502ed16e0aa39d2
|
|
| BLAKE2b-256 |
3714e519e3f432c2b8ed4a18c7faef844cf3be724da1a041f8fb9bf474d1b582
|