XPath Injection Explained: The Bug That Queries Your Database for Attackers


Everyone in bug bounty knows SQL injection — user input slipped into a database query, and suddenly the attacker is reading tables they shouldn't see. But there's a quieter cousin hiding in older and XML-heavy applications: XPath injection. Same idea, different language. If an application stores data in XML and builds an XPath query from your input without sanitising it, an attacker can rewrite that query and pull out data the application never meant to show.

What XPath Is

XPath is a query language for XML documents, the way SQL is a query language for databases. Applications use it to find nodes in an XML file — a user record, a price, a permission flag. A login check might look like this:

//users/user[username='anony' and password='hunter2']

If that query finds a matching user node, the login succeeds. The problem starts when anony and hunter2 come straight from the login form, with no escaping, and get pasted into the query string.

How the Attack Works

An attacker types ' or '1'='1 into the username field. The query becomes:

//users/user[username='' or '1'='1' and password='']

Because or '1'='1' is always true, the query matches the first user in the document — often an admin account — and the attacker logs in without knowing any password. That is the classic authentication bypass, and it works exactly like the SQL injection version.

From there, a hunter can extract data piece by piece using XPath functions. ' or substring(//users/user[1]/password,1,1)='a tests whether the first character of the admin password is a. Repeat for each character, and the whole password leaks one true-or-false question at a time — the same blind-extraction technique used in blind SQL injection. Functions like count() reveal how many records exist, and string-length() tells the hunter how long a secret is before they start guessing it.

Where Hunters Find It

XPath injection lives where XML lives: legacy enterprise apps, SOAP web services, configuration-driven portals, and any application that stores user data in XML files instead of a database. The clues are in the error messages — an XML parser error after submitting a quote character is a strong hint — and in the behaviour: a login that accepts ' or '1'='1 as a username is practically confessing.

How to Test It (Legally)

Only test this on programs where you have written permission — a bug bounty scope, your own app, or a lab. The method is simple:

  1. Find a form or parameter that likely queries XML data — logins, search boxes, and SOAP endpoints are the usual suspects.
  2. Submit a single quote ' and watch the response. An XML parsing error or a 500 status means your input reached the query.
  3. Try the classic bypass: ' or '1'='1 in the username field, with any password. If you get logged in as the first user, you have found the bug.
  4. Document the impact: show which account you reached and what data it exposes. A clean report with a working proof-of-concept and a clear impact statement is what gets paid.

How to Fix It (For Developers)

  • Use parameterised XPath queries. Bind user input as variables instead of pasting it into the query string — the same rule as SQL, for the same reason.
  • Validate and escape input. If you must build queries from strings, escape quotes and special characters properly, and reject input that doesn't match the expected format.
  • Apply least privilege. The XML document the application reads should not contain secrets the application never needs — split credentials and sensitive data into a separate, access-controlled store.
  • Don't leak parser errors. A detailed XML error message tells an attacker exactly how the query is built. Return a generic error instead.

XPath injection is rarer than SQL injection, which is exactly why it pays: fewer hunters look for it, and the applications that have it are often old, important, and poorly tested. Learn to spot the XML smell, and you have a bug class most of your competition is ignoring.

Remember the golden rule: only test systems you own or have written permission to test. Finding this bug in a scoped program earns a bounty. Finding it anywhere else earns trouble.

Comments

Popular Posts