-
Notifications
You must be signed in to change notification settings - Fork 3
/
lightWAF.c
139 lines (114 loc) · 2.79 KB
/
lightWAF.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
#include "php_lightWAF.h"
#include "ext/standard/info.h"
#include "zend_execute.h"
#include <string.h>
#include <sys/stat.h>
static zend_function_entry lightWAF_functions[] = {
{NULL, NULL, NULL}
};
/* LIGHT_DO_FCALL */
static int LIGHT_DO_FCALL(ZEND_OPCODE_HANDLER_ARGS)
{
char *filePath;
struct stat st;
int nRev_st = 0;
php_printf("[Debug] ZEND_DO_FCALL\n<br />");
filePath = zend_get_executed_filename(TSRMLS_C);
php_printf("[Debug] filePath: %s\n<br />", filePath);
if (strstr(filePath, "/upload/"))
{
/* 非法调用,拦截 */
php_printf("[Warning] Execute command via system() etc.\n<br />");
return ZEND_USER_OPCODE_RETURN;
}
else /* Not Found */
{
/* 合法调用,放行 */
return ZEND_USER_OPCODE_DISPATCH;
}
}
/* LIGHT_DO_FCALL_BY_NAME */
static int LIGHT_DO_FCALL_BY_NAME(ZEND_OPCODE_HANDLER_ARGS)
{
char *filePath = zend_get_executed_filename(TSRMLS_C);
php_printf("[Debug] ZEND_DO_FCALL_BY_NAME\n<br />");
php_printf("[Debug] filePath: %s\n<br />", filePath);
if (strstr(filePath, "/upload/"))
{
/* 非法调用,拦截 */
php_printf("[Warning] Execute command via variable.\n<br />");
return ZEND_USER_OPCODE_RETURN;
}
else /* Not Found */
{
/* 合法调用,放行 */
return ZEND_USER_OPCODE_DISPATCH;
}
}
/* LIGHT_INCLUDE_OR_EVAL */
static int LIGHT_INCLUDE_OR_EVAL(ZEND_OPCODE_HANDLER_ARGS)
{
char *filePath = zend_get_executed_filename(TSRMLS_C);
php_printf("[Debug] ZEND_INCLUDE_OR_EVAL\n<br />");
php_printf("[Debug] filePath: %s\n<br />", filePath);
if ( strstr(filePath, "/upload/") )
{
/* 非法调用,拦截 */
php_printf("[Warning] Execute command via include or eval.\n<br />");
return ZEND_USER_OPCODE_RETURN;
}
else /* Not Found */
{
/* 合法调用,放行 */
return ZEND_USER_OPCODE_DISPATCH;
}
}
ZEND_MINIT_FUNCTION(lightWAF)
{
/*
* hook掉ZEND_DO_FCALL
* 处理system函数等
*/
zend_set_user_opcode_handler(ZEND_DO_FCALL, LIGHT_DO_FCALL);
/*
* hook掉ZEND_DO_FCALL_BY_NAME
* 处理$func='system';$func();等类型
*/
zend_set_user_opcode_handler(ZEND_DO_FCALL_BY_NAME, LIGHT_DO_FCALL_BY_NAME);
/*
* hook掉ZEND_INCLUDE_OR_EVAL
* 处理eval, require等
*/
zend_set_user_opcode_handler(ZEND_INCLUDE_OR_EVAL, LIGHT_INCLUDE_OR_EVAL);
return SUCCESS;
}
ZEND_MSHUTDOWN_FUNCTION(lightWAF)
{
return SUCCESS;
}
ZEND_RINIT_FUNCTION(lightWAF)
{
return SUCCESS;
}
ZEND_RSHUTDOWN_FUNCTION(lightWAF)
{
return SUCCESS;
}
ZEND_MINFO_FUNCTION(lightWAF)
{
}
zend_module_entry lightWAF_module_entry = {
STANDARD_MODULE_HEADER,
PHP_LIGHTWAF_EXTNAME,
lightWAF_functions,
ZEND_MINIT(lightWAF),
ZEND_MSHUTDOWN(lightWAF),
ZEND_RINIT(lightWAF),
ZEND_RSHUTDOWN(lightWAF),
ZEND_MINFO(lightWAF),
PHP_LIGHTWAF_EXTVER,
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_LIGHTWAF
ZEND_GET_MODULE(lightWAF)
#endif