File: appsensor_request_exception_profile.lua

package info (click to toggle)
modsecurity-crs 2.2.9-1+deb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 3,064 kB
  • ctags: 219
  • sloc: perl: 1,002; ansic: 727; ruby: 69; makefile: 18
file content (789 lines) | stat: -rwxr-xr-x 41,214 bytes parent folder | download | duplicates (2)
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
function main()

--[[ Global Vars ]]
--[[ Import Profile Learning Thresholds
 
 [resource.min_traffic_threshold] 
 Set the resource.min_traffic_threshold as the minimum number of "clean" transactions
 to profile/inspect before enforcement of the profile begins.

 [resource.min_pattern_threshold]
 resource.min_pattern_threshold is the minimum number of times that an individual match should occur
 in order to include the it into the learned profile

]]

MinPatternThreshold = tonumber(m.getvar("RESOURCE.min_pattern_threshold"))
MinTrafficThreshold = tonumber(m.getvar("RESOURCE.min_traffic_threshold")) 
TrafficCounter = m.getvar("RESOURCE.traffic_counter")
	if TrafficCounter == nil then
		TrafficCounter = "1"
		m.setvar("RESOURCE.traffic_counter", TrafficCounter)
        	m.log(4, "Traffic Counter: " ..TrafficCounter.. ".") 
	else
		TrafficCounter = tonumber(TrafficCounter + 1)
		m.setvar("RESOURCE.traffic_counter", TrafficCounter)
		m.log(4, "Traffic Counter: " ..TrafficCounter.. ".")
	end

	--[[ Profile Request Method ]]
	ProfileRequestMethod()

	--[[ Profile Number of Parameters/ARGS ]]
	ProfileNumOfArgs()

	--[[ Profile Parameter Names ]]
	ProfileArgsNames()

	--[[ Profile Parameter Lengths ]]
	ProfileArgsLength()

	--[[ Profile Parameter Character Class ]]
	ProfileArgCharClass()

	if (TrafficCounter == MinTrafficThreshold) then
		m.setvar("RESOURCE.enforce_re_profile", "1")
	end

m.log(4, "Ending Profile Analyzer Script")
return nil
end

--[[ Begin Profiler Functions ]]

