# WEB06: XXE ![](https://i.imgur.com/gk8E8JQ.png) ## Lab: Exploiting XXE using external entities to retrieve files ```! This lab has a "Check stock" feature that parses XML input and returns any unexpected values in the response. To solve the lab, inject an XML external entity to retrieve the contents of the /etc/passwd file. ``` Access vào bài lab, ở chức năng `Check stock` ![](https://i.imgur.com/rYYw1op.png) Một POST request được gửi lên server với nội dung định dạng xml ![](https://i.imgur.com/WGW6eZO.png) Ta khai báo mới một external entity `xxe` trỏ đến nội dung file passwd và đọc được nội dung của file này ![](https://i.imgur.com/7DAxfHQ.png) ## Lab: Exploiting XXE to perform SSRF attacks ```! This lab has a "Check stock" feature that parses XML input and returns any unexpected values in the response. The lab server is running a (simulated) EC2 metadata endpoint at the default URL, which is http://169.254.169.254/. This endpoint can be used to retrieve data about the instance, some of which might be sensitive. To solve the lab, exploit the XXE vulnerability to perform an SSRF attack that obtains the server's IAM secret access key from the EC2 metadata endpoint. ``` Gửi GET request đến server chạy EC2 metadata endpoint và đọc response thông qua XXE ![](https://i.imgur.com/686lUqN.png) Tiếp tục gửi đến endpoint `latest` của server này, ta được một endpoint khác là `meta-data` ![](https://i.imgur.com/6DqMaq8.png) Dựa theo link [này](https://github.com/aws/amazon-ec2-metadata-mock#making-a-request), ta tiếp tục SSRF để tìm đến endpoint chứa IAM secret access key ![](https://i.imgur.com/G0aMZkK.png) -> Solve bài lab ## Lab: Blind XXE with out-of-band interaction ```! This lab has a "Check stock" feature that parses XML input but does not display the result. You can detect the blind XXE vulnerability by triggering out-of-band interactions with an external domain. To solve the lab, use an external entity to make the XML parser issue a DNS lookup and HTTP request to Burp Collaborator. ``` Ở bài lab này yêu cầu detect XXE bằng kĩ thuật OOB nên ta sẽ tạo một burp collaborator server và solve bài lab với payload như sau ![](https://i.imgur.com/pjPFXpM.png) ## Lab: Blind XXE with out-of-band interaction via XML parameter entities ```! This lab has a "Check stock" feature that parses XML input, but does not display any unexpected values, and blocks requests containing regular external entities. To solve the lab, use a parameter entity to make the XML parser issue a DNS lookup and HTTP request to Burp Collaborator. ``` Bài này khác so với các bài trước ở chỗ đó là server đã chặn và k parse external entity ![](https://i.imgur.com/vJITpVk.png) Nhưng ta có thể bypass bằng cách sử dụng parameter entity và gửi một DNS lookup query đến burp collab client ![](https://i.imgur.com/MeTMpsg.png) -> Thành công solve bài lab ![](https://i.imgur.com/y3pNJCG.png) ## Lab: Exploiting blind XXE to exfiltrate data using a malicious external DTD ```! This lab has a "Check stock" feature that parses XML input but does not display the result. To solve the lab, exfiltrate the contents of the /etc/hostname file. ``` Ở bài lab này, ta sẽ cần đọc nội dung file `/etc/hostname` và gửi đến exploit server. Lí thuyết sẽ như sau * Defines an XML parameter entity called `file`, containing the contents of the `/etc/hostname` file. * Defines an XML parameter entity called `eval`, containing a dynamic declaration of another XML parameter entity called `exfiltrate`. The `exfiltrate` entity will be evaluated by making an HTTP request to the attacker's web server containing the value of the file entity within the URL query string. * Uses the `eval` entity, which causes the dynamic declaration of the `exfiltrate` entity to be performed. * Uses the exfiltrate `entity`, so that its value is evaluated by requesting the specified URL. Setup up exploit server ![](https://i.imgur.com/udfXUXY.png) Gửi payload trigger XXE để vulnerable server fetch nội dung của file exploit.dtd và thực hiện parse file này ![](https://i.imgur.com/YieAJ0F.png) Kết quả ![](https://i.imgur.com/amagZ3W.png) Submit solution và solve bài lab ![](https://i.imgur.com/9ivjePP.png) ## Lab: Exploiting blind XXE to retrieve data via error messages ```! This lab has a "Check stock" feature that parses XML input but does not display the result. To solve the lab, use an external DTD to trigger an error message that displays the contents of the /etc/passwd file. The lab contains a link to an exploit server on a different domain where you can host your malicious DTD. ``` Bài lab này, ta có sử dụng kĩ thuật đọc nội dung của một file bất kì thông qua error message, lí thuyết như sau: * Defines an XML parameter entity called `file`, containing the contents of the `/etc/passwd` file. * Defines an XML parameter entity called `eval`, containing a dynamic declaration of another XML parameter entity called `error`. The `error` entity will be evaluated by loading a nonexistent file whose name contains the value of the `file` entity. * Uses the `eval` entity, which causes the dynamic declaration of the `error` entity to be performed. * Uses the `error` entity, so that its value is evaluated by attempting to load the nonexistent file, resulting in an error message containing the name of the nonexistent file, which is the contents of the `/etc/passwd` file. Setup exploit server ![](https://i.imgur.com/fskGWil.png) Gửi payload trigger XXE và kết quả: ![](https://i.imgur.com/MXEy2X9.png) ## Lab: Exploiting XXE to retrieve data by repurposing a local DTD ```! This lab has a "Check stock" feature that parses XML input but does not display the result. To solve the lab, trigger an error message containing the contents of the /etc/passwd file. You'll need to reference an existing DTD file on the server and redefine an entity from it. ``` Lợi dụng load một local dtd file và sau đó định nghĩa lại một entity trong file đó, sau đó sử dụng kĩ thuật error message như lab trước để đọc content của một file bất kì. Lí thuyết cụ thể: * Defines an XML parameter entity called `local_dtd`, containing the contents of the external DTD file that exists on the server filesystem. * Redefines the XML parameter entity called `custom_entity`, which is already defined in the external DTD file. The entity is redefined as containing the error-based XXE exploit that was already described, for triggering an error message containing the contents of the /etc/passwd file. * Uses the `local_dtd` entity, so that the external DTD is interpreted, including the redefined value of the custom_entity entity. This results in the desired error message. ![](https://i.imgur.com/95dk72y.png) ## Lab: Exploiting XInclude to retrieve files ```! This lab has a "Check stock" feature that embeds the user input inside a server-side XML document that is subsequently parsed. Because you don't control the entire XML document you can't define a DTD to launch a classic XXE attack. To solve the lab, inject an XInclude statement to retrieve the contents of the /etc/passwd file. ``` Ở bài này, server lấy input từ các trường productId và stockId để đưa vào một file XML và thực hiện parse file này. Ta không thể áp dụng kĩ thuật tấn công XXE bởi vì trong TH này không thể kiểm soát hoàn toàn nội dung của file XML. Vì thế có thể sử dụng một kĩ thuật khác để đọc nội dung file đó là XInclude, include một sub-document là file /etc/passwd: ![](https://i.imgur.com/35wknh8.png) ## Lab: Exploiting XXE via image file upload ```! This lab lets users attach avatars to comments and uses the Apache Batik library to process avatar image files. To solve the lab, upload an image that displays the contents of the /etc/hostname file after processing. Then use the "Submit solution" button to submit the value of the server hostname. ``` XXE thông qua file upload, ở bài này server cho phép chúng ta chức năng upload avatar khi post comment, vì vậy ta có thể lợi dụng format của file svg để khai thác XXE. Tạo một file `exploit.svg` với content như sau: ![](https://i.imgur.com/D44yQW9.png) Sau đó upload file này ![](https://i.imgur.com/4ZQDUCb.png) Reload lại trang và một GET request để fetch file avatar trả về nội dung của `/etc/hostname` ![](https://i.imgur.com/qtuv9dq.png) Submit solution và solve bài lab ![](https://i.imgur.com/unxjYvk.png) ###### tags: `portswigger`