Skip to content
XXE Write-up Intermediate

XML External Entity injection with error-based data exfiltration

The application allowed uploading XML schema files that were parsed with external entity support, enabling an attacker to trigger server-side requests and read local files. By referencing an external DTD, the attacker caused Java exceptions that included file contents or directory listings in error logs, which were then displayed.

Serj Novoselov Follow

Published on infosecwriteups.com

In a recent project, I’ve uncovered a significant security issue that revolves around XML External Entity attacks.

Serj Novoselov, on infosecwriteups.com

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

The target was a project the author was testing, a solution that accepted uploads of schema files containing XML markup. The application processed an uploaded schema automatically after the upload, and it also had a logging functionality whose logs could be displayed. That combination, XML parsing of user-supplied schema files plus a readable error log, is what the whole write-up turns on.

The article describes the discovery and exploitation of an XML External Entity (XXE) vulnerability in that project, and the unusual route used to get the results out: Java exceptions written to the log files. XXE itself is the familiar class of bug in which an application's XML parser resolves external entities, letting an attacker read local files, make the server issue requests, or in the worst case execute code on the server.

From the original on infosecwriteups.com
XXE

Setup

The report does not describe accounts, roles, tooling versions or any other preconditions beyond the feature itself. What matters is that the tester could upload a schema file, that the application parsed it on upload, and that the resulting errors were visible in a log the tester could read. The schema file had to follow a specific template, a constraint that shaped every payload in the article.

Finding it

Testing the schema upload functionality, the author noticed that the uploaded schema was parsed as XML. That made external entity injection worth trying. The first success was a blind one: an XXE that produced Server-Side Request Forgery, with the server's backend making an HTTP request to a destination chosen by the injected entity.

Reading files was not feasible at that point. There was no direct output, only a generic error message. The schema also had to adhere to a specific template, which made techniques such as a DTD with multiple instructions impractical, because the XML parsing error occurred after the first instruction.

The way forward came from the application's logging functionality: logs could be displayed. If the parser's exception text could be made to contain the data the author wanted, that data would land in the error log.

Exploitation

Step 1: XXE to SSRF

The author created a schema file from a template and appended the XXE payload to it, then uploaded the file. The report does not reproduce the payload text itself.

Uploading the evil schema file From the original on infosecwriteups.com

The application auto-processes the schema after upload. Loading fails because the injected schema is invalid, and an XML error is shown.

XML Error From the original on infosecwriteups.com

Despite the error, the injected XML entity was still processed, and the server's backend performed an HTTP request. That confirmed the SSRF.

SSRF successful From the original on infosecwriteups.com

Step 2: Referencing an external DTD

To get data out, the author built a schema from the template containing a payload that declares an entity loading an external XML DTD. An external DTD lets the application reference a separate file that defines the XML structure, which is the avenue used here. The evil.dtd file was prepared on a remote host.

The schema, again a template with the payload appended, was uploaded.

Referencing an external DTD From the original on infosecwriteups.com

The upload succeeds but the loading process fails with an error as usual.

From the original on infosecwriteups.com

Even so, the external DTD (evil.dtd) is still loaded and processed.

From the original on infosecwriteups.com

Step 3: Turning the exception into an output channel

The payload in the DTD triggers a server exception by referencing a non-existent file. The file name is built by combining the nonexistent/ path with a string that represents the listing of the C:/ drive. The unhandled exception text therefore looks like this:

Text
Error, non-existing file at: file:///nonexistent/(C:/)LISTING.

That text appears in the error log, which the author could display.

Listing of C:/ From the original on infosecwriteups.com

Step 4: Reading a file

The same technique works for file contents, not just directory listings. By manipulating the content of the DTD file, an attacker can target specific files within the system.

From the original on infosecwriteups.com
File read successful

Root cause

The application parses user-uploaded schema files as XML with external entity resolution enabled, so an entity declared in the uploaded schema, or in an external DTD it references, is resolved by the parser. The parser processes the entity even when the surrounding document is invalid and the load fails, which is why the SSRF and the DTD load both happened despite the XML error.

