Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Web attacks

Lab · Medium · 1.5 hoursFree account

Injection Attacks: XPath, LDAP & HTML-to-PDF

Exploit XML XPath and LDAP directory injection vulnerabilities.

  • xpath-injection
  • ldap-injection
  • injection
  • xml
On this page 10 sections
  1. ·Lab Setup
  2. ·Build the target
  3. 1Fingerprint the XPath backend
  4. 2Bypass XPath authentication
  5. Locked: Bypass LDAP authentication with filter injection
  6. Locked: Enumerate with a boolean LDAP probe
  7. Locked: Read a local file through the PDF renderer
  8. Locked: SSRF through the same renderer
  9. Locked: Defence and Detection
  10. Locked: Check Your Work

Before you start

SQL is not the only data store that speaks a query language. XPath, LDAP and HTML-to-PDF renderers all parse attacker-controlled strings, and each of them can hand over authentication, directory data or local files. Real breaches at identity providers and reporting pipelines have used these sinks. The lab fingerprints an XPath login, bypasses an LDAP filter, and reads local files through a PDF renderer.

What You'll Learn

  • Fingerprint an XPath-backed login form from its error messages
  • Bypass XPath authentication with a boolean injection
  • Bypass and enumerate an LDAP directory search with filter injection
  • Abuse an HTML-to-PDF renderer to read a local file
  • Abuse the same renderer for SSRF against an internal host
  • Map the correct remediation to each of the three sinks

Lab Setup

Run this lab only against the target that you build in the next section.

Tools and versions

  • curl 8.x for every request.
  • pdftotext (from poppler-utils) to read the text out of a generated PDF.
  • A box you control reachable from the target, for the SSRF proof in Step 6.

Build the target

KaliRange hosts no targets, no virtual machines and no lab files. You run this lab on your own machine, against a small web application that you build. The names kalirange-directory.lab and internal.kalirange.lab are the example names of this lab. Point them at your own hosts (for example in /etc/hosts) or replace them in the commands.

Your lab must have these parts:

HostRole
kalirange-directory.labThe web application, served over HTTPS. With a self-signed certificate, add -k to the curl commands
internal.kalirange.lab (port 8080)An internal service that only the application server can reach
  • POST /login takes username and password. It checks them against an XML user store with an XPath query built by string concatenation, like //user[username='X' and password='Y']. It returns the XPath parser's error text in the JSON field error. A good login returns {"status": "ok", "logged_in_as": "admin", "role": "administrator"}. The user store holds a user admin with the role administrator.
  • GET /staff/search?uid= queries an LDAP directory with a filter built by concatenation, like (&(uid=INPUT)(userPassword=INPUT)). It returns the entries as JSON in results. The directory holds jsmith (Jane Smith, Support), mchen (Mike Chen, Engineering) and admin (Site Administrator, IT). Use a test LDAP server such as OpenLDAP, with fake data only.
  • GET /staff/exists?uid= uses the same filter style. It returns only {"exists": true} or {"exists": false}. The admin title must start with I.
  • POST /invoice takes note, puts it into an HTML page as it is, and converts the page to a PDF with a server-side HTML-to-PDF renderer. The renderer must allow file:// and http:// resources in frames. This is the flaw of Steps 5 and 6. Some renderer versions or settings do this by default, such as wkhtmltopdf with local file access enabled.
  • On internal.kalirange.lab:8080, the path /status returns {"service":"internal-billing","version":"3.2.1","env":"production"}. A firewall rule or a private network blocks it from every host except the application server.

No public training application has this exact set of flaws, so build a small application and a small internal service with the flaws that the steps use. Run them as containers or virtual machines on your own computer, and take a snapshot of each virtual machine (or note the container state) before Step 1. The Kali machine needs curl and pdftotext only.

Test only a target that you own or have written permission to test, here kalirange-directory.lab and internal.kalirange.lab as you set them up. Authentication bypass, directory enumeration and SSRF against a real system are each unauthorised access on their own. Stay inside your own lab.

Step 1: Fingerprint the XPath backend

1

Different query languages fail differently on a broken quote. Before you craft a bypass, confirm the backend is XPath and not SQL, so you pick the right syntax.

curl -s -X POST https://kalirange-directory.lab/login \
  -d "username=admin'&password=x"

Example response:

{"error": "XPath expression error near position 24: unterminated string"}

What to notice: the error names XPath, not a database driver. A single unescaped quote broke the query string that the application builds for a lookup like //user[username='admin'' and password='x'], and the parser's own error told you exactly which technology to target next.

Step 2: Bypass XPath authentication

2

If the login query concatenates your input straight into the XPath predicate, you can rewrite the condition itself so it is always true, with no valid password at all.

curl -s -X POST https://kalirange-directory.lab/login \
  -d "username=admin' or '1'='1&password=anything"

Example response:

{"status": "ok", "logged_in_as": "admin", "role": "administrator"}

What to notice: the query the server built became effectively //user[username='admin' or '1'='1' and password='anything']. The or '1'='1' clause makes the predicate true for the first matching node regardless of the password, and the app logged you in as admin. Note the role in the response: this single injection crossed straight from no account to the highest privilege the app has.

XPath has no concept of multiple statements like SQL's ;, so classic SQL stacked-query tricks do not apply. The attack surface here is entirely the boolean logic inside one predicate.

Sign in to continue

6 more sections are for signed-in members. It is free: sign in with Google.

Related