function ProfileArgCharClass()
	local Args = {}
        Args = m.getvars("ARGS", {"none"})

	for k,v in pairs(Args) do
  		name = v["name"];
  		value = v["value"];
  		m.log(4, "CharClass Check - Arg Name: " ..name.. " and Value: " ..value.. ".");

		--[[ Check for Digits Character Class ]]
		if string.match(value, "^%d+$") then
			m.log(4, "Parameter " ..name.. " payload matches digit class.")
			local EnforceArgCharClassDigits = m.getvar("RESOURCE.enforce_charclass_digits")
			if not (EnforceArgCharClassDigits) then
				local ArgDigitCounter = m.getvar("RESOURCE." ..name.. "_digit_counter")
				if not (ArgDigitCounter) then
                                	ArgDigitCounter = 1
                                	m.log(4, "Creating " .. name .. " Digit Counter: " .. ArgDigitCounter)
                                	m.setvar("RESOURCE." .. name .. "_digit_counter", ArgDigitCounter)
                        	else
                                	ArgDigitCounter = ArgDigitCounter + 1
                                	m.log(4, "Updating " .. name .. " Digit Counter: " .. ArgDigitCounter)
                                	m.setvar("RESOURCE." .. name .. "_digit_counter", ArgDigitCounter)
                        	end
				
				if (ArgDigitCounter == MinPatternThreshold) then
                			if not (EnforceArgCharClassDigits) then
                        			EnforceArgCharClassDigits = name
                			else
                        			EnforceArgCharClassDigits = EnforceArgCharClassDigits .. ", " .. name
                			end

                			m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Digits Enforcement list: " .. EnforceArgCharClassDigits)
                			m.setvar("RESOURCE.enforce_charclass_digits", EnforceArgCharClassDigits)
					m.setvar("!RESOURCE." .. name .. "_digit_counter", "0")
        			end
			else
				local CheckArgCharClassDigits = string.find(EnforceArgCharClassDigits, name)
   				if (CheckArgCharClassDigits) then
        				m.log(4, "Arg Name: " .. name .. " already in Digits Enforcement list.")
   				else
					local ArgDigitCounter = m.getvar("RESOURCE." ..name.. "_digit_counter")
                                	if not (ArgDigitCounter) then
                                        	ArgDigitCounter = 1
                                        	m.log(4, "Creating " .. name .. " Digit Counter: " .. ArgDigitCounter)
                                        	m.setvar("RESOURCE." .. name .. "_digit_counter", ArgDigitCounter)
                                	else
                                        	ArgDigitCounter = ArgDigitCounter + 1
                                        	m.log(4, "Updating " .. name .. " Digit Counter: " .. ArgDigitCounter)
                                        	m.setvar("RESOURCE." .. name .. "_digit_counter", ArgDigitCounter)
                                	end

                                	if (ArgDigitCounter == MinPatternThreshold) then
                                        	if not (EnforceArgCharClassDigits) then
                                                	EnforceArgCharClassDigits = name
                                        	else                                                
							EnforceArgCharClassDigits = EnforceArgCharClassDigits .. ", " .. name
                                        	end

                                        	m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Digits Enforcement list: " .. EnforceArgCharClassDigits)
                                        	m.setvar("RESOURCE.enforce_charclass_digits", EnforceArgCharClassDigits)
                                	end
				end
			end
                        if (TrafficCounter == MinTrafficThreshold) then
                                m.setvar("!RESOURCE." .. name .. "_digit_counter", "0")
                        end

	

		--[[ Check for Email Class ]]
                elseif string.match(value, "^[A-Za-z0-9%.%%%+%-]+@[A-Za-z0-9%.%%%+%-]+%.%w%w%w?%w?$") then
                        m.log(4, "Parameter " ..name.. " payload matches email class.")
			local EnforceArgCharClassEmail = m.getvar("RESOURCE.enforce_charclass_email")
			if not (EnforceArgCharClassEmail) then
				local ArgEmailCounter = m.getvar("RESOURCE." ..name.. "_email_counter")
				if not (ArgEmailCounter) then
                                	ArgEmailCounter = 1
                                	m.log(4, "Creating " .. name .. " Email Counter: " .. ArgEmailCounter)
                                	m.setvar("RESOURCE." .. name .. "_email_counter", ArgEmailCounter)
                        	else
                                	ArgEmailCounter = ArgEmailCounter + 1
                                	m.log(4, "Updating " .. name .. " Email Counter: " .. ArgEmailCounter)
                                	m.setvar("RESOURCE." .. name .. "_email_counter", ArgEmailCounter)
                        	end
				
				if (ArgEmailCounter == MinPatternThreshold) then
                			if not (EnforceArgCharClassEmail) then
                        			EnforceArgCharClassEmail = name
                			else
                        			EnforceArgCharClassEmail = EnforceArgCharClassEmail .. ", " .. name
                			end

                			m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Email Enforcement list: " .. EnforceArgCharClassEmail)
                			m.setvar("RESOURCE.enforce_charclass_email", EnforceArgCharClassEmail)
        			end
			else
				local CheckArgCharClassEmail = string.find(EnforceArgCharClassEmail, name)
   				if (CheckArgCharClassEmail) then
        				m.log(4, "Arg Name: " .. name .. " already in Email Enforcement list.")
   				else
					local ArgEmailCounter = m.getvar("RESOURCE." ..name.. "_email_counter")
                                	if not (ArgEmailCounter) then
                                        	ArgEmailCounter = 1
                                        	m.log(4, "Creating " .. name .. " Email Counter: " .. ArgEmailCounter)
                                        	m.setvar("RESOURCE." .. name .. "_email_counter", ArgEmailCounter)
                                	else
                                        	ArgEmailCounter = ArgEmailCounter + 1
                                        	m.log(4, "Updating " .. name .. " Email Counter: " .. ArgEmailCounter)
                                        	m.setvar("RESOURCE." .. name .. "_email_counter", ArgEmailCounter)
                                	end

                                	if (ArgEmailCounter == MinPatternThreshold) then
                                        	if not (EnforceArgCharClassEmail) then
                                                	EnforceArgCharClassEmail = name
                                        	else                                                
							EnforceArgCharClassEmail = EnforceArgCharClassEmail .. ", " .. name
                                        	end

                                        	m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Email Enforcement list: " .. EnforceArgCharClassEmail)
                                        	m.setvar("RESOURCE.enforce_charclass_email", EnforceArgCharClassEmail)
                                	end
				end
			end
			if (TrafficCounter == MinTrafficThreshold) then
                		m.setvar("!RESOURCE." .. name .. "_email_counter", "0")
        		end



		--[[ Check for URL Class ]]
                elseif string.match(value, "[A-Za-z]+://[A-Za-z0-9-_]+%.[A-Za-z0-9-_.]+/?") then
                        m.log(4, "Parameter " ..name.. " payload matches url class.")
			local EnforceArgCharClassUrl = m.getvar("RESOURCE.enforce_charclass_url")
			if not (EnforceArgCharClassUrl) then
				local ArgUrlCounter = m.getvar("RESOURCE." ..name.. "_url_counter")
				if not (ArgUrlCounter) then
                                	ArgUrlCounter = 1
                                	m.log(4, "Creating " .. name .. " Url Counter: " .. ArgUrlCounter)
                                	m.setvar("RESOURCE." .. name .. "_url_counter", ArgUrlCounter)
                        	else
                                	ArgUrlCounter = ArgUrlCounter + 1
                                	m.log(4, "Updating " .. name .. " Url Counter: " .. ArgUrlCounter)
                                	m.setvar("RESOURCE." .. name .. "_url_counter", ArgUrlCounter)
                        	end
				
				if (ArgUrlCounter == MinPatternThreshold) then
                			if not (EnforceArgCharClassUrl) then
                        			EnforceArgCharClassUrl = name
                			else
                        			EnforceArgCharClassUrl = EnforceArgCharClassUrl .. ", " .. name
                			end

                			m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Url Enforcement list: " .. EnforceArgCharClassUrl)
                			m.setvar("RESOURCE.enforce_charclass_url", EnforceArgCharClassUrl)
        			end
			else
				local CheckArgCharClassUrl = string.find(EnforceArgCharClassUrl, name)
   				if (CheckArgCharClassUrl) then
        				m.log(4, "Arg Name: " .. name .. " already in Url Enforcement list.")
   				else
					local ArgUrlCounter = m.getvar("RESOURCE." ..name.. "_url_counter")
                                	if not (ArgUrlCounter) then
                                        	ArgUrlCounter = 1
                                        	m.log(4, "Creating " .. name .. " Url Counter: " .. ArgUrlCounter)
                                        	m.setvar("RESOURCE." .. name .. "_url_counter", ArgUrlCounter)
                                	else
                                        	ArgUrlCounter = ArgUrlCounter + 1
                                        	m.log(4, "Updating " .. name .. " Url Counter: " .. ArgUrlCounter)
                                        	m.setvar("RESOURCE." .. name .. "_url_counter", ArgUrlCounter)
                                	end

                                	if (ArgUrlCounter == MinPatternThreshold) then
                                        	if not (EnforceArgCharClassUrl) then
                                                	EnforceArgCharClassUrl = name
                                        	else                                                
							EnforceArgCharClassUrl = EnforceArgCharClassUrl .. ", " .. name
                                        	end

                                        	m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Url Enforcement list: " .. EnforceArgCharClassUrl)
                                        	m.setvar("RESOURCE.enforce_charclass_url", EnforceArgCharClassUrl)
                                	end
				end
			end
			if (TrafficCounter == MinTrafficThreshold) then
                		m.setvar("!RESOURCE." .. name .. "_url_counter", "0")
        		end



		--[[ Check for Path Class ]]
                elseif string.match(value, "[-a-zA-Z0-9/._]*/[-a-zA-Z0-9/._]*") then
                        m.log(4, "Parameter " ..name.. " payload matches path class.")
			local EnforceArgCharClassPath = m.getvar("RESOURCE.enforce_charclass_path")
			if not (EnforceArgCharClassPath) then
				local ArgPathCounter = m.getvar("RESOURCE." ..name.. "_path_counter")
				if not (ArgPathCounter) then
                                	ArgPathCounter = 1
                                	m.log(4, "Creating " .. name .. " Path Counter: " .. ArgPathCounter)
                                	m.setvar("RESOURCE." .. name .. "_path_counter", ArgPathCounter)
                        	else
                                	ArgPathCounter = ArgPathCounter + 1
                                	m.log(4, "Updating " .. name .. " Path Counter: " .. ArgPathCounter)
                                	m.setvar("RESOURCE." .. name .. "_path_counter", ArgPathCounter)
                        	end
				
				if (ArgPathCounter == MinPatternThreshold) then
                			if not (EnforceArgCharClassPath) then
                        			EnforceArgCharClassPath = name
                			else
                        			EnforceArgCharClassPath = EnforceArgCharClassPath .. ", " .. name
                			end

                			m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Path Enforcement list: " .. EnforceArgCharClassPath)
                			m.setvar("RESOURCE.enforce_charclass_path", EnforceArgCharClassPath)
        			end
			else
				local CheckArgCharClassPath = string.find(EnforceArgCharClassPath, name)
   				if (CheckArgCharClassPath) then
        				m.log(4, "Arg Name: " .. name .. " already in Path Enforcement list.")
   				else
					local ArgPathCounter = m.getvar("RESOURCE." ..name.. "_path_counter")
                                	if not (ArgPathCounter) then
                                        	ArgPathCounter = 1
                                        	m.log(4, "Creating " .. name .. " Path Counter: " .. ArgPathCounter)
                                        	m.setvar("RESOURCE." .. name .. "_path_counter", ArgPathCounter)
                                	else
                                        	ArgPathCounter = ArgPathCounter + 1
                                        	m.log(4, "Updating " .. name .. " Path Counter: " .. ArgPathCounter)
                                        	m.setvar("RESOURCE." .. name .. "_path_counter", ArgPathCounter)
                                	end

                                	if (ArgPathCounter == MinPatternThreshold) then
                                        	if not (EnforceArgCharClassPath) then
                                                	EnforceArgCharClassPath = name
                                        	else                                                
							EnforceArgCharClassPath = EnforceArgCharClassPath .. ", " .. name
                                        	end

                                        	m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Path Enforcement list: " .. EnforceArgCharClassPath)
                                        	m.setvar("RESOURCE.enforce_charclass_path", EnforceArgCharClassPath)
                                	end
				end
			end
			if (TrafficCounter == MinTrafficThreshold) then
                		m.setvar("!RESOURCE." .. name .. "_path_counter", "0")
        		end



		--[[ Check for Flag Parameter Class ]]
                elseif string.match(value, "^$") then
                        m.log(4, "Parameter " ..name.. " payload matches flag parameter class.")
			local EnforceArgCharClassFlag = m.getvar("RESOURCE.enforce_charclass_flag")
			if not (EnforceArgCharClassFlag) then
				local ArgFlagCounter = m.getvar("RESOURCE." ..name.. "_flag_counter")
				if not (ArgFlagCounter) then
                                	ArgFlagCounter = 1
                                	m.log(4, "Creating " .. name .. " Flag Counter: " .. ArgFlagCounter)
                                	m.setvar("RESOURCE." .. name .. "_flag_counter", ArgFlagCounter)
                        	else
                                	ArgFlagCounter = ArgFlagCounter + 1
                                	m.log(4, "Updating " .. name .. " Flag Counter: " .. ArgFlagCounter)
                                	m.setvar("RESOURCE." .. name .. "_flag_counter", ArgFlagCounter)
                        	end
				
				if (ArgFlagCounter == MinPatternThreshold) then
                			if not (EnforceArgCharClassFlag) then
                        			EnforceArgCharClassFlag = name
                			else
                        			EnforceArgCharClassFlag = EnforceArgCharClassFlag .. ", " .. name
                			end

                			m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Flag Enforcement list: " .. EnforceArgCharClassFlag)
                			m.setvar("RESOURCE.enforce_charclass_flag", EnforceArgCharClassFlag)
        			end
			else
				local CheckArgCharClassFlag = string.find(EnforceArgCharClassFlag, name)
   				if (CheckArgCharClassFlag) then
        				m.log(4, "Arg Name: " .. name .. " already in Flag Enforcement list.")
   				else
					local ArgFlagCounter = m.getvar("RESOURCE." ..name.. "_flag_counter")
                                	if not (ArgFlagCounter) then
                                        	ArgFlagCounter = 1
                                        	m.log(4, "Creating " .. name .. " Flag Counter: " .. ArgFlagCounter)
                                        	m.setvar("RESOURCE." .. name .. "_flag_counter", ArgFlagCounter)
                                	else
                                        	ArgFlagCounter = ArgFlagCounter + 1
                                        	m.log(4, "Updating " .. name .. " Flag Counter: " .. ArgFlagCounter)
                                        	m.setvar("RESOURCE." .. name .. "_flag_counter", ArgFlagCounter)
                                	end

                                	if (ArgFlagCounter == MinPatternThreshold) then
                                        	if not (EnforceArgCharClassFlag) then
                                                	EnforceArgCharClassFlag = name
                                        	else                                                
							EnforceArgCharClassFlag = EnforceArgCharClassFlag .. ", " .. name
                                        	end

                                        	m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Flag Enforcement list: " .. EnforceArgCharClassFlag)
                                        	m.setvar("RESOURCE.enforce_charclass_flag", EnforceArgCharClassFlag)
                                	end
				end
			end
			if (TrafficCounter == MinTrafficThreshold) then
                		m.setvar("!RESOURCE." .. name .. "_flag_counter", "0")
        		end




		--[[ Check for Alpha/Letters Character Class ]]
		elseif string.match(value, "^%a+$") then
			m.log(4, "Parameter " ..name.. " payload matches alpha class.")
			local EnforceArgCharClassAlpha = m.getvar("RESOURCE.enforce_charclass_alphas")
			if not (EnforceArgCharClassAlpha) then
				local ArgAlphaCounter = m.getvar("RESOURCE." ..name.. "_alpha_counter")
				if not (ArgAlphaCounter) then
                                	ArgAlphaCounter = 1
                                	m.log(4, "Creating " .. name .. " Alpha Counter: " .. ArgAlphaCounter)
                                	m.setvar("RESOURCE." .. name .. "_alpha_counter", ArgAlphaCounter)
                        	else
                                	ArgAlphaCounter = ArgAlphaCounter + 1
                                	m.log(4, "Updating " .. name .. " Alpha Counter: " .. ArgAlphaCounter)
                                	m.setvar("RESOURCE." .. name .. "_alpha_counter", ArgAlphaCounter)
                        	end
				
				if (ArgAlphaCounter == MinPatternThreshold) then
                			if not (EnforceArgCharClassAlpha) then
                        			EnforceArgCharClassAlpha = name
                			else
                        			EnforceArgCharClassAlpha = EnforceArgCharClassAlpha .. ", " .. name
                			end

                			m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Alpha Enforcement list: " .. EnforceArgCharClassAlpha)
                			m.setvar("RESOURCE.enforce_charclass_alphas", EnforceArgCharClassAlpha)
					m.setvar("!RESOURCE." .. name .. "_alpha_counter", "0")
        			end
			else
				local CheckArgCharClassAlpha = string.find(EnforceArgCharClassAlpha, name)
   				if (CheckArgCharClassAlpha) then
        				m.log(4, "Arg Name: " .. name .. " already in Alpha Enforcement list.")
   				else
					local ArgAlphaCounter = m.getvar("RESOURCE." ..name.. "_alpha_counter")
                                	if not (ArgAlphaCounter) then
                                        	ArgAlphaCounter = 1
                                        	m.log(4, "Creating " .. name .. " Alpha Counter: " .. ArgAlphaCounter)
                                        	m.setvar("RESOURCE." .. name .. "_alpha_counter", ArgAlphaCounter)
                                	else
                                        	ArgAlphaCounter = ArgAlphaCounter + 1
                                        	m.log(4, "Updating " .. name .. " Alpha Counter: " .. ArgAlphaCounter)
                                        	m.setvar("RESOURCE." .. name .. "_alpha_counter", ArgAlphaCounter)
                                	end

                                	if (ArgAlphaCounter == MinPatternThreshold) then
                                        	if not (EnforceArgCharClassAlpha) then
                                                	EnforceArgCharClassAlpha = name
                                        	else                                                
							EnforceArgCharClassAlpha = EnforceArgCharClassAlpha .. ", " .. name
                                        	end

                                        	m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the Alpha Enforcement list: " .. EnforceArgCharClassAlpha)
                                        	m.setvar("RESOURCE.enforce_charclass_alphas", EnforceArgCharClassAlpha)
                                	end
				end

			end
			if (TrafficCounter == MinTrafficThreshold) then
                                m.setvar("!RESOURCE." .. name .. "_alpha_counter", "0")
                        end



		--[[ Check for AlphaNumeric Character Class ]]
		elseif string.match(value, "^%w+$") then
			m.log(4, "Parameter " ..name.. " payload matches alphanumeric class.")
			local EnforceArgCharClassAlphaNumeric = m.getvar("RESOURCE.enforce_charclass_alphanumeric")
			if not (EnforceArgCharClassAlphaNumeric) then
				local ArgAlphaNumericCounter = m.getvar("RESOURCE." ..name.. "_alphanumeric_counter")
				if not (ArgAlphaNumericCounter) then
                                	ArgAlphaNumericCounter = 1
                                	m.log(4, "Creating " .. name .. " AlphaNumeric Counter: " .. ArgAlphaNumericCounter)
                                	m.setvar("RESOURCE." .. name .. "_alphanumeric_counter", ArgAlphaNumericCounter)
                        	else
                                	ArgAlphaNumericCounter = ArgAlphaNumericCounter + 1
                                	m.log(4, "Updating " .. name .. " AlphaNumeric Counter: " .. ArgAlphaNumericCounter)
                                	m.setvar("RESOURCE." .. name .. "_alphanumeric_counter", ArgAlphaNumericCounter)
                        	end
				
				if (ArgAlphaNumericCounter == MinPatternThreshold) then
                			if not (EnforceArgCharClassAlphaNumeric) then
                        			EnforceArgCharClassAlphaNumeric = name
                			else
                        			EnforceArgCharClassAlphaNumeric = EnforceArgCharClassAlphaNumeric .. ", " .. name
                			end

                			m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the AlphaNumeric Enforcement list: " .. EnforceArgCharClassAlphaNumeric)
                			m.setvar("RESOURCE.enforce_charclass_alphanumeric", EnforceArgCharClassAlphaNumeric)
					m.setvar("!RESOURCE." .. name .. "_alphanumeric_counter", "0")
        			end
			else
				local CheckArgCharClassAlphaNumeric = string.find(EnforceArgCharClassAlphaNumeric, name)
   				if (CheckArgCharClassAlphaNumeric) then
        				m.log(4, "Arg Name: " .. name .. " already in AlphaNumeric Enforcement list.")
   				else
					local ArgAlphaNumericCounter = m.getvar("RESOURCE." ..name.. "_alphanumeric_counter")
                                	if not (ArgAlphaNumericCounter) then
                                        	ArgAlphaNumericCounter = 1
                                        	m.log(4, "Creating " .. name .. " AlphaNumeric Counter: " .. ArgAlphaNumericCounter)
                                        	m.setvar("RESOURCE." .. name .. "_alphanumeric_counter", ArgAlphaNumericCounter)
                                	else
                                        	ArgAlphaNumericCounter = ArgAlphaNumericCounter + 1
                                        	m.log(4, "Updating " .. name .. " AlphaNumeric Counter: " .. ArgAlphaNumericCounter)
                                        	m.setvar("RESOURCE." .. name .. "_alphanumeric_counter", ArgAlphaNumericCounter)
                                	end

                                	if (ArgAlphaNumericCounter == MinPatternThreshold) then
                                        	if not (EnforceArgCharClassAlphaNumeric) then
                                                	EnforceArgCharClassAlphaNumeric = name
                                        	else                                                
							EnforceArgCharClassAlphaNumeric = EnforceArgCharClassAlphaNumeric .. ", " .. name
                                        	end

                                        	m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the AlphaNumeric Enforcement list: " .. EnforceArgCharClassAlphaNumeric)
                                        	m.setvar("RESOURCE.enforce_charclass_alphanumeric", EnforceArgCharClassAlphaNumeric)
                                	end
				end
			end
                        if (TrafficCounter == MinTrafficThreshold) then
                                m.setvar("!RESOURCE." .. name .. "_alphanumeric_counter", "0")
                        end


		--[[ Check for SafeText Character Class ]]
                elseif string.match(value, "^[a-zA-Z0-9%s_%.%-]+$") then
                        m.log(4, "Parameter " ..name.. " payload matches safetext class.")
			local EnforceArgCharClassSafeText = m.getvar("RESOURCE.enforce_charclass_safetext")
			if not (EnforceArgCharClassSafeText) then
				local ArgSafeTextCounter = m.getvar("RESOURCE." ..name.. "_safetext_counter")
				if not (ArgSafeTextCounter) then
                                	ArgSafeTextCounter = 1
                                	m.log(4, "Creating " .. name .. " SafeText Counter: " .. ArgSafeTextCounter)
                                	m.setvar("RESOURCE." .. name .. "_safetext_counter", ArgSafeTextCounter)
                        	else
                                	ArgSafeTextCounter = ArgSafeTextCounter + 1
                                	m.log(4, "Updating " .. name .. " SafeText Counter: " .. ArgSafeTextCounter)
                                	m.setvar("RESOURCE." .. name .. "_safetext_counter", ArgSafeTextCounter)
                        	end
				
				if (ArgSafeTextCounter == MinPatternThreshold) then
                			if not (EnforceArgCharClassSafeText) then
                        			EnforceArgCharClassSafeText = name
                			else
                        			EnforceArgCharClassSafeText = EnforceArgCharClassSafeText .. ", " .. name
                			end

                			m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the SafeText Enforcement list: " .. EnforceArgCharClassSafeText)
                			m.setvar("RESOURCE.enforce_charclass_safetext", EnforceArgCharClassSafeText)
        			end
			else
				local CheckArgCharClassSafeText = string.find(EnforceArgCharClassSafeText, name)
   				if (CheckArgCharClassSafeText) then
        				m.log(4, "Arg Name: " .. name .. " already in SafeText Enforcement list.")
   				else
					local ArgSafeTextCounter = m.getvar("RESOURCE." ..name.. "_safetext_counter")
                                	if not (ArgSafeTextCounter) then
                                        	ArgSafeTextCounter = 1
                                        	m.log(4, "Creating " .. name .. " SafeText Counter: " .. ArgSafeTextCounter)
                                        	m.setvar("RESOURCE." .. name .. "_safetext_counter", ArgSafeTextCounter)
                                	else
                                        	ArgSafeTextCounter = ArgSafeTextCounter + 1
                                        	m.log(4, "Updating " .. name .. " SafeText Counter: " .. ArgSafeTextCounter)
                                        	m.setvar("RESOURCE." .. name .. "_safetext_counter", ArgSafeTextCounter)
                                	end

                                	if (ArgSafeTextCounter == MinPatternThreshold) then
                                        	if not (EnforceArgCharClassSafeText) then
                                                	EnforceArgCharClassSafeText = name
                                        	else                                                
							EnforceArgCharClassSafeText = EnforceArgCharClassSafeText .. ", " .. name
                                        	end

                                        	m.log(4, "Arg Name: " .. name .. " Reached Pattern Threshold. Adding it to the SafeText Enforcement list: " .. EnforceArgCharClassSafeText)
                                        	m.setvar("RESOURCE.enforce_charclass_safetext", EnforceArgCharClassSafeText)
                                	end
				end
			end
			if (TrafficCounter == MinTrafficThreshold) then
                		m.setvar("!RESOURCE." .. name .. "_safetext_counter", "0")
        		end



		end

	end
