CVE-2023-32679: Craftcms/cms Remote Code Execution Vulnerability

HIGH
Advisory Identifier: GHSA-vqxf-r9ph-cc9c
CVSS 7.2
CWE-74
Published
Advisory Summary

CVE-2023-32679 is a HIGH (CVSS 7.2) remote code execution vulnerability (CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')) affecting Craftcms/cms libraries in the Packagist ecosystem. The issue involves craft cms vulnerable to remote code execution via unrestricted file extension. Remediation is available by upgrading affected packages to version 4.4.6.

CVE-2023-32679 at a Glance

CVE Identifier CVE-2023-32679
GHSA Identifier GHSA-vqxf-r9ph-cc9c
Severity HIGH
CVSS v3.1 Base Score 7.2
CWE Classification CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection')
Affected Product Craftcms/cms
Package Ecosystem Packagist
Published Date
Last Updated

Security Impact

CVSS: 7.2

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required High (PR:H)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality High (C:H)
Integrity High (I:H)
Availability High (A:H)

How to Fix CVE-2023-32679

To remediate this vulnerability, upgrade all affected Craftcms/cms dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

craftcms/cms fixed: 4.4.6
Upgrade craftcms/cms to 4.4.6
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

inspect dependencies for craftcms/cms

How to Check if You Are Affected

To determine whether your application is affected by CVE-2023-32679, inspect your project dependencies for Craftcms/cms:

inspect dependencies for craftcms/cms

Analyze the terminal output to locate any version of Craftcms/cms in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 4.4.6), your application is affected.

