PHP - Useful Functions & disable_functions/open_basedir bypass
Learn & practice AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
PHP Command & Code Execution
PHP Command Execution
Note: A p0wny-shell php webshell can automatically check and bypass the following function if some of them be disabled.
exec - Returns last line of commands output
echo exec("uname -a");passthru - Passes commands output directly to the browser
echo passthru("uname -a");system - Passes commands output directly to the browser and returns last line
echo system("uname -a");shell_exec - Returns commands output
echo shell_exec("uname -a");`` (backticks) - Same as shell_exec()
popen - Opens read or write pipe to process of a command
proc_open - Similar to popen() but greater degree of control
preg_replace
pcntl_exec - Executes a program (by default in modern and not so modern PHP you need to load the pcntl.so module to use this function)
mail / mb_send_mail - This function is used to send mails, but it can also be abused to inject arbitrary commands inside the $options parameter. This is because php mail function usually call sendmail binary inside the system and it allows you to put extra options. However, you won't be able to see the output of the executed command, so it's recommended to create shell script that writes the output to a file, execute it using mail, and print the output:
dl - This function can be used to dynamically load a PHP extension. This function won't be present always, so you should check if it's available before trying to exploit it. Read this page to learn how to exploit this function.
PHP Code Execution
Apart from eval there are other ways to execute PHP code: include/require can be used for remote code execution in the form of Local File Include and Remote File Include vulnerabilities.
disable_functions & open_basedir
Disabled functions is the setting that can be configured in .ini files in PHP that will forbid the use of the indicated functions. Open basedir is the setting that indicates to PHP the folder that it can access.
The PHP setting sue to be configured in the path /etc/php7/conf.d or similar.
Both configuration can be seen in the output of phpinfo():

