Recommended resources
The freely available resources this lesson will direct you to.
- Generative AI's Greatest Flaw - Computerphilewww.youtube.com
- How to Build a Scalable RAG System for AI Apps (Full Architecture)www.youtube.com
- The Hidden Attack Surfaces of Retrieval-Augmented Generationdeconvoluteai.com
- RAG security: the forgotten attack surface – Christian Schneiderchristian-schneider.net
Hello! Welcome to the final lesson in our first module on LLM security foundations.
In our previous lesson, we explored how an attacker can exploit In-Context Learning (ICL) to perform direct prompt injection. By embedding malicious instructions or fabricated examples directly within the user prompt, an attacker can hijack the model's behavior for a single interaction. This is what the Deconvolute Labs article we'll see today calls a "Front Door" attack—it's synchronous, transient, and requires direct interaction.
Today, we shift our focus to a more persistent and scalable threat that arises when we connect LLMs to external knowledge. You will learn to diagram a Retrieval-Augmented Generation (RAG) pipeline and identify the new attack surfaces it introduces, particularly for indirect injection.
This is a critical foundation for your demo, as the vulnerable chatbot you will build is a RAG application. Understanding its architecture and weaknesses is the first step toward both exploiting and defending it.
1. What is Retrieval-Augmented Generation (RAG)?
LLMs have a "knowledge cutoff"—they don't know anything about events that occurred after their training data was collected. They also lack access to your private, proprietary data. RAG is an architectural pattern designed to solve both problems by grounding the model in up-to-date, relevant information at query time.
Instead of just sending a user's question to the LLM, a RAG system first retrieves relevant documents from a knowledge base (like a collection of PDFs, internal wiki pages, or support tickets) and then augments the user's prompt with this information before sending it to the LLM for generation.
This two-phase process can be visualized as follows:

Let's break down the two main phases shown in the diagram:
- Ingestion (Offline): This is a preparatory step. Your source documents are loaded, broken down into smaller
chunks, and converted into numerical representations calledembeddingsusing an embedding model. These embeddings are then stored in a specializedVector DB(Vector Database). - Retrieval & Generation (Online): When a user submits a query, it's also converted into an embedding. The system then performs a
semantic searchin the vector database to find the document chunks with embeddings that are most similar to the query's embedding. These retrieved chunks are combined with the original query into a new, augmented prompt, which is finally sent to the LLM.

