1. 程式人生 > >DVWA-對Command Injection(命令注入)的簡單演示與分析

DVWA-對Command Injection(命令注入)的簡單演示與分析

  1. 前言

上一篇文章中,對命令注入進行了簡單的分析,有興趣的可以去看一看,文章地址 https://www.cnblogs.com/lxfweb/p/12828754.html,今天這篇文章以DVWA的Command Injection(命令注入)模組為例進行演示與分析,本地搭建DVWA程式可以看這篇文章 https://www.cnblogs.com/lxfweb/p/12678463.html,通過對DVWA不同等級的程式碼分析,看看它是如何做的防禦。

漏洞利用與分析

low級別(低級別)

首先檢視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>";
}

?> 

 可以發現上面的程式碼,用了stristr(),php_uname(),函式,這是用來判斷當前的系統是否是Windows,因為Windows和Linux下的ping命令執行引數是不同的。接下來是用shell_exec函式來執行ping命令,並將結果輸出。我們發現low級別的程式碼,對使用者的輸入沒有做任何的過濾。存在很大的安全隱患。例如使用管道符“|”檢視當前埠,輸入下列內容

127.0.0.1|netstat -ano

結果如下圖

 

 

 這裡不止可以使用“|”,在DOS下允許同時執行多條命令的符號主要有以下幾個

  1. & 連線符  執行完第一個命令,執行第二個命令
  2. && 只有上一個執行成功,才執行下一個命令
  3. |  管道符  讓前一命令的輸出當做後一命令的輸入,就是說前面命令的結果當做一個引數傳遞給後面命令處理 
  4. ||    只有上一個執行失敗,才執行下一個命令

可以用連線符直接接net user zhangsan 123/add 建立使用者 接著連線提權命令 net localgroup administrators zhangsan /add 拿下整個伺服器

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

?> 

我們發現,中級的程式碼,對引數做了一點過濾,把&&和;刪除,相當於黑名單的形式,在Linux中;也可以起連線作用,依次執行多個命令。我們可以嘗試|  ||  & &;& ,這裡以||舉例,||前面報錯,後面執行咱們構造的命令,結果如下圖

 

 

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

?> 

 

高級別的程式碼對黑名單進行了進一步完善,好像過濾了所有危險字元,仔細觀察黑名單裡“|”管道符,後面有一個空格“| ” 這樣可以嘗試“ |” 發現成功繞過,結果如下圖

 

 

 Impossible(無漏洞)

檢視核心程式碼

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級別的程式碼加入了Anti-CSRF token,並且採用白名單的方式,對引數ip進行了嚴格的限制,只接受X.X.X.X(X只能為數字),因此不存在命令執行漏洞。

&n