Skip to content

Commit 92447a3

Browse files
committed
✨ Check Cookie
1 parent f999ece commit 92447a3

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

inc/ngx_http_waf_module.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define ARGS_FILE ("args")
1515
#define UA_FILE ("user-agent")
1616
#define REFERER_FILE ("referer")
17+
#define COOKIE_FILE ("cookie")
1718
#define POST_FILE ("post")
1819
#define WHITE_IPV4_FILE ("white-ipv4")
1920
#define WHITE_URL_FILE ("white-url")
@@ -66,6 +67,7 @@ typedef struct {
6667
ngx_array_t *black_args; /* args 黑名单 */
6768
ngx_array_t *black_ua; /* user-agent 黑名单 */
6869
ngx_array_t *black_referer; /* Referer 黑名单 */
70+
ngx_array_t *black_cookie; /* Cookie 黑名单 */
6971
ngx_array_t *black_post; /* 请求体内容黑名单 */
7072
ngx_array_t *white_ipv4; /* IPV4 白名单 */
7173
ngx_array_t *white_url; /* URL 白名单 */
@@ -77,8 +79,8 @@ typedef struct {
7779
hash_table_item_int_ulong_t *ipv4_times_old_cur; /* 执行函数 free_hash_table 时用于记录当前处理到旧的 IPV4 访问频率统计表的哪一项 */
7880
ngx_int_t free_hash_table_step; /* 记录 free_hash_table 执行到哪一阶段 */
7981

80-
ngx_int_t read_body_done; /* 请求体是否读取完毕 */
81-
ngx_int_t waiting_more_body; /* 是否需要接受更多请求体 */
82+
ngx_int_t read_body_done:1; /* 请求体是否读取完毕 */
83+
ngx_int_t waiting_more_body:1; /* 是否需要接受更多请求体 */
8284
}ngx_http_waf_srv_conf_t;
8385

8486
typedef struct {

rules/cookie

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
\.\./
2+
\:\$
3+
\$\{
4+
select.+(from|limit)
5+
(?:(union(.*?)select))
6+
having|rongjitest
7+
sleep\((\s*)(\d*)(\s*)\)
8+
benchmark\((.*)\,(.*)\)
9+
base64_decode\(
10+
(?:from\W+information_schema\W)
11+
(?:(?:current_)user|database|schema|connection_id)\s*\(
12+
(?:etc\/\W*passwd)
13+
into(\s+)+(?:dump|out)file\s*
14+
group\s+by.+\(
15+
xwork.MethodAccessor
16+
(?:define|eval|file_get_contents|include|require|require_once|shell_exec|phpinfo|system|passthru|preg_\w+|execute|echo|print|print_r|var_dump|(fp)open|alert|showmodaldialog)\(
17+
xwork\.MethodAccessor
18+
(gopher|doc|php|glob|file|phar|zlib|ftp|ldap|dict|ogg|data)\:\/
19+
java\.lang
20+
\$_(GET|post|cookie|files|session|env|phplib|GLOBALS|SERVER)\[

src/ngx_http_waf_module.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ static char* ngx_http_waf_rule_path_conf(ngx_conf_t* cf, ngx_command_t* cmd, voi
119119
CHECK_AND_LOAD_CONF(cf, full_path, end, ARGS_FILE, srv_conf->black_args, 0);
120120
CHECK_AND_LOAD_CONF(cf, full_path, end, UA_FILE, srv_conf->black_ua, 0);
121121
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);
122123
CHECK_AND_LOAD_CONF(cf, full_path, end, POST_FILE, srv_conf->black_post, 0);
123124
CHECK_AND_LOAD_CONF(cf, full_path, end, WHITE_IPV4_FILE, srv_conf->white_ipv4, 1);
124125
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) {
182183
srv_conf->black_args = ngx_array_create(cf->pool, 10, sizeof(ngx_regex_elt_t));
183184
srv_conf->black_ua = ngx_array_create(cf->pool, 10, sizeof(ngx_regex_elt_t));
184185
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));
185187
srv_conf->black_post = ngx_array_create(cf->pool, 10, sizeof(ngx_regex_elt_t));
186188
srv_conf->white_ipv4 = ngx_array_create(cf->pool, 10, sizeof(ipv4_t));
187189
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) {
213215
ngx_http_core_main_conf_t* cmcf;
214216

215217
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);
217219
if (h == NULL) {
218220
return NGX_ERROR;
219221
}
@@ -313,7 +315,7 @@ static ngx_int_t ngx_http_waf_handler_ip_url_referer_ua_args_post(ngx_http_reque
313315

314316
if (r->headers_in.user_agent != NULL
315317
&& 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");
317319
return NGX_HTTP_FORBIDDEN;
318320
}
319321

@@ -323,10 +325,21 @@ static ngx_int_t ngx_http_waf_handler_ip_url_referer_ua_args_post(ngx_http_reque
323325
}
324326
if (r->headers_in.referer != NULL
325327
&& 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");
327329
return NGX_HTTP_FORBIDDEN;
328330
}
329331

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+
330343
if (((r->method & NGX_HTTP_POST) != 0) && srv_conf->read_body_done == FALSE) {
331344
r->request_body_in_persistent_file = 0;
332345
r->request_body_in_clean_file = 0;
@@ -421,9 +434,6 @@ void check_post(ngx_http_request_t* r)
421434
continue;
422435
}
423436

424-
u_char str[4096];
425-
to_c_str(str, body_str);
426-
427437
if (ngx_regex_exec_array(srv_conf->black_post, &body_str, r->connection->log) == NGX_OK) {
428438
is_blocked = TRUE;
429439
break;

0 commit comments

Comments
 (0)