DEV Community

BotGuard
BotGuard

Posted on • Originally published at botguard.dev

Memory Poisoning in AI Agents: A New Long-Term Attack Vector

A single, well-crafted input can permanently corrupt the memory of an AI agent, causing it to produce toxic or misleading outputs for months, without any visible signs of compromise.

The Problem

import numpy as np
from scipy import spatial

class AI_Agent:
    def __init__(self):
        self.memory = []

    def interact(self, user_input):
        # Store user input in memory
        self.memory.append(user_input)

        # Calculate similarity with existing memory
        similarities = []
        for existing_input in self.memory:
            similarity = 1 - spatial.distance.cosine(np.array(user_input), np.array(existing_input))
            similarities.append(similarity)

        # Return most similar input
        most_similar_index = np.argmax(similarities)
        return self.memory[most_similar_index]

agent = AI_Agent()
print(agent.interact([0.1, 0.2, 0.3]))  # [0.1, 0.2, 0.3]
print(agent.interact([0.4, 0.5, 0.6]))  # [0.4, 0.5, 0.6]
print(agent.interact([0.1, 0.2, 0.3]))  # [0.1, 0.2, 0.3]
Enter fullscreen mode Exit fullscreen mode

In this example, an attacker can plant a "poisoned" memory by providing a specially crafted input that, when stored, alters the agent's behavior. The attacker exploits the fact that the agent's memory is used to determine the most similar input. By injecting a poisoned input, the attacker can influence the agent's output, even if the original input is harmless.

Why It Happens

The issue arises from the fact that AI agents often rely on persistent memory to store user interactions, conversation history, and other relevant data. This memory can be used to improve the agent's performance over time, but it also creates an attack surface. An attacker can exploit this by injecting malicious data into the agent's memory, which can then be used to alter the agent's behavior. The use of vector databases and other data structures to store this memory can further exacerbate the issue, as they can provide an attacker with a means to inject poisoned data. An effective AI security platform should be able to detect and prevent such attacks.

The lack of proper input validation and sanitization can also contribute to the vulnerability. If an agent does not properly validate user input, an attacker can inject malicious data that can then be stored in the agent's memory. This can be particularly problematic in AI agent security, as the agent's memory can be used to influence its behavior over an extended period. Implementing a robust LLM firewall can help mitigate this risk.

Furthermore, the complexity of AI systems can make it difficult to detect and respond to such attacks. The use of multiple components, such as MCP and RAG pipelines, can create a complex attack surface that can be challenging to secure. Effective MCP security and RAG security measures are essential to preventing attacks on these components.

The Fix

import numpy as np
from scipy import spatial

class AI_Agent:
    def __init__(self):
        self.memory = []
        # Implement input validation and sanitization
        self.validation_threshold = 0.5

    def interact(self, user_input):
        # Validate user input
        if not self.validate_input(user_input):
            return "Invalid input"

        # Store user input in memory
        self.memory.append(user_input)

        # Calculate similarity with existing memory
        similarities = []
        for existing_input in self.memory:
            similarity = 1 - spatial.distance.cosine(np.array(user_input), np.array(existing_input))
            similarities.append(similarity)

        # Return most similar input
        most_similar_index = np.argmax(similarities)
        return self.memory[most_similar_index]

    def validate_input(self, user_input):
        # Check if input is within valid range
        if np.any(np.array(user_input) < 0) or np.any(np.array(user_input) > 1):
            return False
        # Check if input is not too similar to existing memory
        for existing_input in self.memory:
            similarity = 1 - spatial.distance.cosine(np.array(user_input), np.array(existing_input))
            if similarity > self.validation_threshold:
                return False
        return True

agent = AI_Agent()
print(agent.interact([0.1, 0.2, 0.3]))  # [0.1, 0.2, 0.3]
print(agent.interact([0.4, 0.5, 0.6]))  # [0.4, 0.5, 0.6]
print(agent.interact([0.1, 0.2, 0.3]))  # [0.1, 0.2, 0.3]
Enter fullscreen mode Exit fullscreen mode

By implementing proper input validation and sanitization, an agent can prevent an attacker from injecting poisoned data into its memory. An AI security tool can also be used to monitor the agent's memory and detect any suspicious activity.

FAQ

Q: What is the most effective way to prevent memory poisoning attacks?
A: The most effective way to prevent memory poisoning attacks is to implement proper input validation and sanitization, as well as to use a robust AI security platform to monitor and detect suspicious activity. Regularly updating and patching the agent's software can also help to prevent such attacks.
Q: Can an attacker use memory poisoning to compromise an entire AI system?
A: Yes, an attacker can use memory poisoning to compromise an entire AI system, particularly if the system relies heavily on persistent memory. This can be especially problematic in systems that use multiple components, such as MCP and RAG pipelines. Effective MCP security and RAG security measures are essential to preventing such attacks.
Q: How can I detect if my AI agent has been compromised by a memory poisoning attack?
A: Detecting a memory poisoning attack can be challenging, but there are several signs that may indicate an attack, such as unusual or toxic output, or changes in the agent's behavior over time. An AI security tool can also be used to monitor the agent's memory and detect any suspicious activity.

Conclusion

Memory poisoning attacks can have serious consequences for AI systems, and it is essential to take steps to prevent and detect such attacks. By implementing proper input validation and sanitization, and using a robust AI security platform, developers can help to protect their AI systems from these types of attacks. One shield for your entire AI stack — chatbots, agents, MCP, and RAG. BotGuard drops in under 15ms with no code changes required.

Top comments (0)