Описание
Craft CMS vulnerable to Potential Remote Code Execution via missing path normalization & Twig SSTI
Summary
Missing normalizePath in the function FileHelper::absolutePath could lead to Remote Code Execution on the server via twig SSTI.
(Post-authentication, ALLOW_ADMIN_CHANGES=true)
Details
Note: This is a sequel to CVE-2023-40035
In src/helpers/FileHelper.php#L106-L137, the function absolutePath returned $from . $ds . $to without path normalization:
This could leads to multiple security risks, one of them is in src/services/Security.php#L201-L220 where ../templates/poc is not considered a system dir.
Let's see what happens after calling isSystemDir("../templates/poc"):
Now that the path ../templates/poc can bypass isSystemDir, it will also bypass the function validatePath in src/fs/Local.php#L124-L136:
We can now create a Local filesystem within the system directories, particularly in /var/www/html/templates/poc
Then create a new asset volume with that filesystem, upload a poc.ttml file with twig code and execute using a new route with template path poc/poc.ttml
Although craftcms does sandbox twig ssti, the list in src/web/twig/Extension.php#L180-L268 is still incomplete.
These payloads still work, see twigphp/Twig/src/Extension/CoreExtension.php#getFilters() and twigphp/Twig/src/Extension/CoreExtension.php#getOperators() for more informations.
PoC
- Craft CMS was installed using https://craftcms.com/docs/4.x/installation.html#quick-start
- Create a new filesystem with base path
../templates/poc
Notice that the poc directory was created
- Create a new asset volume using the
pocfilesystem
Upload a poc.ttml file with RCE template code
Note: find was added to twig last month. If you're running this poc on an older version of twig try removing the last 2 lines.
- Create a new route
*with templatepoc/poc.ttml
- This leads to Remote Code Execution on arbitrary route
/*
Remediation
See twigphp/Twig/src/Extension/CoreExtension.php for updated filters and operators, a possible fix could look like:
Impact
Take control of vulnerable systems, Data exfiltrations, Malware execution, Pivoting, etc.
Although the vulnerability is exploitable only in the authenticated users, configuration with ALLOW_ADMIN_CHANGES=true, there is still a potential security threat (Remote Code Execution)
Пакеты
craftcms/cms
>= 4.0.0-RC1, <= 4.12.1
4.12.2
craftcms/cms
>= 5.0.0-RC1, <= 5.4.2
5.4.3
Связанные уязвимости
Craft is a content management system (CMS). Prior to 4.12.2 and 5.4.3, Craft is missing normalizePath in the function FileHelper::absolutePath could lead to Remote Code Execution on the server via twig SSTI. This is a sequel to CVE-2023-40035. This vulnerability is fixed in 4.12.2 and 5.4.3.
Уязвимость системы управления контентом Craft CMS, связанная с неверным ограничением имени пути к каталогу с ограниченным доступом, позволяющая нарушителю выполнить произвольный код или реализовать атаку внедрения шаблонов на стороне сервера (Server Side Template Injection (SSTI))