1. 程式人生 > 實用技巧 >DVWA靶場——Command Injection(命令注入)

DVWA靶場——Command Injection(命令注入)

概述

Command Injection,即命令注入,是指通過提交惡意構造的引數破壞命令語句結構,從而達到執行惡意命令的目的。PHP命令注入攻擊漏洞是PHP應用程式中常見的指令碼漏洞之一,國內著名的Web應用程式Discuz!、DedeCMS等都曾經存在過該型別漏洞。

命令執行分隔符:(注:關於分隔符大家可以到網上搜索它們的用法)

  Windows:&&  ||  &  

  Linux:  &&  ||  &  ;

  |:  可作為管道符pipe用於命令執行,其實根本是pipe的作用

  &&:  從前往後按順序執行,遇到出錯的命令,後面的不執行

  
||:  從前往後按順序執行,遇到成功的命令,後面的不執行   &:  同時執行所有命令

low級別

原始碼:

<?php

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

    // 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>"; } ?>

可以看到原始碼中,通過stristrphp_uname兩個函式來判斷作業系統,執行不同的ping命令。但並沒有對ip引數做任何的過濾等安全措施,導致了嚴重的命令注入漏洞!

由於它並沒有做任何的安全措施,所以我們可以利用任意的命令執行分隔符來獲取我們想要的資訊。

www.baidu.com&&pwd

輸入payload後,可以看到不僅獲取到了百度ping後的資訊,還獲取了靶場所在的目錄,可見危害之大。

Medium級別

原始碼:

<?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>";
}

?> 

在原始碼中,可以看到它已經開始設定黑名單限制了,過濾了"&&"";"字元,但也只是過濾了這兩個,我們可以通過更換別的命令執行符號來執行獲取資訊,比如:“&”。

127.0.0.1&pwd

在原始碼中,它把"&&"";"字元過濾為空字元,我們可以利用它在這裡的漏洞來繞過執行payload

127.0.0.1&;&pwd

因為";"會轉換為空字元,所以payload就會變為127.0.0.1& &pwd,這就會繞過它的限制,變成為一個可執行的payload。

High級別

原始碼:

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $target = trim($_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>";
}

?> 

可以看到原始碼中,high級別相比於medium級別的黑名單更加完善了,但這黑名單還是有一定的侷限性,我們依然可以繞過。

注意看,原始碼中的'| '字元它裡面的|後面是跟一個空字元的,而不是'|'字元(拿'| ''|'比較一下就能看出了),這也就成了整個黑名單中的”漏網之魚“,我們可以利用這個漏洞來進行繞過。

127.0.0.1|pwd

Impossible級別

原始碼:

<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Check Anti-CSRF token
    checkToken( $_REQUEST[ 'user_token' ], $_SESSION[ 'session_token' ], 'index.php' );

    // Get input
    $target = $_REQUEST[ 'ip' ];
    $target = stripslashes( $target );

    // Split the IP into 4 octects
    $octet = explode( ".", $target );

    // Check IF each octet is an integer
    if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
        // If all 4 octets are int's put the IP back together.
        $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];

        // 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>";
    }
    else {
        // Ops. Let the user name theres a mistake
        echo '<pre>ERROR: You have entered an invalid IP.</pre>';
    }
}

// Generate Anti-CSRF token
generateSessionToken();

?> 

Impossible級別黑名單完善的更加徹底,已經類似於白名單了,使用了stripslashesexplodeis_numeric函式,還加入了Anti-CSRF token進行了嚴格的限制。