end

function ProfileArgsLength()
	local ArgsLength = {}
        ArgsLength = m.getvars("ARGS", {"none", "length"})

 for k,v in pairs(ArgsLength) do
  name = v["name"];
  value = v["value"];
  m.log(4, "Arg Name: " ..name.. " and Length: " ..value.. ".");

  local EnforceArgLength = m.getvar("RESOURCE.enforce_" ..name .. "_length")
  if EnforceArgsLength ~= nil then
  local CheckArgsLength = string.find(EnforceArgLength, value)
   if (CheckArgsLength) then
	m.log(4, "Arg Name: " .. name .. " with Length: :" ..value.. " already in Enforcement list.")
   else
        local ArgLengthCounter = m.getvar("RESOURCE." .. name .. "_length_" ..value.. "_counter")
        if not (ArgLengthCounter) then
                ArgLengthCounter = 1
                m.log(4, "Creating " .. name .. " Length " ..value.. " Counter: " .. ArgLengthCounter)
                m.setvar("RESOURCE." .. name .. "_length_" ..value.. "_counter", ArgLengthCounter)
        else
                ArgLengthCounter = ArgLengthCounter + 1
                m.log(4, "Increasing " .. name .. " Length " .. value .. " Counter: " .. ArgLengthCounter)
                m.setvar("RESOURCE." .. name .. "_length_" ..value.. "_counter", ArgLengthCounter)
        end


	if (ArgLengthCounter == MinPatternThreshold) then
                if not (EnforceArgLength) then
                        EnforceArgLength = value
                else
                        EnforceArgLength = EnforceArgLength .. ", " .. value
                end

                m.log(4, "Arg Name: " .. name .. " with Length: " .. value .. " Reached Pattern Threshold. Adding it to the Enforcement list: " .. EnforceArgLength)
                m.setvar("RESOURCE.enforce_" ..name .. "_length", EnforceArgLength)
        end
   end
  else
	local ArgLengthCounter = m.getvar("RESOURCE." .. name .. "_length_" ..value.. "_counter") 
        if not (ArgLengthCounter) then
                ArgLengthCounter = 1
		m.log(4, "Creating " .. name .. " Length " ..value.. " Counter: " .. ArgLengthCounter)
                m.setvar("RESOURCE." .. name .. "_length_" ..value.. "_counter", ArgLengthCounter)
        else
                ArgLengthCounter = ArgLengthCounter + 1
                m.log(4, "Increasing " .. name .. " Length " .. value .. " Counter: " .. ArgLengthCounter)
                m.setvar("RESOURCE." .. name .. "_length_" ..value.. "_counter", ArgLengthCounter)
        end

        if (ArgLengthCounter == MinPatternThreshold) then
                if not (EnforceArgLength) then
                        EnforceArgLength = value
                else
                        EnforceArgLength = EnforceArgLength .. ", " .. value
                end

                m.log(4, "Arg Name: " .. name .. " with Length: " .. value .. " Reached Pattern Threshold. Adding it to the Enforcement list: " .. EnforceArgLength)
                m.setvar("RESOURCE.enforce_" ..name.. "_length", EnforceArgLength)
        end
  end

	if (TrafficCounter == MinTrafficThreshold) then
                i=1
                length_of_arg={}
                for num in string.gmatch(EnforceArgLength, "%d+") do
                        length_of_arg[i]=num;i=i+1;
                end
                local MinArgLength = math.min(unpack(length_of_arg))
                m.setvar("RESOURCE." .. name .. "_length_min", MinArgLength)
                local MaxArgLength = math.max(unpack(length_of_arg))
                m.setvar("RESOURCE." .. name .. "_length_max", MaxArgLength)
                m.log(4, "Min Length of " .. name .. ": " ..MinArgLength.. " and Max Length: " ..MaxArgLength.. ".")
		m.setvar("!RESOURCE." .. name .. "_length_" ..value.. "_counter", "0")
        end

 end
