请描述下面代码中的安全漏洞
<?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>\";
}
?>
请描述下面代码中的安全漏洞
<?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>\";
}
?>
请描述下面代码中的安全漏洞
<?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();
?>
请描述下面两段代码在代码安全方面的差异
<?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>\";
}
?>
<?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();
?>
下面代码中存在的漏洞该如何进行修复
<?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>\";
}
?>
比如说让它编写一段python程序扫描指定目录下的webshell文件。