Abstract
The increasing reliance on large language models (LLMs) such as ChatGPT in various fields emphasizes the importance of "prompt engineering," a technology to improve the quality of model outputs. With companies investing significantly in expert prompt engineers and educational resources rising to meet market demand, designing high-quality prompts has become an intriguing challenge. In this paper, we propose a novel attack against LLMs, named prompt stealing attacks. Our proposed prompt stealing attack aims to steal these well-designed prompts based on the generated answers. The prompt stealing attack contains two primary modules: the parameter extractor and the prompt reconstruction. The goal of the parameter extractor is to figure out the properties of the original prompts. We first observe that most prompts fall into one of three categories: direct prompt, role-based prompt, and in-context prompt. Our parameter extractor first tries to distinguish the type of prompts based on the generated answers. Then, it can further predict which role or how many contexts are used based on the types of prompts. Following the parameter extractor, the prompt reconstructor can be used to reconstruct the original prompts based on the generated answers and the extracted features. The final goal of the prompt reconstructor is to generate the reversed prompts, which are similar to the original prompts. Our experimental results show the remarkable performance of our proposed attacks. Our proposed attacks add a new dimension to the study of prompt engineering and call for more attention to the security issues on LLMs.
Introduction
As "prompt engineering" becomes a valuable trade secret, this research introduces the first systematic prompt stealing attack against Large Language Models (LLMs). High-quality prompts are increasingly viewed as intellectual property, with companies investing heavily in engineers to guide LLMs toward precise, high-value outputs. While research has traditionally focused on "jailbreaking" or weight theft, the security of the prompts themselves has been largely overlooked. This paper addresses whether an adversary can extrapolate a hidden prompt simply by analyzing a model's response. If prompts can be stolen, the competitive advantage of "prompt-as-a-service" providers evaporates, and malicious actors could more easily mimic successful prompt structures for harmful purposes.
Methodology
The researchers propose a two-stage attack framework designed to reverse-engineer instructions from LLM outputs.
1. Parameter Extractor
This module identifies the "DNA" of the original prompt using a hierarchical classification approach:
- Primary Classifier: A BERT-based model determines if the prompt was a Direct Prompt (simple instruction), a Role-based Prompt (e.g., "Act as a lawyer"), or an In-context Prompt (using few-shot examples).
- Sub-classifiers: Depending on the type, further classification identifies specific roles (from a taxonomy of 15 common personas) or predicts the number of few-shot examples provided.
2. Prompt Reconstructor
Once parameters are extracted, a second LLM (e.g., ChatGPT) generates a "reversed prompt." For direct prompts, it infers the question from the answer. For role-based or in-context prompts, it wraps the reconstructed core instruction in the predicted parameters (e.g., prepending "Assume you are a Research Analyst").
Datasets
The attack was evaluated using two primary benchmarks: RetrievalQA (question-answering across domains) and Alpaca-GPT4 (instruction-following). A custom taxonomy of 15 common roles, such as Legal Advisor and Financial Advisor, was developed to train the role-based sub-classifiers.
Key Insights and Results
The core finding is that prompts leave "fingerprints." Different prompt styles produce statistically distinct output patterns that BERT-based classifiers can detect. Hierarchical prediction proved essential; attempting to guess all parameters simultaneously is significantly less accurate than first identifying the prompt type.
Experimental results demonstrate the effectiveness of the attack:
- High Extraction Accuracy: The primary classifier achieved an accuracy of 0.833 in predicting prompt types for ChatGPT.
- Effective Reconstruction: When parameters were known, the Prompt Similarity (PS) reached up to 0.832.
- Defense Challenges: Defending against these attacks involves a utility-security trade-off. Adding "don't leak" instructions or rewriting outputs (Answer-Based Defense) lowers attack accuracy but often degrades the quality of the AI's response.
Conclusion and Future Work
While the attack is highly effective for roles and prompt types, reconstructing the exact content of few-shot examples remains challenging as they often leave subtle traces. Current defenses are subpar because they force a choice between security and utility. Future research should focus on automated, robust defense mechanisms that protect prompt privacy without sacrificing LLM performance, especially as models continue to evolve.
Summary updated