GHSA-3qgc-jrrr-25jv / CVE-2024-4577: PHP RCE: A Bypass of CVE-2012-1823, Argument Injection in PHP-CGI
- Published:
- Fetched:
- Updated:
- Affects:
- php php-src
- KEV due:
- 2022-04-15
https://github.com/php/php-src/security/advisories/GHSA-3qgc-jrrr-25jv ↗
PHP RCE: A Bypass of CVE-2012-1823, Argument Injection in PHP-CGI Hi, I am Orange Tsai from DEVCORE Research Team. We recently found a vulnerability on PHP. We have reproduced the issues in the latest version of PHP 8.3.6 and would like to report it to you. Please check the attached document for details. This advisory is in accordance with our vulnerability disclosure policy, which will be publicly disclosed after 90 days. Our aim is to ensure that vulnerabilities can be patched in a timely manner. Although it’s not a hard deadline, we still hope you can fix this vulnerability before August 04, 2024. Please let me know if you have any questions, thanks! # Impact An attacker can execute arbitrary PHP code on the remote server. This vulnerability affects the PHP built with Windows. # Details The PHP-CGI which configured under Apache HTTP Server accepts the command-line arguments by default. This behavior was prove to vulnerable, and tracked as [CVE-2012-1823](https://bugs.php.net/bug.php?id=61910) before. The PHP published the following code to patch the vulnerability: ***# Path: sapi/cgi/cgi_main.c:1801** https://github.com/php/php-src/blob/php-8.0.30/sapi/cgi/cgi_main.c#L1801* ```c /* {{{ main */ int main(int argc, char *argv[]) { int free_query_string = 0; int exit_status = SUCCESS; int cgi = 0, c, i; size_t len; zend_file_handle file_handle; char *s; // ... // [1] this is the patch of CVE-2012-1823 if((query_string = getenv("QUERY_STRING")) != NULL && strchr(query_string, '=') == NULL) { /* we've got query string that has no = - apache CGI will pass it to command line */ unsigned char *p; decoded_query_string = strdup(query_string); php_url_decode(decoded_query_string, strlen(decoded_query_string)); for (p = (unsigned char *)decoded_query_string; *p && *p <= ' '; p++) { /* skip all leading spaces */ } if(*p == '-') { skip_getopt = 1; } free(decoded_query_string); } while (!skip_getopt && (c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) { switch (c) { // process the arguments } } ``` This argument `argv` is based on the `QUERY_STRING`, and the patch checks for no leading hyphen on the `QUERY_STRING` to ensure there are no dangerous arguments. However, if the binding between `argv` and `QUERY_STRING` is broken, the vulnerability of PHP-CGI Argument Injection would reappear! This patch will fail under the Windows system. Internally, Windows uses UCS-2 as its character set. Any characters passed to the ANSI Win32 API (such as `GetCommandLineA`), especially those with code point greater than 0x80, will be mapped to the corresponding UCS-2 code points by the current code page. The Windows internal also introduces a "Best-Fit" feature that replace the character if there is a more suitable character to display in the destination code page. The description and specification can be found here: * [WindowsBestFit/readme.txt](https://ww Patched: 8.3.8, 8.2.20, 8.1.29