I'm trying to build a list of functions that can be used for arbitrary code execution. The purpose isn't to list functions that should be blacklisted or otherwise disallowed. Rather, I'd like to have a grep
-able list of red-flag keywords handy when searching a compromised server for back-doors.
The idea is that if you want to build a multi-purpose malicious PHP script -- such as a "web shell" script like c99 or r57 -- you're going to have to use one or more of a relatively small set of functions somewhere in the file in order to allow the user to execute arbitrary code. Searching for those those functions helps you more quickly narrow down a haystack of tens-of-thousands of PHP files to a relatively small set of scripts that require closer examination.
Clearly, for example, any of the following would be considered malicious (or terrible coding):
<? eval($_GET['cmd']); ?>
<? system($_GET['cmd']); ?>
<? preg_replace('/.*/e',$_POST['code']); ?>
and so forth.
Searching through a compromised website the other day, I didn't notice a piece of malicious code because I didn't realize preg_replace
could be made dangerous by the use of the /e
flag (which, seriously? Why is that even there?). Are there any others that I missed?
Here's my list so far:
Shell Execute
system
exec
popen
backtick operator
pcntl_exec
PHP Execute
eval
preg_replace
(with/e
modifier)create_function
include
[_once
] /require
[_once
] (see mario's answer for exploit details)
It might also be useful to have a list of functions that are capable of modifying files, but I imagine 99% of the time exploit code will contain at least one of the functions above. But if you have a list of all the functions capable of editing or outputting files, post it and I'll include it here. (And I'm not counting mysql_execute
, since that's part of another class of exploit.)
Several buffer overflows were discovered using 4bit characters functions that interpret text. htmlentities() htmlspecialchars()
were at the top, a good defence is to use mb_convert_encoding() to convert to single encoding prior to interpretation.
I'm surprised no one has mentioned
echo
andprint
as points of security exploitation.Cross-Site Scripting (XSS) is a serious security exploit, because it's even more common than server-side code execution exploits.
I know
move_uploaded_file
has been mentioned, but file uploading in general is very dangerous. Just the presence of$_FILES
should raise some concern.It's quite possible to embed PHP code into any type of file. Images can be especially vulnerable with text comments. The problem is particularly troublesome if the code accepts the extension found within the
$_FILES
data as-is.For example, a user could upload a valid PNG file with embedded PHP code as "foo.php". If the script is particularly naive, it may actually copy the file as "/uploads/foo.php". If the server is configured to allow script execution in user upload directories (often the case, and a terrible oversight), then you instantly can run any arbitrary PHP code. (Even if the image is saved as .png, it might be possible to get the code to execute via other security flaws.)
A (non-exhaustive) list of things to check on uploads:
Let's add
pcntl_signal
andpcntl_alarm
to the list.With the help of those functions you can work around any set_time_limit restriction created int the php.ini or in the script.
This script for example will run for 10 seconds despite of
set_time_limit(1);
(Credit goes to Sebastian Bergmanns tweet and gist:
Here is a list of functions my provider disables for security purposes:
What about dangerous syntactic elements?
The "variable variable" (
$$var
) will find a variable in the current scope by the name of $var. If used wrong, the remote user can modify or read any variable in the current scope. Basically a weakereval
.Ex: you write some code
$$uservar = 1;
, then the remote user sets$uservar
to "admin", causing$admin
to be set to1
in the current scope.