FilterPrompt — AI Firewall logo

How to Implement an LLM Vulnerability Scanner: A Step-by-Step Implementation Guide

Guide · 2022-01-18 · 14 min read · FilterPrompt Team

A practical LLM security implementation guide. Architecture, threat model, rule design, monitoring, and rollout — everything an engineering team needs to ship an LLM vulnerability scanner to production.

Knowing you need an LLM vulnerability scanner is easy. Implementing one well is the hard part. This LLM security implementation guide is the playbook we wish we'd had when we shipped our first production GenAI feature: how to architect the proxy, how to design rules that actually fire, how to monitor what you cannot see, and how to roll the scanner out to a live customer base without breaking anything.

Threat model first

Before you write a single rule, write the threat model. For most LLM apps the top risks are: prompt injection from user input, indirect injection from retrieved documents, PII leakage on the prompt side, sensitive output disclosure on the response side, system-prompt extraction, data exfiltration via markdown or tool-call URLs, abuse of the model for off-topic generation, and cost runaway from automated abuse.

Architecture: proxy, not library

We strongly recommend the proxy pattern over a library you import into your application. A proxy gives you one chokepoint to inspect every request, one place to rotate provider keys, one place to enforce per-tenant quotas, and one audit log that does not depend on every developer remembering to call your wrapper.

The five layers of an LLM vulnerability scanner

  1. Authentication and tenancy — every request carries a scanner key that resolves to a tenant
  2. Input inspection — pattern, semantic, and ML detectors score the prompt before the model sees it
  3. Provider call — the proxy forwards to OpenAI, Anthropic, Gemini, Azure, or any compatible endpoint
  4. Output inspection — the response is scored for PII, secrets, exfiltration markdown, and policy violations
  5. Audit and metering — verdict, rules fired, latency, tokens, and cost are written to a per-tenant log

Designing rules that fire

The most common LLM vulnerability scanner failure mode is rules that look impressive on paper and never trigger in production. Three principles keep your rule set healthy. First, instrument hit counts on every rule and review monthly. Second, prefer narrow, high-precision rules over broad catch-alls. Third, always pair an input-side rule with an output-side rule for the same risk.

Latency budget

An LLM vulnerability scanner that adds 800ms to every request will be removed in week two. Aim for a verdict accuracy above 99%. Run detection layers in parallel, cache embeddings for repeated system prompts, and short-circuit when a high-precision pattern layer already returns a confident block.

Monitoring what you cannot see

  • Verdict distribution per tenant per day — sudden spikes in 'block' are an attack or a regression
  • Top fired rules — your real-world threat distribution, not your assumed one
  • False-positive rate — sample 50 blocks/week and have a human review them
  • Latency P50, P95, P99 — track per layer so you know what to optimize
  • Token spend per tenant — anomalies often indicate prompt-flooding or model-scraping

Rolling out to production

Phase one: shadow mode. Mirror traffic through the scanner and log verdicts but do not enforce. Phase two: enforce on internal users only. Phase three: enforce on a single low-risk customer cohort. Phase four: enforce globally with a feature flag you can flip back. This phased rollout is non-negotiable if you have paying customers.

Common implementation mistakes

  • Implementing the scanner as an SDK wrapper instead of a network proxy — leaks happen
  • Logging full prompts without redacting PII first — you've created a new compliance liability
  • Storing provider keys in plain text — encrypt at rest, mask in the UI
  • One global rule set across all tenants — your enterprise customer's rules are not your free-tier rules
  • No way to override a block — give your support team a one-click 'allow this prompt' for false positives

Leveraging OWASP LLM Top 10 for Prioritized Rule Design

The OWASP Top 10 for Large Language Model Applications (LLM Top 10) provides a critically important framework for prioritizing the development of scanner rules. Instead of broad, generic security checks, aligning with these well-researched categories ensures your scanner focuses on the most prevalent and impactful threats. This systematic approach allows for resource allocation to high-risk areas first, iteratively building a robust defense based on community consensus and real-world attack vectors.

For instance, categories like 'Prompt Injection' (LLM01) and 'Sensitive Information Disclosure' (LLM03) should be foundational to your rule set. These are direct, frequently exploited vulnerabilities that can lead to data breaches, system compromise, or unauthorized access. Regularly review updates to the OWASP LLM Top 10 to adapt your rule definitions and maintain scanner efficacy against emerging threats. This isn't a static list; it evolves with the LLM threat landscape.

Practical Implementation Patterns: Agent-Based vs. Pattern-Matching

When designing the actual detection logic within your scanner, you'll generally find yourself employing a blend of agent-based methods and traditional pattern-matching approaches. Pattern matching, often implemented with regular expressions, keyword lists, or simple string comparisons, is high-performance and excellent for detecting known, explicit attack signatures or sensitive data formats. It's deterministic and has a low false-positive rate when rules are well-tuned.

Agent-based patterns, on the other hand, involve using smaller, purpose-built LLMs or fine-tuned sequence models to detect more nuanced or semantic attacks. For example, an agent might classify an incoming prompt's intent as 'harmful content generation' or 'system prompt extraction attempt' even if no specific keywords are present. This adds a layer of intelligence, albeit with higher computational cost and a potentially greater false-positive rate, requiring careful calibration. A hybrid approach provides both speed and sophisticated detection.

