dvwa之命令列注入
在只需要資料的地方惡意插入了命令,而系統沒有過濾時會造成命令列注入。dvwa提供了練習的地方
正常情況下這是用來測試網址能否連線
1.Security:Low
一旦我們插入惡意命令127.0.0.1&&net user就能發現命令被成功執行了
檢視原始碼:
<?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 ')。
函式:
stristr(string,search,before_searc)string是被搜尋的字串,search是搜尋的字串,before_search有true和false兩種取值(預設為false,返回匹配點到之後的部分)如stristr(''hello world!","WORld")返回world!,若第三個值為true時返回hello
php_uname(mode)
mode是單個字元,用於定義要返回什麼資訊:
- 'a':此為預設。包含序列 "s n r v m" 裡的所有模式。
- 's':作業系統名稱。例如: FreeBSD。
- 'n':主機名。例如: localhost.example.com。
- 'r':版本名稱,例如: 5.1.2-RELEASE。
- 'v':版本資訊。作業系統之間有很大的不同。
- 'm':機器型別。例如:i386。
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位且每一位是否為數字。。。。。。。甘拜下風
再總結一下命令連線符:
command1 && command2 先執行command1後執行command2
command1 | command2 只執行command2
command1 & command2 先執行command2後執行command1