The exfiltration channel is a second design mistake: the parser's exception text, which includes attacker-influenced strings such as the constructed file path, is written to a log that the application then displays. The report does not describe any entity-resolution hardening or output encoding on that log.

Impact

An attacker who can upload a schema file can make the server issue HTTP requests to destinations of their choosing (SSRF), and can read files and list directories on the target machine, with the results surfacing in the error log. The technique depends on the schema template constraint and on the log being displayable; the author notes that a DTD with multiple instructions was impractical because parsing failed after the first instruction.

Fix

The measures it implies are to disable external entity and external DTD resolution in the XML parser used for uploaded schemas, and to stop writing raw parser exception text, including attacker-controlled paths, into a log that users can display.

Variants to test

  1. Upload any XML-parsed document (schema, SVG, SOAP envelope, Office file) and declare an entity pointing at an external DTD you host. A positive result is an inbound request to your host from the target.
  2. Use the same external DTD to reference a non-existent path built from a directory listing or file content, and check whether the exception text appears in any log, error page or response. A positive result is the constructed string echoed back.
  3. Try the technique where the document must match a template: append the entity declaration after the template content and see whether the parser still resolves it despite a validation error.
  4. Test whether the parser resolves entities even when the document fails to load, by combining a deliberately invalid schema with an entity that triggers an out-of-band request.
  5. Look for other places where parser or framework exception messages containing user input are persisted and later displayed, since the same channel can carry file contents or listings.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 11 min read.

What happened, in plain words

A developer was testing a project. That project let people upload a file called a schema, and the application read that file as XML. XML is a way of writing data using tags, a bit like a very strict form of HTML. The application also kept a log, which is a running diary of what the program did and what went wrong, and that log could be shown on screen.

Those two things together, a file the user controls being read as XML, and an error diary the user can read, turned out to be enough to make the server do things it should never do. The author could make the server send a request to a destination of their choosing, and later could make the server list folders and read files on its own disk, with the results appearing in the error log.

The unusual part is the way the results came out. Normally, when you read a file through this kind of bug, you need some way to see the file's contents. Here there was no direct output, only a generic error message. The author found that the error message written to the log could be made to contain the data they wanted, so the log became the output channel.

From the original on infosecwriteups.com

What XXE is, from zero

Every time you type something into a website and press a button, the website sends your input to a program on a server. That program has to understand what you sent. If you send XML, the program uses a piece of software called an XML parser. The parser's job is to read the tags and turn them into something the program can work with, the same way a person reads a form and writes the answers into the right boxes.

XML has a feature called entities. An entity is a shortcut, a name that stands for some text. You declare it once and then use it wherever you like. For example, you can declare an entity called company that stands for the text Acme Ltd, and then write &company; in the document instead of typing the name every time. That is convenient and harmless.

The problem is that entities can also point outside the document. Instead of standing for a piece of text, an entity can say "go and fetch this file from the server's disk" or "go and make a request to this web address". When the parser is configured to allow that, an attacker who can send XML to the application can make the server fetch things on their behalf. That is XML External Entity injection, usually shortened to XXE.

XXE From the original on infosecwriteups.com

XXE

The consequences depend on what the parser is allowed to reach. It can read local files, it can make the server send requests to other machines, and in the worst case it can lead to running code on the server. The everyday comparison is a helpful assistant who is told to fetch a document. If you can slip an extra instruction into the request, the assistant will fetch whatever you name, including things in the boss's private filing cabinet.

The issue: the first foothold

The author was testing a project that accepted uploads of schema files containing XML markup. A schema file is a description of what a document should look like: which tags are allowed, in what order, with what kinds of values. Applications use schemas to check that incoming data is well formed before they trust it. Because the schema itself is written in XML, the application has to parse it, and that is where the danger starts.

The first thing the author tried was to put an entity declaration into the uploaded schema. The goal was not yet to read a file. The goal was to see whether the parser would resolve the entity at all, and whether the server would make an outbound request when it did. That is Server-Side Request Forgery, or SSRF: the attacker tricks the server into making a request the attacker chooses, to a place the attacker cannot reach directly, such as an internal service or a machine the attacker controls.

