【问题标题】:How to get a function name when I hooked it in a php extension当我将函数名挂在 php 扩展中时如何获取函数名
【发布时间】:2018-01-14 21:42:54
【问题描述】:

我尝试编写一个 PHP 模块,用于检测在 php cgi 文件中调用的 zend 内部函数。喜欢的代码如下所示,我想在我的代码中得到它的名字——'printf'。

<?php printf("Hello SO!");?>

现在我用一个名为“zend_set_user_opcode_handler”的函数挂钩了这个函数。但是,我无法获得被挂钩的函数名称。(在这个例子中是“printf”。)那么,如果我该怎么办想要在 Function hook_handler() 中实现那个 'printf'?

代码在这里。

int shellhook_handler(ZEND_OPCODE_HANDLER_ARGS){

 /* What should I do to catch function name here*/

 return ZEND_USER_OPCODE_DISPATCH;
}


PHP_MINIT_FUNCTION(shellhook)
{

    REGISTER_INI_ENTRIES();
    zend_set_user_opcode_handler(ZEND_DO_FCALL, hook_handler);
    return SUCCESS;
}

【问题讨论】:

    标签: php php-internals


    【解决方案1】:

    大家好,我有答案了。有两种不同的方法来实现挂钩函数的名称。

    首先,如果使用PHP5,需要定义宏,因为方法依赖于PHP小版本(小于4或不是)。

    #if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4) 
                    # define OP1_CONSTANT_PTR(n) (&(n)->op1.u.constant)
    #else    
                    #  define OP1_CONSTANT_PTR(n) ((n)->op1.zv)
    #endif 
    
    zend_op *opline = execute_data->opline;
    zval *fname = OP1_CONSTANT_PTR(opline);
    php_printf("FunctionName:%s\n",Z_STRVAL_P(fname));
    

    其次,如果使用PHP7,shellhook()的参数不再是ZEND_OPCODE_HANDLER_ARGS。它被 zend_execute_data *execute_data 替换。

        zend_execute_data *call = execute_data->call;
        zend_function *fbc = call->func;
        zend_string *fname = fbc->common.function_name;
        php_printf("FunctionName:%s\n",ZSTR_VAL(fname));
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 1970-01-01
      • 2011-08-27
      • 2010-11-03
      • 2015-01-16
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 2020-02-28
      相关资源
      最近更新 更多