Giter Club home page Giter Club logo

php-eval-hook's Introduction

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('LUnHEqxVDvyaiWx7w9PEnvDeey4ba++hMV8/1bvLTkQSqgyVUXOxtdPz5xi/6f5Z9fZ0nqqNwP6zYVi2YX/KqXjK5//O36quwWnFbI6jsDfENXDEcqf+6qgkv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r24qK+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

php-eval-hook's People

Contributors

extremecoders-re avatar snake66 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

php-eval-hook's Issues

Php 8

Please support Php 8

fails to compile, perhaps gcc-13 issue?

PHP 8.1
gcc-13
Please let me know if I can provide any additional detrails.

>>> Source prepared.
>>> Configuring source in /var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php-eval-hook-25e4e2a9b84b4f4c45f3d2dfa35121ed7938b889 ...
 * econf: updating php8.1/config.sub with /usr/share/gnuconfig/config.sub
 * econf: updating php8.1/config.guess with /usr/share/gnuconfig/config.guess
 * econf: updating php8.1/build/config.sub with /usr/share/gnuconfig/config.sub
 * econf: updating php8.1/build/config.guess with /usr/share/gnuconfig/config.guess
./configure --prefix=/usr --build=x86_64-pc-linux-gnu --host=x86_64-pc-linux-gnu --mandir=/usr/share/man --infodir=/usr/share/info --datadir=/usr/share --sysconfdir=/etc --localstatedir=/var/lib --datarootdir=/usr/share --disable-static --docdir=/usr/share/doc/evalhook-0.1_p20231013 --htmldir=/usr/share/doc/evalhook-0.1_p20231013/html --libdir=/usr/lib64 --with-php-config=/usr/lib64/php8.1/bin/php-config
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for a sed that does not truncate output... /bin/sed
checking for x86_64-pc-linux-gnu-pkg-config... /usr/bin/x86_64-pc-linux-gnu-pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for x86_64-pc-linux-gnu-cc... x86_64-pc-linux-gnu-cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables... 
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether x86_64-pc-linux-gnu-cc accepts -g... yes
checking for x86_64-pc-linux-gnu-cc option to enable C11 features... none needed
checking how to run the C preprocessor... x86_64-pc-linux-gnu-cc -E
checking for icc... no
checking for suncc... no
checking for system library directory... lib
checking if compiler supports -Wl,-rpath,... yes
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for PHP prefix... /usr/lib64/php8.1
checking for PHP includes... -I/usr/lib64/php8.1/include/php -I/usr/lib64/php8.1/include/php/main -I/usr/lib64/php8.1/include/php/TSRM -I/usr/lib64/php8.1/include/php/Zend -I/usr/lib64/php8.1/include/php/ext -I/usr/lib64/php8.1/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib64/php8.1/lib/extensions/no-debug-non-zts-20210902
checking for PHP installed headers prefix... /usr/lib64/php8.1/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for gawk... gawk
checking whether to enable evalhook support... yes, shared
checking for a sed that does not truncate output... /bin/sed
checking for ld used by x86_64-pc-linux-gnu-cc... /usr/x86_64-pc-linux-gnu/bin/ld
checking if the linker (/usr/x86_64-pc-linux-gnu/bin/ld) is GNU ld... yes
checking for /usr/x86_64-pc-linux-gnu/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm -B
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for stdio.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for strings.h... yes
checking for sys/stat.h... yes
checking for sys/types.h... yes
checking for unistd.h... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 1572864
checking command to parse /usr/bin/nm -B output from x86_64-pc-linux-gnu-cc object... ok
checking for objdir... .libs
checking for x86_64-pc-linux-gnu-ar... x86_64-pc-linux-gnu-ar
checking for x86_64-pc-linux-gnu-ranlib... x86_64-pc-linux-gnu-ranlib
checking for x86_64-pc-linux-gnu-strip... x86_64-pc-linux-gnu-strip
checking if x86_64-pc-linux-gnu-cc supports -fno-rtti -fno-exceptions... no
checking for x86_64-pc-linux-gnu-cc option to produce PIC... -fPIC
checking if x86_64-pc-linux-gnu-cc PIC flag -fPIC works... yes
checking if x86_64-pc-linux-gnu-cc static flag -static works... yes
checking if x86_64-pc-linux-gnu-cc supports -c -o file.o... yes
checking whether the x86_64-pc-linux-gnu-cc linker (/usr/x86_64-pc-linux-gnu/bin/ld -m elf_x86_64) supports shared libraries... yes
checking whether -lc should be explicitly linked in... no
checking dynamic linker characteristics... GNU/Linux ld.so
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... no

creating libtool
appending configuration tag "CXX" to libtool
configure: patching config.h.in
configure: creating ./config.status
config.status: creating config.h
>>> Source configured.
>>> Compiling source in /var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php-eval-hook-25e4e2a9b84b4f4c45f3d2dfa35121ed7938b889 ...
make -j10 
/bin/sh /var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/libtool --mode=compile x86_64-pc-linux-gnu-cc -I. -I/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1 -I/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/include -I/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/main -I/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1 -I/usr/lib64/php8.1/include/php -I/usr/lib64/php8.1/include/php/main -I/usr/lib64/php8.1/include/php/TSRM -I/usr/lib64/php8.1/include/php/Zend -I/usr/lib64/php8.1/include/php/ext -I/usr/lib64/php8.1/include/php/ext/date/lib  -DHAVE_CONFIG_H  -O2 -pipe -frecord-gcc-switches    -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c -o evalhook.lo  -MMD -MF evalhook.dep -MT evalhook.lo
mkdir .libs
 x86_64-pc-linux-gnu-cc -I. -I/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1 -I/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/include -I/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/main -I/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1 -I/usr/lib64/php8.1/include/php -I/usr/lib64/php8.1/include/php/main -I/usr/lib64/php8.1/include/php/TSRM -I/usr/lib64/php8.1/include/php/Zend -I/usr/lib64/php8.1/include/php/ext -I/usr/lib64/php8.1/include/php/ext/date/lib -DHAVE_CONFIG_H -O2 -pipe -frecord-gcc-switches -DZEND_COMPILE_DL_EXT=1 -c /var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c -MMD -MF evalhook.dep -MT evalhook.lo  -fPIC -DPIC -o .libs/evalhook.o
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c:11:74: error: unknown type name ‘zend_compile_position’; did you mean ‘zend_llist_position’?
   11 | static zend_op_array* (*old_compile_string)(zend_string *, const char *, zend_compile_position);
      |                                                                          ^~~~~~~~~~~~~~~~~~~~~
      |                                                                          zend_llist_position
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c:17:17: error: unknown type name ‘zend_compile_position’; did you mean ‘zend_llist_position’?
   17 |                 zend_compile_position pos)
      |                 ^~~~~~~~~~~~~~~~~~~~~
      |                 zend_llist_position
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c: In function ‘evalhook_extension_loaded’:
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c:72:13: warning: implicit declaration of function ‘zend_string_equals_cstr’; did you mean ‘zend_string_equals_ci’? [-Wimplicit-function-declaration]
   72 |         if (zend_string_equals_cstr(module, module_name, sizeof module_name) == 0) {
      |             ^~~~~~~~~~~~~~~~~~~~~~~
      |             zend_string_equals_ci
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c: In function ‘zm_activate_evalhook’:
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c:106:9: error: ‘old_compile_string’ undeclared (first use in this function); did you mean ‘zend_compile_string’?
  106 |         old_compile_string = zend_compile_string;
      |         ^~~~~~~~~~~~~~~~~~
      |         zend_compile_string
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c:106:9: note: each undeclared identifier is reported only once for each function it appears in
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c:107:31: error: ‘evalhook_compile_string’ undeclared (first use in this function); did you mean ‘zend_compile_string’?
  107 |         zend_compile_string = evalhook_compile_string;
      |                               ^~~~~~~~~~~~~~~~~~~~~~~
      |                               zend_compile_string
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c: In function ‘zm_deactivate_evalhook’:
/var/tmp/portage/dev-php/evalhook-0.1_p20231013/work/php8.1/evalhook.c:114:31: error: ‘old_compile_string’ undeclared (first use in this function); did you mean ‘zend_compile_string’?
  114 |         zend_compile_string = old_compile_string;
      |                               ^~~~~~~~~~~~~~~~~~
      |                               zend_compile_string
make: *** [Makefile:208: evalhook.lo] Error 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.