Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

XML external entities (XXE) and safe parsing

After this lesson: I can configure an XML parser so a document cannot read files or addresses.

55 min lesson6 commandsWebGoat lab: guided web security lessons with WebWolf in your own copy

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Key words

WordWhat it meansExample
XMLa text format of nested tags for data<qty>3</qty>
Parserthe code that reads text into data
DTDa part at the top of an XML document that defines its rules
Entitya name the parser replaces with a value&who;
External entityan entity whose value is fetched from a file or address
XXEabusing 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

An 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

Code: 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 lineWhat a careless parser doesRisk
noneReads the tagsNone
<!ENTITY who "Ana">Replaces &who; with AnaLow, 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 addressSSRF from the parser

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Send 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

Command
curl -s -c goat -d "username=kimlab1&password=labpass1&matchingPassword=labpass1&agree=agree" -o /dev/null -w "%{http_code}\n" http://web/WebGoat/register.mvc

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Sign in and keep the session

Command
curl -s -b goat -c goat -d "username=kimlab1&password=labpass1" -o /dev/null -w "%{http_code} %{redirect_url}\n" http://web/WebGoat/login

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Post a plain comment

Command
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/simple

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read your newest comment

Command
curl -s -b goat http://web/WebGoat/xxe/comments | grep -o '"text" : "[^"]*"' | head -1

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Post a comment with an internal entity

Command
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/simple

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read your newest comment again

Command
curl -s -b goat http://web/WebGoat/xxe/comments | grep -o '"text" : "[^"]*"' | head -1

Shown 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 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

How 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.

Code: python
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

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.