代码审计
打开dvwa靶场,把难度设置为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>";
}
?>
trim
结论
tirm():移除字符串两侧的空白字符或其他预定义字符
str_replace
发现3的1被换成了2
结论:
str_replace函数:替换字符串中的一些字符(区分大小写)
array_keys
array_keys()
是 PHP 中的一个内置函数,用于返回数组中所有的键名作为新的数组。
这个函数接受一个数组作为参数,并返回一个新数组,其中包含了原数组的所有键名(a,b,c)。
stristr
stristr()
是 PHP 中的一个函数,用于在字符串中搜索一个子串,忽略大小写。
这个函数接受两个参数:第一个参数是要搜索的目标字符串,第二个参数是要搜索的子串。
如果找到了匹配的子串,那么就返回包含该子串及其后面部分的字符串;否则返回 FALSE
php_uname
php_uname()
是 PHP 中的一个函数,用于获取系统的信息。
这个函数返回一个包含系统信息的字符串,包括操作系统名称、版本号、主机名等内容。
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();
?>
stripslashes
stripslashes():用于移除字符串中的反斜杠转义字符 \
。 在 PHP 中,反斜杠是一个转义字符,用于表示一些特殊字符(例如 \"
表示双引号,\n
表示换行符等)。然而,在某些情况下,您可能不需要这些转义字符,这时可以使用 stripslashes()
函数将其移除。
explode
explode():用于将字符串按照指定的分隔符拆分为数组。
这个函数接受两个参数:第一个参数是要分割的字符串,第二个参数是用于拆分的分隔符。
sizeof
sizeof用于获取数组的长度。
这个函数接受一个数组作为参数,并返回数组中元素的数量。
is_numeric
is_numeric用于检查一个变量是否为数字。
这个函数接受一个变量作为参数,并返回 TRUE 或 FALSE 来指示该变量是否为数字。
如果该变量可以转换为合法的浮点数,那么就返回 TRUE;否则返回 FALSE。