在只須要數據的地方惡意插入了命令,而系統沒有過濾時會形成命令行注入。dvwa提供了練習的地方php
正常狀況下這是用來測試網址可否鏈接shell
1.Security:Low數組
然鵝當咱們插入惡意命令127.0.0.1&&net usersession
查看源代碼:函數
<?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函數中執行了,執行語句爲shell_exec( 'ping 127.0.0.1&&net user ')。測試
函數:spa
stristr(string,search,before_searc)string是被搜索的字符串,search是搜索的字符串,before_search有true和false兩種取值(默認爲false,返回匹配點到以後的部分)如stristr(''hello world!","WORld")返回world!,若第三個值爲true時返回hello操作系統
php_uname(mode)命令行
mode是單個字符,用於定義要返回什麼信息:3d
2.Security:Medium
重複上述輸入時返回Ping 請求找不到主機 127.0.0.1net。請檢查該名稱,而後重試。可知&&符號被替換爲空了,嘗試&與|均可成功。
查閱源代碼:
<?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>"; } ?>
標紅的地方將&&與;都替換爲空,但並未過濾&與|
3.Security:High
調整爲高級別後&與&&都返回Ping 請求找不到主機 127.0.0.1net。請檢查該名稱,而後重試。可知這兩個都被過濾了,然而使用|依然能夠順利執行。
查看源碼:
<?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>"; } ?>
能夠看出這裏先用trim去掉兩邊的空格,而後過濾了&,&&,與| 等符號,然而過濾的是|+空格,並無過濾|,也是很尷尬了。。。。。。讓咱們再看看Impossible級別,開開眼
4.Security:Impossible
上面使用的全部符號都失效了,無盡的ERROR: You have entered an invalid IP.從返回結果也看不出過濾方式
查看源碼:
<?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(); ?>
首先檢查了token,這個我不懂。再看它先將ip按"."拆分爲數組,而後判斷位數是否爲4位且每一位是否爲數字。。。。。。。甘拜下風
再總結一下命令鏈接符:
& 順序執行命令,無論是否執行成功
; 按順序執行
&& 前面的命令執行成功後面的命令才執行
|| 前面的命令執行失敗後面的命令才執行
| 後面的執行失敗才執行前面的