Manuscript Offprint

arXiv:2608.27800

ContextLeak: Exfiltrating LLM Agent Context via Malicious Tools

Yuqi Jia, Ruiqi Wang, Patrick Li, Yuepeng Hu, Peinian Li, Neil Gong

Abstract

Exfiltrating an LLM agent’s runtime context—such as the user prompt, execution trajectory, and tool list—poses severe security and privacy risks to users. Such attacks can be carried out via malicious tools and typically require three conditions: (1) the agent selects the malicious tool for task execution, (2) the agent passes its runtime context as input arguments to the tool, and (3) the tool’s implementation transmits these inputs to an attacker-controlled endpoint. Existing work primarily focuses on conditions (1) and (3), leaving condition (2) largely unexplored, despite its critical role in enabling successful context exfiltration.

In this work, we bridge this gap by developing ContextLeak, a malicious tool attack that induces the agent to both select the tool and disclose its context as input arguments. We realize this attack by carefully crafting the tool's name and description using reinforcement learning. Specifically, ContextLeak employs an LLM, referred to as the attack LLM, to automatically generate the malicious tool's name and description. To improve attack effectiveness, we fine-tune the attack LLM via reinforcement learning on a set of shadow users with diverse, simulated agent contexts. Our key technical contribution is the design of novel reward functions tailored to the context exfiltration objective, enabling effective reinforcement-learning-based fine-tuning of the attack LLM. Extensive evaluation demonstrates that our attack remains highly effective even when the shadow users' contexts differ substantially from those of the victim users. Moreover, ContextLeak significantly outperforms existing malicious tool attacks when adapted to this setting.

1 Introduction

When processing a user task, an LLM agent maintains a runtime context, which may include the user prompt, the conversation history between the user and the agent, and the list of tools installed by the user. This context is user-specific and may contain highly sensitive information; consequently, its exfiltration poses significant security and privacy risks. For instance, the user prompt and conversation history may reveal private details such as health conditions, financial status, personal relationships, or confidential work-related information. Furthermore, the list of tools installed by a user may expose their interests, professional activities, or organizational affiliations. An attacker who gains access to such information could leverage it to craft targeted attacks, infer sensitive attributes, or identify high-value tools to exploit.

Tool use substantially augments LLM agents' capabilities. However, it also introduces new security risks, including vulnerability to context exfiltration. Specifically, an attacker can develop a malicious tool and publish it on tool platforms such as MCP.so [34] and Skillsmp [30]. When a victim user inadvertently installs such a tool, it may exfiltrate the user's runtime context if three conditions are satisfied: (1) the agent selects the malicious tool for task execution, (2) the agent passes its runtime context as input arguments to the tool, and (3) the tool's implementation transmits these inputs to an attacker-controlled endpoint.

Existing malicious tool attacks primarily focus on conditions (1) and (3), leaving condition (2) largely unexplored, despite its critical role in enabling successful context exfiltration. For example, some attacks [26, 27, 36, 16, 31] carefully craft a tool's name and description to increase the likelihood that it is selected by the agent, thereby satisfying condition (1). However, they do not

Figure

Figure 1: Illustration of the context exfiltration attack pipeline.

control what data is provided as input arguments to the tool. As a result, these attacks remain insufficient—even if extended toward satisfying conditions (1) and (2)—as demonstrated in our experiments. Other attacks [10] focus on generating malicious tool implementations, enabling the tool to exfiltrate any input arguments it receives to an attacker-controlled endpoint. However, malicious code alone is insufficient for context exfiltration, as the tool cannot access the agent's runtime context unless such information is explicitly passed as input arguments.

Our work: We aim to bridge this gap by developing ContextLeak. ContextLeak is the first approach that strategically crafts a malicious tool's name and description to induce the agent to both select the tool and pass its context as input arguments. Once invoked, the tool's malicious implementation, as developed in prior work [10], can exfiltrate this context to an attacker. Figure 1 illustrates the attack pipeline. Our threat model captures a realistic setting in which the attacker cannot directly access the victim user's LLM agent instance, but can install an instance of the agent and interact with it as a regular user in a black-box manner.

To ensure that the generated name and description of the malicious tool remain semantically meaningful and human-readable, ContextLeak leverages an LLM—referred to as the attack LLM—to produce them. However, since the LLM is not originally trained for this objective, it often generates suboptimal names and descriptions, as demonstrated in our experiments. To address this limitation, we employ reinforcement learning (RL) to fine-tune the attack LLM to generate more effective tool names and descriptions. Specifically, the attacker simulates a set of shadow users, each associated with a shadow context that may include a user prompt, conversation history, and the names and descriptions of both benign tools and the malicious tool. The agent is then used to perform tool selection for each shadow user, deciding whether to invoke a tool, which tool to use, and what data to provide as input arguments. The attack LLM is fine-tuned such that the generated malicious tool name and description induce the agent to both select the tool and pass the shadow context as input arguments for each shadow user.

RL has been widely used to fine-tune LLMs. Such RL-based fine-tuning typically follows the following pipeline: in each iteration, the LLM (i.e., the attack LLM in our setting) generates multiple candidates (i.e., tool names and descriptions in our setting), which are then evaluated using a reward function aligned with the desired objective (i.e., context exfiltration in our setting). The resulting rewards are used to update the LLM. The central challenges of this process are (1) designing an effective reward function, and (2) ensuring the quality of the generated candidates. To address the first challenge, we design a reward function specifically tailored to context exfiltration. To address the second, we introduce a set of strategies dynamically derived from high-level patterns observed in past candidate tool names and descriptions. These strategies guide the attack LLM in generating higher-quality candidates at each fine-tuning iteration.

We apply ContextLeak to exfiltrate three types of context—user prompts, conversation history, and tool lists. We find that ContextLeak is highly effective and substantially outperforms various baselines, including two general prompt injection attacks [12, 36], two jailbreak attacks [2, 14], and two malicious tool attacks [27, 26], which we adapt to our problem setting. In addition, ContextLeak remains effective even when the victim user's context distribution differs substantially from that of the shadow users, or when the victim agent instance uses a different backend LLM from the one used to fine-tune the attack LLM. Moreover, we find that existing defenses—including both prevention-based defenses [3, 4, 5] that fine-tune the backend LLM to improve robustness in tool selection, and detection-based defenses [15, 13, 28] that classify tools as malicious or benign based on their names and descriptions—are insufficient against ContextLeak, highlighting the need for new defense mechanisms.

To summarize, our contributions are as follows:

  • We propose ContextLeak, the first attack that strategically crafts a malicious tool's name and description to induce the agent to both select the tool and pass its context as input arguments, enabling context exfiltration.

  • We design a reward function tailored to context exfiltration and introduce several strategies to guide candidate generation during RL-based fine-tuning of the attack LLM used to produce the malicious tool's name and description.

  • We empirically show the effectiveness of ContextLeak and compare it against a range of baselines. In addition, we show that existing defenses are insufficient against ContextLeak.

References (24)