往期博文:
DVWA靶場-Brute Force Source 暴力破解
靶場環境搭建
目錄
Command Injection 命令注入
Low Command Injection
核心代碼
<?php
if( isset( $_POST[ 'Submit' ] ) ) { // get 方式獲取ip $target = $_REQUEST[ 'ip' ];
// 判斷操作系統來指定相應的ping 命令 if( stristr( php_uname( 's' ), 'Windows NT' ) ) { // Windows 默認ping 4次 $cmd = shell_exec( 'ping ' . $target ); } else { // *nix 手動指定ping 次數 $cmd = shell_exec( 'ping -c 4 ' . $target ); }
//輸出命令執行的結果 echo "<pre>{$cmd}</pre>"; }
?> |
這裏直接將target 變量放入 shell_exec()執行,不進行任何過濾,用戶端可以直接加入特定的命令,來執行並獲取想要的信息。
我們可以以下命令來拼接輸入的命令
A;B |
A不論正確與否都會執行B |
A&B |
A後臺運行,A和B同時執行 |
A&&B |
A執行成功後纔會執行B |
A|B |
A執行的輸出結果作爲B命令的參數,A不論正確與否,都會執行B |
A||B |
A執行失敗後纔會執行B命令 |
我們可以
127.0.0.1 ; ipconfig
127.0.0.1 & ipconfig
127.0.0.1 && ipconfig
127.0.0.1 | ipconfig
111 || ipconfig
Medium Command Injection
核心代碼
<?php ··· // 設置黑名單 $substitutions = array( '&&' => '', ';' => '', ); // '' 替換黑名單中存在的字符 $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
··· ?> |
|
中級難度設置了黑名單過濾規則,但是他只過濾了兩種字符'&&' ';',前面我們列舉了5種,這三種還是可以繞過的
127.0.0.1 & ipconfig
127.0.0.1 | ipconfig
111 || ipconfig
High Command Injection
核心代碼
<?php ···
// 設置黑名單 $substitutions = array( '&' => '', ';' => '', '| ' => '', '-' => '', '$' => '', '(' => '', ')' => '', '`' => '', '||' => '', );
//替換 $target = str_replace( array_keys( $substitutions ), $substitutions, $target );
··· ?> |
看上去,似乎敏感的字符都被過濾了,但是| 明顯後面有個空格,所以如果我們不使用空格的話依然可以繞過
127.0.0.1 |ipconfig
Impossible Command Injection
核心代碼
<?php ···
// 以 . 作爲分割符 分割target爲4個字段 $octet = explode( ".", $target );
// 檢測分割後的4個字段,是否都爲數字型 if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) { //如果4個字段都是數字型,還原target 變量 $target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3]; } else { // 提示 輸入爲無效 echo '<pre>ERROR: You have entered an invalid IP.</pre>'; }
··· ?> |
這種類似於白名單的過濾方式,相較於黑名單過濾更加徹底。
參考文章: