# Medium

Di bawah ini adalah *source-code* dari CSRF level medium di DVWA.

```
vulnerabilities/csrf/source/medium.php<?php​if( isset( $_GET[ 'Change' ] ) ) {    // Checks to see where the request came from    if( stripos( $_SERVER[ 'HTTP_REFERER' ] ,$_SERVER[ 'SERVER_NAME' ]) !== false ) {        // Get input        $pass_new  = $_GET[ 'password_new' ];        $pass_conf = $_GET[ 'password_conf' ];​        // Do the passwords match?        if( $pass_new == $pass_conf ) {            // They do!            $pass_new = ((isset($GLOBALS["___mysqli_ston"]) && is_object($GLOBALS["___mysqli_ston"])) ? mysqli_real_escape_string($GLOBALS["___mysqli_ston"],  $pass_new ) : ((trigger_error("[MySQLConverterToo] Fix the mysql_escape_string() call! This code does not work.", E_USER_ERROR)) ? "" : ""));            $pass_new = md5( $pass_new );​            // Update the database            $insert = "UPDATE `users` SET password = '$pass_new' WHERE user = '" . dvwaCurrentUser() . "';";            $result = mysqli_query($GLOBALS["___mysqli_ston"],  $insert ) or die( '<pre>' . ((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . '</pre>' );​            // Feedback for the user            echo "<pre>Password Changed.</pre>";        }        else {            // Issue with passwords matching            echo "<pre>Passwords did not match.</pre>";        }    }    else {        // Didn't come from a trusted source        echo "<pre>That request didn't look correct.</pre>";    }​    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);}​?> 
```

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

​[`stripos()`](https://www.w3schools.com/php/func_string_stripos.asp) berfungsi untuk mencari adanya substring dalam sebuah string (*case-insensitive*). Dalam kasus ini mungkin developer bermaksud ingin membuat semua *request* berasal dari website-nya. Terlihat bahwa di baris ke-5 terdapat validasi bahwa jika `HTTP_REFERER` mengandung kata dari `SERVER_NAME` maka akan bernilai `true` dan proses akan dilanjutkan.

Jika kita menggunakan cara sebelumnya, maka proses perubahan password akan gagal karena *referer* tidak mengandung kata dari *server name/host*.

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-LzeScRXTzwCkx8H09IQ%2F-LzeXr9e32B37vJU-9Iv%2Fimage.png?alt=media\&token=277d8eca-3cf0-4a5f-8edc-30fc32ecd5a9)

Untuk mengatasi hal ini kita bisa saja merubah file **index.html** yang sebelumnya kita buat, dirubah menjadi nama host target (dalam kasus saya ini menjadi **172.17.0.2.html**).

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

Seperti dari hasil *recon*, kita akan merubah nama file-nya menjadi host dari website target. Setelah itu, kita akan membuat victim mengakses mengakses website yang telah kita buat.

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-LzeScRXTzwCkx8H09IQ%2F-LzeZspJcmK3ksW1vN8b%2Fimage.png?alt=media\&token=18385876-9718-4f22-9b82-7287b73a8c5b)

Setelah victim yang telah terautentikasi mengakses website tersebut, maka password akan berhasil diubah. Dan sekarang kita bisa menggunakan password baru yang telah ditentukan yaitu *"pwned"*.

Happy Hacking! 🍻


---

# Agent Instructions: 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:

```
GET https://notes.anggipradana.com/tutorial/dvwa/csrf/medium.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
