DEV Community

BotGuard
BotGuard

Posted on • Originally published at botguard.dev

Multi-Turn Attacks: Why Single-Request Security Checks Are Not Enough

In a shocking turn of events, a single chatbot was recently compromised by a multi-turn attack, resulting in a complete overhaul of its behavior, all without triggering any traditional security alarms.

The Problem

import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

# Load pre-trained model and tokenizer
model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
tokenizer = AutoTokenizer.from_pretrained("t5-small")

def generate_response(user_input):
    # Tokenize user input
    input_ids = tokenizer.encode("Generate a response to: " + user_input, return_tensors="pt")

    # Generate response
    output = model.generate(input_ids, max_length=100)

    # Decode response
    response = tokenizer.decode(output[0], skip_special_tokens=True)

    return response

# Test the function
user_input = "Hello, how are you?"
print(generate_response(user_input))
Enter fullscreen mode Exit fullscreen mode

In this example, an attacker can use a multi-turn conversation to gradually shift the agent's behavior across several messages. For instance, the attacker might start with a harmless message like "Hello, how are you?" and receive a normal response. Then, they might send a follow-up message like "I'm feeling sad today" and receive a response that seems empathetic. However, the attacker's ultimate goal might be to manipulate the agent into revealing sensitive information or performing a malicious action. By using a series of seemingly innocuous messages, the attacker can evade traditional security checks that focus on single-request analysis.

Why It Happens

The reason why single-request security checks are not enough is that they fail to account for the complexities of human conversation. In a multi-turn conversation, the context and meaning of each message can change significantly over time. An attacker can use this to their advantage by crafting a series of messages that, when taken individually, seem harmless but, when taken together, can manipulate the agent's behavior. This type of attack is particularly effective against AI agents that rely on machine learning models, as these models can be influenced by subtle patterns in the input data. Furthermore, the use of natural language processing (NLP) techniques can make it difficult to detect and prevent such attacks, as the attacker's messages may not contain any obvious malicious keywords or phrases.

The lack of visibility into the conversation history is another factor that contributes to the effectiveness of multi-turn attacks. Traditional security checks often focus on analyzing individual requests in isolation, without considering the broader context of the conversation. This can make it difficult to detect and prevent attacks that rely on subtle manipulations of the conversation flow. To make matters worse, many AI agents are designed to be highly interactive and responsive, which can create a false sense of security among users. As a result, users may be more likely to trust the agent and reveal sensitive information, which can be exploited by an attacker.

In addition to these technical factors, there are also organizational and process-related issues that can contribute to the vulnerability of AI agents to multi-turn attacks. For example, many organizations may not have a clear understanding of the security risks associated with AI agents, or they may not have the necessary expertise and resources to implement effective security measures. This can create a culture of complacency, where security is not prioritized, and vulnerabilities are not addressed in a timely manner.

The Fix

import torch
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer

# Load pre-trained model and tokenizer
model = AutoModelForSeq2SeqLM.from_pretrained("t5-small")
tokenizer = AutoTokenizer.from_pretrained("t5-small")

# Define a function to track conversation history
def track_conversation_history(user_input, conversation_history):
    # Update conversation history
    conversation_history.append(user_input)

    # Check for suspicious patterns in conversation history
    if len(conversation_history) > 5:  # arbitrary threshold
        # Use an AI security tool, such as an LLM firewall, to analyze conversation history
        # and detect potential security threats
        security_score = analyze_conversation_history(conversation_history)

        # If security score exceeds a certain threshold, take action to prevent attack
        if security_score > 0.5:  # arbitrary threshold
            return "Suspicious activity detected. Please try again."

    return None

