@@ -119,6 +119,7 @@ static char* ngx_http_waf_rule_path_conf(ngx_conf_t* cf, ngx_command_t* cmd, voi
119
119
CHECK_AND_LOAD_CONF (cf , full_path , end , ARGS_FILE , srv_conf -> black_args , 0 );
120
120
CHECK_AND_LOAD_CONF (cf , full_path , end , UA_FILE , srv_conf -> black_ua , 0 );
121
121
CHECK_AND_LOAD_CONF (cf , full_path , end , REFERER_FILE , srv_conf -> black_referer , 0 );
122
+ CHECK_AND_LOAD_CONF (cf , full_path , end , COOKIE_FILE , srv_conf -> black_cookie , 0 );
122
123
CHECK_AND_LOAD_CONF (cf , full_path , end , POST_FILE , srv_conf -> black_post , 0 );
123
124
CHECK_AND_LOAD_CONF (cf , full_path , end , WHITE_IPV4_FILE , srv_conf -> white_ipv4 , 1 );
124
125
CHECK_AND_LOAD_CONF (cf , full_path , end , WHITE_URL_FILE , srv_conf -> white_url , 0 );
@@ -182,6 +183,7 @@ static void* ngx_http_waf_create_srv_conf(ngx_conf_t* cf) {
182
183
srv_conf -> black_args = ngx_array_create (cf -> pool , 10 , sizeof (ngx_regex_elt_t ));
183
184
srv_conf -> black_ua = ngx_array_create (cf -> pool , 10 , sizeof (ngx_regex_elt_t ));
184
185
srv_conf -> black_referer = ngx_array_create (cf -> pool , 10 , sizeof (ngx_regex_elt_t ));
186
+ srv_conf -> black_cookie = ngx_array_create (cf -> pool , 10 , sizeof (ngx_regex_elt_t ));
185
187
srv_conf -> black_post = ngx_array_create (cf -> pool , 10 , sizeof (ngx_regex_elt_t ));
186
188
srv_conf -> white_ipv4 = ngx_array_create (cf -> pool , 10 , sizeof (ipv4_t ));
187
189
srv_conf -> white_url = ngx_array_create (cf -> pool , 10 , sizeof (ngx_regex_elt_t ));
@@ -213,7 +215,7 @@ static ngx_int_t ngx_http_waf_init_after_load_config(ngx_conf_t* cf) {
213
215
ngx_http_core_main_conf_t * cmcf ;
214
216
215
217
cmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_core_module );
216
- h = ngx_array_push (& cmcf -> phases [NGX_HTTP_ACCESS_PHASE ].handlers );
218
+ h = ngx_array_push (& cmcf -> phases [NGX_HTTP_PREACCESS_PHASE ].handlers );
217
219
if (h == NULL ) {
218
220
return NGX_ERROR ;
219
221
}
@@ -313,7 +315,7 @@ static ngx_int_t ngx_http_waf_handler_ip_url_referer_ua_args_post(ngx_http_reque
313
315
314
316
if (r -> headers_in .user_agent != NULL
315
317
&& ngx_regex_exec_array (srv_conf -> black_ua , & r -> headers_in .user_agent -> value , r -> connection -> log ) == NGX_OK ) {
316
- ngx_log_error (NGX_LOG_WARN , r -> connection -> log , 0 , "ngx_waf: USER-AGENT" );
318
+ ngx_log_error (NGX_LOG_ALERT , r -> connection -> log , 0 , "ngx_waf: USER-AGENT" );
317
319
return NGX_HTTP_FORBIDDEN ;
318
320
}
319
321
@@ -323,10 +325,21 @@ static ngx_int_t ngx_http_waf_handler_ip_url_referer_ua_args_post(ngx_http_reque
323
325
}
324
326
if (r -> headers_in .referer != NULL
325
327
&& ngx_regex_exec_array (srv_conf -> black_referer , & r -> headers_in .referer -> value , r -> connection -> log ) == NGX_OK ) {
326
- ngx_log_error (NGX_LOG_WARN , r -> connection -> log , 0 , "ngx_waf: REFERER" );
328
+ ngx_log_error (NGX_LOG_ALERT , r -> connection -> log , 0 , "ngx_waf: REFERER" );
327
329
return NGX_HTTP_FORBIDDEN ;
328
330
}
329
331
332
+ if (r -> headers_in .cookies .nelts != 0 ) {
333
+ ngx_table_elt_t * * p = r -> headers_in .cookies .elts ;
334
+ size_t i = 0 ;
335
+ for (; i < r -> headers_in .cookies .nelts ; i ++ , p ++ ) {
336
+ if (ngx_regex_exec_array (srv_conf -> black_cookie , & ((* p )-> value ), r -> connection -> log ) == NGX_OK ) {
337
+ ngx_log_error (NGX_LOG_ALERT , r -> connection -> log , 0 , "ngx_waf: COOKIE" );
338
+ return NGX_HTTP_FORBIDDEN ;
339
+ }
340
+ }
341
+ }
342
+
330
343
if (((r -> method & NGX_HTTP_POST ) != 0 ) && srv_conf -> read_body_done == FALSE) {
331
344
r -> request_body_in_persistent_file = 0 ;
332
345
r -> request_body_in_clean_file = 0 ;
@@ -421,9 +434,6 @@ void check_post(ngx_http_request_t* r)
421
434
continue ;
422
435
}
423
436
424
- u_char str [4096 ];
425
- to_c_str (str , body_str );
426
-
427
437
if (ngx_regex_exec_array (srv_conf -> black_post , & body_str , r -> connection -> log ) == NGX_OK ) {
428
438
is_blocked = TRUE;
429
439
break ;
0 commit comments