天天看點

Command Injection(指令注入)Command Injection(指令注入)

Command Injection(指令注入)

Command Injection,即指令注入,是指攻擊者可以能夠控制作業系統上的一些指令。和RCE不同,指令注入執行的是一個指令,而RCE是執行代碼。

Command Injection(指令注入)Command Injection(指令注入)

LOW

Command Injection Source
vulnerabilities/exec/source/low.php
<?php

if( isset( $_POST[ 'Submit' ]  ) ) {
    // Get input
    $target = $_REQUEST[ 'ip' ];
//stristr() 函數搜尋字元串在另一字元串中的第一次出現。
//php_uname( 's' ) 擷取系統類型
    // 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>";
}

?>

           

伺服器隻是判斷作業系統的類型,沒有對IP這個參數進行過濾,導緻了指令注入。

php_uname(mode)

這個函數會傳回運作php的作業系統的相關描述,參數mode可取值”a” (此為預設,包含序列”s n r v m”裡的所有模式),”s ”(傳回作業系統名稱),”n”(傳回主機名),” r”(傳回版本名稱),”v”(傳回版本資訊), ”m”(傳回機器類型)。

用邏輯運算符連接配接系統指令即可。

Command Injection(指令注入)Command Injection(指令注入)

Medium

Command Injection Source
vulnerabilities/exec/source/medium.php
<?php

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

    // Set blacklist
    $substitutions = array(
        '&&' => '',
        ';'  => '',
    );
//array_keys() 函數傳回包含數組中所有鍵名的一個新數組。
//str_replace() 函數替換字元串中的一些字元(區分大小寫)。
    // 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>";
}

?>

           

代碼過濾了 && 、; 還可以利用&進行連接配接,&&和&的差別,a&&b ,a成功執行後b才執行,a&b,不論a是否成功,b都會執行。

Command Injection(指令注入)Command Injection(指令注入)

High

Command Injection Source
vulnerabilities/exec/source/high.php
<?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>";
}

?>

           

很明顯,過濾的字元更多了,但是畢竟是黑名單,|是未被過濾。代碼中過濾的是| ,後面有個空格。“|”是管道符,a|b表示将a的輸出作為b的輸入,并且隻列印b執行的結果。

Command Injection(指令注入)Command Injection(指令注入)

Impossible

Command Injection Source
vulnerabilities/exec/source/impossible.php
<?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 );
//is_numeric()檢測string是否為數字或數字字元串,如果是傳回TRUE,否則傳回FALSE。
//explode(separator,string,limit) 把字元串打散為數組,傳回字元串的數組。參數separator規定在哪裡分割字元串,參數string是要分割的字元串,可選參數limit規定所傳回的數組元素的數目。
    // 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();

?>

           

加入了token,同時把IP的4個段放進數組去比對,全數字才會被成功執行。寫死了的果然強悍!!!

繼續閱讀