end

function ProfileArgsNames()
	local ArgsNames = {}
        ArgsNames = m.getvars("ARGS_NAMES", {"none"})
	local EnforceArgsNames = m.getvar("RESOURCE.enforce_args_names")

 for k,v in pairs(ArgsNames) do
  name = v["name"];
  value = v["value"];
  m.log(4, "ArgsName: " ..value.. ".");

  if EnforceArgsNames ~= nil then
  local CheckArgsNames = string.find(EnforceArgsNames, value)
   if (CheckArgsNames) then
	m.log(4, "Arg Name: " .. value .. " already in Enforcement list.")
   else

        local ArgsNamesCounter = m.getvar("RESOURCE.args_names_counter_" .. value)
        if not (ArgsNamesCounter) then
                ArgsNamesCounter = 1
                m.log(4, "Creating " .. value .. " Pattern Score: " .. ArgsNamesCounter)
                m.setvar("RESOURCE.args_names_counter_" .. value, ArgsNamesCounter)
        else
                ArgsNamesCounter = ArgsNamesCounter + 1
                m.log(4, "Increasing " .. value .. " Pattern Score to: " .. ArgsNamesCounter)
                m.setvar("RESOURCE.args_names_counter_" .. value, ArgsNamesCounter)
        end


	if (ArgsNamesCounter == MinPatternThreshold) then
                if not (EnforceArgsNames) then
                        EnforceArgsNames = value
                else
                        EnforceArgsNames = EnforceArgsNames .. ", " .. value
                end

                m.log(4, "Args Names: " .. value .. " Reached Pattern Threshold. Adding it to the Enforcement list: " .. EnforceArgsNames)
                m.setvar("RESOURCE.enforce_args_names", EnforceArgsNames)
                m.setvar("!RESOURCE.args_names_counter_" .. value, "0")
        end
   end
  else
	local ArgsNamesCounter = m.getvar("RESOURCE.args_names_counter_" .. value)
        if not (ArgsNamesCounter) then
                ArgsNamesCounter = 1
                m.log(4, "Creating " .. value .. " Pattern Score: " .. ArgsNamesCounter)
                m.setvar("RESOURCE.args_names_counter_" .. value, ArgsNamesCounter)
        else
                ArgsNamesCounter = ArgsNamesCounter + 1
                m.log(4, "Increasing " .. value .. " Pattern Score to: " .. ArgsNamesCounter)
                m.setvar("RESOURCE.args_names_counter_" .. value, ArgsNamesCounter)
        end


        if (ArgsNamesCounter == MinPatternThreshold) then
                if not (EnforceArgsNames) then
                        EnforceArgsNames = value
                else
                        EnforceArgsNames = EnforceArgsNames .. ", " .. value
                end

                m.log(4, "Args Names: " .. value .. " Reached Pattern Threshold. Adding it to the Enforcement list: " .. EnforceArgsNames)
                m.setvar("RESOURCE.enforce_args_names", EnforceArgsNames)
                m.setvar("!RESOURCE.args_names_counter_" .. value, "0")
        end
  end
 end
