Lezione 3 di 18 · 9 min
Direct prompt injection, step by step
See how an AI application assembles one prompt from three sources, how a user can override the developer’s instructions, and what OWASP and Microsoft recommend testing and building.
Per ora questa pagina è in inglese.
Most real attacks on language-model applications begin with prompt injection. The series’ third episode explains it with a typical application, an email summarisation assistant, and the same structure applies to a support bot, a scheduling agent or an HR assistant [1].
The anatomy of an AI application
The model is not only responding to what someone types. It processes everything the application passes in, which usually has three parts:
- The system prompt, also called the metaprompt: hidden instructions that guide behaviour, tone and output format, such as “you are an email summarisation bot”.
- The user input: what the person types, such as “what are my emails?”.
- Retrieved data: emails, web search results, documents, tool responses or database output that the application fetches.
All of it is joined in a fusion step and flattened into a single token sequence, the context window. The model does not inherently know where one part ends and the next begins. It sees a stream and predicts what follows. That is what makes injection possible: if an attacker can influence any part of the input, they may override instructions, extract information or hijack behaviour.
The attack
In a direct injection the attacker changes the user input. Instead of a question, the user types an instruction, for example to send emails to an external account, which is a form of data exfiltration. After the fusion step the model sees the system prompt and the new instruction together, and it does not know which came from the developer and which from the user. The effect spreads: what it replies, what actions it triggers through tools or agents, and what instructions it carries forward [1].
A case from the series
The episode recounts a 2023 case in which a car dealership’s chatbot was told to agree with everything and to close every message with a phrase calling the reply a binding offer, and was then asked to sell an SUV for one dollar. The bot agreed, repeatedly. As the presenter tells it, the model was not connected to any payment system, so there was no financial loss, but the screenshots spread widely and the dealership took the bot down. The lesson drawn is general: these models are trained to follow instructions, not to understand intent, so “it didn’t take code or elevated access, just clever prompting” [1].
What the standards say
OWASP defines a prompt injection vulnerability as occurring “when user prompts alter the LLM’s behavior or output in unintended ways”. It separates direct injections, where a user’s prompt directly alters the model’s behaviour, from indirect ones, where content from external sources does (the next lesson) [2]. Its prevention list names seven measures:
- Constrain model behaviour.
- Define and validate expected output formats.
- Implement input and output filtering.
- Enforce privilege control and least-privilege access.
- Require human approval for high-risk actions.
- Segregate and identify external content.
- Conduct adversarial testing and attack simulations.
OWASP adds a caution that matters for how you report results: given the stochastic nature of the models, “it is unclear if there are fool-proof methods of prevention for prompt injection” [2].
What a tester checks
- Can the developer’s instructions be overridden by user text, and how easily? Test the whole system through its real interface, not only the model.
- Can the system prompt be extracted? The series’ rule of thumb is never to put confidential data in a metaprompt, because it can usually be extracted [3].
- Can anything the model says or does be changed to something the developer did not intend, such as an action, a data flow or a policy?
To practise safely, plant a canary: a fake secret such as a made-up token in the system prompt of your own test application, and an automated check that detects when it appears in a reply. Microsoft’s open-source AI Red Teaming Playground Labs follow this pattern with twelve challenges, run locally with Docker Compose against an Azure OpenAI or OpenAI endpoint you provide. The direct-injection pair is credential exfiltration (labs 1 and 7, easy and medium) and extracting a secret from the metaprompt (labs 2 and 8): the first asks you to get a model to reveal the contents of a protected file, the second a secret word held in its hidden instructions [4].
Da ricordare
- An AI application fuses a system prompt, user input and retrieved data into one prompt.
- The model cannot tell which part is the developer’s instruction and which is the attacker’s.
- Direct injection targets the user input; the effects reach replies, actions and later instructions.
- OWASP lists seven mitigations and admits no fool-proof prevention is known.
- Test with planted canary secrets in an application you own.
Mettiti alla prova
1. What is the “fusion” step?
- Joining the system prompt, user input and retrieved data into one prompt — Esatto.
- Merging two models
- Compressing the model
- Encrypting the prompt
All inputs are flattened into a single sequence before the model reads them.
2. In a direct injection, which input does the attacker change?
- The system prompt
- The user input — Esatto.
- The training data
- The GPU
Direct injection is delivered through what the user types.
3. Why does the dealership case matter even though no money was lost?
- It shows that models follow instructions rather than understand intent — Esatto.
- It shows chatbots always handle payments
- It proves models cannot be used
- It was a hardware failure
The same weakness would be serious in a system connected to payments or data.
4. What is the safest way to practise?
- On a stranger’s chatbot
- On your own test app with a planted canary secret — Esatto.
- On a production system with real data
- Without logging
Authorised scope and planted data keep practice safe and measurable.
Mettilo in pratica con FireAI
Metti in pratica questa lezione sul tuo Mac.
Fonti
- Microsoft, AI Red Teaming 101, Episode 3: Direct prompt injection explained
- OWASP GenAI, LLM01:2025 Prompt Injection
- Microsoft, AI Red Teaming 101, Episode 5: Prompt injection attacks, single-turn
- Microsoft, AI Red Teaming Playground Labs (GitHub)
Mettilo in pratica sul tuo Mac
Prova tutte le funzioni gratis per 17 giorni, senza carta.