Note: Verify declared build manifests and deployed runtime container environments.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection'), where the product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component. In Craftcms/cms, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### Summary Unrestricted file extension lead to a potential Remote Code Execution (Authenticated, ALLOW_ADMIN_CHANGES=true) ### Details #### Vulnerability Cause : If the name parameter value is not empty string('') in the View.php's doesTemplateExist() -> resolveTemplate() -> _resolveTemplateInternal() -> _resolveTemplate() function, it returns directly without extension verification, so that arbitrary extension files are rendered as twig templates (even if they are not extensions set in defaultTemplateExtensions = ['html', 'twig']) ```php /** * Searches for a template files, and returns the first match if there is one. * * @param string $basePath The base path to be looking in. * @param string $name The name of the template to be looking for. * @param bool $publicOnly Whether to only look for public templates (template paths that don’t start with the private template trigger). * @return string|null The matching file path, or `null`. */ private function _resolveTemplate(string $basePath, string $name, bool $publicOnly): ?string { // Normalize the path and name $basePath = FileHelper::normalizePath($basePath); $name = trim(FileHelper::normalizePath($name), '/'); // $name could be an empty string (e.g. to load the homepage template) if ($name !== '') { if ($publicOnly && preg_match(sprintf('/(^|\/)%s/', preg_quote($this->_privateTemplateTrigger, '/')), $name)) { return null; } // Maybe $name is already the full file path $testPath = $basePath . DIRECTORY_SEPARATOR . $name; if (is_file($testPath)) { return $testPath; } foreach ($this->_defaultTemplateExtensions as $extension) { $testPath = $basePath . DIRECTORY_SEPARATOR . $name . '.' . $extension; if (is_file($testPath)) { return $testPath; } } } foreach ($this->_indexTemplateFilenames as $filename) { foreach ($this->_defaultTemplateExtensions as $extension) { $testPath = $basePath . ($name !== '' ? DIRECTORY_SEPARATOR . $name : '') . DIRECTORY_SEPARATOR . $filename . '.' . $extension; if (is_file($testPath)) { return $testPath; } } } return null; } ``` When attacker with admin privileges on the DEV or Misconfigured STG, PROD, they can exploit this vulnerability to remote code execution **(ALLOW_ADMIN_CHANGES=true)** ### PoC **Step 1)** Create a new filesystem. **Base Path: /var/www/html/templates** ![1](https://user-images.githubusercontent.com/30969523/228049254-6c3a9897-c26a-46a5-96ad-41c7b769116a.png) **Step 2)** Create a new asset volume. **Asset Filesystem: template** ![2](https://user-images.githubusercontent.com/30969523/228049839-d2d42245-fa6e-4245-9fd2-967f1b9f4a74.png) **Step 3)** Upload poc file( .txt , .js , .json , etc ) with twig template rce payload ```twig {{'<pre>'}} {{1337*1337}} {{['cat /etc/passwd']|map('passthru')|join}} {{['id;pwd;ls -altr /']|map('passthru')|join}} ``` ![7](https://user-images.githubusercontent.com/30969523/228051307-623b78d0-4792-44ae-af0f-aff6b16f8d87.png) ![5](https://user-images.githubusercontent.com/30969523/228051064-cfaad338-3aff-4c4f-a177-9b42e473142b.png) **Step 4)** Create a new global set with template layout. The template filename is poc.js ![8](https://user-images.githubusercontent.com/30969523/228051430-365457eb-2a10-47a8-aed9-fb400e80c6d5.png) **Step 5)** When access global menu or /admin/global/test, poc.js is rendered as a template file and RCE confirmed ![9](https://user-images.githubusercontent.com/30969523/228053142-62a0f1ad-bbfa-4b8d-b6bd-28ed26c1cc18.png) **Step 6)** RCE can be confirmed on other menus(Entries, Categories) where the template file is loaded. ![10](https://user-images.githubusercontent.com/30969523/228054216-5dcd0c30-85bd-4603-84e5-944cfe9ad93c.png) ![11](https://user-images.githubusercontent.com/30969523/228054146-d5a3ceea-e13d-461a-bcd6-abf260761d62.png) **Poc Environment)** ALLOW_ADMIN_CHANGES=true, defaultTemplateExtensions=['html','twig'] ![0](https://user-images.githubusercontent.com/30969523/228054764-37d78cf5-5eca-442f-873a-99e6676b8173.png) ![13](https://user-images.githubusercontent.com/30969523/228054803-1a2c40b0-e515-46b3-a653-bb5ef1a287a2.png) ![14](https://user-images.githubusercontent.com/30969523/228054821-c7b0cfd6-126a-4722-846c-26d725af1a6a.png) ### Impact Take control of vulnerable systems, Data exfiltrations, Malware execution, Pivoting, etc. Additionally, there are 371 domains using CraftCMS exposed on Shodan, and among them, 33 servers have "stage" or "dev" included in their hostnames. 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) ![2023-03-31 10 29 53](https://user-images.githubusercontent.com/30969523/229001176-4c235b2f-e1a3-4b96-965a-78f227546a12.png) ### Remediation Recommend taking measures by referring to https://github.com/craftcms/cms-ghsa-9f84-5wpf-3vcf/pull/1 ```php // Maybe $name is already the full file path $testPath = $basePath . DIRECTORY_SEPARATOR . $name; if (is_file($testPath)) { // Remedation: Verify template file extension, before return $fileExt = pathinfo($testPath, PATHINFO_EXTENSION); $isDisallowed = false; if (isset($fileExt)) { $isDisallowed = !in_array($fileExt, $this->_defaultTemplateExtensions); if($isDisallowed) { return null; } else { return $testPath; } } } ``` ![remediation](https://user-images.githubusercontent.com/30969523/228841202-43079754-0d9d-47fa-8ae3-ce5dd509796b.png)

CVE-2023-32679 Affected Craftcms/cms Versions

1 component(s)
Fixed Versions:
4.4.6
Product / Package Ecosystem Affected Versions Fixed Version
craftcms/cms
Packagist
≥ 4.0.0
< 4.4.6
4.4.6

References

Frequently Asked Questions

