Prompt Injection Explained: How the Attack Works and How to Defend Against It

✨ AI Summary
By Updated

Prompt injection is an attack where text that an AI model reads is treated as an instruction instead of as information. The model cannot reliably tell the difference between what its developer told it to do and what an attacker wrote into a message, a web page or a document. OWASP ranks it as the number one risk for applications built on large language models, and it has held that position in every edition of the list.

This guide explains how the attack works, the forms it takes, how to test for it in systems you are authorised to test, and which defences hold up. Each section links to a practice lab where you can see the behaviour on a live model.

Why prompt injection works

A traditional program keeps code and data apart. A database query has a fixed structure, and user input is passed in as a value. A language model has no such separation. Everything it receives arrives as one stream of text: the developer’s system prompt, the conversation so far, the user’s message, and anything the application fetched on the user’s behalf.

The model then predicts the most suitable reply to that whole stream. If part of the stream reads like a newer or more specific instruction, the model may follow it. The model is working as designed, which is why prompt injection is treated as a class of weakness to be managed, not a bug that one patch removes.

The main types of prompt injection

Direct prompt injection

The attacker types into the application themselves. They are a user of the chatbot, and they write a message meant to make the model set aside its instructions. The goals are usually to reveal hidden instructions, to produce content the application is meant to refuse, or to make the assistant act outside its role.

the sales bot prompt injection lab.

Indirect prompt injection

The attacker never talks to the model. They place instructions inside content the model will read later: a web page it summarises, an email it triages, a PDF it analyses, a product review, a code comment. The victim is the person who asks the assistant to process that content. This form matters most in practice, because it turns any data source into a possible attack path.

the indirect injection lab. Background reading: our tutorial on indirect prompt injection.

Injection through images and files

Models that accept images, audio or documents read instructions from those too. Text can be hidden in an image, in document metadata, or in characters a person cannot see on screen.

the visual prompt injection lab and the hidden character lab.

Injection against agents and tools

When a model can call tools, an injected instruction can do more than change words on a screen. It can send an email, read a file, call an API or change a record, using the permissions the agent was given. The damage is set by what the agent is allowed to do, which is why OWASP’s 2026 list moved Excessive Agency up to third place.

the agent hijack lab and the confused deputy lab.

Prompt injection and jailbreaking are different things

The two are often mixed up. Jailbreaking targets the model’s own safety training, to get output the model’s maker did not want it to produce. Prompt injection targets an application: the instructions and data a developer wrapped around the model. A system can resist jailbreaks well and still be open to injection, because the injected instruction may ask for something perfectly ordinary, such as forwarding a document.

What an attacker gains

  • Hidden context. System prompts, tool definitions and business rules that reveal how the application works.
  • Data. Other users’ information, internal documents reached through retrieval, or secrets placed in the prompt.
  • Actions. Anything the assistant’s tools can do, carried out with the assistant’s permissions.
  • Trust. Output the user believes because it came from an assistant they rely on, such as a false summary or a link.

Real cases

Prompt injection is not theoretical. These three were found by security researchers, reported to the vendors and written up publicly.

  • Bing Chat, 2023. A research team led by Kai Greshake showed that instructions hidden in web pages could take over Bing’s GPT-4 chat when it read them, and coined the term indirect prompt injection. Their paper described data theft and manipulation of what the assistant told the user. Source: Greshake et al., 2023.
  • Microsoft 365 Copilot, 2024. Johann Rehberger showed a chain in which a shared document carried hidden instructions, Copilot then searched the user’s emails and documents, and the data was packed into a link using invisible characters. If the user clicked it, the data went to the attacker. It was reported in January 2024 and Microsoft fixed it. Source: The Hacker News, August 2024.
  • ChatGPT memory, 2024. Rehberger also showed that untrusted content, such as a document or web page the assistant was asked to read, could plant false long-term memories about the user. Those memories then shaped every later conversation, and a follow-up demonstration used the same route to send conversations to an outside server. Source: Schneier on Security, October 2024.

The pattern is the same each time: the assistant read something its user did not write, treated it as an instruction, and had access to something worth taking.

How to test for prompt injection

Test only systems you own or have written permission to test. A sound test follows the data, not a list of clever phrases.

  1. Map every input the model reads. User messages are the obvious one. Then list retrieved documents, web content, file uploads, tool results, memory and anything another system writes into the prompt.
  2. Map what the model can do. List each tool, what it can reach, and whose permissions it runs with.
  3. Decide what counts as a failure. For example: the assistant reveals its instructions, returns another customer’s data, or calls a tool the user did not ask for.
  4. Test each input against each failure. Check whether text arriving through that input can change the model’s behaviour.
  5. Record evidence. Keep the input, the output and the tool calls, so a developer can reproduce the result.
  6. Retest after every change. Model updates and prompt edits change behaviour in both directions.

Our AI and LLM hacking course walks through this method day by day.

Defences that hold up

No single control stops prompt injection. The aim is to make sure that when an injection succeeds, it achieves little.

DefenceWhat it doesIts limit
Least privilege for toolsThe assistant can only do what the task needs, with the user’s own permissionsNeeds careful design of every tool
Human approval for risky actionsSending, paying, deleting and sharing need a person to confirmPeople approve without reading if asked too often
Separating trusted and untrusted contentFetched content is marked as data and handled by a restricted stepReduces risk; the model can still be influenced
Output handlingModel output is validated before it reaches a browser, database or shellProtects downstream systems, not the conversation
Input and output filtersCatch known attack patterns and data leaving the systemAttackers rephrase; filters alone are not enough
Keeping secrets out of promptsNothing in the context is worth stealingRequires moving checks into code
Logging and monitoringUnusual tool use and repeated probing are noticedDetects; does not prevent

The rule that follows from all of these: never let the model be the only thing standing between an attacker and something valuable. Permission checks belong in code.

Frequently asked questions

Can prompt injection be fully fixed?

Not with current models. It can be reduced and contained. Design the system so that a successful injection has little it can reach.

Is prompt injection the same as SQL injection?

The idea is similar, since both mix instructions with data. SQL injection has a complete fix in parameterised queries. Prompt injection has no equivalent, because a language model has no strict boundary between the two.

Does a stronger system prompt stop it?

It helps a little and fails often. Instructions in a system prompt are still text the model weighs against other text.

Who needs to learn this?

Anyone building, testing or approving an application that puts a language model in front of users or data: developers, security testers, product owners and architects.

Where can I practise legally?

On systems built for it. The SecurityElites labs run against live models and are free to start. When you are ready to prove the skill, the SE-ARTCP certification tests it hands-on.

⚡
Join free to earn XP for reading this article Track your progress, build streaks and compete on the leaderboard.
Join Free
Lokesh N. Singh aka Mr Elite
Lokesh N. Singh aka Mr Elite
Founder, Securityelites · AI Red Team Educator
Founder of Securityelites and creator of the SE-ARTCP credential. Working penetration tester focused on AI red team, prompt injection research, and LLM security education.
About Lokesh ->

Leave a Comment