An order file that read the server's disk
XML external entity injection, or XXE, is when an XML document makes the parser fetch a file or an address into the result. If the server parses XML from visitors, they can read its files.
A wholesaler takes orders as XML files. One order defines a short word that means "the contents of /etc/passwd". The confirmation page prints the order back, with the password file inside it.
The one rule
Warning
This lab is a website that is insecure on purpose. Attack your own private copy and nothing else; it has no way out to the internet. A real system needs the owner's written permission first. Without it, in the UK, this is a crime under the Computer Misuse Act 1990.
By the end of this lesson
Note
I can configure an XML parser so a document cannot read files or addresses.
Quick check: the rule
Have a go
Quick check: the rule
3 questionsKey words
| Word | What it means | Example |
|---|---|---|
| XML | a text format of nested tags for data | <qty>3</qty> |
| Parser | the code that reads text into data | |
| DTD | a part at the top of an XML document that defines its rules | |
| Entity | a name the parser replaces with a value | &who; |
| External entity | an entity whose value is fetched from a file or address | |
| XXE | abusing external entities to read files or reach addresses |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
3 questionsAn entity is a name filled in
XML lets a document define names in a DTD at its top. Wherever &name; appears, the parser puts the value in.
An internal entity's value is written in the document. An external entity says SYSTEM and gives a file or an address instead. A parser that obeys it reads that file and pastes it into the data your program receives.
An internal entity filled in by Python
import xml.etree.ElementTree as ET
# The DTD defines "who". Change Ana to your own name and press Run.
doc = '<!DOCTYPE order [<!ENTITY who "Ana">]><order><name>&who;</name><qty>3</qty></order>'
root = ET.fromstring(doc)
print(root.find("name").text, root.find("qty").text)
Make a free account to save your work and track your progress.
Worked example: three documents
| DTD line | What a careless parser does | Risk |
|---|---|---|
| none | Reads the tags | None |
| <!ENTITY who "Ana"> | Replaces &who; with Ana | Low, but it can grow: one entity of ten others, ten times over |
| <!ENTITY f SYSTEM "file:///etc/hostname"> | Reads the file into &f; | Files leak |
| <!ENTITY u SYSTEM "http://10.0.0.7/"> | Fetches the address | SSRF from the parser |
The cells students fill in are left blank in this preview.
Check
Have a go
Check
3 questionsSend the comment box two documents
Your lab runs WebGoat, lessons with flaws on purpose. Its XXE lesson has a photo with a comment box that sends each comment as XML. Start the lab, then make an account and sign in with the first two Runs. Then post a plain comment and read it back. Last, post one with an internal entity, a word defined in the comment itself, and read that back.
Make an account in your copy
curl -s -c goat -d "username=kimlab1&password=labpass1&matchingPassword=labpass1&agree=agree" -o /dev/null -w "%{http_code}\n" http://web/WebGoat/register.mvcShown here, not run. Press Start lab to type it into your own lab’s terminal.
Sign in and keep the session
curl -s -b goat -c goat -d "username=kimlab1&password=labpass1" -o /dev/null -w "%{http_code} %{redirect_url}\n" http://web/WebGoat/loginShown here, not run. Press Start lab to type it into your own lab’s terminal.
Post a plain comment
curl -s -b goat -H "Content-Type: application/xml" -d '<?xml version="1.0"?><comment><text>Plain hello</text></comment>' -o /dev/null -w "%{http_code}\n" http://web/WebGoat/xxe/simpleShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read your newest comment
curl -s -b goat http://web/WebGoat/xxe/comments | grep -o '"text" : "[^"]*"' | head -1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Post a comment with an internal entity
curl -s -b goat -H "Content-Type: application/xml" -d '<?xml version="1.0"?><!DOCTYPE comment [<!ENTITY who "Sam">]><comment><text>Hello from &who;</text></comment>' -o /dev/null -w "%{http_code}\n" http://web/WebGoat/xxe/simpleShown here, not run. Press Start lab to type it into your own lab’s terminal.
Read your newest comment again
curl -s -b goat http://web/WebGoat/xxe/comments | grep -o '"text" : "[^"]*"' | head -1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
What the answers tell you
Registering prints 302, and signing in prints 302 http://web/WebGoat/welcome.mvc. Each comment is accepted with 200. The first read prints "text" : "Plain hello".
The second prints "text" : "Hello from Sam". You never typed Sam in the text: the parser read your DTD and filled &who; in. A parser that does that for a quoted word will do it for SYSTEM too.
A common mistake
Warning
Many people think XXE needs the result printed back to be dangerous. It does not: a parser that fetches addresses can send a file's contents out in the address it fetches, or reach inside the network. Turn off DTDs and external entities wherever XML arrives, whether or not anything is shown.
The fix: switch DTDs off
Configure the parser to refuse DTDs, or at least external entities and entity expansion. In PHP, never pass LIBXML_NOENT; in Java, set disallow-doctype-decl; in Python, use defusedxml for anything a visitor sends.
Better still, accept JSON where you can. Where XML is needed, refuse a document with <!DOCTYPE before it reaches the parser, and keep the parser's libraries up to date.
Check
Have a go
Check
3 questionsHow to detect XXE
Log the start of every XML body a service receives and alert on <!DOCTYPE or <!ENTITY, which ordinary orders never carry. Watch what the parsing server connects to. A web server that suddenly asks for an outside address while handling an upload is a sign. Parser errors that name a local file are another.
Match each part to what it does
Have a go
Loading…
Your answers are marked here and are not kept. An account keeps your marks.
Assignment: check an XML order
Write two functions. xml_problems(text) returns a list holding "doctype" if the text contains <!DOCTYPE, then "entity" if it contains <!ENTITY, then "external" if it has an entity and also SYSTEM or PUBLIC, in any case. read_order(text) returns None for any text with a problem, or without an item or a qty tag. Otherwise it returns a dictionary with the item as text and the qty as a whole number.
import xml.etree.ElementTree as ET
def xml_problems(text):
# Return a list: "doctype", then "entity", then "external", for each one found.
return []
def read_order(text):
# Refuse (return None) any text with a problem. Otherwise return item and qty.
root = ET.fromstring(text)
return {"item": root.find("item").text, "qty": root.find("qty").text}
Make a free account to save your work and track your progress.
Exit quiz
Have a go