end

function ProfileRequestMethod()
	local RequestMethod = m.getvar("REQUEST_METHOD", {"none"})

	local EnforceRequestMethods = m.getvar("RESOURCE.enforce_request_methods")
	if EnforceRequestMethods ~= nil then
	local CheckEnforceMethods = string.find(EnforceRequestMethods, RequestMethod)
        	if (CheckEnforceMethods) then
			m.log(4, "Request Method " .. RequestMethod .. " already in Enforcement List.")
		end
	end

	local RequestMethodCounter = m.getvar("RESOURCE.request_method_counter_" .. RequestMethod)
        if not (RequestMethodCounter) then
                RequestMethodCounter = 1
		m.log(4, "Creating " .. RequestMethod .. " Pattern Score: " .. RequestMethodCounter)
                m.setvar("RESOURCE.request_method_counter_" .. RequestMethod, RequestMethodCounter) 
        else
                RequestMethodCounter = RequestMethodCounter + 1
		m.log(4, "Increasing " .. RequestMethod .. " Pattern Score to: " .. RequestMethodCounter)
                m.setvar("RESOURCE.request_method_counter_" .. RequestMethod, RequestMethodCounter)
        end

	if (RequestMethodCounter == MinPatternThreshold) then
                if not (EnforceRequestMethods) then
                        EnforceRequestMethods = RequestMethod 
                else
                        EnforceRequestMethods = EnforceRequestMethods .. ", " .. RequestMethod 
                end

		m.log(4, "Request Method Reached Pattern Threshold. Adding it to the EnforceRequestMethods list: " .. EnforceRequestMethods)
		m.setvar("RESOURCE.enforce_request_methods", EnforceRequestMethods)
	end

	if (TrafficCounter == MinTrafficThreshold) then
		m.setvar("!RESOURCE.request_method_counter_" .. RequestMethod, "0")
	end

