This commit is contained in:
hailin 2025-07-30 19:18:53 +08:00
parent be725921f7
commit fe288136f8
1 changed files with 35 additions and 13 deletions

View File

@ -2,22 +2,29 @@
#include "php_ini.h" #include "php_ini.h"
#include "ext/standard/info.h" #include "ext/standard/info.h"
#include "php_dec_interceptor.h" #include "php_dec_interceptor.h"
#include "main/php_streams.h" // ✅ 必须包含,用于 php_stream 结构 #include "main/php_streams.h"
static zend_op_array* (*original_compile_file)(zend_file_handle *file_handle, int type); 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) { 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) { if (file_handle && file_handle->filename) {
const char *source_path = file_handle->filename; const char *source_path = file_handle->filename;
php_printf("[dec_interceptor] compiling: %s\n", source_path); fprintf(stderr, "[hook] filename = %s\n", source_path);
fprintf(stderr, "[hook] file_handle->type = %d\n", file_handle->type);
// ✅ 只处理 install.php
if (!strstr(source_path, "install.php")) {
fprintf(stderr, "[skip] not install.php, skip dump: %s\n", source_path);
return original_compile_file(file_handle, type);
}
char *buffer = NULL; char *buffer = NULL;
size_t size = 0; size_t size = 0;
// ✅ 情况1标准文件句柄
if (file_handle->type == ZEND_HANDLE_FP && file_handle->handle.fp) { 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; FILE *fp = file_handle->handle.fp;
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
size = ftell(fp); size = ftell(fp);
@ -25,12 +32,17 @@ static zend_op_array* custom_compile_file(zend_file_handle *file_handle, int typ
if (size > 0 && size < 100 * 1024 * 1024) { if (size > 0 && size < 100 * 1024 * 1024) {
buffer = emalloc(size + 1); buffer = emalloc(size + 1);
fread(buffer, 1, size, fp); 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'; buffer[size] = '\0';
} }
}
// ✅ 情况2stream 句柄swoole_loader 加密文件解密后走这个分支)
} else if (file_handle->type == ZEND_HANDLE_STREAM && file_handle->handle.stream.handle) { } 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 *stream = (php_stream *)file_handle->handle.stream.handle;
php_stream_seek(stream, 0, SEEK_END); php_stream_seek(stream, 0, SEEK_END);
@ -40,12 +52,17 @@ static zend_op_array* custom_compile_file(zend_file_handle *file_handle, int typ
if (size > 0 && size < 100 * 1024 * 1024) { if (size > 0 && size < 100 * 1024 * 1024) {
buffer = emalloc(size + 1); buffer = emalloc(size + 1);
size_t read_bytes = php_stream_read(stream, buffer, size); 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'; buffer[read_bytes] = '\0';
size = read_bytes; size = read_bytes;
} }
} }
}
// ✅ 保存为 .dec.php
if (buffer && size > 0) { if (buffer && size > 0) {
char output_path[PATH_MAX]; char output_path[PATH_MAX];
snprintf(output_path, sizeof(output_path), "%s.dec.php", source_path); snprintf(output_path, sizeof(output_path), "%s.dec.php", source_path);
@ -54,27 +71,32 @@ static zend_op_array* custom_compile_file(zend_file_handle *file_handle, int typ
if (out) { if (out) {
fwrite(buffer, 1, size, out); fwrite(buffer, 1, size, out);
fclose(out); fclose(out);
fprintf(stderr, "[dump] saved to: %s\n", output_path); fprintf(stderr, "[dump] saved to: %s (%zu bytes)\n", output_path, size);
} else { } else {
fprintf(stderr, "[dump] failed to save: %s\n", output_path); fprintf(stderr, "[error] failed to open output file: %s\n", output_path);
} }
efree(buffer); efree(buffer);
} else {
fprintf(stderr, "[skip] buffer is NULL or size = 0\n");
} }
} else {
fprintf(stderr, "[warn] file_handle or filename is NULL\n");
} }
return original_compile_file(file_handle, type); return original_compile_file(file_handle, type);
} }
PHP_MINIT_FUNCTION(dec_interceptor) { PHP_MINIT_FUNCTION(dec_interceptor) {
original_compile_file = zend_compile_file; original_compile_file = zend_compile_file;
zend_compile_file = custom_compile_file; zend_compile_file = custom_compile_file;
fprintf(stderr, "[init] dec_interceptor loaded\n");
return SUCCESS; return SUCCESS;
} }
PHP_MSHUTDOWN_FUNCTION(dec_interceptor) { PHP_MSHUTDOWN_FUNCTION(dec_interceptor) {
zend_compile_file = original_compile_file; zend_compile_file = original_compile_file;
fprintf(stderr, "[shutdown] dec_interceptor unloaded\n");
return SUCCESS; return SUCCESS;
} }