前言
Vulnerability: Command Injection
LOW級別
代碼:
<?php if( isset( $_POST[ 'Submit' ] ) ) { // 幾首一個變量為ip的參數 $target = $_REQUEST[ 'ip' ]; // 判斷系統 if( stristr( php_uname( 's' ), 'Windows NT' ) ) { // Windows $cmd = shell_exec( 'ping ' . $target ); } else { // *nix $cmd = shell_exec( 'ping -c 4 ' . $target ); } echo "<pre>{$cmd}</pre>"; } ?>
我們分析這個靶場的代碼可以看到$_REQUEST接受用戶傳過來的值 我們並沒有看到有什么過濾機制的代碼所以 可以輸入任何東西。
測試執行ping127.0.0.1沒問題 我們利用管道命令來 再加命令語句
Medium級別
代碼:
<?php if( isset( $_POST[ 'Submit' ] ) ) { $target = $_REQUEST[ 'ip' ]; // 黑名單過濾 $substitutions = array( '&&' => '', ';' => '', ); // 如果有黑名單字符則進行替換 $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); // 判斷系統 if( stristr( php_uname( 's' ), 'Windows NT' ) ) { // Windows $cmd = shell_exec( 'ping ' . $target ); } else { // *nix $cmd = shell_exec( 'ping -c 4 ' . $target ); } echo "<pre>{$cmd}</pre>"; } ?>
我們注意6-9行 這里是個黑名單過濾 我們可以想辦法繞過 這里雖然把&&和分號;加入了黑名單,但是我們還可以用邏輯或(||)、管道符(|)或(&)來命令執行 繞過
High級別
代碼:
<?php if( isset( $_POST[ 'Submit' ] ) ) { // Get input $target = trim($_REQUEST[ 'ip' ]); // 白名單 $substitutions = array( '&' => '', ';' => '', '| ' => '', //主義這一行 l后面是空格說明僅僅過濾了l+空格 沒過濾單獨的l '-' => '', '$' => '', '(' => '', ')' => '', '`' => '', '||' => '', ); $target = str_replace( array_keys( $substitutions ), $substitutions, $target ); if( stristr( php_uname( 's' ), 'Windows NT' ) ) { // Windows $cmd = shell_exec( 'ping ' . $target ); } else { // *nix $cmd = shell_exec( 'ping -c 4 ' . $target ); } echo "<pre>{$cmd}</pre>"; } ?>
127.0.0.1|net user照樣繞過
impossible級別
代碼:
<?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 ); //stripslashes()過濾刪除由 addslashes() 函數添加的反斜杠。 // 以.分割命令 $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 ) ) { // 以.拼接 $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(); ?>