Skip to content

Commit 92d6d84

Browse files
committed
🐛 segmentation fault
1 parent 92447a3 commit 92d6d84

File tree

3 files changed

+596
-236
lines changed

3 files changed

+596
-236
lines changed

config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
ngx_addon_name=ngx_http_waf_module
22
HTTP_MODULES="$HTTP_MODULES ngx_http_waf_module"
3-
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_waf_module.c"
3+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/src/ngx_http_waf_module_core.c"

inc/ngx_http_waf_module.h renamed to inc/ngx_http_waf_module_core.h

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#ifndef NGX_HTTP_WAF_MODULE
2-
#define NGX_HTTP_WAF_MODULE
3-
41
#include <ngx_config.h>
52
#include <ngx_core.h>
63
#include <ngx_http.h>
74
#include <ngx_regex.h>
85
#include <ngx_inet.h>
96
#include "uthash/src/uthash.h"
107

8+
#ifndef NGX_HTTP_WAF_MODULE_CORE
9+
#define NGX_HTTP_WAF_MODULE_CORE
10+
1111
/* 对应配置文件的文件名 */
1212
#define IPV4_FILE ("ipv4")
1313
#define URL_FILE ("url")
@@ -25,6 +25,8 @@
2525
#define FAIL (0)
2626
#define TRUE (1)
2727
#define FALSE (0)
28+
#define MATCHED (1)
29+
#define NOT_MATCHED (0)
2830

2931

3032
#define RULE_MAX_LEN (256 * 4 * 8)
@@ -53,6 +55,14 @@ typedef struct {
5355
ngx_int_t ngx_waf_mult_mount;
5456
} ngx_http_waf_main_conf_t;
5557

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+
5666
typedef struct {
5767
ngx_log_t *ngx_log; /* 记录内存池在进行操作时的错误日志 */
5868
ngx_pool_t *ngx_pool; /* 模块所使用的内存池 */
@@ -79,13 +89,12 @@ typedef struct {
7989
hash_table_item_int_ulong_t *ipv4_times_old_cur; /* 执行函数 free_hash_table 时用于记录当前处理到旧的 IPV4 访问频率统计表的哪一项 */
8090
ngx_int_t free_hash_table_step; /* 记录 free_hash_table 执行到哪一阶段 */
8191

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

8694
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 的整数形式 */
8998
}ipv4_t;
9099

91100

@@ -113,10 +122,19 @@ static void* ngx_http_waf_create_main_conf(ngx_conf_t* cf);
113122
static void* ngx_http_waf_create_srv_conf(ngx_conf_t* cf);
114123

115124

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+
116134
static ngx_int_t ngx_http_waf_handler_url_args(ngx_http_request_t* r);
117135

118136

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);
120138

121139
/*
122140
* 将一个字符串形式的 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
125143
*/
126144
static ngx_int_t parse_ipv4(ngx_str_t text, ipv4_t* ipv4);
127145

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);
134146

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);
144147

145148

146149
/* 将 ngx_str 转化为 C 风格的字符串 */
@@ -155,16 +158,59 @@ static char* to_c_str(u_char* destination, ngx_str_t ngx_str);
155158
static ngx_int_t load_into_array(ngx_conf_t* cf, const char* file_name, ngx_array_t* ngx_array, ngx_int_t mode);
156159

157160

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-
164161
/*
165162
* 检查请求体内容是否存在于黑名单中
166163
* 存在则拦截,反之放行。
167164
*/
168165
void check_post(ngx_http_request_t* r);
169166

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

Comments
 (0)