CVE-2024-4577: PHP-CGI Argument Injection via Windows Best-Fit Character Mapping
A remote code execution flaw in PHP running under CGI mode on Windows systems. Official upstream security fixes were released for PHP 8.1+ only, leaving legacy PHP 5.6 through 8.0 deployments vulnerable unless mitigated or backported.
1. Affected Environments
This vulnerability affects installations meeting all three of the following conditions:
- Operating System: Windows running with locale character sets using best-fit mapping (including Traditional Chinese CP950, Simplified Chinese CP936, and Japanese CP932).
- SAPI Interface: PHP executed via CGI (such as Apache
ScriptAlias /php-cgi/directives or default XAMPP configurations). PHP-FPM or Apache Module (mod_php) setups are not directly exposed unless explicitly configured to route requests through the CGI binary. - PHP Versions: PHP 5.6.x, 7.0.x, 7.1.x, 7.2.x, 7.3.x, 7.4.x, and 8.0.x. None of these versions received official upstream security releases from PHP.net because their official lifecycle had ended. Upstream fixes shipped in PHP 8.1.29, 8.2.20, and 8.3.8.
2. Technical Root Cause
Under RFC 3875 (CGI 1.1 specification), query strings that do not contain an unencoded equals sign (=) must be passed as command-line arguments to the target executable. In 2012, CVE-2012-1823 revealed that attackers could abuse this behavior to pass CLI switches like -d directly to php-cgi.exe.
To defend against CVE-2012-1823, PHP developers added input verification in sapi/cgi/cgi_main.c that inspects the URL-decoded query string when no = is present, setting skip_getopt = 1 if it begins with an ASCII hyphen (0x2D).
The Bypass in CVE-2024-4577: When Windows converts incoming wide characters or code pages, it applies "Best-Fit" mappings. A soft hyphen character (byte 0xAD, or URL-encoded %AD) does not match ASCII 0x2D during the initial guard check in cgi_main.c. However, subsequent Windows routines map 0xAD to an ASCII hyphen (0x2D), injecting dangerous command-line arguments after the guard check has already run.
3. The Attack Vector
An attacker submits a POST request containing soft hyphens to inject PHP runtime directives. For example, injecting -d allow_url_include=1 and -d auto_prepend_file=php://input causes PHP to execute the payload supplied in the HTTP request body:
POST /test.php?%ADd+allow_url_include%3d1+%ADd+auto_prepend_file%3dphp://input HTTP/1.1
Host: target.internal
Content-Type: application/x-www-form-urlencoded
Content-Length: 28
<?php phpinfo(); die(); ?>
4. Remediation & Operational Mitigation
Because unverified rewrite regexes can either miss bypass encodings or inadvertently block legitimate query parameters containing hyphens, and C source backports must be compiled and tested against your exact Windows PHP build, DIY patches are withheld here. Recommended actions:
- Upgrade if on PHP 8.1+: Update directly to PHP 8.1.29+, 8.2.20+, or 8.3.8+, which check high-byte characters in
sapi/cgi/cgi_main.cusing WindowsWideCharToMultiByte(CP_ACP, ...)before allowing option parsing. - Disable PHP-CGI exposure: Where possible on legacy Windows servers, migrate from PHP-CGI to Apache
mod_phpor FastCGI, and remove unnecessaryScriptAlias /php-cgi/directives (including default XAMPP CGI mappings). - Request a tested binary backport: If your Windows application depends on PHP 5.6 through 8.0 in CGI mode and cannot upgrade to PHP 8.1+, use a compiled and regression-tested binary patch rather than an untested source snippet.
Running Legacy PHP in Production?
Applying manual source patches across multiple production servers is error-prone. CrawlShield delivers tested, pre-compiled binary patches and patch verification records for legacy PHP (5.6, 7.0, 7.1, 7.2, 7.3, 7.4, 8.0) directly compatible with your existing distribution packages.
Request production patch for PHP