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

# Medium

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

```php
vulnerabilities/exec/source/medium.php
<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $target = $_REQUEST[ 'ip' ];

    // Set blacklist
    $substitutions = array(
        '&&' => '',
        ';'  => '',
    );

    // Remove any of the charactars in the array (blacklist).
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target );

    // Determine OS and execute the ping command.
    if( stristr( php_uname( 's' ), 'Windows NT' ) ) {
        // Windows
        $cmd = shell_exec( 'ping  ' . $target );
    }
    else {
        // *nix
        $cmd = shell_exec( 'ping  -c 4 ' . $target );
    }

    // Feedback for the end user
    echo "<pre>{$cmd}</pre>";
}

?>
```

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

Terlihat dibaris ke-7 sampai 11, terdapat *blacklist* untuk string `&&` dan `;` pada inputan. Tetapi seperti yang telah di bahas sebelumnya terdapat alternatif lain yaitu `|`. 😁

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

Cukup ganti inputan menjadi menggunakan tanda `|`, dan hasilnya akan seperti berikut:

{% hint style="info" %}
Jika menggunakan `|` maka hasil dari perintah **ping** tidak akan muncul. Karena pipeline akan melakukan perintah terakhir apabila perintah-perintah sebelumnya tidak terkait atau tidak dapat menjadi input untuk perintah berikutnya.
{% endhint %}
