1. 程式人生 > 其它 >DVWA靶場通關----Command Injection教程

DVWA靶場通關----Command Injection教程

Command Injection(命令注入)

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

Command Injection主題:

Low

原始碼解析

<?php

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

    // 確定作業系統並執行ping命令
    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,不管127.0.0.1是否執行成功都會執行ipconfig
127.0.0.1 && ipconfig    先執行127.0.0.1,127.0.0.1執行成功後才會執行ipconfig
127.0.0.1 | ipconfig    不管127.0.0.1執行是否成功都會執行ipconfig
127.0.0 || ipconfig      前面的命令要執行失敗,才可以執行後面的命令

在這我們直接使用第一個 127.0.0.1 & ipconfig 就好了:

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

漏洞復現

  從原始碼中可以看出,相比於Low難度,增加了黑名單,將 "&&",";" 做了限制,將其改成空格,但是別的沒有什麼改變,在這裡依舊可以通過 127.0.0.1 & ipconfig 來繞過

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

?>

漏洞復現

  看到程式碼,發現黑名單中的限制更多了,像 '&','| ','||',';','$' 等許多都加了限制,但是要仔細觀察 ,比如說這個 '| ' ,它是在管道符後面加了個空格,因此考慮使用 127.0.0.1 |ipconfig 來繞過

Impossible

原始碼解析

<?php

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

    // Get input
    $target = $_REQUEST[ 'ip' ];
  // stripslashes函式會剝離字串中的反斜槓,然後返回剝離完反斜槓的字串
    $target = stripslashes( $target );

    // Split the IP into 4 octects,以.作為分隔符,分割$target
    $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.入過都是數字型別的話,就將2他們再合併成$torget
        $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();

?>

  像這種是白名單限制,相比於黑名單來說,還是比較安全的,對於這個難度的程式碼,兄弟們學習一下怎麼寫的吧。