1. 程式人生 > 實用技巧 >DVWA大通關(二、命令執行)

DVWA大通關(二、命令執行)

命令執行介紹

Command Injection,即命令注入,是指通過提交惡意構造的引數破壞命令語句結構,從而達到執行惡意命令的目的。

命令執行(low)

分析以下原始碼,其中Shell_exec函式是導致命令執行漏洞的原因,作用是讓php執行作業系統的命令。程式碼中做了一個基於windows還是linux的判斷,
windows和linux的ping有所不同,windows預設發4個包,linux一直髮包

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

?> 
  通過執行以下命令,達到命令執行目的
  127.0.0.1 || ipconfig           或,前面執行失敗才執行後面的
  127.0.0.1 & ipconfig            And,前面執行後接著執行後面的
  127.0.0.1 && ipconfig           與,前面執行成功才執行後面的
  127.0.0.1 | ipconfig            管道符,將前面的輸出結果作為後面的輸出內容
  127.0.0.1 ; ipconfig            分號,此命令在Linux下才生效,同時執行多條命令
  127.0.0.1 & echo test>c:\1.txt  還可以使用重定向(>)在伺服器中生成檔案,或是使用(<)將準備好的檔案讀入

將以上幾條命令製作成字典進行測試

命令執行(medium)

$substitutions = array(
        '&&' => '',
        ';'  => '',
    );

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

定義了一個黑名單,其中包含陣列array中的兩個鍵&&和;,target變量表示將&&和;替換成空,其實就是把&&和;過濾了,但是還可以通過其他符合,比如&繞過黑名單執行

  解決亂碼問題:在DVWA-master\dvwa\includes目錄下找到dvwaPage.inc.php檔案中所有的”charset=utf-8”,修改”charset=gb2312”,即可。

命令執行(high)

high級別相比於medium級別對符號進行了增加,但是還是有符號沒有過濾,如|(這裡是|後面不加空格)

    $target = trim($_REQUEST[ 'ip' ]);

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

命令執行(impossible)

增加了$octet = explode( ".", $target );通過explode函式以"."為分隔符將$target變數中的IP地址進行分割,分割後會得到一個數組,並複製給變數$octet
定義一個白名單,只允許輸入IP。if判斷語句,判斷陣列中的元素是否為數字,並且判斷是否為4個元素,否則不執行。

<?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();

?> 

常見防範措施

1、對傳入的命令進行嚴格過濾
2、在後臺對應用的許可權進行控制
3、EscapeShellCmd()函式,把字串中所有可能瞞過shell而去執行另一個命令的字元進行轉義
4、EscapeShellArg()函式,在給定的字串兩邊加上單引號,並把字串中的單引號轉義,這樣這個字元就可以安全地作為命令的引數