Manuscript Offprint

arXiv:2611.12241

SNAPRAG: Cost-Efficient Knowledge Extraction Attack on RAG Systems via Adversarial Perturbations

Melih Coşğun, Mehmet Emre Gürsoy, Alptekin Küpçü, Sinem Sav

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Abstract

Retrieval-Augmented Generation (RAG) systems improve Large Language Model (LLM) responses by incorporating information from external knowledge bases. However, recent studies have shown that attackers can extract substantial portions of private knowledge bases from RAG systems through repeated querying. Yet, state-of-the-art extraction attacks either rely heavily on external LLMs to generate adversarial queries or require a large number of queries. In this paper, we propose SNAPRAG, a novel and cost-efficient attack for extracting RAG knowledge bases through adversarial perturbations. Our approach algorithmically generates adversarial queries without relying on external LLMs and allows for a strictly configurable query budget. In particular, SNAPRAG uses a discrete token-mutation algorithm to generate novel queries in a strict black-box setting. Our adaptive dissimilarity threshold calibration systematically forces queries into unexplored semantic regions of the target database, enabling extraction with fewer queries. Specifically, to extract half of the target knowledge base, our method requires up to 6.77×6.77\times fewer queries than existing approaches. Furthermore, within a fixed query budget, our method recovers up to 96.9%, outperforming the closest approach by 7.7–19.9%. We further evaluate SNAPRAG against active defenses, where it remains the leading attack on four of five defended targets, achieving 50.7–96.3% extraction rate.

1 Introduction

Large Language Models (LLMs) have become key building blocks in numerous applications, from chatbots to decision-support tools [2, 25]. However, as their use has grown, so have concerns about their security. Prompt injection attacks, for example, demonstrate that strategically crafted inputs can override an LLM's original instructions, causing it to follow commands of an attacker instead [9, 21, 22, 26, 37]. Similarly, prompt extraction attacks have shown that specific queries can trick models into revealing their hidden system prompts,

exposing proprietary instructions and internal settings [3, 12, 26, 29, 39]. These weaknesses highlight a core problem: the same ability to follow instructions that makes LLMs effective also makes them susceptible to manipulation.

Retrieval-Augmented Generation (RAG) [18] systems extend LLMs by connecting them to an external, often private, knowledge base (KB) [7, 10, 14]. In a standard RAG pipeline, a user query is first used to find relevant documents from the KB, and these documents are then fed into the LLM's context to help it generate a more accurate response. While this setup allows access to up-to-date and domain-specific information, it also opens up a new risk: the retrieved documents themselves can be leaked [19, 27, 38]. Since the LLM processes private content during inference time, an attacker who can manipulate the model's output (for instance, through prompt injection) may potentially induce the model to leak retrieved passages verbatim.

This leads to what is known as the knowledge extraction attack, where an attacker systematically queries a RAG system with adversarial inputs to incrementally leak and reconstruct its private KB. While several existing attacks achieve this step-by-step extraction by appending injection commands to their queries, the real difference between them is how they generate new queries to maximize their exploration of the KB. For example, Maio et al. introduce PirateRAG [23], which uses an attacker-side open-source LLM together with an anchor-based mechanism to generate queries that actively explore the target's KB. Similarly, RAGThief [15] works as a black-box attack, using an external LLM to generate continuations of already extracted chunks to generate new queries.

While these methods are effective, they come with several limitations. First, they require a large and often unpredictable number of queries to the target system. This makes the attack expensive when the target is a commercial RAG service billed by usage, such as applications built on the OpenAI Assistants API.¹ The number of queries is a direct measure of the cost because the target LLM returns chunks from its

¹https://developers.openai.com/api/docs/assistants/tools

Figure

Figure 1: Overview of our attack. The attacker iteratively queries the target RAG system. Extracted chunks pass through a three-level deduplication filter before being stored in the Stolen KB. New chunks are pushed to a frontier queue, where the query generation algorithm (Qgen) generates novel query candidates by modifying extracted chunks at different levels. A ranked dispatch strategy prioritizes the most exploratory queries. No external LLM is used at any stage.

