-
Notifications
You must be signed in to change notification settings - Fork 6
/
ngx_http_waf_module.c
4616 lines (3646 loc) · 124 KB
/
ngx_http_waf_module.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) vislee
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_md5.h>
#include "libinjection/libinjection.h"
#include "libinjection/libinjection_sqli.h"
#ifdef NGX_WAF_MAGIC
#include <magic.h>
#endif
#ifdef NGX_WAF_HS
#include <hs/hs.h>
#include <hs/hs_runtime.h>
#endif
#ifdef NGX_DEBUG
#include <assert.h>
#endif
/*
* id 规则的标识,白名单和日志使用。
* str 字符串匹配
* sc 规则记分
* z 匹配区域
* * * * *
* security_rule id:1001 "str:xxx" "s:$TT:1,$QQ:2" "z:ARGS|V_HEADERS:cookie";
* -> ARGS:1001
* -> V_HEADERS:cookie:1001
*
* security_rule id:1002 "str:yyy" "sc:$TT:2,$QQ:2" "z:V_ARGS:foo|V_ARGS:bar|HEADERS";
* -> V_ARGS:foo: 1002
* -> HEADERS:1002
*
* security_loc_rule "wl:1001,1002" "z:V_ARGS:foo|HEADERS";
*
* -> 1001: $ARGS ...
* $HEADERS ...
* -> 1002: $ARGS ...
* $HEADERS ...
*
* security_check "$TTT > 5" BLOCK;
*
* =>:
* ARGS:1001
* |-> ARGS
|->wl: V_ARGS:foo, V_ARGS:bar
* HEADERS:1002
* |-> HEADERS
* |-> wl:HEADERS
*
* ARGS_VAR:1002
* |-> V_ARGS:foo
* |-> V_ARGS:bar
* |-> wl:V_ARGS:foo
* HEADERS_VAR:1001
* |-> V_HEADERS:cookie
* |-> wl:HEADERS
*
*/
#define NGX_HTTP_WAF_MAX_LOG_STR 4096
#define NGX_HTTP_WAF_SYSLOG_MAX_STR \
NGX_HTTP_WAF_MAX_LOG_STR + sizeof("<255>Jan 01 00:00:00 ") - 1 \
+ (NGX_MAXHOSTNAMELEN - 1) + 1 /* space */ \
+ 32 /* tag */ + 2 /* colon, space */
// rule status
// action flag
// ngx_http_waf_rule_t->sts
#define NGX_HTTP_WAF_STS_RULE_ACTION 0x0FF00000
#define NGX_HTTP_WAF_STS_RULE_LOG 0x00100000
#define NGX_HTTP_WAF_STS_RULE_BLOCK 0x00200000
#define NGX_HTTP_WAF_STS_RULE_DROP 0x00400000
#define NGX_HTTP_WAF_STS_RULE_ALLOW 0x00800000
#define NGX_HTTP_WAF_STS_RULE_DONE 0x00E00000 // (BLOCK|DROP|ALLOW)
// #define NGX_HTTP_WAF_STS_RULE_NULL 0x01800000
// #define NGX_HTTP_WAF_STS_RULE_NULL 0x02800000
#define NGX_HTTP_WAF_STS_RULE_MZ_WL 0x04000000
#define NGX_HTTP_WAF_STS_RULE_VAR 0x08000000
#define NGX_HTTP_WAF_STS_MZ_SIGN 0xF0000000
#define NGX_HTTP_WAF_STS_MZ_COUNT 0x10000000
#define NGX_HTTP_WAF_STS_MZ_KEY 0x20000000
#define NGX_HTTP_WAF_STS_MZ_VAL 0x40000000
#define NGX_HTTP_WAF_STS_MZ_KV 0x60000000
#define NGX_HTTP_WAF_STS_RULE_INVALID 0x80000000
#define NGX_HTTP_WAF_STS_RULE_WL_INVALID 0x80000001
#define NGX_HTTP_WAF_STS_RULE_SC_INVALID 0x80000002
// NGX_HTTP_WAF_STS_MZ_KEY & NGX_HTTP_WAF_STS_RULE_MZ_WL
#define NGX_HTTP_WAF_STS_RULE_MZ_KEY_WL 0x24000000
// NGX_HTTP_WAF_STS_MZ_VAL & NGX_HTTP_WAF_STS_RULE_MZ_WL
#define NGX_HTTP_WAF_STS_RULE_MZ_VAL_WL 0x44000000
#define ngx_http_waf_ctx_copy_act(dst_act, src_act) \
((dst_act) |= (src_act) & NGX_HTTP_WAF_STS_RULE_ACTION)
#define ngx_http_waf_wl_copy_mz(dst_mz, src_mz) \
(dst_mz) |= (NGX_HTTP_WAF_STS_RULE_MZ_WL \
| ((src_mz) & NGX_HTTP_WAF_STS_MZ_KV));
#define ngx_http_waf_rule_invalid(sts) \
((sts) & NGX_HTTP_WAF_STS_RULE_INVALID)
#define ngx_http_waf_rule_set_wl_invalid(sts) \
((sts) |= NGX_HTTP_WAF_STS_RULE_WL_INVALID)
#define ngx_http_waf_rule_set_sc_invalid(sts) \
((sts) |= NGX_HTTP_WAF_STS_RULE_SC_INVALID)
#define ngx_http_waf_rule_wl_mz_key(sts) \
((sts) & NGX_HTTP_WAF_STS_RULE_MZ_KEY_WL)
#define ngx_http_waf_rule_wl_mz_val(sts) \
((sts) & NGX_HTTP_WAF_STS_RULE_MZ_VAL_WL)
#define ngx_http_waf_score_is_done(flag) \
((flag) & NGX_HTTP_WAF_STS_RULE_DONE)
#define ngx_http_waf_sts_has_block(flag) \
((flag) & NGX_HTTP_WAF_STS_RULE_BLOCK)
#define ngx_http_waf_sts_act_set_block(flag) \
((flag) |= NGX_HTTP_WAF_STS_RULE_BLOCK)
#define ngx_http_waf_sts_has_drop(flag) \
((flag) & NGX_HTTP_WAF_STS_RULE_DROP)
#define ngx_http_waf_sts_has_var(flag) \
((flag) & NGX_HTTP_WAF_STS_RULE_VAR)
#define ngx_http_waf_sts_has_allow(flag) \
((flag) & NGX_HTTP_WAF_STS_RULE_ALLOW)
#define ngx_http_waf_act_set_var_block(flag) \
((flag) |= (NGX_HTTP_WAF_STS_RULE_BLOCK | NGX_HTTP_WAF_STS_RULE_VAR))
static char*
ngx_http_waf_get_act(ngx_uint_t flag) {
switch(flag & NGX_HTTP_WAF_STS_RULE_ACTION) {
case NGX_HTTP_WAF_STS_RULE_BLOCK: return "BLOCK";
case NGX_HTTP_WAF_STS_RULE_DROP: return "DROP";
case NGX_HTTP_WAF_STS_RULE_ALLOW: return "ALLOW";
default:
return "LOG";
}
return "LOG";
}
// 0xFFFFF
// match zone types
// ngx_http_waf_zone_t->flag
// general
#define NGX_HTTP_WAF_MZ_G 0x0300F
#define NGX_HTTP_WAF_MZ_G_URL 0x00001
#define NGX_HTTP_WAF_MZ_G_ARGS 0x00002
#define NGX_HTTP_WAF_MZ_G_HEADERS 0x00004
#define NGX_HTTP_WAF_MZ_G_BODY 0x00008
#define NGX_HTTP_WAF_MZ_G_RAW_BODY 0x01000
// specify variable
#define NGX_HTTP_WAF_MZ_VAR 0x040F0
#define NGX_HTTP_WAF_MZ_VAR_URL 0x00010
#define NGX_HTTP_WAF_MZ_VAR_ARGS 0x00020
#define NGX_HTTP_WAF_MZ_VAR_HEADERS 0x00040
#define NGX_HTTP_WAF_MZ_VAR_BODY 0x00080
// regex
#define NGX_HTTP_WAF_MZ_X 0x08F00
#define NGX_HTTP_WAF_MZ_X_URL 0x00100
#define NGX_HTTP_WAF_MZ_X_ARGS 0x00200
#define NGX_HTTP_WAF_MZ_X_HEADERS 0x00400
#define NGX_HTTP_WAF_MZ_X_BODY 0x00800
#define NGX_HTTP_WAF_MZ_URL 0x00111
#define NGX_HTTP_WAF_MZ_ARGS 0x00222
#define NGX_HTTP_WAF_MZ_HEADERS 0x00444
#define NGX_HTTP_WAF_MZ_BODY 0x01888
#define NGX_HTTP_WAF_MZ_FILE_BODY 0x0E000
// multipart/form-data filename= content=
#define NGX_HTTP_WAF_MZ_G_FILE_BODY 0x02000
// #define NGX_HTTP_WAF_MZ_VAR_FILE_BODY 0x04000
#define NGX_HTTP_WAF_MZ_X_FILE_BODY 0x08000
#define ngx_http_waf_mz_gt(one, two) \
((one & ((two & NGX_HTTP_WAF_MZ_VAR) >> 4)) \
|| (one & ((two & NGX_HTTP_WAF_MZ_VAR) >> 8)) \
|| ((one & NGX_HTTP_WAF_MZ_G) == (two & NGX_HTTP_WAF_MZ_G) \
&& (one & NGX_HTTP_WAF_MZ_G) != 0))
#define ngx_http_waf_mz_is_general(flag) \
((flag) & NGX_HTTP_WAF_MZ_G)
#define ngx_http_waf_mz_is_regex(flag) \
((flag) & NGX_HTTP_WAF_MZ_X)
#define ngx_http_waf_mz_key(flag) \
((flag) & NGX_HTTP_WAF_STS_MZ_KEY)
#define ngx_http_waf_mz_val(flag) \
((flag) & NGX_HTTP_WAF_STS_MZ_VAL)
typedef struct ngx_http_waf_log_s ngx_http_waf_log_t;
typedef struct ngx_http_waf_rule_s ngx_http_waf_rule_t;
typedef struct ngx_http_waf_public_rule_s ngx_http_waf_public_rule_t;
typedef void (*ngx_http_waf_log_write_pt) (ngx_http_waf_log_t *log,
u_char *buf, size_t len);
typedef ngx_int_t (*ngx_http_waf_rule_match_pt)(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
typedef ngx_int_t (*ngx_http_waf_rule_decode_pt)(ngx_http_request_t *r,
ngx_str_t *dst, ngx_str_t *src, ngx_uint_t destory);
// for check rule
typedef struct ngx_http_waf_check_s {
ngx_uint_t idx; /* the ctx check array index */
ngx_str_t tag; /* check socre tag */
ngx_int_t score; /* the rule socre */
ngx_int_t threshold; /* the check rule threshold*/
ngx_uint_t action_flag; /* the check rule action */
} ngx_http_waf_check_t;
typedef struct ngx_http_waf_log_ctx_s {
ngx_str_t str;
ngx_http_waf_rule_t *rule;
ngx_int_t score;
} ngx_http_waf_log_ctx_t;
// the rule score
typedef struct ngx_http_waf_score_s {
ngx_str_t tag;
ngx_int_t score;
ngx_array_t *log_ctx; /* ngx_http_waf_log_ctx_t */
} ngx_http_waf_score_t;
// the rule
struct ngx_http_waf_public_rule_s {
ngx_int_t id;
ngx_str_t str;
#ifdef NGX_WAF_HS
hs_database_t *db;
hs_scratch_t *scratch;
#else
ngx_regex_t *regex;
#endif
ngx_array_t *scores; /* ngx_http_waf_score_t. maybe null */
ngx_array_t *decode_handlers;
ngx_http_waf_rule_match_pt handler;
unsigned not:1;
};
typedef struct ngx_http_waf_zone_s {
ngx_uint_t flag; /* match zone types */
ngx_str_t name; /* the specify variable for match zone */
#ifdef NGX_WAF_HS
hs_database_t *db;
hs_scratch_t *scratch;
#else
ngx_regex_t *regex;
#endif
// ngx_str_t spec_url_suffix; // TODO
} ngx_http_waf_zone_t;
// for parse rules
typedef struct ngx_http_waf_rule_opt_s {
ngx_http_waf_public_rule_t *p_rule;
ngx_array_t *wl_ids; /* ngx_int_t */
ngx_array_t *m_zones; /* ngx_http_waf_zone_t */
} ngx_http_waf_rule_opt_t;
struct ngx_http_waf_rule_s {
ngx_http_waf_public_rule_t *p_rule; /* opt->p_rule */
ngx_http_waf_zone_t *m_zone; /* opt->m_zones[x] */
ngx_array_t *wl_zones; /* ngx_http_waf_zone_t */
ngx_array_t *score_checks; /* ngx_http_waf_check_t*/
// rule status:
//include rule invalid, rule action, rule withelist type ...
ngx_uint_t sts;
};
typedef struct ngx_http_waf_whitelist_s {
ngx_int_t id; /* opt->wl_ids[x] */
ngx_array_t *url_zones; /* ngx_http_waf_zone_t* */
ngx_array_t *args_zones; /* opt->m_zones */
ngx_array_t *headers_zones;
ngx_array_t *body_zones;
ngx_array_t *body_file_zones;
// not specify the match zone.
unsigned all_zones:1;
} ngx_http_waf_whitelist_t;
typedef struct {
// ngx_http_waf_rule_t
// general and regex
ngx_array_t *url;
ngx_array_t *args;
ngx_array_t *headers;
ngx_array_t *body;
ngx_array_t *raw_body;
ngx_array_t *body_file;
// ngx_http_waf_rule_t
// specify variable
ngx_array_t *url_var;
ngx_array_t *args_var;
ngx_array_t *headers_var;
ngx_array_t *body_var;
} ngx_http_waf_main_conf_t;
struct ngx_http_waf_log_s {
ngx_uint_t off;
ngx_uint_t flat;
ngx_open_file_t *file;
ngx_http_waf_log_write_pt writer;
void *wdata;
};
typedef struct {
ngx_flag_t security_waf;
ngx_http_waf_log_t *log;
ngx_msec_t security_timeout;
ngx_array_t *check_rules; /* ngx_http_waf_check_t */
ngx_array_t *whitelists; /* ngx_http_waf_whitelist_t */
// ngx_http_waf_rule_t
// include general and regex rules
ngx_array_t *url;
ngx_array_t *args;
ngx_array_t *headers;
ngx_array_t *body;
ngx_array_t *body_file;
ngx_array_t *raw_body;
// ngx_http_waf_rule_t
// only specify variable rules
ngx_array_t *url_var;
ngx_hash_t url_var_hash;
ngx_array_t *args_var;
ngx_hash_t args_var_hash;
ngx_array_t *headers_var;
ngx_hash_t headers_var_hash;
ngx_array_t *body_var;
ngx_hash_t body_var_hash;
} ngx_http_waf_loc_conf_t;
typedef struct ngx_http_waf_ctx_s {
ngx_array_t *scores; /* ngx_http_waf_score_t */
ngx_http_waf_log_ctx_t *logc;
ngx_uint_t status;
unsigned wait_body:1;
unsigned check_done:1;
unsigned interrupt:1;
} ngx_http_waf_ctx_t;
typedef struct ngx_http_waf_add_rule_s {
ngx_uint_t flag;
ngx_uint_t offset;
ngx_uint_t loc_offset;
ngx_int_t (*handler)(ngx_conf_t *cf, ngx_http_waf_public_rule_t *pr,
ngx_http_waf_zone_t *mz,
void *conf, ngx_uint_t offset);
} ngx_http_waf_add_rule_t;
typedef struct ngx_http_waf_add_wl_part_s {
ngx_uint_t flag;
ngx_uint_t offset;
ngx_int_t (*handler)(ngx_conf_t *cf, ngx_http_waf_zone_t *mz,
void *conf, ngx_uint_t offset);
} ngx_http_waf_add_wl_part_t;
typedef struct ngx_http_waf_rule_decode_s {
ngx_str_t suffix;
ngx_http_waf_rule_decode_pt handler;
} ngx_http_waf_rule_decode_t;
typedef struct ngx_http_waf_rule_parser_s ngx_http_waf_rule_parser_t;
typedef ngx_int_t (*ngx_http_waf_rule_item_parse_pt)(ngx_conf_t *cf,
ngx_str_t *str, ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
struct ngx_http_waf_rule_parser_s {
ngx_str_t prefix;
ngx_http_waf_rule_item_parse_pt handler;
};
/**
* include: line:2-5: kv include k or v line:6-11: MZ_G include MZ_VAR or MZ_X
* equal: line:13-14 MZ==MZ and name==name
*/
#define ngx_http_waf_mz_ge(one, two) ( \
( \
( ((one)->flag&NGX_HTTP_WAF_STS_MZ_KV) \
& ((two)->flag&NGX_HTTP_WAF_STS_MZ_KV) ) \
&& ( ((one)->flag&NGX_HTTP_WAF_STS_MZ_KV) \
>= ((two)->flag&NGX_HTTP_WAF_STS_MZ_KV) ) \
&& ( ( (two)->flag < NGX_HTTP_WAF_MZ_G_RAW_BODY \
&& ((((one)->flag<<4)&(two)->flag) \
|| (((one)->flag<<8)&(two)->flag)) ) \
|| ( (two)->flag > NGX_HTTP_WAF_MZ_G_RAW_BODY \
&& ((((one)->flag<<1)&(two)->flag) \
|| (((one)->flag<<2)&(two)->flag)) ) ) \
) \
|| ( ((one)->flag == (two)->flag) && (one)->name.len == (two)->name.len \
&& ngx_strncmp((one)->name.data, (two)->name.data, (one)->name.len) == 0 )\
)
/**
* one: #ARGS or @ARGS
* two: ARGS
*/
#define ngx_http_waf_wl_mz(one, two) ( \
(((one)->flag & NGX_HTTP_WAF_MZ_G) == ((two)->flag & NGX_HTTP_WAF_MZ_G)) \
&& (((one)->flag & NGX_HTTP_WAF_STS_MZ_KV) != NGX_HTTP_WAF_STS_MZ_KV) \
&& (((two)->flag & NGX_HTTP_WAF_STS_MZ_KV) == NGX_HTTP_WAF_STS_MZ_KV) \
)
static ngx_int_t ngx_http_waf_init(ngx_conf_t *cf);
static ngx_int_t ngx_http_waf_handler(ngx_http_request_t *r);
static void *ngx_http_waf_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_waf_create_loc_conf(ngx_conf_t *cf);
// static char *ngx_http_waf_init_main_conf(ngx_conf_t *cf, void *conf);
static char *ngx_http_waf_merge_loc_conf(ngx_conf_t *cf,
void *parent, void *child);
static char *ngx_http_waf_main_rule(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_waf_loc_rule(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_waf_check_rule(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_http_waf_set_log(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static void ngx_http_waf_syslog_writer(ngx_http_waf_log_t *log,
u_char *buf, size_t len);
static ngx_int_t ngx_http_waf_parse_rule(ngx_conf_t *cf,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_decode_base64(ngx_http_request_t *r,
ngx_str_t *dst, ngx_str_t *src, ngx_uint_t destory);
static ngx_int_t ngx_http_waf_decode_url(ngx_http_request_t *r,
ngx_str_t *dst, ngx_str_t *src, ngx_uint_t destory);
static ngx_int_t ngx_http_waf_parse_rule_id(ngx_conf_t *cf, ngx_str_t *str,
ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_parse_rule_str(ngx_conf_t *cf, ngx_str_t *str,
ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_parse_rule_score(ngx_conf_t *cf, ngx_str_t *str,
ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_parse_rule_note(ngx_conf_t *cf, ngx_str_t *str,
ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_parse_rule_zone(ngx_conf_t *cf, ngx_str_t *str,
ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_parse_rule_whitelist(ngx_conf_t *cf,
ngx_str_t *str, ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_parse_rule_libinj(ngx_conf_t *cf,
ngx_str_t *str, ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_parse_rule_libmagic(ngx_conf_t *cf,
ngx_str_t *str, ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_parse_rule_hash(ngx_conf_t *cf,
ngx_str_t *str, ngx_http_waf_rule_parser_t *parser,
ngx_http_waf_rule_opt_t *opt);
static ngx_int_t ngx_http_waf_add_rule_handler(ngx_conf_t *cf,
ngx_http_waf_public_rule_t *pr, ngx_http_waf_zone_t *mz,
void *conf, ngx_uint_t offset);
static ngx_int_t ngx_http_waf_add_wl_part_handler(ngx_conf_t *cf,
ngx_http_waf_zone_t *mz, void *wl, ngx_uint_t offset);
static ngx_int_t ngx_http_waf_rule_str_ge_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_str_le_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_str_ct_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_str_eq_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_str_startwith_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_str_endwith_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_str_rx_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_str_sqli_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_str_xss_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_magic_mimetype_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_hash_md5_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_hash_crc32_short_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_rule_hash_crc32_long_handler(
ngx_http_waf_public_rule_t *pr, ngx_str_t *s);
static ngx_int_t ngx_http_waf_check_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_conf_bitmask_t ngx_http_waf_rule_actions[] = {
{ngx_string("LOG"), NGX_HTTP_WAF_STS_RULE_LOG},
{ngx_string("BLOCK"), NGX_HTTP_WAF_STS_RULE_BLOCK},
{ngx_string("DROP"), NGX_HTTP_WAF_STS_RULE_DROP},
{ngx_string("ALLOW"), NGX_HTTP_WAF_STS_RULE_ALLOW},
{ngx_null_string, 0}
};
static ngx_conf_bitmask_t ngx_http_waf_rule_zone_item[] = {
{ ngx_string("URL"),
NGX_HTTP_WAF_MZ_G_URL|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("#URL"),
NGX_HTTP_WAF_MZ_G_URL|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("V_URL:"),
NGX_HTTP_WAF_MZ_VAR_URL|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("X_URL:"),
NGX_HTTP_WAF_MZ_X_URL|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("ARGS"),
NGX_HTTP_WAF_MZ_G_ARGS|NGX_HTTP_WAF_STS_MZ_KV },
{ ngx_string("@ARGS"),
(NGX_HTTP_WAF_MZ_G_ARGS|NGX_HTTP_WAF_STS_MZ_KEY) },
{ ngx_string("#ARGS"),
(NGX_HTTP_WAF_MZ_G_ARGS|NGX_HTTP_WAF_STS_MZ_VAL) },
// TODO: %ARGS args count.
{ ngx_string("V_ARGS:"),
NGX_HTTP_WAF_MZ_VAR_ARGS|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("X_ARGS:"),
NGX_HTTP_WAF_MZ_X_ARGS|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("HEADERS"),
NGX_HTTP_WAF_MZ_G_HEADERS|NGX_HTTP_WAF_STS_MZ_KV },
{ ngx_string("@HEADERS"),
(NGX_HTTP_WAF_MZ_G_HEADERS|NGX_HTTP_WAF_STS_MZ_KEY) },
{ ngx_string("#HEADERS"),
(NGX_HTTP_WAF_MZ_G_HEADERS|NGX_HTTP_WAF_STS_MZ_VAL) },
{ ngx_string("V_HEADERS:"),
NGX_HTTP_WAF_MZ_VAR_HEADERS|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("X_HEADERS:"),
NGX_HTTP_WAF_MZ_X_HEADERS|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("BODY"),
NGX_HTTP_WAF_MZ_G_BODY|NGX_HTTP_WAF_STS_MZ_KV },
{ ngx_string("@BODY"),
NGX_HTTP_WAF_MZ_G_BODY|NGX_HTTP_WAF_STS_MZ_KEY },
{ ngx_string("#BODY"),
NGX_HTTP_WAF_MZ_G_BODY|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("#RAW_BODY"),
NGX_HTTP_WAF_MZ_G_RAW_BODY|NGX_HTTP_WAF_STS_MZ_VAL },
// TODO: file
{ ngx_string("#FILE"),
NGX_HTTP_WAF_MZ_G_FILE_BODY|NGX_HTTP_WAF_STS_MZ_VAL },
// { ngx_string("V_FILE"),
// NGX_HTTP_WAF_MZ_VAR_FILE_BODY|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("X_FILE:"),
NGX_HTTP_WAF_MZ_X_FILE_BODY |NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("V_BODY:"),
NGX_HTTP_WAF_MZ_VAR_BODY|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_string("X_BODY:"),
NGX_HTTP_WAF_MZ_X_BODY|NGX_HTTP_WAF_STS_MZ_VAL },
{ ngx_null_string, 0 }
};
static ngx_http_waf_add_rule_t ngx_http_waf_conf_add_rules[] = {
{ NGX_HTTP_WAF_MZ_G_URL|NGX_HTTP_WAF_MZ_X_URL,
offsetof(ngx_http_waf_main_conf_t, url),
offsetof(ngx_http_waf_loc_conf_t, url),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_G_ARGS|NGX_HTTP_WAF_MZ_X_ARGS,
offsetof(ngx_http_waf_main_conf_t, args),
offsetof(ngx_http_waf_loc_conf_t, args),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_G_HEADERS|NGX_HTTP_WAF_MZ_X_HEADERS,
offsetof(ngx_http_waf_main_conf_t, headers),
offsetof(ngx_http_waf_loc_conf_t, headers),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_G_BODY|NGX_HTTP_WAF_MZ_X_BODY,
offsetof(ngx_http_waf_main_conf_t, body),
offsetof(ngx_http_waf_loc_conf_t, body),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_G_RAW_BODY,
offsetof(ngx_http_waf_main_conf_t, raw_body),
offsetof(ngx_http_waf_loc_conf_t, raw_body),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_G_FILE_BODY|NGX_HTTP_WAF_MZ_X_FILE_BODY,
offsetof(ngx_http_waf_main_conf_t, body_file),
offsetof(ngx_http_waf_loc_conf_t, body_file),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_VAR_URL,
offsetof(ngx_http_waf_main_conf_t, url_var),
offsetof(ngx_http_waf_loc_conf_t, url_var),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_VAR_ARGS,
offsetof(ngx_http_waf_main_conf_t, args_var),
offsetof(ngx_http_waf_loc_conf_t, args_var),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_VAR_HEADERS,
offsetof(ngx_http_waf_main_conf_t, headers_var),
offsetof(ngx_http_waf_loc_conf_t, headers_var),
ngx_http_waf_add_rule_handler },
{ NGX_HTTP_WAF_MZ_VAR_BODY,
offsetof(ngx_http_waf_main_conf_t, body_var),
offsetof(ngx_http_waf_loc_conf_t, body_var),
ngx_http_waf_add_rule_handler },
{ 0,
0,
0,
NULL }
};
static ngx_http_waf_add_wl_part_t ngx_http_waf_conf_add_wl[] = {
{ NGX_HTTP_WAF_MZ_URL,
offsetof(ngx_http_waf_whitelist_t, url_zones),
ngx_http_waf_add_wl_part_handler },
{ NGX_HTTP_WAF_MZ_ARGS,
offsetof(ngx_http_waf_whitelist_t, args_zones),
ngx_http_waf_add_wl_part_handler},
{ NGX_HTTP_WAF_MZ_HEADERS,
offsetof(ngx_http_waf_whitelist_t, headers_zones),
ngx_http_waf_add_wl_part_handler},
{ NGX_HTTP_WAF_MZ_BODY,
offsetof(ngx_http_waf_whitelist_t, body_zones),
ngx_http_waf_add_wl_part_handler},
{ NGX_HTTP_WAF_MZ_G_FILE_BODY,
offsetof(ngx_http_waf_whitelist_t, body_file_zones),
ngx_http_waf_add_wl_part_handler},
{0, 0, NULL}
};
static ngx_http_waf_rule_decode_t ngx_http_waf_rule_decode[] = {
{ngx_string("base64"), ngx_http_waf_decode_base64},
{ngx_string("url"), ngx_http_waf_decode_url},
{ngx_null_string, NULL}
};
static ngx_http_waf_rule_parser_t ngx_http_waf_rule_parser_item[] = {
{ngx_string("id:"), ngx_http_waf_parse_rule_id},
{ngx_string("s:"), ngx_http_waf_parse_rule_score},
{ngx_string("str:"), ngx_http_waf_parse_rule_str},
{ngx_string("libinj:"), ngx_http_waf_parse_rule_libinj},
{ngx_string("libmagic:"), ngx_http_waf_parse_rule_libmagic},
{ngx_string("hash:"), ngx_http_waf_parse_rule_hash},
{ngx_string("z:"), ngx_http_waf_parse_rule_zone},
{ngx_string("wl:"), ngx_http_waf_parse_rule_whitelist},
{ngx_string("note:"), ngx_http_waf_parse_rule_note},
{ngx_null_string, NULL}
};
static ngx_command_t ngx_http_waf_commands[] = {
{ ngx_string("security_rule"),
NGX_HTTP_MAIN_CONF|NGX_CONF_1MORE,
ngx_http_waf_main_rule,
NGX_HTTP_MAIN_CONF_OFFSET,
0,
NULL },
{ ngx_string("security_loc_rule"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_1MORE,
ngx_http_waf_loc_rule,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("security_check"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE2,
ngx_http_waf_check_rule,
NGX_HTTP_LOC_CONF_OFFSET,
0,
&ngx_http_waf_rule_actions },
{ ngx_string("security_waf"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_waf_loc_conf_t, security_waf),
NULL },
{ ngx_string("security_log"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE12,
ngx_http_waf_set_log,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("security_timeout"),
NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_waf_loc_conf_t, security_timeout),
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_waf_module_ctx = {
NULL, /* preconfiguration */
ngx_http_waf_init, /* postconfiguration */
ngx_http_waf_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_waf_create_loc_conf, /* create location configuration */
ngx_http_waf_merge_loc_conf /* merge location configuration */
};
ngx_module_t ngx_http_waf_module = {
NGX_MODULE_V1,
&ngx_http_waf_module_ctx, /* module context */
ngx_http_waf_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
//-- init -------------------
static void *
ngx_http_waf_create_main_conf(ngx_conf_t *cf)
{
ngx_http_waf_main_conf_t *wmcf;
wmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_waf_main_conf_t));
if (wmcf == NULL) {
return NULL;
}
return wmcf;
}
static void *
ngx_http_waf_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_waf_loc_conf_t *wlcf;
wlcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_waf_loc_conf_t));
if (wlcf == NULL) {
return NULL;
}
wlcf->security_waf = NGX_CONF_UNSET;
wlcf->security_timeout = NGX_CONF_UNSET_MSEC;
wlcf->log = NULL;
return wlcf;
}
// -- public ---------------
static u_char *
ngx_strpbrk(u_char *b, u_char *e, char *s) {
u_char *p = b;
while (p < e) {
if (ngx_strchr(s, *p) != NULL) {
return p;
}
p++;
}
return NULL;
}
static u_char *
ngx_memnstr(u_char *s1, char *s2, size_t len)
{
u_char c1, c2;
size_t n;
c2 = *(u_char *) s2++;
n = ngx_strlen(s2);
do {
do {
if (len-- == 0) {
return NULL;
}
c1 = *s1++;
} while (c1 != c2);
if (n > len) {
return NULL;
}
} while (ngx_memcmp((char *)s1, s2, n) != 0);
return --s1;
}
static void
ngx_http_waf_unescape_uri(u_char **dst, u_char **src, size_t size,
ngx_uint_t type)
{
u_char *d, *s, ch, c, decoded;
enum {
sw_usual = 0,
sw_quoted,
sw_quoted_second
} state;
d = *dst;
s = *src;
state = 0;
decoded = 0;
while (size--) {
ch = *s++;
switch (state) {
case sw_usual:
if (ch == '?'
&& (type & (NGX_UNESCAPE_URI|NGX_UNESCAPE_REDIRECT)))
{
*d++ = ch;
goto done;
}
if (ch == '%') {
state = sw_quoted;
break;
}
if (ch == '+') {
*d++ = ' ';
break;
}
*d++ = ch;
break;
case sw_quoted:
if (ch >= '0' && ch <= '9') {
decoded = (u_char) (ch - '0');
state = sw_quoted_second;
break;
}
c = (u_char) (ch | 0x20);
if (c >= 'a' && c <= 'f') {
decoded = (u_char) (c - 'a' + 10);
state = sw_quoted_second;
break;
}
/* the invalid quoted character */
state = sw_usual;
*d++ = ch;
break;
case sw_quoted_second:
state = sw_usual;
if (ch >= '0' && ch <= '9') {
ch = (u_char) ((decoded << 4) + ch - '0');
if (type & NGX_UNESCAPE_REDIRECT) {
if (ch > '%' && ch < 0x7f) {
*d++ = ch;
break;
}
*d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
break;
}
*d++ = ch;
break;
}
c = (u_char) (ch | 0x20);
if (c >= 'a' && c <= 'f') {
ch = (u_char) ((decoded << 4) + c - 'a' + 10);
if (type & NGX_UNESCAPE_URI) {
if (ch == '?') {
*d++ = ch;
goto done;
}
*d++ = ch;
break;
}
if (type & NGX_UNESCAPE_REDIRECT) {
if (ch == '?') {
*d++ = ch;
goto done;
}
if (ch > '%' && ch < 0x7f) {
*d++ = ch;
break;
}
*d++ = '%'; *d++ = *(s - 2); *d++ = *(s - 1);
break;
}
*d++ = ch;
break;
}
/* the invalid quoted character */
break;
}