源代码分析:
<?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();
?>
这段代码的功能是接收一个IP地址并使用ping命令对其进行探测。它首先检查了Anti-CSRF令牌以防止跨站请求伪造攻击,然后检查了用户输入的IP地址是否合法,最后根据操作系统的不同,执行相应的ping命令。
审计结果如下:
使用stripslashes()函数清理用户的输入,但没有进行任何其他类型的验证或过滤,可能存在注入攻击的风险。
在确定操作系统之后,直接使用shell_exec()函数执行ping命令,存在命令注入风险。
如果执行的ping命令失败,程序将返回错误消息“ERROR: You have entered an invalid IP”,可能会暴露系统内部的信息给攻击者。
建议采取以下改进措施:
对用户输入的IP地址进行严格的验证和过滤,确保其格式正确且不存在恶意内容。
尽量避免使用shell_exec()函数执行外部命令,可以选择使用更安全的方法,如使用内置函数或者限制命令参数。
在发生错误时,不要返回详细的错误消息,以免泄露敏感信息。