Skip to content

A PHP extension for hooking eval()

License

Notifications You must be signed in to change notification settings

extremecoders-re/php-eval-hook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-eval-hook

A PHP extension for hooking eval(). Useful for dumping eval-obfuscated code. The extension is expected to work with PHP 7.x in general. May work with PHP 5.x but is untested.

Compiling steps

A precompiled extension can be downloaded from releases. It has been compiled against PHP 7.2.24 available on Ubuntu 18.04 repos. The precompiled extension is not guaranteed to work on your system. Hence it's always recommended to compile from source as shown below.

  1. Install php and php-dev packages. Here we are using the packages available on the official Ubuntu 18.04 repos.

    $ sudo apt install php7.2 php7.2-dev
    
  2. Clone the repository

    $ git clone https://github.com/extremecoders-re/php-eval-hook
    
  3. Run phpize. This will generate the Makefile and other files needed for buidling the extension.

    $ cd php-eval-hook
    $ phpize
    
  4. Build the extension. make install copies the .so to the appropriate location.

    $ ./configure --enable-evalhook
    
    $ make
    
    $ make install
    Installing shared extensions:     /usr/lib/php/20170718/
    

Registering the extension with PHP

  1. Find the location of php.ini.

    $ php -r 'phpinfo();' | grep php.ini
    Configuration File (php.ini) Path => /etc/php/7.2/cli
    Loaded Configuration File => /etc/php/7.2/cli/php.ini
    
  2. Edit php.ini and add the line extension=evalhook.so at the end.

    $ echo "extension=evalhook.so" >> /etc/php/7.2/cli/php.ini
    
  3. Ensure that the extension is properly loaded.

    $ php -r 'print_r(get_loaded_extensions());' | grep evalhook
        [14] => evalhook
    
    $ php -r 'phpinfo();' | grep eval
    evalhook
    eval() hooking => enabled
    callback function => __eval
    

Usage

You must define a callback function named __eval in your PHP code. The extension will call this function whenever it encounters an eval. Inside your callback you can print the code to stdout or dump to a file or whatever you want.

Example

Original Code

<?php
function test_obfuscated()
{
	echo("This is an obfuscated function\n");
}

test_obfuscated();
?>

obfuscated.php [Generated from Simple online PHP obfuscator]

<?php
eval(str_rot13(gzinflate(str_rot13(base64_decode('LUnHEqxVDvyaiWx7w9PEnvDe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o5QyvurFRrFP0/uI9Zt4X7ewWPlxIRCfG02k0AwDWEgGG5YS22dgupM3nyT8hLgoefpW3TccASg5oQbQsq2qr/DpCE9N9t+fBxHr24qK+NWSqaFgjF/NWngIws2uVZYE0r2hU8tgUyi9+HJuOdPLKjI6eUo/MTcPZ8a3vrkGr5CFqRr1UZxVhj2d9mitp1c2zf4jKIkR6Pq/E2rLX6j997/A8+9/AA==')))));
?>

harness.php

<?php
function __eval($code, $file) {
  echo "eval() @ {$file}:\n{$code}\n\n";

  // return FALSE if you want to prevent the eval()
  // return false;

  // return a string if you want to replace the eval-d code
  // return 'echo 2;';

  // return nothing to continue execution normally
}

include("obfuscated.php");
?>
$ php obfuscated.php
This is an obfuscated function
$ php harness.php | tail

eval() @ /workspace/php7/obfuscated.php(2) : eval()'d code(1) : eval()'d code(1) : eval()'d code(1) : eval()'d code(1) : eval()'
d code(1) : eval()'d code(1) : eval()'d code(1) : eval()'d code(1) : eval()'d code(1) : eval()'d code(1) : eval()'d code(1) : ev
al()'d code(1) : eval()'d code(1) : eval()'d code(1) : eval()'d code(1) : eval()'d code:
function test_obfuscated()
{
        echo("This is an obfuscated function\n");
}

test_obfuscated();

This is an obfuscated function

Credits

The extension is based on php-eval.

License

MIT