What is CVE-2023-32679?
CVE-2023-32679 is a security vulnerability in Craftcms/cms involving Craft CMS vulnerable to Remote Code Execution via unrestricted file extension under CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection').
Which Craftcms/cms versions are affected?
Affected versions include releases prior to 4.4.6 across affected packages: craftcms/cms.
What version fixes CVE-2023-32679?
Upgrading to 4.4.6 (or the latest patched release for your respective branch) resolves the vulnerability.
Is craftcms/cms affected?
Yes, craftcms/cms is affected in releases prior to 4.4.6. Upgrade your dependency to 4.4.6 or later.
What is the CVSS score of CVE-2023-32679?
CVE-2023-32679 has a CVSS v3.1 base score of 7.2 (HIGH) with vector string CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H.
What CWE is associated with CVE-2023-32679?
CVE-2023-32679 is classified under CWE-74: Improper Neutralization of Special Elements in Output Used by a Downstream Component ('Injection') (The product constructs all or part of a command, data structure, or record using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify how it is parsed or interpreted when it is sent to a downstream component.).
How can I check Packagist dependencies for CVE-2023-32679?
Run the dependency tree command: 'inspect dependencies for craftcms/cms'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-vqxf-r9ph-cc9c",
  "modified" : "2023-05-22T20:36:06Z",
  "published" : "2023-05-22T20:36:06Z",
  "aliases" : [ "CVE-2023-32679" ],
  "summary" : "Craft CMS vulnerable to Remote Code Execution via unrestricted file extension ",
  "details" : "### Summary\nUnrestricted file extension lead to a potential Remote Code Execution\n(Authenticated, ALLOW_ADMIN_CHANGES=true)\n\n### Details\n#### Vulnerability Cause : \nIf the name parameter value is not empty string('') in the View.php's doesTemplateExist() -> resolveTemplate() -> _resolveTemplateInternal() -> _resolveTemplate() function, it returns directly without extension verification, so that arbitrary extension files are rendered as twig templates (even if they are not extensions set in defaultTemplateExtensions = ['html', 'twig'])\n```php\n    /**\n     * Searches for a template files, and returns the first match if there is one.\n     *\n     * @param string $basePath The base path to be looking in.\n     * @param string $name The name of the template to be looking for.\n     * @param bool $publicOnly Whether to only look for public templates (template paths that don’t start with the private template trigger).\n     * @return string|null The matching file path, or `null`.\n     */\n    private function _resolveTemplate(string $basePath, string $name, bool $publicOnly): ?string\n    {\n        // Normalize the path and name\n        $basePath = FileHelper::normalizePath($basePath);\n        $name = trim(FileHelper::normalizePath($name), '/');\n\n        // $name could be an empty string (e.g. to load the homepage template)\n        if ($name !== '') {\n            if ($publicOnly && preg_match(sprintf('/(^|\\/)%s/', preg_quote($this->_privateTemplateTrigger, '/')), $name)) {\n                return null;\n            }\n\n            // Maybe $name is already the full file path\n            $testPath = $basePath . DIRECTORY_SEPARATOR . $name;\n\n            if (is_file($testPath)) {\n                return $testPath;\n            }\n\n            foreach ($this->_defaultTemplateExtensions as $extension) {\n                $testPath = $basePath . DIRECTORY_SEPARATOR . $name . '.' . $extension;\n\n                if (is_file($testPath)) {\n                    return $testPath;\n                }\n            }\n        }\n\n        foreach ($this->_indexTemplateFilenames as $filename) {\n            foreach ($this->_defaultTemplateExtensions as $extension) {\n                $testPath = $basePath . ($name !== '' ? DIRECTORY_SEPARATOR . $name : '') . DIRECTORY_SEPARATOR . $filename . '.' . $extension;\n\n                if (is_file($testPath)) {\n                    return $testPath;\n                }\n            }\n        }\n\n        return null;\n    }\n```\n\nWhen attacker with admin privileges on the DEV or Misconfigured STG, PROD, they can exploit this vulnerability to remote code execution **(ALLOW_ADMIN_CHANGES=true)**\n\n\n### PoC\n**Step 1)** Create a new filesystem. **Base Path: /var/www/html/templates**\n![1](https://user-images.githubusercontent.com/30969523/228049254-6c3a9897-c26a-46a5-96ad-41c7b769116a.png)\n\n**Step 2)** Create a new asset volume. **Asset Filesystem: template**\n![2](https://user-images.githubusercontent.com/30969523/228049839-d2d42245-fa6e-4245-9fd2-967f1b9f4a74.png)\n\n**Step 3)** Upload poc file( .txt , .js , .json , etc ) with twig template rce payload\n```twig\n{{'<pre>'}}\n{{1337*1337}}\n{{['cat /etc/passwd']|map('passthru')|join}}\n{{['id;pwd;ls -altr /']|map('passthru')|join}}\n```\n![7](https://user-images.githubusercontent.com/30969523/228051307-623b78d0-4792-44ae-af0f-aff6b16f8d87.png)\n![5](https://user-images.githubusercontent.com/30969523/228051064-cfaad338-3aff-4c4f-a177-9b42e473142b.png)\n\n**Step 4)** Create a new global set with template layout. The template filename is poc.js\n![8](https://user-images.githubusercontent.com/30969523/228051430-365457eb-2a10-47a8-aed9-fb400e80c6d5.png)\n\n**Step 5)** When access global menu or /admin/global/test, poc.js is rendered as a template file and RCE confirmed\n![9](https://user-images.githubusercontent.com/30969523/228053142-62a0f1ad-bbfa-4b8d-b6bd-28ed26c1cc18.png)\n\n**Step 6)** RCE can be confirmed on other menus(Entries, Categories) where the template file is loaded.\n![10](https://user-images.githubusercontent.com/30969523/228054216-5dcd0c30-85bd-4603-84e5-944cfe9ad93c.png)\n![11](https://user-images.githubusercontent.com/30969523/228054146-d5a3ceea-e13d-461a-bcd6-abf260761d62.png)\n\n\n**Poc Environment)** ALLOW_ADMIN_CHANGES=true, defaultTemplateExtensions=['html','twig']\n![0](https://user-images.githubusercontent.com/30969523/228054764-37d78cf5-5eca-442f-873a-99e6676b8173.png)\n![13](https://user-images.githubusercontent.com/30969523/228054803-1a2c40b0-e515-46b3-a653-bb5ef1a287a2.png)\n![14](https://user-images.githubusercontent.com/30969523/228054821-c7b0cfd6-126a-4722-846c-26d725af1a6a.png)\n\n### Impact\nTake control of vulnerable systems, Data exfiltrations, Malware execution, Pivoting, etc.\n\nAdditionally, there are 371 domains using CraftCMS exposed on Shodan, and among them, 33 servers have \"stage\" or \"dev\" included in their hostnames. \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\n![2023-03-31 10 29 53](https://user-images.githubusercontent.com/30969523/229001176-4c235b2f-e1a3-4b96-965a-78f227546a12.png)\n\n### Remediation\nRecommend taking measures by referring to https://github.com/craftcms/cms-ghsa-9f84-5wpf-3vcf/pull/1\n```php\n            // Maybe $name is already the full file path\n            $testPath = $basePath . DIRECTORY_SEPARATOR . $name;\n\n            if (is_file($testPath)) {\n                // Remedation: Verify template file extension, before return\n                $fileExt = pathinfo($testPath, PATHINFO_EXTENSION);\n                $isDisallowed = false;\n\n                if (isset($fileExt)) {\n                    $isDisallowed = !in_array($fileExt, $this->_defaultTemplateExtensions);\n\n                    if($isDisallowed) {\n                        return null;\n                    } else {\n                        return $testPath;\n                    }\n                }\n            }\n```\n\n![remediation](https://user-images.githubusercontent.com/30969523/228841202-43079754-0d9d-47fa-8ae3-ce5dd509796b.png)",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "Packagist",
      "name" : "craftcms/cms"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "4.0.0"
      }, {
        "fixed" : "4.4.6"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/craftcms/cms/security/advisories/GHSA-vqxf-r9ph-cc9c"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2023-32679"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/craftcms/cms"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/craftcms/cms/releases/tag/4.4.6"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-74" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2023-05-22T20:36:06Z",
    "nvd_published_at" : "2023-05-19T20:15:09Z"
  }
}