Integrating with Regulatory Frameworks: EU AI Act and NIST AI RMF

The evolving regulatory landscape, particularly key frameworks like the EU AI Act and NIST AI Risk Management Framework (RMF), significantly impacts how LLM vulnerability scanners need to be designed and operated. The EU AI Act categorizes AI systems by risk level, with 'high-risk' systems facing stringent requirements for data governance, quality, transparency, and human oversight. Your scanner, especially if protecting high-risk LLM applications, must provide auditable logs and verifiable compliance artifacts.

NIST AI RMF offers a more flexible, voluntary framework centered on govern, map, measure, and manage functions. A vulnerability scanner feeds directly into the 'measure' and 'manage' functions by providing concrete metrics on security performance, identifying new risks, and enabling rapid mitigation. Ensure your scanner's logging capabilities can generate reports suitable for demonstrating compliance with these frameworks, focusing on data protection, bias detection, and robust security measures.

Advanced Detection Techniques: Entropy, Canary Tokens, and Adversarial ML

Beyond simple pattern matching, effective LLM vulnerability scanning demands more advanced detection techniques. Entropy analysis, for example, can be used to detect obfuscated prompt injections or data exfiltration attempts. High-entropy sequences in prompts or responses might indicate base64 encoded data, encrypted strings, or highly compressed content designed to bypass simpler filters. Establishing baseline entropy levels for typical interactions helps flag anomalies.

Canary tokens are another powerful tool. These are embedded, unique strings (or even specific factual inaccuracies) placed in system prompts or RAG context. If a canary token appears unexpectedly in the LLM's output, it's a strong indicator of system prompt extraction or data exfiltration. Finally, adversarial machine learning techniques involve training models to identify patterns characteristic of adversarial examples, which can significantly improve detection of novel prompt injections that bypass rule-based systems.

  • Entropy Anomaly Detection: Monitor Shannon entropy of input/output tokens. Spikes can indicate obfuscation or exfiltration attempts, setting thresholds based on statistical norms.
  • Canary Token Injection: Embed unique, trackable identifiers within system prompts or sensitive data retrieval. Monitor for their unauthorized appearance in model outputs.
  • Adversarial ML Model: Train a small classifier on datasets of known attack prompts (e.g., specific evasions, jailbreaks) and benign prompts to detect new variations.
  • Semantic Similarity (with embeddings): Convert prompts to embeddings and compare against a vectorized database of known malicious prompts or sensitive information patterns.
  • Behavioral Anomaly Detection: Analyze sequences of user prompts or tool calls for anomalous patterns, such as rapid, sequential queries attempting to enumerate system capabilities.

Benchmarking and Performance Metrics for Scanner Efficacy

Measuring the true efficacy of your LLM vulnerability scanner goes beyond simply counting hits. You need a rigorous benchmarking process employing standard security metrics like True Positives (TP), False Positives (FP), True Negatives (TN), and False Negatives (FN). From these, calculate Precision (TP / (TP + FP)), Recall (TP / (TP + FN)), and F1-Score (harmonic mean of Precision and Recall) to get a balanced view of your scanner's performance. Conduct regular tests against a known dataset of benign and malicious prompts, continuously updating this dataset as new threats emerge.

Beyond accuracy, evaluate the scanner's operational performance, prioritizing minimal latency impact. Track P50, P95, and P99 latency for each detection layer and for the overall proxy. Monitor resource consumption (CPU, memory, network I/O) to ensure the scanner scales efficiently under load. A scanner that is highly accurate but introduces unacceptable latency or cost will ultimately fail to gain adoption. Establish service level objectives (SLOs) for these metrics before rollout.

Vendor Comparisons and Build vs. Buy Decision Factors

The 'build vs. buy' decision for an LLM vulnerability scanner is complex. Building allows for complete customization, tight integration with existing infrastructure, and control over intellectual property. However, it requires significant investment in specialized engineering talent, continuous threat intelligence updates, and ongoing maintenance. For many organizations, the speed to market and lower operational overhead of a commercial solution are compelling.

When evaluating commercial vendors, consider their coverage of the OWASP LLM Top 10, their flexibility in custom rule creation, their integration options (API, proxy, SDK), performance metrics (latency, throughput), and how they handle threat intelligence updates. Look for transparent reporting, clear pricing models, and robust support. Key vendors in this space include FilterPrompt (for comprehensive proxy-based solutions), Lakera Guard, and Portkey.ai, each with varying strengths in feature sets and target use cases (e.g., enterprise vs. developer-focused).

  • Build: High customization, full control, intellectual property ownership. High upfront cost, continuous maintenance, specialized talent required, slower threat update cycles.
  • Buy: Faster deployment, lower operational overhead, immediate access to expert threat intelligence, vendor support. Less customization, potential vendor lock-in, reliance on vendor security practices.
  • Hybrid: Utilize commercial solutions for core detection, build custom rules/layers for unique business logic or highly specific sensitive data handling. Balances flexibility and speed.

Related