-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathevalhook.c
145 lines (114 loc) · 3.31 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* $Id$ */
#include "php.h"
#include "ext/standard/info.h"
#define EVAL_CALLBACK_FUNCTION "__eval"
static const char module_name[] = "evalhook";
static zend_op_array* (*old_compile_string)(zend_string *, const char *, zend_compile_position);
static zend_op_array* evalhook_compile_string(
zend_string *source_string,
const char *filename,
zend_compile_position pos)
{
zend_op_array *op_array = NULL;
int op_compiled = 0;
if(strstr(filename, "eval()'d code")) {
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, pos);
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, pos);
}
}
/* Evasion protection
* ==================
*
* Some code try to evade analysis by checking if the evalhook extension is
* loaded before calling eval(). So we override the extension_loaded function
* so it will return false if the module name is the same as our module name.
*/
zif_handler original_handler_extension_loaded;
ZEND_NAMED_FUNCTION(evalhook_extension_loaded)
{
zend_string * module;
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(module)
ZEND_PARSE_PARAMETERS_END();
if (zend_string_equals_cstr(module, module_name, sizeof module_name) == 0) {
RETURN_FALSE;
}
// Pass control on to the original handler
original_handler_extension_loaded(INTERNAL_FUNCTION_PARAM_PASSTHRU);
}
PHP_MINIT_FUNCTION(evalhook)
{
// If the ZEND_TSRMLS_CACHE_UPDATE() is in RINIT, move it
// to MINIT to ensure access to the compiler globals
#if defined(COMPILE_DL_MY_EXTENSION) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
zend_function * original =
zend_hash_str_find_ptr(CG(function_table), "extension_loaded", sizeof "extension_loaded" - 1);
if (original) {
original_handler_extension_loaded = original->internal_function.handler;
original->internal_function.handler = evalhook_extension_loaded;
}
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,
module_name,
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)