ghsa-vqxf-r9ph-cc9c
Vulnerability from github
Published
2023-05-22 20:36
Modified
2023-05-22 20:36
Summary
Craft CMS vulnerable to Remote Code Execution via unrestricted file extension
Details

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, ornull`. */ 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

Step 2) Create a new asset volume. Asset Filesystem: template 2

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 5

Step 4) Create a new global set with template layout. The template filename is poc.js 8

Step 5) When access global menu or /admin/global/test, poc.js is rendered as a template file and RCE confirmed 9

Step 6) RCE can be confirmed on other menus(Entries, Categories) where the template file is loaded. 10 11

Poc Environment) ALLOW_ADMIN_CHANGES=true, defaultTemplateExtensions=['html','twig'] 0 13 14

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

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

Show details on source website


{
  "affected": [
    {
      "package": {
        "ecosystem": "Packagist",
        "name": "craftcms/cms"
      },
      "ranges": [
        {
          "events": [
            {
              "introduced": "4.0.0"
            },
            {
              "fixed": "4.4.6"
            }
          ],
          "type": "ECOSYSTEM"
        }
      ]
    }
  ],
  "aliases": [
    "CVE-2023-32679"
  ],
  "database_specific": {
    "cwe_ids": [
      "CWE-74"
    ],
    "github_reviewed": true,
    "github_reviewed_at": "2023-05-22T20:36:06Z",
    "nvd_published_at": "2023-05-19T20:15:09Z",
    "severity": "HIGH"
  },
  "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(\u0027\u0027) in the View.php\u0027s doesTemplateExist() -\u003e resolveTemplate() -\u003e _resolveTemplateInternal() -\u003e _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 = [\u0027html\u0027, \u0027twig\u0027])\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\u2019t 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), \u0027/\u0027);\n\n        // $name could be an empty string (e.g. to load the homepage template)\n        if ($name !== \u0027\u0027) {\n            if ($publicOnly \u0026\u0026 preg_match(sprintf(\u0027/(^|\\/)%s/\u0027, preg_quote($this-\u003e_privateTemplateTrigger, \u0027/\u0027)), $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-\u003e_defaultTemplateExtensions as $extension) {\n                $testPath = $basePath . DIRECTORY_SEPARATOR . $name . \u0027.\u0027 . $extension;\n\n                if (is_file($testPath)) {\n                    return $testPath;\n                }\n            }\n        }\n\n        foreach ($this-\u003e_indexTemplateFilenames as $filename) {\n            foreach ($this-\u003e_defaultTemplateExtensions as $extension) {\n                $testPath = $basePath . ($name !== \u0027\u0027 ? DIRECTORY_SEPARATOR . $name : \u0027\u0027) . DIRECTORY_SEPARATOR . $filename . \u0027.\u0027 . $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{{\u0027\u003cpre\u003e\u0027}}\n{{1337*1337}}\n{{[\u0027cat /etc/passwd\u0027]|map(\u0027passthru\u0027)|join}}\n{{[\u0027id;pwd;ls -altr /\u0027]|map(\u0027passthru\u0027)|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=[\u0027html\u0027,\u0027twig\u0027]\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-\u003e_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)",
  "id": "GHSA-vqxf-r9ph-cc9c",
  "modified": "2023-05-22T20:36:06Z",
  "published": "2023-05-22T20:36:06Z",
  "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"
    }
  ],
  "schema_version": "1.4.0",
  "severity": [
    {
      "score": "CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H",
      "type": "CVSS_V3"
    }
  ],
  "summary": "Craft CMS vulnerable to Remote Code Execution via unrestricted file extension "
}


Log in or create an account to share your comment.




Tags
Taxonomy of the tags.


Loading…

Loading…

Loading…

Sightings

Author Source Type Date

Nomenclature

  • Seen: The vulnerability was mentioned, discussed, or seen somewhere by the user.
  • Confirmed: The vulnerability is confirmed from an analyst perspective.
  • Exploited: This vulnerability was exploited and seen by the user reporting the sighting.
  • Patched: This vulnerability was successfully patched by the user reporting the sighting.
  • Not exploited: This vulnerability was not exploited or seen by the user reporting the sighting.
  • Not confirmed: The user expresses doubt about the veracity of the vulnerability.
  • Not patched: This vulnerability was not successfully patched by the user reporting the sighting.