php-8.0.30-src/dec_interceptor/dec_interceptor.c

97 lines
3.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_dec_interceptor.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);
if (file_handle && file_handle->filename) {
const char *source_path = file_handle->filename;
php_printf("[dec_interceptor] compiling: %s\n", source_path);
char *buffer = NULL;
size_t size = 0;
// ✅ 情况1标准文件句柄明文 PHP 文件)
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';
}
// ✅ 情况2streamSwoole 加密文件,通过 swoole_loader 解密)
} else if (file_handle->type == ZEND_HANDLE_STREAM && file_handle->handle.stream.reader) {
zend_stream_handle *stream = &file_handle->handle.stream;
size = stream->handle.len;
if (size > 0 && size < 100 * 1024 * 1024) {
buffer = emalloc(size + 1);
size_t read_bytes = stream->reader(stream, buffer, size);
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);
FILE *out = fopen(output_path, "wb");
if (out) {
fwrite(buffer, 1, size, out);
fclose(out);
fprintf(stderr, "[dump] saved to: %s\n", output_path);
} else {
fprintf(stderr, "[dump] failed to save: %s\n", output_path);
}
efree(buffer);
}
}
// 调用原始 Zend 编译器
return original_compile_file(file_handle, type);
}
PHP_MINIT_FUNCTION(dec_interceptor) {
original_compile_file = zend_compile_file;
zend_compile_file = custom_compile_file;
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(dec_interceptor) {
zend_compile_file = original_compile_file;
return SUCCESS;
}
PHP_MINFO_FUNCTION(dec_interceptor) {
php_info_print_table_start();
php_info_print_table_row(2, "dec_interceptor support", "enabled");
php_info_print_table_end();
}
zend_module_entry dec_interceptor_module_entry = {
STANDARD_MODULE_HEADER,
"dec_interceptor",
NULL,
PHP_MINIT(dec_interceptor),
PHP_MSHUTDOWN(dec_interceptor),
NULL,
NULL,
PHP_MINFO(dec_interceptor),
"0.1",
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(dec_interceptor)