防止在图像文件中使用PHP include


Prevent use of PHP include with image files

我最近有一个WordPress网站,它包含保存为PNG文件的恶意软件代码,但通过@include_once('images/sidebar2.png');拉入我的模板文件,然后被解释为PHP。

我一直在想办法阻止这种活动,但没有成功。我可以在文件夹中阻止PHP的执行,但这对这种情况没有帮助。我也可以强制MIME类型,但这也不起作用

是否可以将PHP包含仅限于.php文件?还有其他想法吗?

只需一枪:用include_once_secure和替换模板中的所有include_once引用

function include_once_secure($fileName) {
    if (strtolower(pathinfo($fileName, PATHINFO_EXTENSION))=='php') {
        include_once($fileName);
    } else {
        //here you could throw an error or exception
    }
}