XPath Injection Lab: Bypass an XML Login With Query Syntax

✨ AI Summary
XmlAuth keeps its users in an XML document and logs you in by building an XPath query from what you type. XPath is a query language, just like SQL — and the moment your input becomes part of the query, you can make it select the admin node no matter what password you supply.
Lab at a glance: Intermediate · 80 XP · about 15 minutes · runs in your browser, no signup or setup. Try it yourself first; the full solution is behind a spoiler below.

What this lab teaches

XPath injection targets apps that query XML with XPath built from user input. By injecting XPath syntax, an attacker rewrites the query's logic — classically turning a username-and-password lookup into one that matches a node regardless of the password.

The lesson: XML is data, and XPath over it is a query language with the same injection risk as SQL when it is built by string concatenation.

How XmlAuth looks users up

XmlAuth builds an XPath expression meaning 'select the user whose name and password both match', inserting your values straight into the expression. Your input is parsed as part of the XPath, so XPath operators you send change which nodes the expression selects.

The attack approach

  • Notice authentication is an XPath query over an XML user store.
  • Inject XPath syntax so the password part of the condition is always satisfied.
  • Steer the expression to select the admin node on the username alone.
  • Submit and gain the admin session.

Full walkthrough (spoiler)

If the query still fails to match, the condition is not closing correctly — open the steps.

Show the step-by-step solution
  1. In the login fields, supply the admin username plus XPath syntax that neutralises the password condition.
  2. Use an always-true comparison so the AND between username and password resolves on the username alone.
  3. Submit. The XPath now selects the admin node regardless of the password value.
  4. You are authenticated as admin and the lab marks you solved.

Why it works

The application assumes your input is a plain value, but it is concatenated into an XPath expression. XPath operators and quotes you send are interpreted as query logic, so the password comparison collapses into something always true and the admin node is selected.

Core principleNever build XPath from raw user input. Use parameterised/variable-bound XPath, or escape and strictly validate input first.

Where this happens in the real world

XPath injection appears wherever apps authenticate or search against XML documents by concatenating input into a query — older SOAP services and XML-backed logins are typical. It is structurally identical to SQL injection and falls under Injection (A03) in the OWASP Top 10 (2021).

How to defend against it

  • Use parameterised XPath (variable binding) so user data never becomes query syntax.
  • Escape and validate input if you must build expressions as strings.
  • Store credentials properly (hashed, in a real datastore) rather than matching passwords inside an XML query.
  • Return generic auth errors so injection attempts get no extra feedback.
Mr Elite tip: If a login or search is backed by XML and a stray quote or bracket in your input changes the result, the input is being concatenated into an XPath expression.

Try it, then go further

Reading about it is the easy part. Open the lab, try your own approach before peeking at the spoiler, and see how many attempts it takes.

❓ Frequently asked questions

What is XPath injection?
An attack on apps that build XPath queries over XML from untrusted input, letting an attacker inject XPath syntax to change which nodes the query selects — for example to bypass a login.
Why is it compared to SQL injection?
Because the root cause is identical: untrusted input concatenated into a query language. Only the language (XPath over XML) differs.
How do I prevent it?
Use parameterised XPath with variable binding so data never becomes syntax, and validate or escape any input you must place in an expression.
Where does XPath injection show up?
In apps that store users or data in XML and authenticate or search by building XPath from form input, including some older SOAP and XML-backed services.
⚡
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

Your email address will not be published. Required fields are marked *