Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code execution after __halt_compiler

Tags:

php

symfony

Next time in my life i see some bytecode after function __halt_compiler. In this case it's symfony installer. How to do that ? How to generate code which will be executable after __halt_compiler ?

like image 656
Abyss Avatar asked Jun 02 '17 17:06

Abyss


1 Answers

I guess you mean https://symfony.com/installer, a PHAR archive that starts like this:

#!/usr/bin/env php
<?php
/**
 * Generated by Box.
 *
 * @link https://github.com/herrera-io/php-box/
 */
if (class_exists('Phar')) {
Phar::mapPhar('default.phar');
require 'phar://' . __FILE__ . '/symfony';
}
__HALT_COMPILER(); ?>

Conceptually speaking, PHAR archives are not too different from self-extracting ZIP files (in fact ZIP is among the supported internal formats). The archive contains:

  • A stub, which is the short PHP script you can see.

  • A manifest with a description of the archive contentes

  • An archive with the contents themselves

  • An optional signature

The stub on top is a regular PHP script so the PHAR archive can run with the regular PHP interpreter rather than requiring a dedicated executable:

php foo.phar

This script block ends with __HALT_COMPILER(); to::

  1. Prevent the rest of the file from being parsed and possibly executed as PHP code (the main script does all that's needed for the initial phase).

  2. Populate the __COMPILER_HALT_OFFSET__ constant so the PHAR library can easily detect where archived data starts.

This scripts does three things:

  1. Detect whether the Phar library is available in the computer.
  2. Load current file as Phar archive and register the manifest with the content description.
  3. Execute the symfony script inside the archive, which is the actual installer code.
like image 148
Álvaro González Avatar answered Nov 18 '22 15:11

Álvaro González