Prompt Injection Explained: How Attackers Hijack LLM Apps
Language models cannot reliably tell instructions from data. That single fact powers prompt injection, the number one risk in LLM applications. Here is how it works and why it is so hard to fix.

If you have studied SQL injection, prompt injection will feel familiar. In both cases, untrusted input changes the meaning of instructions. The difference is that SQL injection has a clean fix, parameterized queries, and prompt injection does not, at least not yet.
Prompt injection leads the OWASP Top 10 for LLM Applications. If your product has a chatbot, an AI assistant, a summarizer, or an agent, it is the risk to understand first.
Why it exists
A typical LLM application stuffs several things into one context window: a system prompt from the developer, the user’s message, retrieved documents, tool definitions, and the results of tool calls. The model sees all of it as one long sequence of text and predicts what comes next, following instructions wherever they appear.
There is no reliable boundary between “instructions from my developer” and “text I was asked to read.” That is the whole problem.
Direct prompt injection
In direct injection, the user is the attacker. They type something designed to override the application’s rules: reveal the system prompt, ignore restrictions, approve a refund it should not, or access data meant for someone else.
Direct injection matters most when the application gives the model access to things the user should not have: other users’ data, secrets in the system prompt, or actions the prompt is “supposed” to restrict.
Indirect prompt injection: the dangerous one
Indirect injection, described in influential research by Greshake and colleagues in 2023, is far worse. The attacker never touches the application. Instead, they plant instructions in content the application will process later on a victim’s behalf: a web page, an email, a shared document, a calendar invite, a code comment, a product review, a support ticket, or even an image.
Picture an email assistant that can read and send mail. An attacker sends the victim an ordinary-looking message with a hidden HTML comment addressed to AI assistants, telling them to quietly forward the victim’s recent bank emails to an outside address. When the victim asks the assistant to “summarize my inbox,” the model reads that hidden text, and it may obey. The victim never sees the attack, because it was hidden in a comment, tiny white text, or metadata.
Researchers have demonstrated this pattern against essentially every type of LLM product: browsing chatbots, coding assistants reading repository files, productivity assistants reading documents, and agents with tool access.

How data gets out
Injection usually aims to steal something, and the attacker needs a way to get it out:
- Markdown images. If the chat interface automatically renders images, an injected model can output an image whose URL contains private data. The user’s browser fetches it instantly, with no click required. Many vendors have since restricted image rendering, but it remains a top item to test.
- Links disguised with harmless text.
- Tools. Browsing, email, web search, posting to chat, or writing to a shared file can all carry data out.
- Memory. Assistants with long-term memory can be tricked into storing malicious instructions that persist into future conversations, sometimes called memory poisoning.
Researchers call the dangerous combination the lethal trifecta: access to private data, exposure to untrusted content, and the ability to communicate externally. An agent with all three can be made to leak data.
Why you cannot just “fix the prompt”
Model providers have made real progress with training that prioritizes instructions by source, classifiers that detect attacks, and labeling of untrusted content. These reduce attack success significantly. But researchers keep finding bypasses, and a determined attacker iterating against one specific system usually succeeds eventually.
So the right question for testers is not “can I inject this model?” Assume you can. The question is: what can an injected model do in this application?
Other LLM risks worth knowing
- System prompt leakage. Assume your system prompt is public. Never put secrets or authorization rules in it.
- Improper output handling. Model output that reaches HTML, SQL, or a shell without validation creates classic XSS, injection, and command execution bugs.
- RAG risks. Retrieval systems can leak documents across users or tenants, or be poisoned with malicious content.
- Excessive agency. Agents with broad tools and broad permissions turn a clever sentence into a real-world action.
- Confused deputies and tool parameter injection. If a tool trusts IDs or arguments the model supplies, injection becomes IDOR or worse.
- MCP and tool ecosystem attacks, such as tool poisoning, where a tool’s description carries hidden instructions, or a tool server changes its behavior after approval.
- Coding agents, which read untrusted repositories and can run commands.
Testing LLM apps safely
A systematic test of an LLM application looks like this:
- Map its capabilities: data access, tools, rendering, memory.
- List every input channel, direct and indirect.
- Test direct injection against business rules.
- Plant harmless test instructions in indirect channels using your own accounts and content, and trigger processing as a victim account you control.
- Check exfiltration channels with your own out-of-band server.
- Test whether tool calls respect user authorization.
- Check whether model output reaches interpreters unvalidated.
A good test payload is a harmless canary, such as asking the model to include a unique phrase and load an image from your own server. If the phrase appears, the channel follows instructions. If your server gets a hit, there is an exfiltration path.
Test only AI applications you own or are explicitly authorized to assess.
Key takeaways
- LLMs process instructions and data in one context and cannot reliably separate them.
- Indirect injection, hidden in content the app processes, is the most dangerous form.
- Data leaks through rendered images, links, tools, and memory.
- Prompt injection cannot currently be fully fixed at the model level, so focus on what an injected model can do.
- Treat system prompts as public and model output as untrusted input.
Go deeper with the book
This article is adapted from Web Security in the Age of AI: Attacks, Defenses, Tools, and Bug Hunting for Modern Web Applications and LLM Systems by Rupak (see Chapter 23, “Attacking LLM Applications and AI Agents”).
The book pairs every attack with its fix across 27 chapters, from core vulnerabilities and professional tools to bug bounty hunting, attacking and defending LLM applications, and lessons from real breaches. It includes hands-on labs, review questions, a full testing checklist, a security headers reference, and an LLM red teaming playbook.
Related reading
- How to Secure AI Agents: Design for a Compromised Model
- How AI Is Changing Offensive Security (and What Defenders Must Do)







