text
stringlengths
0
1.99k
So, in this section, let me introduce two classic examples that you
shouldn't miss when talking about PHP 0days born in CTFs!
------[ 5.1 - Hack the Scoreboard!
Whenever I talk to people outside the Infosec community about hacking
competitions, they often jokingly say, "Come on, real hackers wouldn't
follow the rules - they'd just hack and change their scores, right?" Well,
to be fair, they're actually right! There's indeed plenty of history where
scoreboards got hacked (and to be honest, I've contributed a few myself).
But if we're talking about the most legendary case, I'd say it's
definitely the PHP-CGI 0day discovered by Eindbazen team - right there on
a CTF scoreboard [82]!
During Nullcon HackIM CTF, the organizers directly used a CGI environment
provided by their cloud provider. Since the CGI-spec itself is inherently
vulnerable to argument injection by design, it became even more
unfortunate (or fortunate - choose your side) when PHP developers forgot
about this and completely removed the defensive logic. These coincidences
combined ultimately allowed Eindbazen team to control PHP's command-line
arguments directly through the query string. For example, they can simply
append a `?-s` at the end of the URL to leak any PHP source code on the
remote server - and escalating it further into full RCE is just as
trivial!
This vulnerability impacted a huge number of websites back then -
especially those web hosting providers heavily relying on CGI for
privilege isolation and PHP version switching. And because this
vulnerability was so ridiculously easy to exploit, it quickly became
notorious worldwide. Even Facebook - famous for its PHP-based
infrastructure at the time - put an Easter egg right on its homepage
(linking the URL to their security engineer recruitment page) to
acknowledge this vulnerability, too!
----------------------[ Easter Egg on facebook.com! ]----------------------
$ curl https://www.facebook.com/?-s
<?php
include_once 'https://www.facebook.com/careers/
department?dept=engineering&req=a2KA0000000Lt8LMAS';
---------------------------------------------------------------------------
This vulnerability was eventually patched and assigned CVE-2012-1823. The
PHP team solved this issue by checking that the query string can't start
with a hyphen `-` (0x2D). This fix kept PHP safe for about 12 years -
until I broke it again last year.
=> The patch of CVE-2012-1823: PHP-CGI Argument Injection
---------------------------------------------------------------------------
if((qs = getenv("QUERY_STRING")) != NULL && strchr(qs, '=') == NULL) {
/* ... omitted ... */
for (p = decoded_qs; *p && *p <= ' '; p++) {/* skip leading spaces */}
if (*p == '-') {
skip_getopt = 1;
}
---------------------------------------------------------------------------
While revisiting PHP's source code, I found that by leveraging the Windows
"BestFit" feature, I could completely bypass this fix. "BestFit" is
basically a backward-compatibility feature introduced by Windows. It tries
to minimize garbled characters through a series of weird character
mappings when dealing with older ANSI APIs (thanks a lot, Microsoft). This
mechanism also came with some strange side effects - for example, you can
use the infinity symbol `∞` (U+221E) to represent the digit `8` (U+0038)
right on the command line:
=> Microsoft maps the characters to their "lookalikes"
---------------------------------------------------------------------------
C:\Users\Orange> type Hello.c
int main(int argc, char* argv[], char* envp[]) {
printf("Hello %s!\n", argv[1]);
}
C:\Users\Orange> cl.exe Hello.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30140 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
[...]
C:\Users\Orange> Hello.exe World
Hello World!
C:\Users\Orange> Hello.exe √π⁷≤∞
Hello vp7=8!
---------------------------------------------------------------------------
So, by simply replacing the originally blocked hyphen (0x2D) with a "soft
hyphen" (0xAD), the original attack could be revived effortlessly! This
bypass affects practically every PHP version running on Windows - even a
default XAMPP installation was vulnerable. This vulnerability has also
earned its CVE number (CVE-2024-4577). If you're curious about the
technical details behind it, you should definitely check out WorstFit
Attack [46] - a joint work with the brilliant @splitline!