2017-08-07 132 views
1

我試圖編寫一個PHP模塊,用於檢測在php cgi文件中調用的zend內部函數。喜歡的代碼如下所示,我想在我的代碼中找到它的名字 - 'printf'。如何獲取函數名稱,當我把它掛在一個php擴展中

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

Now我hooked與一個named function此function「zend_set_user_opcode_handler'.However我am not able到get which was hooked的function name。(它是 'printf' 在這個example。)So,what should如果我想在函數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; 
} 

回答

0

嘿傢伙我已經得到了答案。有兩種不同的方法來實現掛鉤函數的名稱。

首先,如果使用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)); 
相關問題