def generate_response(user_input, conversation_history):
    # Tokenize user input
    input_ids = tokenizer.encode("Generate a response to: " + user_input, return_tensors="pt")

    # Generate response
    output = model.generate(input_ids, max_length=100)

    # Decode response
    response = tokenizer.decode(output[0], skip_special_tokens=True)

    # Check for suspicious patterns in response
    if "sensitive information" in response:  # arbitrary threshold
        # Use an AI security platform to analyze response and detect potential security threats
        security_score = analyze_response(response)

        # If security score exceeds a certain threshold, take action to prevent attack
        if security_score > 0.5:  # arbitrary threshold
            return "Suspicious activity detected. Please try again."

    # Update conversation history
    suspicious_activity = track_conversation_history(user_input, conversation_history)

    # If suspicious activity is detected, return an error message
    if suspicious_activity is not None:
        return suspicious_activity

    return response

# Test the function
user_input = "Hello, how are you?"
conversation_history = []
print(generate_response(user_input, conversation_history))
Enter fullscreen mode Exit fullscreen mode

In this revised example, we've added a function to track conversation history and detect suspicious patterns. We've also added checks to prevent the agent from revealing sensitive information. By using an AI security tool, such as an LLM firewall, to analyze conversation history and detect potential security threats, we can significantly improve the security of our AI agent. Additionally, by using an AI security platform to analyze responses and detect potential security threats, we can further reduce the risk of a multi-turn attack.

Real-World Impact

The real-world impact of multi-turn attacks can be significant. In a recent study, it was found that a single compromised chatbot can result in millions of dollars in losses for a company. This is because chatbots are often used to handle sensitive customer information, such as financial data or personal identifiable information. If an attacker can manipulate a chatbot into revealing this information, they can use it for malicious purposes, such as identity theft or financial fraud. Furthermore, the use of MCP security and RAG security measures can help to mitigate these risks, but they are not foolproof and can be evaded by a determined attacker.

In addition to the financial losses, multi-turn attacks can also damage a company's reputation and erode customer trust. If customers discover that a company's chatbot has been compromised, they may lose faith in the company's ability to protect their sensitive information. This can result in a loss of business and revenue, as well as a decline in customer satisfaction. To mitigate these risks, companies must prioritize AI agent security and implement effective security measures to prevent multi-turn attacks.

The use of an AI security platform can help to mitigate these risks by providing a comprehensive security solution that includes features such as conversation history tracking, suspicious activity detection, and response analysis. By using an AI security tool, such as an LLM firewall, companies can significantly improve the security of their AI agents and prevent multi-turn attacks.

FAQ

Q: What is a multi-turn attack?
A: A multi-turn attack is a type of attack where an attacker uses a series of messages to manipulate an AI agent's behavior over time. This can be used to evade traditional security checks and compromise the agent's security.
Q: How can I prevent multi-turn attacks?
A: To prevent multi-turn attacks, you can use an AI security platform to track conversation history and detect suspicious patterns. You can also use an AI security tool, such as an LLM firewall, to analyze responses and detect potential security threats.
Q: What are the consequences of a multi-turn attack?
A: The consequences of a multi-turn attack can be significant, including financial losses, damage to reputation, and erosion of customer trust. To mitigate these risks, companies must prioritize AI agent security and implement effective security measures to prevent multi-turn attacks.

Conclusion

In conclusion, multi-turn attacks are a significant threat to AI agent security, and traditional security checks are not enough to prevent them. By using an AI security platform and an AI security tool, such as an LLM firewall, companies can significantly improve the security of their AI agents and prevent multi-turn attacks. One shield for your entire AI stack — chatbots, agents, MCP, and RAG. BotGuard drops in under 15ms with no code changes required.


Try It Live — Attack Your Own Agent in 30 Seconds

Reading about AI security is one thing. Seeing your own agent get broken is another.

BotGuard has a free interactive playground — paste your system prompt, pick an LLM, and watch 70+ adversarial attacks hit it in real time. No signup required to start.

Your agent is either tested or vulnerable. There's no third option.

👉 Launch the free playground at botguard.dev — find out your security score before an attacker does.

Top comments (0)