disable_functions bypass - php-fpm/FastCGI

PHP-FPM

PHP fastCGI Process Manager is an alternative PHP FastCGI implementation with some additional features (mostly) useful for heavy-loaded sites. Internally, PHP-FPM is organized as a “master process” managing pools of individual “worker processes.” When the web server has a request for a PHP script, the web server uses a proxy, FastCGI connection to forward the request to the PHP-FPM service. The PHP-FPM service can listen for these requests on the host server’s network ports or through Unix sockets. Although requests pass via a proxy connection, the PHP-FPM service must run on the same server as the web server. Notably, the proxy connection for PHP-FPM is not the same as a traditional proxy connection. As PHP-FPM receives a proxied connection, a free PHP-FPM worker accepts the web server’s request. PHP-FPM then compiles and executes the PHP script, sending the output back to the web server. Once a PHP-FPM worker finishes handling a request, the system releases the worker and waits for new requests.

But what is CGI and FastCGI?

CGI

Normally web pages, files and all of the documents which are transferred from the web server to the browser are stored in a specific public directory such as home/user/public_html. When the browser requests certain content, the server checks this directory and sends the required file to the browser.

If CGI is installed on the server, the specific cgi-bin directory is also added there, for example home/user/public_html/cgi-bin. CGI scripts are stored in this directory. Each file in the directory is treated as an executable program. When accessing a script from the directory, the server sends request to the application, responsible for this script, instead of sending file's content to the browser. After the input data processing is completed, the application sends the output data to the web server which forwards the data to the HTTP client.

For example, when the CGI script http://mysitename.com/**cgi-bin/file.pl** is accessed, the server will run the appropriate Perl application through CGI. The data generated from script execution will be sent by the application to the web server. The server, on the other hand, will transfer data to the browser. If the server did not have CGI, the browser would have displayed the .pl file code itself. (explanation from here)

FastCGI

FastCGI is a newer web technology, an improved CGI version as the main functionality remains the same.

The need to develop FastCGI is that Web was arisen by applications' rapid development and complexity, as well to address the scalability shortcomings of CGI technology. To meet those requirements Open Market introduced FastCGI – a high performance version of the CGI technology with enhanced capabilities.

disable_functions bypass

It's possible to run PHP code abusing the FastCGI and avoiding the disable_functions limitations.

Via Gopherus

Using Gopherus you can generate a payload to send to the FastCGI listener and execute arbitrary commands:

Then, you can grab the urlencoded payload and decode it and transform to base64, [using this recipe of cyberchef for example](http://icyberchef.com/#recipe=URL_Decode%28%29To_Base64%28'A-Za-z0-9%2B/%3D'%29&input=JTAxJTAxJTAwJTAxJTAwJTA4JTAwJTAwJTAwJTAxJTAwJTAwJTAwJTAwJTAwJTAwJTAxJTA0JTAwJTAxJTAxJTA0JTA0JTAwJTBGJTEwU0VSVkVSX1NPRlRXQVJFZ28lMjAvJTIwZmNnaWNsaWVudCUyMCUwQiUwOVJFTU9URV9BRERSMTI3LjAuMC4xJTBGJTA4U0VSVkVSX1BST1RPQ09MSFRUUC8xLjElMEUlMDJDT05URU5UX0xFTkdUSDc2JTBFJTA0UkVRVUVTVF9NRVRIT0RQT1NUJTA5S1BIUF9WQUxVRWFsbG93X3VybF9pbmNsdWRlJTIwJTNEJTIwT24lMEFkaXNhYmxlX2Z1bmN0aW9ucyUyMCUzRCUyMCUwQWF1dG9fcHJlcGVuZF9maWxlJTIwJTNEJTIwcGhwJTNBLy9pbnB1dCUwRiUxN1NDUklQVF9GSUxFTkFNRS92YXIvd3d3L2h0bWwvaW5kZXgucGhwJTBEJTAxRE9DVU1FTlRfUk9PVC8lMDAlMDAlMDAlMDAlMDElMDQlMDAlMDElMDAlMDAlMDAlMDAlMDElMDUlMDAlMDElMDBMJTA0JTAwJTNDJTNGcGhwJTIwc3lzdGVtJTI4JTI3d2hvYW1pJTIwJTNFJTIwL3RtcC93aG9hbWkudHh0JTI3JTI5JTNCZGllJTI4JTI3LS0tLS1NYWRlLWJ5LVNweUQzci0tLS0tJTBBJTI3JTI5JTNCJTNGJTNFJTAwJTAwJTAwJTAw). And then copy/pasting the abse64 in this php code:

Uploading and accessing this script the exploit is going to be sent to FastCGI (disabling disable_functions) and the specified commands are going to be executed.

PHP exploit

Code from here.

Using the previous function you will see that the function system is still disabled but phpinfo() shows a disable_functions empty:

So, I think that you can only set disable_functions via php .ini config files and the PHP_VALUE won't override that setting.

****FuckFastGCI****

This is a php script to exploit fastcgi protocol to bypass open_basedir and disable_functions. It will help you to bypass strict disable_functions to RCE by loading the malicious extension. You can access it here: https://github.com/w181496/FuckFastcgi

You will find that the exploit is very similar to the previous code, but instead of trying to bypass disable_functions using PHP_VALUE, it tries to load an external PHP module to execute code using the parameters extension_dir and extension inside the variable PHP_ADMIN_VALUE. NOTE1: You probably will need to recompile the extension with the same PHP version that the server is using (you can check it inside the output of phpinfo):

PHP-FPM Remote Code Execution Vulnerability (CVE-2019–11043)

You can exploit this vulnerability with phuip-fpizdam and test is using this docker environment: https://github.com/vulhub/vulhub/tree/master/php/CVE-2019-11043. You can also find an analysis of the vulnerability here.

Last updated