天天看點

DVWA之指令注入(Command Injection)指令注入(Command Injection)Security Level: lowSecurity Level: mediumSecurity Level: highSecurity Level: impossible

指令注入(Command Injection)

指令注入是一種攻擊,其目标是通過易受攻擊的應用程式在主機作業系統上執行任意指令。當應用程式将不安全的使用者提供的資料(表格,cookie,HTTP标頭等)傳遞到系統外殼時,可能會發生指令注入攻擊。在這種攻擊中,攻擊者提供的作業系統指令通常是使用易受攻擊的應用程式的特權執行的。由于沒有足夠的輸入驗證,是以可能會發生指令注入攻擊。

注:關于亂碼問題請參照小編的“DVWA之指令注入(Command Injection)亂碼問題解決”進行解決

Security Level: 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>";
}

?>
           
分析

這個源碼并沒有做任何的限制,代碼不檢查$ target是否與IP位址比對,我們可以利用這個并使用(;或者&&)進行指令組合。

進行下方的實驗
DVWA之指令注入(Command Injection)指令注入(Command Injection)Security Level: lowSecurity Level: mediumSecurity Level: highSecurity Level: impossible
DVWA之指令注入(Command Injection)指令注入(Command Injection)Security Level: lowSecurity Level: mediumSecurity Level: highSecurity Level: impossible

Security Level: 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>";
}

?>
           
分析

這個源碼對(; &&)進行了簡單的過濾,使得(; &&)不再生效。但是沒關系,我們可以使用(&)進行組合

進行下方的實驗
DVWA之指令注入(Command Injection)指令注入(Command Injection)Security Level: lowSecurity Level: mediumSecurity Level: highSecurity Level: impossible

Security Level: 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>";
}

?>

           
分析

這個源碼所有的組合操作進行了過濾,但是這個過濾存在一個錯誤,就是管道符(“|”)後面有一個空格,如果我們使用管道符後面沒有空格則不會被過濾

進行下方的實驗
DVWA之指令注入(Command Injection)指令注入(Command Injection)Security Level: lowSecurity Level: mediumSecurity Level: highSecurity Level: impossible

Security Level: 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();

?> 
           

分析

源碼已經加入了Anti-CSRF token,同時對參數ip進行了嚴格的限制,隻有諸如“數字.數字.數字.數字”的輸入才會被接收執行,是以不存在指令注入漏洞

繼續閱讀