To see this in action and understand the common failure points (even in non-adversarial settings), let's watch a short video.
How to Build a Scalable RAG System for AI Apps (Full Architecture)
This video from ByteMonk provides a concise overview of the basic RAG architecture and why simply retrieving context isn't enough for a production system. It highlights how poor-quality retrieved data can lead to confident but wrong answers.
Please watch the first four minutes of the video (00:00 - 03:57). Focus on understanding the three core steps—Retrieve, Augment, Generate—and the examples of how this simple flow can break down due to outdated, incomplete, or jumbled context.
The key insight here is that the quality of the LLM's output is now heavily dependent on the quality of the retrieved context. As the video notes, "bad retrieval is worse than no retrieval."
2. The Unchecked Trust: Introducing Indirect Prompt Injection
In a RAG architecture, a new trust boundary emerges. While we might scrutinize the user's input, the system implicitly trusts the content it retrieves from its own knowledge base. After all, it's "internal" data. This flawed assumption creates the "Back Door" for a new class of attacks.
Indirect Prompt Injection is an attack where malicious instructions are embedded within the documents in the knowledge base. When an unsuspecting user asks a relevant question, the system retrieves the poisoned document, and the malicious instructions are passed to the LLM as trusted context.
To understand this powerful attack vector, please watch the following video.
Generative AI's Greatest Flaw - Computerphile
The Computerphile channel offers an excellent, accessible explanation of indirect prompt injection. The video uses practical, business-relevant examples that directly align with the scenarios you might build for your demo.
Please watch from the beginning to 05:52. Pay close attention to: The diagram of the RAG process and how data sources are added to the user's prompt. The email summarization example, where a hidden instruction is embedded using small, white text. The CV-screening example, which shows how this could be used to manipulate an automated hiring process. \nThe video makes a crucial comparison to SQL injection, but highlights why this is harder to defend against in LLMs: there's no clear separation between data and instructions in the token stream.
The video clearly illustrates the core threat: the LLM can't distinguish between the legitimate context it's supposed to summarize and the attacker's instructions hidden within that context. From the model's perspective, it's all just text.
3. Formalizing the Attack: Front Door vs. Back Door
To build a robust mental model for your demo, it's useful to formalize these attack vectors. The distinction between "Front Door" (direct) and "Back Door" (indirect) attacks is a powerful way to categorize these threats.
AI Security - The Hidden Attack Surfaces of RAG and MCP
This article from Deconvolute Labs, 'The Hidden Attack Surfaces of RAG and MCP', provides an excellent security analysis of RAG pipelines. It formally defines the two primary entry points for malicious content.
Please read the following sections: 'The RAG Attack Surfaces': Focus on Figure 1 and the explanation of how both user query and retrieved context are fed into the model's prompt. 'Back Door (Persistent and Asynchronous Attacks)': Understand why this threat model is different (asynchronous, persistent, scalable). 'Indirect Prompt Injection (The Payload)': This provides a concise definition of the attack itself. \nThis will give you the precise terminology to describe the vulnerabilities in your demo.
As the article explains, Back Door attacks are particularly dangerous due to their characteristics:
- Asynchronous: The attacker plants the payload now, and it gets triggered later by a legitimate user.
- Persistent: The payload remains in the knowledge base, ready to be activated multiple times.
- Scalable: A single poisoned document can affect every user whose query retrieves it.
This stands in stark contrast to the direct injections we studied previously, which are synchronous, transient, and affect only the current user's session.
4. The Delivery Mechanism: Corpus Poisoning
It's not enough to simply place a malicious file in a vast knowledge base. The attacker must ensure their payload is retrieved for specific, high-value queries. This is achieved through an attack on the retrieval process itself, known as Corpus Poisoning.
Since RAG relies on semantic similarity, attackers can't just use keywords. Instead, they must craft their malicious document so its embedding is mathematically close to the embedding of their target query.
AI Security - The Hidden Attack Surfaces of RAG and MCP
The same Deconvolute Labs article provides a superb explanation of how attackers can manipulate the retrieval system.
Please read the section titled 'Corpus Poisoning (Controlling Retrieval)', including its sub-section 'Why Dense Retrieval Enables This'. Pay special attention to Figure 3 and the concept of a 'Vector Magnet'. This explains how an attacker optimizes a document to be the 'perfect' match for a target query, ensuring it gets retrieved.
The efficiency of this attack is alarming. You don't need to compromise the entire knowledge base.
RAG security: the forgotten attack surface
This blog post by Christian Schneider, a security architect, reinforces the severity of corpus poisoning with findings from recent research.
Please read the introduction and the section titled 'The attack math: five documents in millions'. This short section provides a stark, quantitative look at how effective a targeted corpus poisoning attack can be.
From your background in infrastructure security, you might see a parallel here to a DNS cache poisoning attack. An attacker crafts a malicious record (the poisoned document and its embedding) that causes a common name resolution request (a user's query) to resolve to a malicious IP address (the attacker's payload), redirecting the victim without their knowledge.
Conclusion
In this lesson, we dissected the RAG architecture and uncovered its primary security flaw: the implicit trust placed in retrieved data. This "Back Door" enables persistent, scalable attacks that are much harder to detect and mitigate than the "Front Door" attacks we studied previously.
Key Takeaways:
- RAG Architecture: A two-phase process (Ingestion and Retrieval/Generation) that grounds LLMs in external data, solving the knowledge cutoff problem but introducing a new trust boundary.
- The Attack Surface: The knowledge base itself becomes a primary attack vector. Any data source that feeds your RAG system (document uploads, web scrapers, API integrations) is a potential entry point for malicious content.
- Indirect Prompt Injection: The core attack, where malicious instructions hidden in a retrieved document are executed by the LLM as if they were trusted context.
- Corpus Poisoning: The delivery mechanism. Attackers optimize a document's content to ensure its embedding is semantically close to a target query, guaranteeing its retrieval. This attack is highly efficient, requiring only a handful of poisoned documents.
We have now concluded our foundational module. You understand how LLMs process information, how direct prompt injection exploits in-context learning, and how RAG architectures create an even more potent vector for indirect injection.
In our next module, we will move from theory to practice. The first lesson, "Set up a local development environment with Ollama and a small language model (e.g., Llama 3 8B, Phi-3)," will guide you through installing the tools you need to build and test the vulnerable application for your demo.