This commit is contained in:
parent
fe288136f8
commit
b884b966ca
|
|
@ -9,84 +9,97 @@ static zend_op_array* (*original_compile_file)(zend_file_handle *file_handle, in
|
||||||
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] zend_compile_file called\n");
|
fprintf(stderr, "[hook] zend_compile_file called\n");
|
||||||
|
|
||||||
if (file_handle && file_handle->filename) {
|
if (file_handle) {
|
||||||
const char *source_path = file_handle->filename;
|
if (file_handle->filename) {
|
||||||
fprintf(stderr, "[hook] filename = %s\n", source_path);
|
const char *source_path = file_handle->filename;
|
||||||
fprintf(stderr, "[hook] file_handle->type = %d\n", file_handle->type);
|
fprintf(stderr, "[hook] filename = %s\n", source_path);
|
||||||
|
fprintf(stderr, "[hook] file_handle->type = %d\n", file_handle->type);
|
||||||
|
|
||||||
// ✅ 只处理 install.php
|
// ✅ 强化路径判断:只要末尾是 install.php 就处理
|
||||||
if (!strstr(source_path, "install.php")) {
|
const char *p = strrchr(source_path, '/');
|
||||||
fprintf(stderr, "[skip] not install.php, skip dump: %s\n", source_path);
|
const char *basename = p ? (p + 1) : source_path;
|
||||||
return original_compile_file(file_handle, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *buffer = NULL;
|
if (strcmp(basename, "install.php") != 0) {
|
||||||
size_t size = 0;
|
fprintf(stderr, "[skip] not install.php, skip dump: %s\n", source_path);
|
||||||
|
return original_compile_file(file_handle, type);
|
||||||
|
}
|
||||||
|
|
||||||
if (file_handle->type == ZEND_HANDLE_FP && file_handle->handle.fp) {
|
fprintf(stderr, "[match] matched install.php, start dumping...\n");
|
||||||
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) {
|
char *buffer = NULL;
|
||||||
buffer = emalloc(size + 1);
|
size_t size = 0;
|
||||||
if (fread(buffer, 1, size, fp) != size) {
|
|
||||||
fprintf(stderr, "[warn] fread failed on %s\n", source_path);
|
if (file_handle->type == ZEND_HANDLE_FP && file_handle->handle.fp) {
|
||||||
efree(buffer);
|
fprintf(stderr, "[info] Detected ZEND_HANDLE_FP\n");
|
||||||
buffer = NULL;
|
FILE *fp = file_handle->handle.fp;
|
||||||
} else {
|
fseek(fp, 0, SEEK_END);
|
||||||
buffer[size] = '\0';
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (file_handle->type == ZEND_HANDLE_STREAM && file_handle->handle.stream.handle) {
|
if (buffer && size > 0) {
|
||||||
fprintf(stderr, "[info] Detected ZEND_HANDLE_STREAM\n");
|
char output_path[PATH_MAX];
|
||||||
php_stream *stream = (php_stream *)file_handle->handle.stream.handle;
|
snprintf(output_path, sizeof(output_path), "/tmp/dump_install_%ld.dec.php", time(NULL));
|
||||||
|
|
||||||
php_stream_seek(stream, 0, SEEK_END);
|
FILE *out = fopen(output_path, "wb");
|
||||||
size = php_stream_tell(stream);
|
if (out) {
|
||||||
php_stream_seek(stream, 0, SEEK_SET);
|
fwrite(buffer, 1, size, out);
|
||||||
|
fclose(out);
|
||||||
|
fprintf(stderr, "[dump] saved to: %s (%zu bytes)\n", output_path, size);
|
||||||
|
|
||||||
if (size > 0 && size < 100 * 1024 * 1024) {
|
// ✅ 打印内容预览(前200字节)
|
||||||
buffer = emalloc(size + 1);
|
fprintf(stderr, "[preview] %.200s\n", buffer);
|
||||||
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 {
|
} else {
|
||||||
buffer[read_bytes] = '\0';
|
fprintf(stderr, "[error] failed to open output file: %s\n", output_path);
|
||||||
size = read_bytes;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buffer && size > 0) {
|
efree(buffer);
|
||||||
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 (%zu bytes)\n", output_path, size);
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "[error] failed to open output file: %s\n", output_path);
|
fprintf(stderr, "[skip] buffer is NULL or size = 0\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
efree(buffer);
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "[skip] buffer is NULL or size = 0\n");
|
fprintf(stderr, "[warn] file_handle->filename is NULL\n");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "[warn] file_handle or filename is NULL\n");
|
fprintf(stderr, "[warn] file_handle 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;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue