- May 12, 2020
- Posted by: administrator
- Category: Acunetix
In part 1 of this series, we looked at what a web shell is and why an attacker would seek to use one. In part 2 of this series, we’ll be looking at some specific examples of web shells developed using the PHP programming language.
Web shells exist for almost every web programming language you can think of. We chose to focus on PHP because it is the most widely-used programming language on the web.
PHP web shells do nothing more than use in-built PHP functions to execute commands. The following are some of the most common functions used to execute shell commands in PHP.Note: For the purposes of this article, we edited our hosts file and pointed the domain www.example.com to a test server.
system() function accepts the command as a parameter and it outputs the result.
The following example on a Microsoft Windows machine will run the
dir command to return a directory listing of the directory in which the PHP file is executed.
<?php // Return the listing of the directory where the file runs (Windows) system("dir"); ?> --> Volume in drive C has no label. Volume Serial Number is A08E-9C63 Directory of C:\webserver\www\demo 02/27/2020 10:21 PM <DIR> . 02/27/2020 10:21 PM <DIR> .. 02/27/2020 10:19 PM 22 shell.php 1 File(s) 22 bytes 2 Dir(s) 31,977,467,904 bytes free
ls command on a Linux machine achieves a similar result.
<?php // Return the listing of the directory where the file runs (Linux) system("ls -la"); ?> --> total 12 drwxrwxr-x 2 secuser secuser 4096 Feb 27 20:43 . drwxr-xr-x 6 secuser secuser 4096 Feb 27 20:40 .. -rw-rw-r-- 1 secuser secuser 26 Feb 27 20:41 shell.php
Other commands have the same effect.
<?php // Return the user the script is running under system("whoami"); ?> --> www-data
exec() function accepts a command as a parameter but does not output the result. If a second optional parameter is specified, the result will be returned as an array. Otherwise, only the last line of the result will be shown if echoed.
<?php // Executes but returns nothing exec("ls -la"); ?> -->
echo with the
exec() function will only print the last line of the command output.
<?php // Executes, returns only last line of the output echo exec("ls -la"); ?> --> -rw-rw-r-- 1 secuser secuser 29 Feb 27 20:49 shell.php
If a second parameter is specified, the result is returned in an array.
<?php // Executes, returns the output in an array exec("ls -la",$array); print_r($array); ?> --> Array(  => total 12  => drwxrwxr-x 2 secuser secuser 4096 Feb 27 20:55 .  => drwxr-xr-x 6 secuser secuser 4096 Feb 27 20:40 ..  => -rw-rw-r-- 1 secuser secuser 49 Feb 27 20:54 shell.php )
shell_exec() function is similar to
exec(), however, it outputs the entire result as a string.
<?php // Executes, returns the entire output as a string echo shell_exec("ls -la"); ?>
--> total 12 drwxrwxr-x 2 secuser secuser 4096 Feb 28 18:24 . drwxr-xr-x 6 secuser secuser 4096 Feb 27 20:40 .. -rw-rw-r-- 1 secuser secuser 36 Feb 28 18:24 shell.php
passthru() function executes a command and returns output in raw format.
<?php // Executes, returns output in raw format passsthru("ls -la"); ?> --> total 12 drwxrwxr-x 2 secuser secuser 4096 Feb 28 18:23 . drwxr-xr-x 6 secuser secuser 4096 Feb 27 20:40 .. -rw-rw-r-- 1 secuser secuser 29 Feb 28 18:23 shell.php
proc_open() function can be difficult to understand (you can find a detailed description of the function in the PHP docs). By using
proc_open(), we can create a handler (process) that will be used for communication between our script and the program that we want to run.
Surprisingly, not many PHP developers are aware of this but PHP will execute the contents of backticks (`) as a shell command.Note: The backtick character (`) should not to be confused with the single quote character (‘)
<?php $output = `whoami`; echo "<pre>$output</pre>"; ?> --> www-data
Based on the above, the following is a PHP web shell in its simplest form.
It uses the system() function to execute commands that are being passed through ‘cmd’ HTTP request GET parameter.
We have established that these functions (and a few others) can be very dangerous. What is even more dangerous is that all these in-built PHP commands are enabled by default when PHP is installed and the majority of system administrators do not disable them.
If you are unsure whether they are enabled on your system, the following will return a list of the dangerous functions that are enabled.
<?php print_r(preg_grep("/^(system|exec|shell_exec|passthru|proc_open|popen|curl_exec|curl_multi_exec|parse_ini_file|show_source)$/", get_defined_functions(TRUE)["internal"])); ?>
In a default installation, we can see that all of the functions mentioned above are enabled.
 => exec  => system  => passthru  => shell_exec  => proc_open  => show_source  => parse_ini_file  => popen
NOTE:: This is article is copyright by Acunetix and we are used is for education or information purposes only.