---
title: 'Maatphor: Automated Prompt Injection Framework'
url: https://www.emergentmind.com/topics/maatphor-framework
type: topic
---

# Maatphor: Automated Prompt Injection Framework

Maatphor is an automated framework for the generation and analysis of prompt injection variants in large language model (LLM) systems, addressing the challenge of adversarial prompt evasion of guardrails. Unlike reactive or manual approaches, Maatphor introduces a closed-loop variant search mechanism driven by LLMs themselves, enabling both the automated creation of diverse adversarial prompts and black-box evaluation of their effectiveness against arbitrary LLM+guardrail stacks. The system supports extensible evaluation modes and provides a methodology for iteratively discovering, scoring, and cataloging injection variants, facilitating robust security assessment and dataset generation for prompt injection research [2312.11513].

## 1. System Architecture

Maatphor’s architecture is organized as a five-component closed loop:

1. **Seed & Goal Extractor**: Ingests a known prompt injection $P_0$—which may be ineffective—and the original LLM task $T$, extracting the adversary's intent or “real goal” $G$. Extraction can be performed manually or automatically.

2. **Template Generator**: Produces $N$ test templates from an HTML (or other) base, each embedding a single prompt variant $v$ at one “holder” slot, with others cleared. This supports localized injection and positional evaluation, as detailed in Algorithm 2.

3. **Prompt Variation (Variant Generator)**: Utilizes an LLM (e.g., GPT-3.5-Turbo) with the “Seven Principles of Maat” system prompt. Inputs comprise the seed $P_0$, goal $G$, and the top-$K$ historical variants with scores. This module is responsible for context-switching, rephrasing, creative paraphrase, and command-style synthesis.

4. **Victim Model Executor**: Feeds each generated template containing variant $v_t$ to the target LLM (potentially via plugin-enabled wrappers such as LangChain + ReadFile/WriteFile), recording the output $o_t$.

5. **Prompt Evaluation (Scorer)**: Employs two black-box evaluation strategies—string-matching and embedding-based similarity—to score each injected variant. After processing, top variants and their scores are looped back to the generator, steering subsequent search.

The following table summarizes the core components:

| Component              | Input(s)                             | Output(s)         |
|------------------------|--------------------------------------|-------------------|
| Seed & Goal Extractor  | $P_0$, $T$                           | $G$               |
| Template Generator     | Template, $N$, $v$                   | $N$ variant embeds|
| Prompt Variation       | $P_0$, $G$, top-$K$ history          | Candidate $v_t$   |
| Victim Model Executor  | Injected template, LLM               | Output $o_t$      |
| Prompt Evaluation      | $o_t$, (reference sets/$S^+$/ $S^-$) | Score $S(v)$      |

## 2. Variant Generation Methodology

Maatphor’s core search process is an LLM-driven evolutionary hill-climber, though it avoids explicit genetic algorithm machinery. Each iteration leverages feedback to steer the LLM’s sampling distribution:

- **Mutation/Recombination**: Implicitly encoded in the prompt context, encouraging context-switching, instruction permutation, paraphrasing, stylistic drift, error simulation, and command-style injection.
- **Sampling**: Each variant $v_t$ is produced by the LLM under temperature 0.5, treating probability as $P(v|P_0, G, H)$, with $H$ the current history.
- **Feedback**: After evaluation, history $H$ is updated by aggregating (and sorting) top-scoring variants, retaining the top $K$ for subsequent prompt context.
- **Early Stopping**: The process can terminate upon reaching a performance threshold or maximum iteration bound.

The high-level process is:

```
for t in 1..T:
    sys_prompt ← BuildSystemPrompt(P0, G, H)
    vt ← LLM.generate(sys_prompt)
    st ← Evaluate(vt)
    H ← (H ∪ {(vt, st)}) sorted descending by s; keep top K
    if st ≥ threshold: break
```

## 3. Variant Effectiveness Scoring

Effectiveness assessment in Maatphor is modular, supporting both symbolic and neural metrics:

1. **Exact String Matching**: Define $S^+$ (must-include phrases) and $S^-$ (must-exclude phrases). For each output $o_i$ produced by $v$:
   $$
   \mathrm{effective}_i(v) = 1 \textrm{ if } \exists w \in S^+: w \in o_i \textrm{ AND } \forall b \in S^-: b \notin o_i; \textrm{ else } 0
   $$
   The variant’s score is $S(v) = (1/M)\sum_{i=1}^M \mathrm{effective}_i(v) \in [0,1]$ over $M$ trials.

