ToolHazard: Scaling Adversarial Environments for Security Evaluation and Alignment of LLM-based Agents

Yutao Mou, Pengfei Yang, Zhe Yin, Zhangchi Xue, Xiaotian Luan, Dingyao Yu, Tong Zhang, Shikun Zhang, Wei YearXiv 2026

TL;DR

ToolHazard is an automated framework that dynamically synthesizes executable, stateful environments and complex tasks to evaluate and align LLM agents against indirect prompt injection attacks. By automating environment creation and attack discovery, it scales security testing across diverse domains, revealing that even state-of-the-art models remain highly vulnerable to environment-side manipulation.

What Problem Does This Paper Address?

LLM-based agents are increasingly trusted to execute complex workflows by interacting with external tools, databases, and APIs. However, this autonomy introduces a massive security vector: indirect prompt injection (IPI). In an IPI attack, malicious instructions are embedded within the environment state (like an email body or a database record). When the agent reads this data during a benign task, it gets hijacked into executing unauthorized or unsafe actions.

Prior research into agent security faces a major scalability bottleneck. Existing benchmarks rely on manually coded environments, which are expensive to build and expand. Alternatively, some use stochastic LLM simulations that lack reproducible, deterministic feedback. Furthermore, automated red-teaming tools usually optimize payloads for predefined injection sites rather than discovering new, viable propagation paths in unseen environments.

Methods

The authors introduce ToolHazard, a scalable framework that automates the entire pipeline of adversarial environment simulation, task generation, attack execution, and programmatic verification.

Figure 1: Overview of the ToolHazard framework. We scale environments via LLM-based synthesis, develop an attacker agent to proactively plan and execute prompt injections, and design a user simulator to generate benign user tasks. The synthesized data further supports evaluation and alignment of LLM agents.

As shown in Figure 1, the framework consists of three core modules:

  1. Environment Simulator: Takes raw task descriptions from seed datasets and automatically infers environment blueprints (entities, rules, and operations). It then generates executable Object-Oriented Python code implementing these environments as stateful sandboxes. A dual-agent pipeline (Testing Agent + Checking Agent) performs quality control to ensure code correctness.
  2. User Simulator: Automatically initializes realistic environment states and generates complex, state-grounded, long-horizon user tasks.
  3. Attacker Agent: Proactively maps out read/write dependencies within the synthesized tools to discover valid injection points. It then uses a plan-and-execute workflow to inject environment-specific payloads using six distinct attack strategies (e.g., decision hijacking, tool selection manipulation).

Crucially, ToolHazard avoids using an expensive or unreliable LLM-as-a-judge for evaluation. Instead, it programmatically decomposes tasks into verifiable terminal-state conditions, ensuring deterministic and reproducible verification.

Datasets

The framework was used to construct two distinct datasets:

  • ToolHazard-Bench: A challenging evaluation benchmark consisting of 28 stateful environments, 512 tools, and 87 long-horizon tasks (averaging 15.56 steps per task), resulting in 502 unique adversarial test cases.
  • ToolHazard-Align: A large-scale training dataset built from 60 entirely disjoint environments, containing over 1,000 filtered samples used for Supervised Fine-Tuning (SFT) and Reinforcement Learning (RL) via the GRPO algorithm.

Key Insights and Ideas

  • Timing and Placement Bias: IPI attacks are significantly more effective if the agent encounters the malicious instruction early in its execution trajectory (timing) and if the payload is positioned near the tail-end of a tool's text response (placement). This reveals a clear positional bias in how LLM agents process context over multi-turn interactions.
  • The Power of Structure: Free-form text outputs from tools yield substantially higher attack success rates compared to structured formats like JSON or YAML. Structured outputs provide implicit semantic boundaries that help the agent separate data from actionable instructions.
  • Collateral Capability Damage: Exposure to adversarial environments doesn't just cause safety failures; it systematically degrades the agent's ability to complete its original, benign task, even if the attack itself doesn't fully succeed.

Key Results

The authors evaluated leading closed- and open-source models, including GPT-5, GPT-4.1, Gemini-3.1-Pro, and DeepSeek-V3.2.

  • High Vulnerability Across SOTA Models: Nearly all models exhibited severe vulnerabilities. For instance, four out of six attack strategies achieved over a 40% Attack Success Rate (ASR) on GPT-5. DeepSeek-V3.2 achieved the highest benign task completion rate but was also found to be among the most vulnerable to hijacking.
  • Successful Alignment Generalization: Using the ToolHazard-Align dataset to train open-source models (Qwen3-4B/8B) led to massive security improvements. For Qwen3-8B, the framework reduced the ASR from 36.10% to 18.06% on ToolHazard-Bench. Remarkably, this robustness generalized to the completely independent, human-curated AgentDojo benchmark, proving that automated environment scaling transfers well to real-world scenarios.

Limitations and Future Work

While ToolHazard represents a major step forward for automated security benchmarking, the authors note a couple of key limitations:

  • Realism Gap: Synthesized Python sandboxes, while complex, may still fail to capture the subtle long-tail failure modes, proprietary implementations, and deployment-specific quirks found in real-world production enterprise systems.
  • Fixed Attack Strategies: The framework currently relies on six predefined prompt-injection wrappers. It does not dynamically discover entirely new, novel attack strategies or payload wrappers on its own—a direction the authors leave for future "DeepResearch-style" automated red-teaming exploration.

Summary updated

BibTeX
@misc{mou2026toolhazard,
  author = {Yutao Mou and Pengfei Yang and Zhe Yin and Zhangchi Xue and Xiaotian Luan and Dingyao Yu and Tong Zhang and Shikun Zhang and Wei Ye},
  title = {{ToolHazard: Scaling Adversarial Environments for Security Evaluation and Alignment of LLM-based Agents}},
  year = {2026},
  eprint = {2608.11878},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2608.11878},
}
Figure