自从ECSHOP2.7.3起模板不再支持php代码。如何解决?
自从ECSHOP2.7.3起模板不再支持php代码。如何解决?
其实是ECSHOP模板解析时过滤了PHP代码。整个重点主要是改includes目录下的cls_templage.php这个文件,
ECshop的模板是支持php代码的,主要是为了防范挂马,这个给一些不法分子创造了挂马的机会,这些不法分子挂马步骤很可能是:
1、通过ecshop的漏洞搞SQL注入,暴出管理员密码md5值,然后通过**md5得到管理密码。(注:防止暴出管理密码md5值的方法是关闭 display_errors,并且修改cls_mysql.php里的ErrorMsg函数,注释掉所有错误输出代码或把错误写入文件)
2、进入管理后台,通过模板管理->库项目管理,编辑lbi文件,添加php代码,例如<?php @eval($_POST['lx']);?>
3、到这里,就完全控制这个站了,想挂什么马就挂什么马。
可见,ECshop的模板支持php代码这点是非常危险的,因此我们应该过滤模板里的所有php代码。
方法如下:
1、修改cls_templage.php文件,添加函数:
function delete_php_code($content)
{
if(!empty($content))
{
$pattern='/<?(.|rn|s)*?>/U';
return preg_replace($pattern,'',$content);
}
}
2、第165行$out = $this->_eval($this->fetch_str(file_get_contents($filename)));修改为:
$out = $this->_eval($this->fetch_str($this->delete_php_code(file_get_contents($filename))));
3、第260行$source = $this->fetch_str(file_get_contents($filename));修改为:
$source = $this->fetch_str($this->delete_php_code(file_get_contents($filename)));
这样,模板里的php代码就被过滤掉了。
function fetch_str($source)
{
if (!defined('ECS_ADMIN'))
{
$source = $this->smarty_prefilter_preCompile($source);
}
$source=preg_replace("/([^a-zA-Z0-9_]{1,1})+(copy|fputs|fopen|file_put_contents|fwrite|eval|phpinfo)+( |\()/is", "", $source);
if(preg_match_all('~(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?php[\"\']?)~is', $source, $sp_match))
{
$sp_match[1] = array_unique($sp_match[1]);
for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++)
{
$source = str_replace($sp_match[1][$curr_sp],'%%%SMARTYSP'.$curr_sp.'%%%',$source);
}
for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++)
{
$source= str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>'."\n", $source);
}
}
return preg_replace("/{([^\}\{\n]*)}/e", "\$this->select('\\1');", $source);
}
2.73 增加了额
$source=preg_replace("/([^a-zA-Z0-9_]{1,1})+(copy|fputs|fopen|file_put_contents|fwrite|eval|phpinfo)+( |\()/is", "", $source);
if(preg_match_all('~(<\?(?:\w+|=)?|\?>|language\s*=\s*[\"\']?php[\"\']?)~is', $source, $sp_match))
{
$sp_match[1] = array_unique($sp_match[1]);
for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++)
{
$source = str_replace($sp_match[1][$curr_sp],'%%%SMARTYSP'.$curr_sp.'%%%',$source);
}
for ($curr_sp = 0, $for_max2 = count($sp_match[1]); $curr_sp < $for_max2; $curr_sp++)
{
$source= str_replace('%%%SMARTYSP'.$curr_sp.'%%%', '<?php echo \''.str_replace("'", "\'", $sp_match[1][$curr_sp]).'\'; ?>'."\n", $source);
}
}
解决方法 利用 insert_functionname