knowledge base as part of its output, so the output token usage is roughly equal to the number of retrieved chunks times the chunk length. Since API providers typically charge more for output tokens, each query should be carefully designed to extract new, non-repeating content. Second, these methods rely heavily on an external LLM to generate attack queries, which requires more resources and makes the attack dependent on the quality of the local model's output.

To address the resource-intensive nature and unpredictable query budgets associated with LLM-based attacks, we propose SNAPRAG, a cost-efficient, algorithmic knowledge extraction attack that completely removes the need for an external LLM. Our primary objective is to replace resource-intensive, generative query formulation with a tightly controlled strategy based on discrete token mutations. Specifically, we adapt a black-box, discrete token mutation algorithm for query generation, which we name as Qgen. This algorithm perturbs the tokens of already-extracted text chunks, methodically pushing the embedding of each new query away from known data while maintaining domain relevance through small, controlled modifications. By eliminating the external LLM dependency, our approach yields twofold savings: it incurs zero API or external resource overhead for formulating queries, and it successfully extracts a larger volume of the target's retrieval database using significantly fewer API calls to the target compared to prior work. Figure 1 illustrates the high-level overview of our attack pipeline.

Operating without an external LLM introduces a core tech-

nical challenge for query generation: algorithmically determining how semantically novel a newly generated query must be to discover unseen documents. We solve this challenge by introducing a per-chunk adaptive threshold calibration mechanism. Before generating queries from a newly extracted chunk, we run an unconstrained probe using Qgen to discover the most semantically distant text reachable within the attacker's generation budget. This probe establishes a lower bound on achievable dissimilarity, relying entirely on the attacker's resources rather than any prior knowledge of the target system. We then establish nn evenly spaced dissimilarity targets across this reachable range, generating one specifically mutated query for each target level. Finally, to ensure continuous exploration and prevent query clustering, we employ a chaining strategy. As each generated query is accepted, its embedding is immediately added to the seen-embedding set (EseenE_{seen}). This chaining forces each subsequent query to push away from all previously generated variants, systematically spreading the adversarial queries across the target's embedding space.

Our experiments demonstrate that our fully algorithmic approach extracts significantly more data with fewer queries compared to existing LLM-dependent methods. We summarize the main contributions of this paper as follows:

  • We introduce an automated, black-box knowledge extraction attack that works entirely without an external LLM for query generation, which is a dependency shared by all prior state-of-the-art methods [15, 23, 32, 36]. By

Table 1: Comparison of Knowledge Extraction Attacks on RAG Systems. The "Adaptive" column shows attacks that generate queries dynamically. "No Ext. LLM" indicates whether an attacker-side LLM is required for the attack. "Ext. LLM Calls" indicates the number of additional attacker-side LLM calls per query. The works are arranged in chronological order.

MethodAdaptiveBlack-BoxNo Ext. LLMExt. LLM CallsQuery Generation Strategy
TGTB [38]✕✓✓0Static Datasets (Common Crawl)
PIDE [27]✕✓✓0Static Datasets (WikiQA)
DGEA [4]✓✕*✓0Continuous Gradient Descent
RAGThief [15]✓✓✕1Generative LLM (Continuations)
PirateRAG [23]✓✓✕2Generative LLM (Anchor Based)
Silent Leaks [32]✓✓✕1Generative LLM (Anchor Based)
RAGCrawler [36]✓✓✕≥3Generative LLM (Knowledge Graph Guided)
Ours✓✓✓0Discrete Token Mutation
  • DGEA assumes knowledge of the target's text embedder, making it a gray-box attack.

eliminating this dependency, our approach significantly reduces the overall attack cost and gives greater control over the attack.

  • We propose a budget-aware query generation algorithm that combines discrete token manipulation with adaptive thresholding to efficiently maximize KB exploration.

  • We demonstrate that our method outperforms state-of-the-art approaches in query-limited scenarios, achieving 7.7–19.9% higher extraction rate than the strongest competitor. Furthermore, in the task of extracting half of the target's KB, our approach requires up to 6.77× fewer queries.

  • We evaluate SNAPRAG and the competing attacks against target RAG pipelines protected by an active defense that moderates both input queries and output responses. SNAPRAG remains the leading attack on four of five defended targets, showing that such moderation can reduce extraction but does not eliminate knowledge base leakage.

References (37)