Let me just clarify a few things that I may have left unsaid in my previous
message...
- Make it so that include() and require() can only load specific
extensions like ".php" and ".phar" - At the same time, all file functions that write to a file (e.g. fwrite
and file_put_contents) must refuse to write to exactly these extensions.
When I say 2, I also mean
- Any functions like
rename()ormove_uploaded_file()should also be
counted among the files that "can write to files", so an attacker can't
rename a file to have the right extension.
But also let me add another, related improvement for PHP 9:
Heredoc syntax should absolutely be used for provenance of what is being
sent to exec(), eval() and other "open-ended, dangerous functions".
$code = <<<PHP
// this specific tag, "PHP" is needed
// in order to pass $foo to any eval()
You can interpolate $variables here, fine.
But this is at assignment time.
I recommend also allowing {{interpolation}}
that can be passed at eval time.
PHP;
eval($code, ['fields' => ['interpolation' => $whatever]])
the interpolation must be sanitized according to whatever language is used.
In this case, since the language is PHP, you must encode {{interpolation}}
always as a string or number inside PHP. Never raw.
Same for exec:
$cmd = <<<SHELL
some commands {{interpolate}}
SHELL;
$result = exec($cmd, ['fields' => ['interpolation' => $whatever]]);
This can be extended for SQL, JSON, and much more:
$sql = <<<SQL
SELECT {{fields}} FROM
SQL;
You can even have entire autoloaded classes, like \Formats\SQL and
\Formats\JSON, that will handle sanitization for that specific language.
Functions like eval and exec should enforce provenance and proper
sanitizing for anything interpolated (and frankly, I'd do the same for PDO,
mysqli_*, etc. which would eliminate a ton of SQL injection problems too,
and for encoding JSON inside Javascript rather than arbitrary strings,
html-encoding things inside HTML, etc. etc). This approach can
incrementally make PHP apps much safer across the board.
This way you can handle provenance of what you are actually sending to
functions like exec() and eval() and close the remaining GAPING HOLES in
PHP. An attacker who achieves RCE in PHP will still have a hard time
actually executing arbitrary exec() commands, because they wouldn't be able
to write random php files, eval() them, and they'd need a HEREDOC literal
template for each thing.
You'll finally achieve separation between what a developer intended (and
shipped via composer / packagist, for instance), and user-supplied code,
which should never be executed willy-nilly. Then you can lock down exec and
eval in the next version of PHP. The .ini can be used as an escape hatch
for the rare cases you actually need this. And frankly, .ini is too coarse,
I'd recommend doing it on a RegEx basis, where you indicate the regex of
what file paths are the exception. People often have many PHP apps running
on the same machine.
So all in all, to sum up:
- Can't include arbitrary files other than .php and .phar
- Can't write to exactly these files
- Eval and exec enforce provenance and proper sanitizing for anything
interpolated - There's no way userland code to execute RCE anymore, frankly not even
SQL injection, if you are conservative by default.
This would make PHP 9 a lot safer as a language, with minimal incremental
changes.
PS: I'm taking many of these ideas from a language I released this year
called U:
https://ulanguage.org/Formats
https://l.gourl.es/l/48e33c87b06e8c23bfae00402f8b9a57d516410b?u=1056127
Sincerely,
Greg Magarshak
[image: http://qbix.com] http://qbix.com
Hi everyone. Greg Magarshak here again. A few years ago I wrote about my
three proposals for PHP 9. One was to allow all legacy code to run much
faster in an "evented" style using Swoole / FrankenPHP. Since then I
released a webserver written in pure-PHP that already does it – any
legacy code can spin up 100x more workers and work 100x faster... and
besides handling HTTP requests, it also supports 100K websockets at once,
on one machine:https://github.com/Qbix/webserver
https://l.gourl.es/l/f836d2a2f999bdcb110919420aeb983ecc6d53ae?u=1056127By the way, that server doesn't suffer from php2shell, because it doesn't
even require nginx.Anyway I am writing another post today with an idea for PHP 9, that would
close a ton of possibilities for arbitrary RCE. I am tired of seeing PHP
have such gaping holes that are easy to plug. Here is one that I would
absolutely recommend for PHP 9:Make it so that include() and require() can only load specific extensions
like ".php" and ".phar". At the same time, all file functions that write to
a file (e.g. fwrite and file_put_contents) must refuse to write to exactly
these extensions. If a sysadmin wants to change this for a specific app,
they can edit the .ini file, but set_ini() should not be able to affect
these settings.THIS ONE THING ALONE WOULD CLOSE A TON OF GAPING LOOPHOLES THAT ATTACKERS
CAN USE ANYTIME, BUT ACTUAL PHP DEVELOPERS DON'T NEED 99.9% OF THE TIME.
And it's easy to add.My PHP webserver already rewrites PHP files when including them in PHP
userland using stream wrappers, to do exactly things like this. Being able
to rewrite PHP being loaded let me achieve a lot of the speed and
security innovations on the Qbix webserver. That's why it doesn't have
nearly the attack surface that regular PHP-FPM has. Please look at the
GitHub and feel free to lift the ideas from it (it's MIT licensed).Sincerely,
Greg Magarshak
[image: http://qbix.com] <http://qbix.com