DVWA Command Injection 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();
?>
解析
checkToken
checkToken( $_REQUEST[ 'user_token' ],$_SESSION[ 'session_token' ], 'index.php' );
为了防csrf跨站请求伪造(伪造攻击),使用令牌的方式,如果user_token与session_token不匹配就会报错,如果匹配就返回到后面的index.php
stripslashes
$target = stripslashes( $target );
stripslashes()函数删除反斜杠。
举例:
explode
$octet = explode( ".", $target );
explode() 函数使用一个字符串分割另一个字符串,并返回由字符串组成的数组。本函数返回由字符串组成的数组,由点“.”作为边界点分割出来的子字符串。
语法:explode(separator,string,limit)
参数 | 描述 |
---|---|
separator | 必需。规定在哪里分割字符串。 |
string | 必需。要分割的字符串。 |
limit | 可选。规定所返回的数组元素的数目。可能的值:大于 0 返回包含最多 limit 个元素的数组,小于 0返回包含除了最后的 -limit 个元素以外的所有元素的数组, 0会被当做 1, 返回包含一个元素的数组 |
举例:
is_numeric
if( ( is_numeric( $octet[0] ) ) && ( is_numeric( $octet[1] ) ) && ( is_numeric( $octet[2] ) ) && ( is_numeric( $octet[3] ) ) && ( sizeof( $octet ) == 4 ) )
is_numeric()函数是检测变量是否为数字或数字字符串,这里是检查IP地址以点分割后的数组值是否为数字。
语法:bool is_numeric ( mixed v a r ) / / var ) // var)//var:要检测的变量。
拼接
$target = $octet[0] . '.' . $octet[1] . '.' . $octet[2] . '.' . $octet[3];
重新将IP地址拼接。
剩下都在之前文章解释过,不再赘述