1
- #ifndef NGX_HTTP_WAF_MODULE
2
- #define NGX_HTTP_WAF_MODULE
3
-
4
1
#include <ngx_config.h>
5
2
#include <ngx_core.h>
6
3
#include <ngx_http.h>
7
4
#include <ngx_regex.h>
8
5
#include <ngx_inet.h>
9
6
#include "uthash/src/uthash.h"
10
7
8
+ #ifndef NGX_HTTP_WAF_MODULE_CORE
9
+ #define NGX_HTTP_WAF_MODULE_CORE
10
+
11
11
/* 对应配置文件的文件名 */
12
12
#define IPV4_FILE ("ipv4")
13
13
#define URL_FILE ("url")
25
25
#define FAIL (0)
26
26
#define TRUE (1)
27
27
#define FALSE (0)
28
+ #define MATCHED (1)
29
+ #define NOT_MATCHED (0)
28
30
29
31
30
32
#define RULE_MAX_LEN (256 * 4 * 8)
@@ -53,6 +55,14 @@ typedef struct {
53
55
ngx_int_t ngx_waf_mult_mount ;
54
56
} ngx_http_waf_main_conf_t ;
55
57
58
+ typedef struct {
59
+ ngx_int_t blocked ; /* 是否拦截了本次请求 */
60
+ u_char rule_type [128 ]; /* 触发的规则类型 */
61
+ u_char rule_deatils [RULE_MAX_LEN ]; /* 触发的规则内容 */
62
+ ngx_int_t read_body_done ;
63
+ ngx_int_t waiting_more_body ;
64
+ } ngx_http_waf_ctx_t ;
65
+
56
66
typedef struct {
57
67
ngx_log_t * ngx_log ; /* 记录内存池在进行操作时的错误日志 */
58
68
ngx_pool_t * ngx_pool ; /* 模块所使用的内存池 */
@@ -79,13 +89,12 @@ typedef struct {
79
89
hash_table_item_int_ulong_t * ipv4_times_old_cur ; /* 执行函数 free_hash_table 时用于记录当前处理到旧的 IPV4 访问频率统计表的哪一项 */
80
90
ngx_int_t free_hash_table_step ; /* 记录 free_hash_table 执行到哪一阶段 */
81
91
82
- ngx_int_t read_body_done :1 ; /* 请求体是否读取完毕 */
83
- ngx_int_t waiting_more_body :1 ; /* 是否需要接受更多请求体 */
84
92
}ngx_http_waf_srv_conf_t ;
85
93
86
94
typedef struct {
87
- size_t prefix ; /* 相当于 192.168.1.0/24 中的 192.168.1.0 的整数形式 */
88
- size_t suffix ; /* 相当于 192.168.1.0/24 中的 24 的整数形式 */
95
+ u_char text [32 ]; /* 点分十进制表示法 */
96
+ size_t prefix ; /* 相当于 192.168.1.0/24 中的 192.168.1.0 的整数形式 */
97
+ size_t suffix ; /* 相当于 192.168.1.0/24 中的 24 的整数形式 */
89
98
}ipv4_t ;
90
99
91
100
@@ -113,10 +122,19 @@ static void* ngx_http_waf_create_main_conf(ngx_conf_t* cf);
113
122
static void * ngx_http_waf_create_srv_conf (ngx_conf_t * cf );
114
123
115
124
125
+ ngx_int_t ngx_http_waf_blocked_get_handler (ngx_http_request_t * r , ngx_http_variable_value_t * v , uintptr_t data );
126
+
127
+
128
+ ngx_int_t ngx_http_waf_rule_type_get_handler (ngx_http_request_t * r , ngx_http_variable_value_t * v , uintptr_t data );
129
+
130
+
131
+ ngx_int_t ngx_http_waf_rule_deatils_handler (ngx_http_request_t * r , ngx_http_variable_value_t * v , uintptr_t data );
132
+
133
+
116
134
static ngx_int_t ngx_http_waf_handler_url_args (ngx_http_request_t * r );
117
135
118
136
119
- static ngx_int_t ngx_http_waf_handler_ip_url_referer_ua_args_post (ngx_http_request_t * r );
137
+ static ngx_int_t ngx_http_waf_handler_ip_url_referer_ua_args_cookie_post (ngx_http_request_t * r );
120
138
121
139
/*
122
140
* 将一个字符串形式的 IPV4 地址转化为 ngx_ipv4_t
@@ -125,22 +143,7 @@ static ngx_int_t ngx_http_waf_handler_ip_url_referer_ua_args_post(ngx_http_reque
125
143
*/
126
144
static ngx_int_t parse_ipv4 (ngx_str_t text , ipv4_t * ipv4 );
127
145
128
- /*
129
- * 检查 ip 是否属于数组中的某个 ipv4 地址
130
- * 第二个参数是一个元素类型为 ngx_ipv4_t 的数组
131
- * 如果匹配到返回 SUCCESS,反之返回 FAIL
132
- */
133
- static ngx_int_t check_ipv4 (unsigned long ip , ngx_array_t * a );
134
146
135
- /*
136
- * 逐渐释放旧的哈希表所占用的内存
137
- * 第一阶段:备份现有的哈希表和现有的内存池,然后创建新的哈希表和内存池
138
- * 第二阶段:逐渐将旧的哈希表中有用的内容转移到新的哈希表中。
139
- * 第三阶段:清空旧的哈希表
140
- * 第四阶段:销毁旧的内存池,完成释放。
141
- * 如果成功返回 SUCCESS,如果还在释放中(第四阶段之前)返回 PROCESSING,如果出现错误返回 FAIL
142
- */
143
- static ngx_int_t free_hash_table (ngx_http_request_t * r , ngx_http_waf_srv_conf_t * srv_conf );
144
147
145
148
146
149
/* 将 ngx_str 转化为 C 风格的字符串 */
@@ -155,16 +158,59 @@ static char* to_c_str(u_char* destination, ngx_str_t ngx_str);
155
158
static ngx_int_t load_into_array (ngx_conf_t * cf , const char * file_name , ngx_array_t * ngx_array , ngx_int_t mode );
156
159
157
160
158
- /*
159
- * 检查当前的 ip 地址是否超出频率限制
160
- * 如果超出则返回 SUCCESS,反之返回 FAIL
161
- */
162
- static ngx_int_t check_cc_ipv4 (ngx_http_request_t * r , ngx_http_waf_srv_conf_t * srv_conf , unsigned long ipv4 );
163
-
164
161
/*
165
162
* 检查请求体内容是否存在于黑名单中
166
163
* 存在则拦截,反之放行。
167
164
*/
168
165
void check_post (ngx_http_request_t * r );
169
166
170
- #endif // !NGX_HTTP_WAF_MODULE
167
+
168
+ static ngx_int_t check_white_ipv4 (ngx_http_request_t * r );
169
+
170
+
171
+ static ngx_int_t check_black_ipv4 (ngx_http_request_t * r );
172
+
173
+
174
+ static ngx_int_t check_cc_ipv4 (ngx_http_request_t * r );
175
+
176
+
177
+ static ngx_int_t check_white_url (ngx_http_request_t * r );
178
+
179
+
180
+ static ngx_int_t check_black_url (ngx_http_request_t * r );
181
+
182
+
183
+ static ngx_int_t check_black_args (ngx_http_request_t * r );
184
+
185
+
186
+ static ngx_int_t check_black_user_agent (ngx_http_request_t * r );
187
+
188
+
189
+ static ngx_int_t check_white_referer (ngx_http_request_t * r );
190
+
191
+
192
+ static ngx_int_t check_black_referer (ngx_http_request_t * r );
193
+
194
+
195
+ static ngx_int_t check_black_cookie (ngx_http_request_t * r );
196
+
197
+
198
+ /*
199
+ * 检查 ip 是否属于数组中的某个 ipv4 地址
200
+ * 第二个参数是一个元素类型为 ngx_ipv4_t 的数组
201
+ * 如果匹配到返回 SUCCESS,反之返回 FAIL
202
+ */
203
+ static ngx_int_t check_ipv4 (unsigned long ip , const ipv4_t * ipv4 );
204
+
205
+
206
+ /*
207
+ * 逐渐释放旧的哈希表所占用的内存
208
+ * 第一阶段:备份现有的哈希表和现有的内存池,然后创建新的哈希表和内存池
209
+ * 第二阶段:逐渐将旧的哈希表中有用的内容转移到新的哈希表中。
210
+ * 第三阶段:清空旧的哈希表
211
+ * 第四阶段:销毁旧的内存池,完成释放。
212
+ * 如果成功返回 SUCCESS,如果还在释放中(第四阶段之前)返回 PROCESSING,如果出现错误返回 FAIL
213
+ */
214
+ static ngx_int_t free_hash_table (ngx_http_request_t * r , ngx_http_waf_srv_conf_t * srv_conf );
215
+
216
+ #endif // !NGX_HTTP_WAF_MODULE_CORE
0 commit comments