end

function ProfileNumOfArgs()
	local ARGS = {}
	local ARGS = m.getvars("ARGS", {"none"})
        local NumOfArgs = tonumber(#ARGS)

      	local EnforceNumOfArgs = m.getvar("RESOURCE.enforce_num_of_args")
        if EnforceNumOfArgs ~= nil then
		local CheckNumOfArgs = string.find(EnforceNumOfArgs, NumOfArgs)
        	if (CheckNumOfArgs) then
                	m.log(4, "ARGS #: " .. NumOfArgs .. " already in Enforcement List.")
        	end
	end

	local NumOfArgsCounter = m.getvar("RESOURCE.NumOfArgs_counter_" .. NumOfArgs)
        if not (NumOfArgsCounter) then
                NumOfArgsCounter = 1
		m.log(4, "Current # of ARGS: " ..NumOfArgs.. " has not been previously seen.")
                m.log(4, "Creating " .. NumOfArgs .. " Pattern Score to: " .. NumOfArgsCounter)
		m.setvar("RESOURCE.NumOfArgs_counter_" .. NumOfArgs, NumOfArgsCounter)
        else
                NumOfArgsCounter = NumOfArgsCounter + 1
		m.log(4, "Current # of ARGS: " ..NumOfArgs.. " has been previously seen.")
                m.log(4, "Increasing " .. NumOfArgs .. " Pattern Score to: " .. NumOfArgsCounter)
                m.setvar("RESOURCE.NumOfArgs_counter_" .. NumOfArgs, NumOfArgsCounter)
        end


        if (NumOfArgsCounter == MinPatternThreshold) then
        	if not (EnforceNumOfArgs) then
                	EnforceNumOfArgs = NumOfArgs
		else
			EnforceNumOfArgs = EnforceNumOfArgs.. ", " ..NumOfArgs
		end
                m.log(4, "NumOfArgs Reached Pattern Threshold. Adding it to the EnforceRequestMethods list: " .. EnforceNumOfArgs)
                m.setvar("RESOURCE.enforce_num_of_args", EnforceNumOfArgs)
        end

	if (TrafficCounter == MinTrafficThreshold) then 
		i=1
		num_of_args={}
		for num in string.gmatch(EnforceNumOfArgs, "%d+") do 
			num_of_args[i]=num;i=i+1; 
		end
		local MinNumOfArgs = math.min(unpack(num_of_args))
		m.setvar("RESOURCE.MinNumOfArgs", MinNumOfArgs)
		local MaxNumOfArgs = math.max(unpack(num_of_args))
		m.setvar("RESOURCE.MaxNumOfArgs", MaxNumOfArgs)
		m.log(4, "Min # of ARGS: " ..MinNumOfArgs.. " and Max # of ARGS: " ..MaxNumOfArgs.. ".")
		m.setvar("!RESOURCE.NumOfArgs_counter_" .. NumOfArgs, "0")
	end
end