A prompt injection attack happens when text that a large language model reads changes what the application does, against the intentions of the people who built it. As more products ship chatbots, copilots and agents that touch customer data and internal tools, this is one of the first things I check in any LLM feature. This post explains how prompt injection works and gives a practical, authorised-testing approach you can apply to real applications.

What is a prompt injection attack?

An LLM application usually stitches several pieces of text into one prompt: a system prompt written by the developer, the user’s message, and often extra context such as retrieved documents, web pages, emails or tool outputs. The model sees all of it as a single stream of tokens. It has no reliable, built-in way to separate “instructions from the developer” from “data that happens to contain instructions”.

That is the core problem. If an attacker can get text into that stream, they can try to override the developer’s intent. The OWASP Top 10 for LLM Applications lists prompt injection as LLM01, and notes that because these models are probabilistic, there is no guaranteed way to fully prevent it. As a tester your goal is therefore not to prove that injection is possible – it usually is – but to show what an attacker can actually achieve with it.

Direct versus indirect prompt injection

  • Direct injection: the attacker puts instructions straight into the input field, for example asking a support bot to ignore its rules and reveal its configuration.
  • Indirect injection: the attacker plants instructions in content the model will process later – a web page it summarises, a file uploaded to a knowledge base, a product review, an email, a code comment or a calendar invite. The victim never sees the payload; the model does.

Indirect injection is usually the more serious finding, because it can affect other users and needs no access to their session.

Why a prompt injection attack matters

On its own, making a model say something off-script is low impact. The severity comes from what the model is connected to. Think about the model’s privileges, not its words:

  • Data access: can it read another user’s conversation, retrieved documents or account details?
  • Tools and actions: can it call functions, send emails, create tickets, run queries or make purchases?
  • Output rendering: is its output shown as raw HTML or Markdown, so that a crafted response could carry a link or script into a victim’s browser? OWASP tracks this separately as improper output handling, and injection plus unsafe rendering is a common chain.

So the questions that decide impact are: whose data can this model reach, what can it do on a user’s behalf, and where does its output end up?

Testing LLM apps for prompt injection, step by step

Everything below is for systems you are authorised to test – your own application or a program whose scope includes the AI feature. Confirm that LLM and AI endpoints are in scope before you start, because many programs still exclude them.

1. Map the attack surface

Before sending a single payload, understand the plumbing. Work out:

  • What inputs reach the model: chat box, uploaded files, URLs it fetches, data from other users.
  • What external content the model ingests automatically (RAG sources, email, web pages). Every such source is an indirect-injection channel.
  • What tools or functions the model can call, and with what privileges.
  • How the model’s output is displayed and whether it is trusted by any downstream system.

PortSwigger’s Web LLM attacks material is a good structured way to learn this mapping, and includes labs where you can practise safely.

2. Discover the model’s instructions and tools

Gently probe what the model believes its job and constraints are, and which tools it can invoke. Asking it to describe its own capabilities, or to explain why it declined a request, often reveals the available functions and the shape of the system prompt. System prompt leakage is itself an OWASP-listed risk, because secrets or hidden business logic sometimes end up in that prompt.

3. Attempt to override intended behaviour

The classic direct test is to supply content that competes with the developer’s instructions – for example, text that reframes the task, redefines the model’s role, or claims the earlier rules no longer apply. You are checking whether later text in the stream can win against the system prompt. Keep each attempt documented: input sent, full response, and whether behaviour actually changed.

4. Test indirect injection through ingested content

This is where real impact usually lives. If the app summarises a page you control, reads a document you upload, or processes a field another user can set, place instruction-like text in that content and see whether the model follows it when a different user triggers the feature. A realistic objective is: can content authored by attacker A cause the model to leak user B’s data or perform an action for user B? If yes, you have a cross-user finding that is worth reporting in detail.

5. Chain to real impact

Tie the injection to a concrete consequence and stop there – you do not need to cause damage to prove risk. Strong outcomes to demonstrate safely include:

  • Reading data the current user should not see.
  • Triggering a tool or action the user did not request.
  • Getting attacker-controlled markup into another user’s rendered output.
  • Extracting secrets or hidden instructions from the system prompt.

Capture enough evidence (requests, responses, screenshots) to let the team reproduce it, and avoid touching other users’ real data beyond the minimum needed to show the issue.

How developers should defend against prompt injection

When you report, pair the finding with fixes. The OWASP guidance recommends a defence-in-depth approach rather than a single filter:

  • Least privilege for the model: give the LLM the narrowest data access and the fewest tools it needs. This is the single biggest lever on impact.
  • Human approval for high-risk actions: require a confirmation step before the model sends money, emails or destructive operations.
  • Segregate and label untrusted content: clearly mark external or user-supplied text so the application treats it as data, and constrain how much influence it can have.
  • Validate outputs with deterministic code: enforce expected formats and escape or sanitise anything rendered in a browser, rather than trusting the model’s output.
  • Constrain behaviour in the system prompt and test it adversarially, while remembering the system prompt alone is not a security boundary.

The honest message to developers is that you cannot “prompt your way” to safety. Injection is managed by limiting what the model can reach and do, and by treating every output as untrusted until checked.

Key takeaways

  • A prompt injection attack works because the model cannot reliably tell developer instructions from data in the same token stream.
  • Direct injection comes from the user’s input; indirect injection comes from content the model ingests, and is usually higher impact.
  • Severity depends on the model’s data access, tools and how its output is rendered – not on the clever wording.
  • Test only in-scope AI features, map the surface first, then chain injection to a concrete, safely demonstrated impact.
  • Defence is least privilege, human approval for risky actions, content segregation and strict output validation.

Need an LLM feature tested properly?

If your product ships a chatbot, copilot or agent that touches user data or internal tools, it needs an assessment built around these risks. See my API penetration testing and web application penetration testing services, or get in touch to scope a review of your AI feature against the OWASP LLM Top 10.

Suvendu Dash

Offensive security engineer, penetration tester and bug bounty hunter. Ex-Senior Principal Security Engineer at INDmoney. Hall of Fame: Apple, Microsoft, OpenAI.