forked from extremecoders-re/php-eval-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evalhook.c
115 lines (95 loc) · 2.32 KB
/
evalhook.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* $Id$ */
#include "php.h"
#include "ext/standard/info.h"
#define EVAL_CALLBACK_FUNCTION "__eval"
int eval_counter = 0;
static zend_op_array *(*old_compile_string)(zend_string *source_string, const char *filename);
static zend_op_array *evalhook_compile_string(zend_string *source_string, const char *filename)
{
zend_op_array *op_array = NULL;
int op_compiled = 0;
if (strstr(filename, "eval()'d code"))
{
char eval_filename[21];
sprintf(eval_filename, "eval-%03d.php", ++eval_counter);
FILE *fp;
fp = fopen(eval_filename, "w");
if (fp == NULL)
{
// handle error opening file
}
fprintf(fp, "%s", ZSTR_VAL(source_string));
fclose(fp);
if (zend_hash_str_exists(CG(function_table), EVAL_CALLBACK_FUNCTION, strlen(EVAL_CALLBACK_FUNCTION)))
{
zval function;
zval retval;
zval parameter[2];
ZVAL_STR(¶meter[0], source_string);
ZVAL_STRING(&function, EVAL_CALLBACK_FUNCTION);
ZVAL_STRING(¶meter[1], filename);
if (call_user_function(CG(function_table), NULL, &function, &retval, 2, parameter) == SUCCESS)
{
switch (Z_TYPE(retval))
{
case IS_STRING:
op_array = old_compile_string(Z_STR(retval), filename);
case IS_FALSE:
op_compiled = 1;
break;
}
}
zval_dtor(&function);
zval_dtor(&retval);
zval_dtor(¶meter[1]);
}
}
if (op_compiled)
{
return op_array;
}
else
{
return old_compile_string(source_string, filename);
}
}
PHP_MINIT_FUNCTION(evalhook)
{
return SUCCESS;
}
PHP_MSHUTDOWN_FUNCTION(evalhook)
{
return SUCCESS;
}
PHP_RINIT_FUNCTION(evalhook)
{
old_compile_string = zend_compile_string;
zend_compile_string = evalhook_compile_string;
return SUCCESS;
}
PHP_RSHUTDOWN_FUNCTION(evalhook)
{
zend_compile_string = old_compile_string;
return SUCCESS;
}
PHP_MINFO_FUNCTION(evalhook)
{
php_info_print_table_start();
php_info_print_table_row(2, "eval() hooking", "enabled");
php_info_print_table_row(2, "callback function", EVAL_CALLBACK_FUNCTION);
php_info_print_table_end();
}
zend_function_entry evalhook_functions[] = {
ZEND_FE_END};
zend_module_entry evalhook_module_entry = {
STANDARD_MODULE_HEADER,
"evalhook",
evalhook_functions,
PHP_MINIT(evalhook),
PHP_MSHUTDOWN(evalhook),
PHP_RINIT(evalhook),
PHP_RSHUTDOWN(evalhook),
PHP_MINFO(evalhook),
"0.0.1-dev",
STANDARD_MODULE_PROPERTIES};
ZEND_GET_MODULE(evalhook)