The everyday comparison for SSRF is asking a colleague who has building access to go and knock on a door for you. You cannot get past reception, but your colleague can, and you learn from what they report back.

The author created a schema file from a template and appended the payload to it. The report does not reproduce the payload text itself, so there is nothing to copy here. The reason for starting from a template is that the application expected the schema to follow a particular shape. A file that ignored the template would be rejected before the interesting part was ever reached.

Uploading the evil schema file From the original on infosecwriteups.com

Uploading the evil schema file

The application processes the schema automatically after upload. That means the parser runs as soon as the file arrives, without anyone having to click a second button. The load then fails, because the injected schema is not valid, and an XML error is shown.

XML Error From the original on infosecwriteups.com

XML Error

Here is the important observation. Even though the document was invalid and the load failed, the injected entity was still processed. The server's backend made an HTTP request. That confirmed the SSRF. The parser had done the dangerous part before it gave up on the rest of the document.

SSRF successful From the original on infosecwriteups.com

SSRF successful

At this point the author could make the server send requests, but could not read files. There was no direct output, only a generic error message. The schema also had to follow a specific template, which made richer techniques impractical. In particular, a DTD with several instructions would not work, because the XML parsing error happened after the first instruction, so the later ones were never reached. A DTD, short for Document Type Definition, is the part of an XML document that declares entities and describes the structure. Think of it as the legend on a map: it tells you what the symbols mean.

Using the server logs as an output channel

The author kept looking for a way to get file contents out. The application had a logging functionality, and the logs could be displayed. That was the opening. If the parser's exception text could be made to contain the data the author wanted, that data would end up in the error log, and the author could read it there.

An exception is what a program throws when something goes wrong. In Java, an exception carries a message, and that message often includes details such as the name of a file that could not be found. If the program writes that message to a log, and the log is shown to users, then anything an attacker can put into the message becomes visible.

The plan was therefore to build a schema from the template, with a payload that declares an entity loading an external XML DTD. An external DTD is a separate file, hosted somewhere else, that the parser fetches and uses to define the XML structure. The author prepared the evil.dtd file on a remote host. The reason for using an external DTD rather than putting everything in the schema is the template constraint: the schema had to look a certain way, but the DTD it points to could contain whatever the author liked.

From the original on infosecwriteups.com

Then the author uploaded a schema file from the template, with the payload appended, just as before.

Referencing an external DTD From the original on infosecwriteups.com

Referencing an external DTD

The upload succeeds, but the loading process fails with an error as usual.

From the original on infosecwriteups.com

Even so, the external DTD, evil.dtd, is still loaded and processed. The parser fetches the remote file and acts on what it finds there, despite the error on the schema itself.

From the original on infosecwriteups.com

Turning the exception into output

The payload inside the DTD triggers a server exception by referencing a file that does not exist. The name of that file is built by combining the nonexistent/ path with a string that represents the listing of the C:/ drive. The parser tries to open the file, fails, and the exception message includes the path it tried. Because the path contains the directory listing, the listing ends up inside the error message. The unhandled exception text therefore looks like this:

Text
Error, non-existing file at: file:///nonexistent/(C:/)LISTING.

Line by line, in plain words:

  • Error, non-existing file at: is the fixed part of the message. It is the program saying that it could not find a file. The exact wording is whatever the application produced; the point is that the message includes the path.
  • file:///nonexistent/ is the start of the path the parser was told to open. The file:/// prefix means "a file on this machine's disk", as opposed to http:// which would mean a web address. The nonexistent/ folder is chosen because it is guaranteed not to exist, so the open will always fail and always produce an exception.
  • (C:/)LISTING is the part that carries the data. It stands for the listing of the C:/ drive, the main disk on a Windows machine. The listing is what the author wanted to see, and it is now part of the file name that the parser tried to open.
  • The final . is just the end of the sentence in the message.

That text appears in the error log, which the author could display.

Listing of C:/ From the original on infosecwriteups.com

Listing of C:/

Reading a file

