> 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/stored/medium.md).

# Medium

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

```php
vulnerabilities/xss_s/source/medium.php
<?php
​
if( isset( $_POST[ 'btnSign' ] ) ) {
    // Get input
    $message = trim( $_POST[ 'mtxMessage' ] );
    $name    = trim( $_POST[ 'txtName' ] );
​
    // Sanitize message input
    $message = strip_tags( addslashes( $message ) );
    $message = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $message ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
    $message = htmlspecialchars( $message );
​
    // Sanitize name input
    $name = str_replace( '<script>', '', $name );
    $name = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $name ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));
​
    // Update database
    $query  = "INSERT INTO guestbook ( comment, name ) VALUES ( '$message', '$name' );";
    $result = mysqli_query($GLOBALS["___mysqli_ston"],  $query ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );
​
    //mysql_close();
}
​
?> 
```

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

Setelah saya coba-coba dan melihat *source code* pada form **Name**, tag `<script>` tidak diizinkan. Sedangkan pada form **Message** awalan tag (yang mengandung `<`) juga tidak diizinkan.

Sepertinya celah XSS terdapat pada form **Name**, karena kita bisa saja memanggil JavaScript tanpa menggunakan tag `<script>`. Tetapi maksimal inputan pada form **Name** ini hanya 10 karakter. Untuk mengatasi hal tersebut kita bisa merubah nilai `maxlength` nya dengan menggunakan *inspect element*.

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-LzpgZF_KC4sPanQSj0r%2F-Lzql1vtMds7GuY9caIp%2Fimage.png?alt=media\&token=03e918db-4092-4789-9fa2-9293ed3daf14)

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-M-nX-buHPf2O5F5U5ij%2F-M-nkCydHNzRZPC9OqVV%2Fimage.png?alt=media\&token=d5aad2bf-894c-4c47-875a-3f0ae6c7ab94)

Sekarang kita bisa menyisipkan XSS pada form **Name**.

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

**Pertama-tama**, kita buat terlebih dahulu agar form **Name**-nya bisa diinputkan oleh banyak karakter.

**Kedua**, kita inputkan *payload* yang tidak menggunakan tag `<script>`, seperti berikut:

```markup
<body onload="alert(document.cookie)">
```

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-LzpgZF_KC4sPanQSj0r%2F-LzqmjPFbkcIGYNvhEDU%2Fimage.png?alt=media\&token=f68b3485-662c-4fe3-998e-e8d3d0f13057)

Dan **yup!** Kita berhasil menyelesaikan **Stored XSS** pada level medium ini.

Tetap Semangat! 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/stored/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.
