From 6230214f8b2b863866841e6bab4bd719e8dbf91e Mon Sep 17 00:00:00 2001 From: hailin Date: Thu, 31 Jul 2025 10:03:59 +0800 Subject: [PATCH] . --- dec_interceptor/dec_interceptor.c | 75 ++++++++++++--------------- dec_interceptor/php_dec_interceptor.h | 9 ++-- 2 files changed, 35 insertions(+), 49 deletions(-) diff --git a/dec_interceptor/dec_interceptor.c b/dec_interceptor/dec_interceptor.c index 6a794c36..6b712343 100644 --- a/dec_interceptor/dec_interceptor.c +++ b/dec_interceptor/dec_interceptor.c @@ -4,12 +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; +zend_internal_function_handler_t prev_execute_internal = NULL; -/* hook zend_compile_file */ zend_op_array *hook_compile_file(zend_file_handle *file_handle, int type) { FILE *f = fopen("/tmp/dec_interceptor.log", "a"); @@ -20,7 +18,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; } -/* hook zend_compile_string */ zend_op_array *hook_compile_string(zval *source_string, const char *filename) { FILE *f = fopen("/tmp/dec_interceptor.log", "a"); @@ -31,53 +28,24 @@ 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) +void hook_execute_internal(zend_execute_data *execute_data, zval *return_value) { - const zend_function *func = execute_data->func; FILE *f = fopen("/tmp/dec_interceptor.log", "a"); if (f) { + const zend_function *func = execute_data->func; if (func && func->common.function_name) { - fprintf(f, "[%ld] hook_execute_ex: %s\n", (long)time(NULL), ZSTR_VAL(func->common.function_name)); + fprintf(f, "[%ld] hook_execute_internal: %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)); + fprintf(f, "[%ld] hook_execute_internal: (anonymous internal)\n", (long)time(NULL)); } fclose(f); } - if (prev_execute_ex) { - prev_execute_ex(execute_data); + if (prev_execute_internal) { + prev_execute_internal(execute_data, return_value); } } -/* MINIT */ -PHP_MINIT_FUNCTION(dec_interceptor) -{ - FILE *f = fopen("/tmp/dec_interceptor.log", "a"); - if (f) { - fprintf(f, "[%ld] MINIT: dec_interceptor initialized\n", (long)time(NULL)); - fprintf(f, " zend_compile_file addr: %p\n", zend_compile_file); - 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); - } - - prev_compile_file = zend_compile_file; - zend_compile_file = hook_compile_file; - - 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"); @@ -88,12 +56,35 @@ PHP_RINIT_FUNCTION(dec_interceptor) return SUCCESS; } -/* MSHUTDOWN */ +PHP_MINIT_FUNCTION(dec_interceptor) +{ + FILE *f = fopen("/tmp/dec_interceptor.log", "a"); + if (f) { + fprintf(f, "[%ld] MINIT: dec_interceptor initialized\n", (long)time(NULL)); + fprintf(f, " zend_compile_file addr: %p\n", zend_compile_file); + 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); + fclose(f); + } + + prev_compile_file = zend_compile_file; + zend_compile_file = hook_compile_file; + + prev_compile_string = zend_compile_string; + zend_compile_string = hook_compile_string; + + prev_execute_internal = zend_execute_internal; + zend_execute_internal = hook_execute_internal; + + return SUCCESS; +} + PHP_MSHUTDOWN_FUNCTION(dec_interceptor) { zend_compile_file = prev_compile_file; zend_compile_string = prev_compile_string; - zend_execute_ex = prev_execute_ex; + zend_execute_internal = prev_execute_internal; FILE *f = fopen("/tmp/dec_interceptor.log", "a"); if (f) { @@ -104,7 +95,6 @@ PHP_MSHUTDOWN_FUNCTION(dec_interceptor) return SUCCESS; } -/* MINFO */ PHP_MINFO_FUNCTION(dec_interceptor) { php_info_print_table_start(); @@ -112,7 +102,6 @@ PHP_MINFO_FUNCTION(dec_interceptor) php_info_print_table_end(); } -/* MODULE ENTRY */ zend_module_entry dec_interceptor_module_entry = { STANDARD_MODULE_HEADER, "dec_interceptor", diff --git a/dec_interceptor/php_dec_interceptor.h b/dec_interceptor/php_dec_interceptor.h index ce07680f..355079bf 100644 --- a/dec_interceptor/php_dec_interceptor.h +++ b/dec_interceptor/php_dec_interceptor.h @@ -10,20 +10,17 @@ 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); +extern zend_internal_function_handler_t prev_execute_internal; -/* 拦截函数 */ 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); +void hook_execute_internal(zend_execute_data *execute_data, zval *return_value); +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 */