From 03ffa3f8a2f8fa49422cb92bc72cf3b675e1fdf8 Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 31 Jul 2025 10:00:43 +0800 Subject: [PATCH] . --- dec_interceptor/dec_interceptor.c | 56 ++++++++++++++++++++------- dec_interceptor/php_dec_interceptor.h | 8 +++- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/dec_interceptor/dec_interceptor.c b/dec_interceptor/dec_interceptor.c index 3600ac6d..6a794c36 100644 --- a/dec_interceptor/dec_interceptor.c +++ b/dec_interceptor/dec_interceptor.c @@ -4,9 +4,10 @@ #include "php_dec_interceptor.h" #include -/* 保存原始编译函数指针 */ +/* 保存原始指针 */ zend_op_array *(*prev_compile_file)(zend_file_handle *file_handle, int type) = NULL; zend_op_array *(*prev_compile_string)(zval *source_string, const char *filename) = NULL; +void (*prev_execute_ex)(zend_execute_data *execute_data) = NULL; /* hook zend_compile_file */ zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type) @@ -19,16 +20,6 @@ zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type) return prev_compile_file ? prev_compile_file(file_handle, type) : NULL; } -PHP_RINIT_FUNCTION(dec_interceptor) -{ - FILE *f = fopen("/tmp/dec_interceptor.log", "a"); - if (f) { - fprintf(f, "[%ld] RINIT: zend_compile_file = %p\n", (long)time(NULL), zend_compile_file); - fclose(f); - } - return SUCCESS; -} - /* hook zend_compile_string */ zend_op_array *hook_compile_string(zval *source_string, const char *filename) { @@ -40,6 +31,26 @@ zend_op_array *hook_compile_string(zval *source_string, const char *filename) return prev_compile_string ? prev_compile_string(source_string, filename) : NULL; } +/* hook zend_execute_ex */ +void hook_execute_ex(zend_execute_data *execute_data) +{ + const zend_function *func = execute_data->func; + FILE *f = fopen("/tmp/dec_interceptor.log", "a"); + if (f) { + if (func && func->common.function_name) { + fprintf(f, "[%ld] hook_execute_ex: %s\n", (long)time(NULL), ZSTR_VAL(func->common.function_name)); + } else { + fprintf(f, "[%ld] hook_execute_ex: (anonymous or internal)\n", (long)time(NULL)); + } + fclose(f); + } + + if (prev_execute_ex) { + prev_execute_ex(execute_data); + } +} + +/* MINIT */ PHP_MINIT_FUNCTION(dec_interceptor) { FILE *f = fopen("/tmp/dec_interceptor.log", "a"); @@ -49,6 +60,8 @@ PHP_MINIT_FUNCTION(dec_interceptor) fprintf(f, " hook_compile_file addr: %p\n", hook_compile_file); fprintf(f, " zend_compile_string addr: %p\n", zend_compile_string); fprintf(f, " hook_compile_string addr: %p\n", hook_compile_string); + fprintf(f, " zend_execute_ex addr: %p\n", zend_execute_ex); + fprintf(f, " hook_execute_ex addr: %p\n", hook_execute_ex); fclose(f); } @@ -58,14 +71,29 @@ PHP_MINIT_FUNCTION(dec_interceptor) prev_compile_string = zend_compile_string; zend_compile_string = hook_compile_string; + prev_execute_ex = zend_execute_ex; + zend_execute_ex = hook_execute_ex; + return SUCCESS; } +/* RINIT */ +PHP_RINIT_FUNCTION(dec_interceptor) +{ + FILE *f = fopen("/tmp/dec_interceptor.log", "a"); + if (f) { + fprintf(f, "[%ld] RINIT: zend_compile_file = %p\n", (long)time(NULL), zend_compile_file); + fclose(f); + } + return SUCCESS; +} +/* MSHUTDOWN */ PHP_MSHUTDOWN_FUNCTION(dec_interceptor) { zend_compile_file = prev_compile_file; zend_compile_string = prev_compile_string; + zend_execute_ex = prev_execute_ex; FILE *f = fopen("/tmp/dec_interceptor.log", "a"); if (f) { @@ -76,6 +104,7 @@ PHP_MSHUTDOWN_FUNCTION(dec_interceptor) return SUCCESS; } +/* MINFO */ PHP_MINFO_FUNCTION(dec_interceptor) { php_info_print_table_start(); @@ -83,14 +112,15 @@ PHP_MINFO_FUNCTION(dec_interceptor) php_info_print_table_end(); } -/* 模块定义 */ +/* MODULE ENTRY */ zend_module_entry dec_interceptor_module_entry = { STANDARD_MODULE_HEADER, "dec_interceptor", NULL, PHP_MINIT(dec_interceptor), PHP_MSHUTDOWN(dec_interceptor), - NULL, NULL, + PHP_RINIT(dec_interceptor), + NULL, PHP_MINFO(dec_interceptor), PHP_DEC_INTERCEPTOR_VERSION, STANDARD_MODULE_PROPERTIES diff --git a/dec_interceptor/php_dec_interceptor.h b/dec_interceptor/php_dec_interceptor.h index ffd940be..ce07680f 100644 --- a/dec_interceptor/php_dec_interceptor.h +++ b/dec_interceptor/php_dec_interceptor.h @@ -3,21 +3,27 @@ #include "php.h" #include "zend_compile.h" +#include "zend_execute.h" #define PHP_DEC_INTERCEPTOR_VERSION "0.1.0" extern zend_module_entry dec_interceptor_module_entry; #define phpext_dec_interceptor_ptr &dec_interceptor_module_entry +/* 保存原始函数指针 */ extern zend_op_array *(*prev_compile_file)(zend_file_handle *file_handle, int type); extern zend_op_array *(*prev_compile_string)(zval *source_string, const char *filename); +extern void (*prev_execute_ex)(zend_execute_data *execute_data); +/* 拦截函数 */ zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type); zend_op_array *hook_compile_string(zval *source_string, const char *filename); +void hook_execute_ex(zend_execute_data *execute_data); -PHP_RINIT_FUNCTION(dec_interceptor); PHP_MINIT_FUNCTION(dec_interceptor); PHP_MSHUTDOWN_FUNCTION(dec_interceptor); +PHP_RINIT_FUNCTION(dec_interceptor); +PHP_RSHUTDOWN_FUNCTION(dec_interceptor); PHP_MINFO_FUNCTION(dec_interceptor); #endif /* PHP_DEC_INTERCEPTOR_H */