forked from zevenet/pound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
waf.c
468 lines (383 loc) · 10.2 KB
/
waf.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#include "pound.h"
void waf_del_transaction(Transaction ** transac)
{
if (*transac != NULL) {
msc_transaction_cleanup(*transac);
*transac = NULL;
}
}
void waf_create_transaction(Transaction ** t, ModSecurity * ms, Rules * rules)
{
*t = msc_new_transaction(ms, rules, &logmsg);
}
int waf_check_rule(char *rule_str)
{
// waf rules is a global variable
char *err_msg = NULL;
Rules *rule = NULL;
int err_rul = 0;
rule = msc_create_rules_set();
//msc_rules_add
msc_rules_add(rule, rule_str, &err_msg);
if (err_msg) {
err_rul = 1;
logmsg(LOG_ERR, "Error loading waf rules, %s", err_msg);
free(err_msg);
}
msc_rules_cleanup(rule);
return err_rul;
}
int waf_check_set(char *file)
{
// waf rules is a global variable
char *err_msg = NULL;
Rules *rule = NULL;
int err_rul = 0;
rule = msc_create_rules_set();
//msc_rules_add
msc_rules_add_file(rule, file, &err_msg);
if (err_msg) {
err_rul = 1;
logmsg(LOG_ERR, "Error loading waf rules, %s", err_msg);
free(err_msg);
}
msc_rules_cleanup(rule);
return err_rul;
}
void waf_memo_del(WAF_RULESET_MEMO * waf_rules)
{
msc_rules_cleanup(waf_rules->rules);
pthread_mutex_destroy(&waf_rules->mut);
free(waf_rules);
waf_rules = NULL;
}
int waf_memo_create(WAF_RULESET_MEMO ** waf_rules)
{
// waf rules is a global variable
char *err = NULL;
int err_flag = 0;
Rules *tmp_set = NULL;
FILE_LIST *it = NULL;
if (waf_rules_file) {
tmp_set = msc_create_rules_set();
for (it = waf_rules_file; it != NULL; it = it->next) {
//msc_rules_add
msc_rules_add_file(tmp_set, it->file, &err);
if (err) {
err_flag++;
msc_rules_cleanup(tmp_set);
tmp_set = NULL;
logmsg(LOG_ERR, "Error loading waf rules, %s", err);
free(err);
break;
}
}
}
if (!err_flag) {
// create struct
*waf_rules = (WAF_RULESET_MEMO *) malloc(sizeof(WAF_RULESET_MEMO));
if (!*waf_rules) {
msc_rules_cleanup(tmp_set);
err_flag = 1;
logmsg(LOG_ERR, "Error creating the ruleset, the memory is not enough");
} else {
(*waf_rules)->rules = tmp_set;
(*waf_rules)->counter = 0;
pthread_mutex_init(&(*waf_rules)->mut, NULL);
}
}
return err_flag;
}
/* It uses the waf memo global struct "waf_rules_memo" */
int waf_reload_rules(void)
{
// waf rules is a global variable
int err = 0;
WAF_RULESET_MEMO *new_waf_ruleset = NULL;
FILE_LIST *it = NULL;
err = waf_memo_create(&new_waf_ruleset);
if (!err) {
if (err = pthread_mutex_lock(&waf_rules_memo_mtx)) {
logmsg(LOG_WARNING, "locking waf resource: %s", strerror(err));
return err;
}
// if no one is using waf, delete it
if (waf_rules_memo && waf_memo_get_counter(waf_rules_memo) == 0)
waf_memo_del(waf_rules_memo);
// point memo to the new ruleset
waf_rules_memo = new_waf_ruleset;
if (err = pthread_mutex_unlock(&waf_rules_memo_mtx)) {
logmsg(LOG_WARNING, "unlocking waf resource: %s", strerror(err));
}
}
return err;
}
int waf_memo_lock(WAF_RULESET_MEMO * waf_rules)
{
int err;
if (err = pthread_mutex_lock(&waf_rules->mut)) {
logmsg(LOG_WARNING, "waf_memo_lock(): %s", strerror(err));
}
return err;
}
int waf_memo_unlock(WAF_RULESET_MEMO * waf_rules)
{
int err;
if (err = pthread_mutex_unlock(&waf_rules->mut)) {
logmsg(LOG_WARNING, "waf_memo_unlock(): %s", strerror(err));
}
return err;
}
/* */
int waf_memo_get_counter(WAF_RULESET_MEMO * waf_rules)
{
int counter = 0;
waf_memo_lock(waf_rules);
counter = (waf_rules->counter);
waf_memo_unlock(waf_rules);
return counter;
}
int waf_memo_increase(WAF_RULESET_MEMO * waf_rules)
{
waf_memo_lock(waf_rules);
(waf_rules->counter)++;
waf_memo_unlock(waf_rules);
}
int waf_memo_decrease(WAF_RULESET_MEMO * waf_rules)
{
waf_memo_lock(waf_rules);
(waf_rules->counter)--;
waf_memo_unlock(waf_rules);
}
void waf_memo_clean(WAF_RULESET_MEMO * waf_rules)
{
// delete struct if it is the last one
if (waf_memo_get_counter(waf_rules) == 0 && waf_rules != waf_rules_memo)
waf_memo_del(waf_rules);
}
int
waf_body_enabled(int bodybuf, const char *logtag, LONG body_size, int chunked,
int rpc, int no_cont)
{
int ret = 0;
if ( no_cont ) {
// no body is expected (i.e. HEAD verb)
} else if (!bodybuf) {
//~ logmsg(LOG_DEBUG, "%s (%lx) WAF skip body, the body is disabled",
//~ logtag, pthread_self());
} else if (body_size <= 0) {
//~ logmsg(LOG_DEBUG, "%s (%lx) WAF skip body, body lenght is unknown",
//~ logtag, pthread_self());
} else if (body_size >= INT_MAX) {
//~ logmsg(LOG_DEBUG, "%s (%lx) WAF skip body, the body is too big",
//~ logtag, pthread_self());
} else if (body_size >= bodybuf && bodybuf > 0) {
//~ logmsg(LOG_DEBUG, "%s (%lx) WAF skip body, body lenght is bigger than body buffer",
//~ logtag, pthread_self());
} else if (chunked) {
//~ logmsg(LOG_DEBUG, "%s (%lx) WAF skip body, body is chunked",
//~ logtag, pthread_self());
} else if (rpc == 1) {
//~ logmsg(LOG_DEBUG, "%s (%lx) WAF skip body, the connection is using RPC protocol",
//~ logtag, pthread_self());
} else {
ret = 1;
}
return ret;
}
int
parse_headers(const char *header, char **key, int *key_size, char **value,
int *value_size)
{
int fin = 0;
int parsing_value = 0;
int i;
*key = header;
*key_size = 0;
*value_size = 0;
if (header == NULL)
return -1;
// look for the separator, ':'
for (i = 0; i < MAXBUF && !fin; i++) {
if (!parsing_value) {
// end key
if (header[i] == ':')
fin = 1;
else if (header[i] == '\0')
fin = -1; //wrong parse
}
}
if (fin == 1) {
i--;
*key_size = i;
*value = header + i + 2;
*value_size = strlen(header) - 2 - *key_size; // rest size of " :
}
return fin;
}
int waf_add_http_info(Transaction * t, const char *header)
{
int ret = 0;
int version_str_size = 10;
char version[10];
char verb[20];
char uri[MAXBUF];
int i = 0;
int str_ind = 0, version_flag = 0;
int size = strlen(header);
int fin;
// parse verb
for (fin = 0, str_ind = 0; !fin && i < size; i++, str_ind++) {
if (header[i] == ' ') {
verb[str_ind] = '\0';
fin = 1;
} else
verb[str_ind] = header[i];
}
// parse path
for (fin = 0, str_ind = 0; !fin && i < size; i++, str_ind++) {
if (header[i] == ' ') {
uri[str_ind] = '\0';
fin = 1;
} else
uri[str_ind] = header[i];
}
// parse version
for (fin = 0, str_ind = 0; !fin && i < size; i++) {
if (header[i] == '\0' || str_ind == (version_str_size - 1))
fin = 1;
else {
if (version_flag) {
version[str_ind] = header[i];
str_ind++;
// look for after '/': HTTP/<version>
} else if (!version_flag && header[i] == '/')
version_flag = 1;
}
}
version[str_ind] = '\0';
msc_process_uri(t, uri, verb, version);
return ret;
}
int waf_add_req_head(Transaction * t, char const **headers, int num_headers)
{
char *key;
int key_size;
char *value;
int value_size;
int ret = 1;
int cont = 1;
int i;
waf_add_http_info(t, headers[0]);
// skip first header, it is the VERB, URI and VERSION
for (i = 1; cont == 1 && i < num_headers; i++) {
cont = parse_headers(headers[i], &key, &key_size, &value, &value_size);
if (cont == 1)
msc_add_n_request_header(t, key, key_size, value, value_size);
else {
ret = 0;
}
}
msc_process_request_headers(t);
return ret;
}
int waf_add_resp_head(Transaction * t, char const **headers, int num_headers)
{
char *key;
int key_size;
char *value;
int value_size;
int ret = 1;
int cont = 1;
int http_code;
char http_code_str[4];
char http_version[9];
char const *p;
// parse http response code
// parse version
p = headers[0];
int size = strlen(p);
int param = 0;
char aux;
int i, ic;
for (i = 0, ic = 0; param < 2 && i < size; i++, ic++) {
aux = p[i];
if (aux == ' ')
aux = '\0';
if (param == 0)
http_version[ic] = aux;
else if (param == 1)
http_code_str[ic] = aux;
if (aux == '\0') {
ic = -1;
param++;
}
}
http_code = atoi(http_code_str);
// parse response headers
for (i = 1; cont == 1 && i < num_headers; i++) {
cont = parse_headers(headers[i], &key, &key_size, &value, &value_size);
if (cont == 1) {
msc_add_n_response_header(t, key, key_size, value, value_size);
} else {
ret = 0;
}
}
msc_process_response_headers(t, http_code, http_version);
return ret;
}
/* it returns -1 on error or another value on success*/
int read_body(BIO * sock, char **buff, int size)
{
int res=-1;
int res_bytes = 0;
if (*buff) {
logmsg(LOG_NOTICE,
"(%lx) the body buffer is not clean. Cleaning it",
pthread_self());
free (*buff);
*buff = NULL;
}
*buff = (char *) malloc(size * sizeof(char));
while ((res = BIO_read(sock, *(buff + res_bytes), size - res_bytes)) > 0) {
res_bytes += res;
if (res_bytes > size) {
logmsg(LOG_NOTICE,
"(%lx) error reading HTTP body. Body bigger than content-length",
pthread_self());
break;
} else if (res_bytes == size) {
break;
}
}
return res;
}
int waf_resolution(Transaction * t, int *int_code, char *url, char *tag)
{
ModSecurityIntervention intervention;
intervention.status = 200;
intervention.url = NULL;
intervention.log = NULL;
intervention.disruptive = 0;
WAF_ACTION waf_action = ALLOW;
int size_copy;
if (msc_intervention(t, &intervention)) {
if (!msc_process_logging(t)) // log if any error was found
logmsg(LOG_WARNING, "%s (%lx) WAF, error processing the log", tag,
pthread_self());
if (intervention.url) {
waf_action = REDIRECTION;
strcpy(url, intervention.url);
if (intervention.status == 200)
intervention.status = 302; // default value
} else if (intervention.disruptive) {
waf_action = BLOCK;
if (intervention.status == 200)
intervention.status = 403; // default value
}
}
if (intervention.log != NULL)
logmsg(LOG_WARNING, "[WAF,%s] (%lx) %s", tag, pthread_self(),
intervention.log);
return waf_action;
}