open_basedir Bypass
open_basedir will configure the folders that PHP can access, you won't be able to to write/read/execute any file outside those folders, but also you won't even be able to list other directories.
However, if somehow you are able to execute arbitrary PHP code you can try the following chunk of codes to try to bypass the restriction.
Listing dirs with glob:// bypass
In this first example the glob:// protocol with some path bypass is used:
Note1: In the path you can also use /e??/* to list /etc/* and any other folder.
Note2: It looks like part of the code is duplicated, but that's actually necessary!
Note3: This example is only useful to list folders not to read files
Full open_basedir bypass abusing FastCGI
If you want to learn more about PHP-FPM and FastCGI you can read the first section of this page.
If php-fpm is configured you can abuse it to completely bypass open_basedir:


Note that the first thing you need to do is find where is the unix socket of php-fpm. It use to be under /var/run so you can use the previous code to list the directory and find it.
Code from here.
This scripts will communicate with unix socket of php-fpm (usually located in /var/run if fpm is used) to execute arbitrary code. The open_basedir settings will be overwritten by the PHP_VALUE attribute that is sent.
Note how eval is used to execute the PHP code you send inside the cmd parameter.
Also note the commented line 324, you can uncomment it and the payload will automatically connect to the given URL and execute the PHP code contained there.
Just access http://vulnerable.com:1337/l.php?cmd=echo file_get_contents('/etc/passwd'); to get the content of the /etc/passwd file.
You may be thinking that just in the same way we have overwritten open_basedir configuration we can overwrite disable_functions. Well, try it, but it won't work, apparently disable_functions can only be configured in a .ini php configuration file and the changes you perform using PHP_VALUE won't be effective on this specific setting.
disable_functions Bypass
If you manage have PHP code executing inside a machine you probably want to go to the next level and execute arbitrary system commands. In this situation is usual to discover that most or all the PHP functions that allow to execute system commands have been disabled in disable_functions.
So, lets see how you can bypass this restriction (if you can)
Automatic bypass discovery
You can use the tool https://github.com/teambi0s/dfunc-bypasser and it will indicate you which function (if any) you can use to bypass disable_functions.
Bypassing using other system functions
Just return to the beginning of this page and check if any of the command executing functions isn't disabled and available in the environment. If you find just 1 of them, you will be able to use it to execute arbitrary system commands.
LD_PRELOAD bypass
It's well known that some functions in PHP like mail()are going to execute binaries inside the system. Therefore, you can abuse them using the environment variable LD_PRELOAD to make them load an arbitrary library that can execute anything.
Functions that can be used to bypass disable_functions with LD_PRELOAD
mailmb_send_mail: Effective when thephp-mbstringmodule is installed.imap_mail: Works ifphp-imapmodule is present.libvirt_connect: Requires thephp-libvirt-phpmodule.gnupg_init: Utilizable with thephp-gnupgmodule installed.new imagick(): This class can be abused to bypass restrictions. Detailed exploitation techniques can be found in a comprehensive writeup here.
You can find here the fuzzing script that was used to find those functions.
Here is a library you can compile to abuse the LD_PRELOAD env variable:
Bypass using Chankro
In order to abuse this misconfiguration you can Chankro. This is a tool that will generate a PHP exploit that you need to upload to the vulnerable server and execute it (access it via web).
Chankro will write inside the victims disc the library and the reverse shell you want to execute and will use the**LD_PRELOAD trick + PHP mail()** function to execute the reverse shell.
Note that in order to use Chankro, mail and putenv cannot appear inside the disable_functions list.
In the following example you can see how to create a chankro exploit for arch 64, that will execute whoami and save the out in /tmp/chankro_shell.out, chankro will write the library and the payload in /tmp and the final exploit is going to be called bicho.php (that's the file you need to upload to the victims server):
If you find that mail function is blocked by disabled functions, you may still be able to use the function mb_send_mail. More information about this technique and Chankro here: https://www.tarlogic.com/en/blog/how-to-bypass-disable_functions-and-open_basedir/
"Bypass" using PHP capabilities
Note that using PHP you can read and write files, create directories and change permissions. You can even dump databases. Maybe using PHP to enumerate the box you can find a way to escalate privileges/execute commands (for example reading some private ssh key).
I have created a webshell that makes very easy to perform this actions (note that most webshells will offer you this options also): https://github.com/carlospolop/phpwebshelllimited
Modules/Version dependent bypasses
There are several ways to bypass disable_functions if some specific module is being used or exploit some specific PHP version:
5.* - exploitable with minor changes to the PoC
7.0 - all versions to date
7.1 - all versions to date
7.2 - all versions to date
7.3 - all versions to date
7.4 - all versions to date
8.0 - all versions to date
Automatic Tool
The following script tries some of the methods commented here: https://github.com/l3m0n/Bypass_Disable_functions_Shell/blob/master/shell.php
Other Interesting PHP functions
List of functions which accept callbacks
These functions accept a string parameter which could be used to call a function of the attacker's choice. Depending on the function the attacker may or may not have the ability to pass a parameter. In that case an Information Disclosure function like phpinfo() could be used.
Information Disclosure
Most of these function calls are not sinks. But rather it maybe a vulnerability if any of the data returned is viewable to an attacker. If an attacker can see phpinfo() it is definitely a vulnerability.
Other
Filesystem Functions
According to RATS all filesystem functions in php are nasty. Some of these don't seem very useful to the attacker. Others are more useful than you might think. For instance if allow_url_fopen=On then a url can be used as a file path, so a call to copy($_GET['s'], $_GET['d']); can be used to upload a PHP script anywhere on the system. Also if a site is vulnerable to a request send via GET everyone of those file system functions can be abused to channel and attack to another host through your server.
Open filesystem handler
Write to filesystem (partially in combination with reading)
Read from filesystem
Learn & practice AWS Hacking:
HackTricks Training AWS Red Team Expert (ARTE)
Learn & practice GCP Hacking:
HackTricks Training GCP Red Team Expert (GRTE)
Last updated