2. **Embedding-Based Similarity (kNN)**: With reference outputs $O_S$ (successful class) and $O_U$ (unsuccessful), embedded via a model (e.g., MPNet), each output $o$ is embedded as $e = \mathrm{embed}(o)$. Compute $k$-nearest neighbors in $E = \mathrm{embed}(O_S \cup O_U)$:
   $$
   \mathrm{score}(o) = \frac{|\textrm{neighbors from } O_S|}{k}
   $$
   Variant score is again averaged over $M$ runs. Effectiveness threshold is typically set at $S(v) > 0.5$.

Both scorers facilitate continuous or threshold-based measures for injection success.

## 4. Integration with LLM Guardrails

Maatphor employs a fully black-box interface to evaluated systems, supporting flexible testing of diverse defense mechanisms:

- **System Prompt Guardrails**: Defensive prompts included during Victim Model Executor invocation.
- **Input Classifiers/Sanitizers**: If an input classifier filters $v$, that variant cannot succeed.
- **Output Classifiers**: Applied in parallel or as replacement scorers on $o_t$.
- The framework reports all successful evasions, enabling automated analysis of which variants bypass specific protections, and which layers are robust.

## 5. Iterative Dynamics and Convergence

Key operational parameters and process characteristics include:

- **Variants per Round**: 1 by default, parallelizable by drawing multiple LLM samples per iteration.
- **Trials per Variant**: $M = 5$ runs to control stochasticity.
- **Max Iterations**: $T = 50$ (up to 100 explored for coverage).
- **Success Criteria**: Max total iterations or attaining a required mean score (e.g., $60\%$).
- **Effectiveness Over Time**: Performance tracked via $E_t = \max_{i \leq t} S(v_i)$. Empirically, $E_t > 60\%$ within 40 iterations on all benchmark tasks.

Performance characteristics indicate rapid convergence to high-efficacy variants even from ineffective seeds.

## 6. Experimental Evaluation

Maatphor was evaluated on three LLM red-teaming scenarios using GPT-3.5-Turbo with LangChain + AutoGPT and ReadFile/WriteFile plugins on two NVIDIA K80 GPUs. Hyperparameters comprised generation temperature 0.5, agent step bound 15, $M=5$, $k=5$, and $\sim$20 reference examples per class.

### Task Overview and Outcomes

| Task                                         | Seed Success | Top Variant Success | Convergence (Iterations) |
|----------------------------------------------|--------------|--------------------|-------------------------|
| Q&A Misinformation (“Did Einstein win...”)   | 0%           | 60–72% (string/sim)| 30–40                   |
| Fraudulent Link (“Embed http...”)            | 0%           | 100% (string match)| 5–10                    |
| Style: Pirate Accent Summarization           | 0%           | 64–68% (similarity)| 40–50                   |

- **Ablation Studies**: Disabling feedback significantly reduces the frequency of high-scoring variants. Parallel runs (e.g., 3×100) improve solution diversity but 50 iterations suffices for most cases. Maatphor produces both novel injection types and incremental improvements on strong variants.

## 7. Operational Recommendations and Applications

Practical insights and guidelines emerging from empirical study include:

- The feedback loop is essential; top-$K$ historical variants focus LLM generation toward effective mutations.
- Maintaining a “hall of fame” (top-$K$ archive) prevents loss of high-performing strategies and promotes search diversity.
- Manual review of top candidates is recommended to detect boundary cases unrecognized by either scorer.
- Maatphor is suitable for integration in continuous red-teaming and CI pipelines, running on each guardrail or model update.
- Variant generation approaches are modular; operators can incorporate stylistic perturbations, suffix injections, or random string manipulations.
- Defense research can invert the framework to optimize system prompts or locked-down plugin environments.
- Plugin-heavy systems should complement linguistic guardrails with strict plugin access controls.

In summary, Maatphor automates the process of (1) generating candidate prompt injections, (2) evaluating them against protected LLM systems, and (3) iteratively steering generation based on empirical evasion success, equipping defenders and researchers to proactively discover and evaluate variants that bypass contemporary prompt injection mitigations [2312.11513].

Source: https://www.emergentmind.com/topics/maatphor-framework