From e370708ac59dbd547624e779f7a1a7519b8cc4d0 Mon Sep 17 00:00:00 2001 From: hailin Date: Wed, 30 Jul 2025 19:28:14 +0800 Subject: [PATCH] . --- dec_interceptor/dec_interceptor.c | 130 ++++++++++++++++++------------ 1 file changed, 77 insertions(+), 53 deletions(-) diff --git a/dec_interceptor/dec_interceptor.c b/dec_interceptor/dec_interceptor.c index 1cd02e80..21cacba8 100644 --- a/dec_interceptor/dec_interceptor.c +++ b/dec_interceptor/dec_interceptor.c @@ -2,76 +2,98 @@ #include "php_ini.h" #include "ext/standard/info.h" #include "php_dec_interceptor.h" -#include "main/php_streams.h" // ✅ 必须包含,用于 php_stream 结构 -#include // ✅ 用于 strcmp() +#include "main/php_streams.h" static zend_op_array* (*original_compile_file)(zend_file_handle *file_handle, int type); static zend_op_array* custom_compile_file(zend_file_handle *file_handle, int type) { - fprintf(stderr, "[hook] file_handle->type = %d\n", file_handle->type); + fprintf(stderr, "[hook] zend_compile_file called\n"); - if (file_handle && file_handle->filename) { - const char *source_path = file_handle->filename; + if (file_handle) { + if (file_handle->filename) { + const char *source_path = file_handle->filename; + fprintf(stderr, "[hook] filename = %s\n", source_path); + fprintf(stderr, "[hook] file_handle->type = %d\n", file_handle->type); - // ✅ 仅处理 install.php 文件 - const char *filename_only = strrchr(source_path, '/'); - if (!filename_only) filename_only = source_path; // 没有 / 就是本身 - else filename_only += 1; // skip '/' + // ✅ 强化路径判断:只要末尾是 install.php 就处理 + const char *p = strrchr(source_path, '/'); + const char *basename = p ? (p + 1) : source_path; - if (strcmp(filename_only, "install.php") != 0) { - return original_compile_file(file_handle, type); // ⛔ 忽略非目标文件 - } - - php_printf("[dec_interceptor] compiling: %s\n", source_path); - - char *buffer = NULL; - size_t size = 0; - - // ✅ 情况1:标准文件句柄 - if (file_handle->type == ZEND_HANDLE_FP && file_handle->handle.fp) { - FILE *fp = file_handle->handle.fp; - fseek(fp, 0, SEEK_END); - size = ftell(fp); - fseek(fp, 0, SEEK_SET); - - if (size > 0 && size < 100 * 1024 * 1024) { - buffer = emalloc(size + 1); - fread(buffer, 1, size, fp); - buffer[size] = '\0'; + if (strcmp(basename, "install.php") != 0) { + fprintf(stderr, "[skip] not install.php, skip dump: %s\n", source_path); + return original_compile_file(file_handle, type); } - // ✅ 情况2:stream 句柄(swoole_loader 加密文件解密后走这个分支) - } else if (file_handle->type == ZEND_HANDLE_STREAM && file_handle->handle.stream.handle) { - php_stream *stream = (php_stream *)file_handle->handle.stream.handle; + fprintf(stderr, "[match] matched install.php, start dumping...\n"); - php_stream_seek(stream, 0, SEEK_END); - size = php_stream_tell(stream); - php_stream_seek(stream, 0, SEEK_SET); + char *buffer = NULL; + size_t size = 0; - if (size > 0 && size < 100 * 1024 * 1024) { - buffer = emalloc(size + 1); - size_t read_bytes = php_stream_read(stream, buffer, size); - buffer[read_bytes] = '\0'; - size = read_bytes; + if (file_handle->type == ZEND_HANDLE_FP && file_handle->handle.fp) { + fprintf(stderr, "[info] Detected ZEND_HANDLE_FP\n"); + FILE *fp = file_handle->handle.fp; + fseek(fp, 0, SEEK_END); + size = ftell(fp); + fseek(fp, 0, SEEK_SET); + + if (size > 0 && size < 100 * 1024 * 1024) { + buffer = emalloc(size + 1); + if (fread(buffer, 1, size, fp) != size) { + fprintf(stderr, "[warn] fread failed on %s\n", source_path); + efree(buffer); + buffer = NULL; + } else { + buffer[size] = '\0'; + } + } + + } else if (file_handle->type == ZEND_HANDLE_STREAM && file_handle->handle.stream.handle) { + fprintf(stderr, "[info] Detected ZEND_HANDLE_STREAM\n"); + php_stream *stream = (php_stream *)file_handle->handle.stream.handle; + + php_stream_seek(stream, 0, SEEK_END); + size = php_stream_tell(stream); + php_stream_seek(stream, 0, SEEK_SET); + + if (size > 0 && size < 100 * 1024 * 1024) { + buffer = emalloc(size + 1); + size_t read_bytes = php_stream_read(stream, buffer, size); + if (read_bytes <= 0) { + fprintf(stderr, "[warn] php_stream_read failed: %zu bytes\n", read_bytes); + efree(buffer); + buffer = NULL; + } else { + buffer[read_bytes] = '\0'; + size = read_bytes; + } + } } - } - // ✅ 保存为 .dec.php - if (buffer && size > 0) { - char output_path[PATH_MAX]; - snprintf(output_path, sizeof(output_path), "%s.dec.php", source_path); + if (buffer && size > 0) { + char output_path[PATH_MAX]; + snprintf(output_path, sizeof(output_path), "/tmp/dump_install_%ld.dec.php", time(NULL)); - FILE *out = fopen(output_path, "wb"); - if (out) { - fwrite(buffer, 1, size, out); - fclose(out); - fprintf(stderr, "[dump] saved to: %s\n", output_path); + FILE *out = fopen(output_path, "wb"); + if (out) { + fwrite(buffer, 1, size, out); + fclose(out); + fprintf(stderr, "[dump] saved to: %s (%zu bytes)\n", output_path, size); + + // ✅ 打印内容预览(前200字节) + fprintf(stderr, "[preview] %.200s\n", buffer); + } else { + fprintf(stderr, "[error] failed to open output file: %s\n", output_path); + } + + efree(buffer); } else { - fprintf(stderr, "[dump] failed to save: %s\n", output_path); + fprintf(stderr, "[skip] buffer is NULL or size = 0\n"); } - - efree(buffer); + } else { + fprintf(stderr, "[warn] file_handle->filename is NULL\n"); } + } else { + fprintf(stderr, "[warn] file_handle is NULL\n"); } return original_compile_file(file_handle, type); @@ -81,11 +103,13 @@ static zend_op_array* custom_compile_file(zend_file_handle *file_handle, int typ PHP_MINIT_FUNCTION(dec_interceptor) { original_compile_file = zend_compile_file; zend_compile_file = custom_compile_file; + fprintf(stderr, "[init] dec_interceptor loaded\n"); return SUCCESS; } PHP_MSHUTDOWN_FUNCTION(dec_interceptor) { zend_compile_file = original_compile_file; + fprintf(stderr, "[shutdown] dec_interceptor unloaded\n"); return SUCCESS; }