DVWA靶場通關----(2) 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();

?>

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM