> For the complete documentation index, see [llms.txt](https://notes.anggipradana.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.anggipradana.com/tutorial/dvwa/cross-site-scripting-xss/dom/medium.md).

# Medium

Di bawah ini adalah *source-code* dari XSS (DOM) level medium di DVWA.

```php
vulnerabilities/xss_d/source/medium.php
<?php
​
// Is there any input?
if ( array_key_exists( "default", $_GET ) && !is_null ($_GET[ 'default' ]) ) {
    $default = $_GET['default'];
    
    # Do not allow script tags
    if (stripos ($default, "<script") !== false) {
        header ("location: ?default=English");
        exit;
    }
}
​
?> 
```

## Mencari Informasi <a href="#mencari-informasi" id="mencari-informasi"></a>

Secara visual tidak ada perbedaan pada level ini dan sebelumnya. Jika kita mengubah nilai dari parameter tersebut, maka akan tampil seperti sebelumnya.

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-LzjWm7ooXqMX6HDi6yo%2F-LzjlW-P9uZk1AmQOAu_%2Fimage.png?alt=media\&token=9cc55612-b6a3-4a72-a5e1-853aad57ae6b)

Perhatikan pada *source code* baris ke-8, bahwa terdapat validasi jika mengandung kata *"\<script"* pada nilai parameter **default**, maka akan di *redirect* ke parameter **?default=English**.

Untuk mengatasi ini kita harus mencari cara lain untuk menjalankan JavaScript tanpa menggunakan tag `<script>`.

Terdapat cara yaitu dengan fungsi `onload` pada tag HTML. Tetapi untuk melakukan hal tersebut kita harus tutup terlebih dahulu tag sebelumnya (di sini `<select>`) agar script yang kita *inject* bisa berjalan.

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-LzgbM1FuyQgmjmyzl0g%2F-LzgpR4MRe3ZjaLeQXJI%2Fimage.png?alt=media\&token=33085df4-29e0-4b72-a14a-e78cd1930830)

## Melakukan Serangan <a href="#melakukan-serangan" id="melakukan-serangan"></a>

*Payload* yang kita akan dibuat sesuai dengan hasil *recon* di atas. Sekarang kita akan menutup tag **\<select>** terlebih dahulu dan dilanjutkan oleh *script* yang menjalankan JavaScript seperti berikut:

```markup
?default=</select><body onload="alert('Hacked')">
```

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-LzjWm7ooXqMX6HDi6yo%2F-Lzjsmo62RGAViZiJ7Z-%2Fimage.png?alt=media\&token=db3a933a-6cd4-4ac6-9add-d5d0252bde27)

Perhatikan dari hasil *injetion* di atas, tag **\<select>** tertutup terlebih dahulu sebelum tag **\<option>** dan tag `<body>` memanggil fungsi JavaScript.

Kita juga bisa mendapatkan *cookie* seperti biasa:

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-Lzjw2vnbgz2EXMb1Jkv%2F-Lzjwu_WNvzQ8UYic-6t%2Fimage.png?alt=media\&token=cadd3645-fceb-4ee5-a716-2dfb1280f36b)

Untuk melakukan serangan XSS, peretas harus paham tentang pola dari HTML website tersebut.

Mudah bukan? Happy Hacking!😁


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://notes.anggipradana.com/tutorial/dvwa/cross-site-scripting-xss/dom/medium.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