The same technique works for file contents, not just directory listings. By manipulating the content of the DTD file, an attacker can target specific files within the system. The idea is the same: put the data you want into the name of a file that does not exist, let the parser fail, and read the resulting exception message in the log.

From the original on infosecwriteups.com
File read successful

File read successful

Why it worked

The application parses user-uploaded schema files as XML with external entity resolution enabled. That means an entity declared in the uploaded schema, or in an external DTD that the schema references, is resolved by the parser. The parser processes the entity even when the surrounding document is invalid and the load fails, which is why the SSRF and the DTD load both happened despite the XML error.

The exfiltration channel is a second design mistake. The parser's exception text, which includes attacker-influenced strings such as the constructed file path, is written to a log that the application then displays. The report does not describe any entity-resolution hardening or output encoding on that log.

What an attacker could do

Someone who can upload a schema file can make the server issue HTTP requests to destinations of their choosing, which is SSRF, and can read files and list directories on the target machine, with the results surfacing in the error log. The report does not state which files were read or what the listing contained. The technique depends on the schema template constraint and on the log being displayable. The author notes that a DTD with multiple instructions was impractical because parsing failed after the first instruction.

How to fix it

The measures it implies are to disable external entity and external DTD resolution in the XML parser used for uploaded schemas, and to stop writing raw parser exception text, including attacker-controlled paths, into a log that users can display.

Other places to try the same idea

  1. Upload any document that gets parsed as XML, such as a schema, an SVG image, a SOAP envelope or an Office file, and declare an entity pointing at an external DTD you host. A positive result is an inbound request to your host from the target.
  2. Use the same external DTD to reference a non-existent path built from a directory listing or file content, and check whether the exception text appears in any log, error page or response. A positive result is the constructed string echoed back.
  3. Try the technique where the document must match a template: append the entity declaration after the template content and see whether the parser still resolves it despite a validation error.
  4. Test whether the parser resolves entities even when the document fails to load, by combining a deliberately invalid schema with an entity that triggers an out-of-band request.
  5. Look for other places where parser or framework exception messages containing user input are persisted and later displayed, since the same channel can carry file contents or listings.

Glossary

  • XML: a way of writing structured data using tags, similar in appearance to HTML but stricter and meant for data rather than pages.
  • XML parser: the software that reads an XML document and turns it into something the program can use.
  • Entity: a named shortcut inside XML that stands for some text, or, when external entities are allowed, for something fetched from outside the document.
  • XXE (XML External Entity injection): a vulnerability where an attacker can make an XML parser resolve an entity they control, leading to file reads, server-side requests or worse.
  • DTD (Document Type Definition): the part of an XML document that declares entities and describes the allowed structure.
  • External DTD: a DTD stored in a separate file, which the parser fetches from a location given in the document.
  • Schema file: a file that describes what a document should look like, used to check incoming data.
  • SSRF (Server-Side Request Forgery): a vulnerability where an attacker tricks the server into making a request to a destination the attacker chooses.
  • HTTP request: a message a program sends to a web address to ask for something, such as a page or a file.
  • Backend: the part of an application that runs on the server and does the actual work, as opposed to the part the user sees in the browser.
  • Exception: what a program throws when something goes wrong, usually carrying a message that describes the problem.
  • Log: a running diary that a program writes, recording what it did and what went wrong.
  • Error log: the part of the log that records errors and exceptions.
  • Payload: the piece of input an attacker crafts to trigger a vulnerability.
  • Template: a fixed starting shape that a file must follow to be accepted.
  • Directory listing: the list of files and folders inside a folder on a disk.
  • Exfiltration: getting data out of a system, especially data the attacker should not be able to see.
  • Output encoding: transforming data before displaying it so that it cannot be interpreted as instructions by whatever displays it.
  • Entity resolution: the parser's act of looking up an entity and substituting what it stands for.
  • Out-of-band request: a request the target makes to a server the attacker controls, used to confirm that something happened even when no direct response is visible.

More by Serj Novoselov

View all

3 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

XML external entities (XXE)

The lesson behind this report.

An XML parser resolves entities the attacker defines, reading local files or making the server send requests.