-
Notifications
You must be signed in to change notification settings - Fork 190
/
rules.go
1432 lines (1351 loc) · 79.7 KB
/
rules.go
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 2020 ZUP IT SERVICOS EM TECNOLOGIA E INOVACAO SA
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//nolint:lll // multiple regex is not possible broken lines
package csharp
import (
"regexp"
"github.com/ZupIT/horusec-devkit/pkg/enums/confidence"
"github.com/ZupIT/horusec-devkit/pkg/enums/severities"
engine "github.com/ZupIT/horusec-engine"
"github.com/ZupIT/horusec-engine/text"
)
func NewCommandInjection() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-1",
Name: "Command Injection",
Description: "If a malicious user controls either the FileName or Arguments, he might be able to execute unwanted commands or add unwanted argument. This behavior would not be possible if input parameter are validate against a white-list of characters. For more information access: (https://security-code-scan.github.io/#SCS0001).",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP1,
UnsafeExample: SampleVulnerableHSCSHARP1,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`new Process\(\)`),
regexp.MustCompile(`StartInfo.FileName`),
regexp.MustCompile(`StartInfo.Arguments`),
},
}
}
func NewXPathInjection() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-2",
Name: "XPath Injection",
Description: "If the user input is not properly filtered, a malicious user could extend the XPath query. For more information access: (https://security-code-scan.github.io/#SCS0003).",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP2,
UnsafeExample: SampleVulnerableHSCSHARP2,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`new XmlDocument {XmlResolver = null}`),
regexp.MustCompile(`Load\(.*\)`),
regexp.MustCompile(`SelectNodes\(.*\)`),
},
}
}
func NewExternalEntityInjection() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-3",
Name: "XML eXternal Entity Injection (XXE)",
Description: "The XML parser is configured incorrectly. The operation could be vulnerable to XML eXternal Entity (XXE) processing. For more information access: (https://security-code-scan.github.io/#SCS0007).",
Severity: severities.Medium.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP3,
UnsafeExample: SampleVulnerableHSCSHARP3,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(new XmlReaderSettings\(\))(.|\s)*(ProhibitDtd\s*=\s*false)(.|\s)*(XmlReader.Create\(.*\))`),
regexp.MustCompile(`(new XmlReaderSettings\(\))(.|\s)*(DtdProcessing\s*=\s*DtdProcessing.Parse)(.|\s)*(XmlReader.Create\(.*\))`),
},
}
}
func NewPathTraversal() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-4",
Name: "Path Traversal",
Description: "A path traversal attack (also known as directory traversal) aims to access files and directories that are stored outside the expected directory.By manipulating variables that reference files with “dot-dot-slash (../)” sequences and its variations or by using absolute file paths, it may be possible to access arbitrary files and directories stored on file system including application source code or configuration and critical system files. For more information access: (https://security-code-scan.github.io/#SCS0018).",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP4,
UnsafeExample: SampleVulnerableHSCSHARP4,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`ActionResult`),
regexp.MustCompile(`System.IO.File.ReadAllBytes\(Server.MapPath\(.*\) \+ .*\)`),
regexp.MustCompile(`(private|public|protected|internal|var)(([^G]|G[^e]|Ge[^t]|Get[^I]|GetI[^n]|GetIn[^v]|GetInv[^a]|GetInva[^l]|GetInval[^i]|GetInvali[^d]|GetInvalid[^F]|GetInvalidF[^i]|GetInvalidFi[^l]|GetInvalidFil[^e]|GetInvalidFile[^N]|GetInvalidFileN[^a]|GetInvalidFileNa[^m]|GetInvalidFileNam[^e]|GetInvalidFileName[^C]|GetInvalidFileNameC[^h]|GetInvalidFileNameCh[^a]|GetInvalidFileNameCha[^r]|GetInvalidFileNameChar[^s])*)(\s*File\(.*,\s*System\.Net\.Mime\.MediaTypeNames\.Application\.Octet\s*,.*\))`),
},
}
}
func NewSQLInjectionWebControls() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-5",
Name: "SQL Injection WebControls",
Description: "Malicious user might get direct read and/or write access to the database. If the database is poorly configured the attacker might even get Remote Code Execution (RCE) on the machine running the database. For more information access: (https://security-code-scan.github.io/#SCS0002).",
Severity: severities.High.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP5,
UnsafeExample: SampleVulnerableHSCSHARP5,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(?i)['|"]Select .* From .* where .*['|"]\s*\+\s*\w+[[:print:]]*\s*\+\s*['|"]`),
},
}
}
func NewWeakCipherOrCBCOrECBMode() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-6",
Name: "Weak Cipher Mode",
Description: "The cipher provides no way to detect that the data has been tampered with. If the cipher text can be controlled by an attacker, it could be altered without detection. The use of AES in CBC mode with a HMAC is recommended guaranteeing integrity and confidentiality. For more information access: (https://security-code-scan.github.io/#SCS0013).",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP6,
UnsafeExample: SampleVulnerableHSCSHARP6,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(using)(([^O]|O[^r]|Or[^g]|Org[^.]|Org\.[^B]|Org\.B[^o]|Org\.Bo[^u]|Org\.Bou[^n]|Org\.Boun[^c]|Org\.Bounc[^y]|Org\.Bouncy[^C]|Org\.BouncyC[^a]|Org\.BouncyCa[^s]|Org\.BouncyCas[^t]|Org\.BouncyCast[^l]|Org\.BouncyCastl[^e])*)(\);)`),
regexp.MustCompile(`CreateEncryptor\(.*\)`),
regexp.MustCompile(`new CryptoStream\(.*\)`),
regexp.MustCompile(`Write\(.*\)`),
regexp.MustCompile(`new BinaryWriter\(.*\)`),
},
}
}
func NewFormsAuthenticationCookielessMode() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-7",
Name: "Forms Authentication Cookieless Mode",
Description: "Authentication cookies should not be sent in the URL. Doing so allows attackers to gain unauthorized access to authentication tokens (web server logs, referrer headers, and browser history) and more easily perform session fixation / hijacking attacks. For more information checkout the CWE-598 (https://cwe.mitre.org/data/definitions/598.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP7,
UnsafeExample: SampleVulnerableHSCSHARP7,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(\<forms)((([^c]|c[^o]|co[^o]|coo[^k]|cook[^i]|cooki[^e]|cookie[^l]|cookiel[^e]|cookiele[^s]|cookieles[^s])*)|([^U]|U[^s]|Us[^e]|Use[^C]|UseC[^o]|UseCo[^o]|UseCoo[^k]|UseCook[^i]|UseCooki[^e]|UseCookie[^s])*)(\/\>)`),
regexp.MustCompile(`\<authentication\s*mode\s*=\s*["|']Forms`),
},
}
}
func NewFormsAuthenticationCrossAppRedirects() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-8",
Name: "Forms Authentication Cross App Redirects",
Description: "Enabling cross-application redirects can allow unvalidated redirect attacks via the returnUrl parameter during the login process. Disable cross-application redirects to by setting the enableCrossAppRedirects attribute to false. For more information checkout the CWE-601 (https://cwe.mitre.org/data/definitions/601.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP8,
UnsafeExample: SampleVulnerableHSCSHARP8,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`\<authentication\s*mode\s*=\s*["|']Forms`),
regexp.MustCompile(`\<forms`),
regexp.MustCompile(`enableCrossAppRedirects\s*=\s*["|']true`),
},
}
}
func NewFormsAuthenticationWeakCookieProtection() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-9",
Name: "Forms Authentication Weak Cookie Protection",
Description: "Forms Authentication cookies must use strong encryption and message authentication code (MAC) validation to protect the cookie value from inspection and tampering. Configure the forms element’s protection attribute to All to enable cookie data validation and encryption. For more information checkout the CWE-565 (https://cwe.mitre.org/data/definitions/565.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP9,
UnsafeExample: SampleVulnerableHSCSHARP9,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`\<authentication\s*mode\s*=\s*["|']Forms`),
regexp.MustCompile(`\<forms`),
regexp.MustCompile(`protection\s*=\s*["|'](None|Encryption|Validation)`),
},
}
}
func NewFormsAuthenticationWeakTimeout() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-10",
Name: "Forms Authentication Weak Timeout",
Description: "Excessive authentication timeout values provide attackers with a large window of opportunity to hijack user’s authentication tokens. For more information checkout the CWE-613 (https://cwe.mitre.org/data/definitions/613.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP10,
UnsafeExample: SampleVulnerableHSCSHARP10,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`\<authentication\s*mode\s*=\s*["|']Forms`),
regexp.MustCompile(`\<forms`),
regexp.MustCompile(`timeout\s*=\s*["|'](1[6-9]|[2-9][0-9]*)`),
},
}
}
func NewHeaderCheckingDisabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-11",
Name: "Header Checking Disabled",
Description: "Disabling the HTTP Runtime header checking protection opens the application up to HTTP Header Injection (aka Response Splitting) attacks. Enable the header checking protection by setting the httpRuntime element’s enableHeaderChecking attribute to true, which is the default value. For more information checkout the CWE-113 (https://cwe.mitre.org/data/definitions/113.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP11,
UnsafeExample: SampleVulnerableHSCSHARP11,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`enableHeaderChecking\s*=\s*["|']false`),
regexp.MustCompile(`\<httpRuntime`),
},
}
}
func NewVersionHeaderEnabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-12",
Name: "Version Header Enabled",
Description: "The Version HTTP response header sends the ASP.NET framework version to the client’s browser. This information can help an attacker identify vulnerabilities in the server’s framework version and should be disabled in production. Disable the version response header by setting the httpRuntime element’s enableVersionHeader attribute to false. For more information checkout the CWE-200 (https://cwe.mitre.org/data/definitions/200.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP12,
UnsafeExample: SampleVulnerableHSCSHARP12,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`enableVersionHeader\s*=\s*["|']true`),
regexp.MustCompile(`\<httpRuntime`),
},
}
}
func NewEventValidationDisabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-13",
Name: "Event Validation Disabled",
Description: "Event validation prevents unauthorized post backs in web form applications. Disabling this feature can allow attackers to forge requests from controls not visible or enabled on a given web form. Enable event validation by setting the page element’s eventValidation attribute to true. For more information checkout the CWE-807 (https://cwe.mitre.org/data/definitions/807.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP13,
UnsafeExample: SampleVulnerableHSCSHARP13,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`enableEventValidation\s*=\s*["|']false`),
regexp.MustCompile(`\<pages`),
},
}
}
func NewWeakSessionTimeout() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-14",
Name: "Weak Session Timeout",
Description: "If session data is used by the application for authentication, excessive timeout values provide attackers with a large window of opportunity to hijack user’s session tokens. Configure the session timeout value to meet your organization’s timeout policy. For more information checkout the CWE-613 (https://cwe.mitre.org/data/definitions/613.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP14,
UnsafeExample: SampleVulnerableHSCSHARP14,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`timeout\s*=\s*["|'](1[6-9]|[2-9][0-9]*)`),
regexp.MustCompile(`\<sessionState`),
},
}
}
func NewStateServerMode() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-15",
Name: "Weak Session Timeout",
Description: "The session StateServer mode transports session data insecurely to a remote server. The remote server also does not require system authentication to access the session data for an application. This risk depends entirely on the sensitivity of the data stored in the user’s session. If the session data is considered sensitive, consider adding an external control (e.g. IPSEC) that provides mutual authentication and transport security. For more information checkout the CWE-319 (https://cwe.mitre.org/data/definitions/319.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP15,
UnsafeExample: SampleVulnerableHSCSHARP15,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`mode\s*=\s*["|']StateServer`),
regexp.MustCompile(`\<sessionState`),
},
}
}
func NewJwtSignatureValidationDisabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-16",
Name: "Jwt Signature Validation Disabled",
Description: "Web service APIs relying on JSON Web Tokens (JWT) for authentication and authorization must sign each JWT with a private key or secret. Each web service endpoint must require JWT signature validation prior to decoding and using the token to access protected resources. The values RequireExpirationTime, RequireSignedTokens, ValidateLifetime can't was false. For more information checkout the CWE-347 (https://cwe.mitre.org/data/definitions/347.html) and CWE-613 (https://cwe.mitre.org/data/definitions/613.html) advisory.",
Severity: severities.Critical.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP16,
UnsafeExample: SampleVulnerableHSCSHARP16,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`AddAuthentication\(.*\)`),
regexp.MustCompile(`AddJwtBearer`),
regexp.MustCompile(`new TokenValidationParameters`),
regexp.MustCompile(`(RequireExpirationTime\s*=\s*false|RequireSignedTokens\s*=\s*false|ValidateLifetime\s*=\s*false)`),
},
}
}
func NewInsecureHttpCookieTransport() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-17",
Name: "Insecure Http Cookie Transport",
Description: "Cookies containing authentication tokens, session tokens, and other state management credentials must be protected in transit across a network. Set the cookie options’ Secure property to true to prevent the browser from transmitting cookies over HTTP. For more information checkout the CWE-614 (https://cwe.mitre.org/data/definitions/614.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP17,
UnsafeExample: SampleVulnerableHSCSHARP17,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`Secure\s*=\s*false`),
regexp.MustCompile(`new\sCookieOptions\(\)`),
},
}
}
func NewHttpCookieAccessibleViaScript() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-18",
Name: "Http Cookie Accessible Via Script",
Description: "Cookies containing authentication tokens, session tokens, and other state management credentials should be protected from malicious JavaScript running in the browser. Setting the httpOnly attribute to false can allow attackers to inject malicious scripts into the site and extract authentication cookie values to a remote server. Configure the cookie options’ httpOnly property to true, which prevents cookie access from scripts running in the browser. For more information checkout the CWE-1004 (https://cwe.mitre.org/data/definitions/1004.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP18,
UnsafeExample: SampleVulnerableHSCSHARP18,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`HttpOnly\s*=\s*false`),
regexp.MustCompile(`new\sCookieOptions\(\)`),
},
}
}
func NewDirectoryListingEnabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-19",
Name: "Directory Listing Enabled",
Description: "Directory listing provides a complete index of the resources located in a web directory. Enabling directory listing can expose sensitive resources such as application binaries, configuration files, and static content that should not be exposed. Unless directory listing is required to meet the application’s functional requirements, disable the listing by setting the directoryBrowse element’s enabled attribute to false. For more information checkout the CWE-548 (https://cwe.mitre.org/data/definitions/548.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP19,
UnsafeExample: SampleVulnerableHSCSHARP19,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`enabled\s*=\s*['|"]true`),
regexp.MustCompile(`\<directoryBrowse`),
},
}
}
func NewLdapAuthenticationDisabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-20",
Name: "Ldap Authentication Disabled",
Description: "Disabling LDAP Authentication configures insecure connections to the backend LDAP provider. Using the DirectoryEntry AuthenticationType property’s Anonymous or None option allows an anonymous or basic authentication connection to the LDAP provider. Set the the DirectoryEntry AuthenticationType property to Secure, which requests Kerberos authentication under the security context of the calling thread or as a provider username and password. For more information checkout the CWE-287 (https://cwe.mitre.org/data/definitions/287.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP20,
UnsafeExample: SampleVulnerableHSCSHARP20,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`AuthenticationTypes.Anonymous`),
regexp.MustCompile(`new\sDirectoryEntry\(.*\)`),
},
}
}
func NewCertificateValidationDisabledAndMatch() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-21",
Name: "Certificate Validation Disabled",
Description: "Disabling certificate validation is common in testing and development environments. Quite often, this is accidentally deployed to production, leaving the application vulnerable to man-in-the-middle attacks on insecure networks. For more information checkout the CWE-295 (https://cwe.mitre.org/data/definitions/295.html) advisory.",
Severity: severities.High.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP21,
UnsafeExample: SampleVulnerableHSCSHARP21,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`ServerCertificateValidationCallback\s*((\+=)|(ServerCertificateValidationCallback\s*\+))\s*\(.*\) => true;`),
regexp.MustCompile(`new WebRequestHandler\(\)`),
},
}
}
func NewActionRequestValidationDisabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-22",
Name: "Action Request Validation Disabled",
Description: "Request validation performs blacklist input validation for XSS payloads found in form and URL request parameters. Request validation has known bypass issues and does not prevent all XSS attacks, but it does provide a strong countermeasure for most payloads targeting a HTML context. For more information checkout the CWE-20 (https://cwe.mitre.org/data/definitions/20.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP22,
UnsafeExample: SampleVulnerableHSCSHARP22,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`\[ValidateInput\(false\)\]`),
regexp.MustCompile(`(\[HttpGet(\(.*\))?\]|\[HttpPost(\(.*\))?\]|\[HttpPut(\(.*\))?\]|\[HttpDelete(\(.*\))?\])`),
},
}
}
func NewXmlDocumentExternalEntityExpansion() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-23",
Name: "Xml Document External Entity Expansion",
Description: "XML External Entity (XXE) vulnerabilities occur when applications process untrusted XML data without disabling external entities and DTD processing. Processing untrusted XML data with a vulnerable parser can allow attackers to extract data from the server, perform denial of service attacks, and in some cases gain remote code execution. The XmlDocument class is vulnerable to XXE attacks when setting the XmlResolver property to resolve external entities. To prevent XmlDocument XXE attacks, set the XmlResolver property to null. For more information checkout the CWE-611 (https://cwe.mitre.org/data/definitions/611.html) advisory.",
Severity: severities.High.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP23,
UnsafeExample: SampleVulnerableHSCSHARP23,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(?i)(\.XmlResolver\s*=\s*)(([^n]|n[^u]|nu[^l]|nul[^l])*)(\.LoadXml\()`),
regexp.MustCompile(`new\sXmlDocument`),
},
}
}
func NewLdapInjectionFilterAssignment() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-24",
Name: "Ldap Injection Filter Assignment",
Description: "LDAP Injection vulnerabilities occur when untrusted data is concatenated into a LDAP Path or Filter expression without properly escaping control characters. This can allow attackers to change the meaning of an LDAP query and gain access to resources for which they are not authorized. Fixing the LDAP Injection Filter Assignment vulnerability requires untrusted data to be encoded using the Web Protection Library (aka AntiXSS) LDAP encoding method 'Encoder.LdapFilterEncode()'. For more information checkout the CWE-90 (https://cwe.mitre.org/data/definitions/90.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP24,
UnsafeExample: SampleVulnerableHSCSHARP24,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(\.Filter)(([^E]|E[^n]|En[^c]|Enc[^o]|Enco[^d]|Encod[^e]|Encode[^r]|Encoder[^.]|Encoder\.[^L]|Encoder\.L[^d]|Encoder\.Ld[^a]|Encoder\.Lda[^p]|Encoder\.Ldap[^F]|Encoder\.LdapF[^i]|Encoder\.LdapFi[^l]|Encoder\.LdapFil[^t]|Encoder\.LdapFilt[^e]|Encoder\.LdapFilte[^r]|Encoder\.LdapFilter[^E]|Encoder\.LdapFilterE[^n]|Encoder\.LdapFilterEn[^c]|Encoder\.LdapFilterEnc[^o]|Encoder\.LdapFilterEnco[^d]|Encoder\.LdapFilterEncod[^e])*)(\);)`),
regexp.MustCompile(`new DirectoryEntry\(.*\)`),
regexp.MustCompile(`new DirectorySearcher\(.*\)`),
},
}
}
func NewSqlInjectionDynamicNHibernateQuery() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-25",
Name: "Sql Injection: Dynamic NHibernate Query",
Description: "Concatenating untrusted data into a dynamic SQL string and calling vulnerable NHibernate Framework methods can allow SQL Injection. To ensure calls to vulnerable NHibernate Framework methods are parameterized, pass positional or named parameters in the statement. The following NHibernate methods allow for raw SQL queries to be executed: CreateQuery CreateSqlQuery To ensure calls to vulnerable NHibernate methods are parameterized, use named parameters in the raw SQL query. Then, set the named parameter values when executing the query. For more information checkout the CWE-89 (https://cwe.mitre.org/data/definitions/89.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP25,
UnsafeExample: SampleVulnerableHSCSHARP25,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(?i)["|'](SELECT|INSERT|UPDATE|DELETE).*\+`),
regexp.MustCompile(`(CreateQuery\(.*\);)(([^S]|S[^e]|Se[^t]|Set[^S]|SetS[^t]|SetSt[^r]|SetStr[^i]|SetStri[^n]|SetStrin[^g])*)(;)`),
},
}
}
func NewLdapInjectionDirectorySearcher() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-26",
Name: "Ldap Injection Directory Searcher",
Description: "LDAP Injection vulnerabilities occur when untrusted data is concatenated into a LDAP Path or Filter expression without properly escaping control characters. This can allow attackers to change the meaning of an LDAP query and gain access to resources for which they are not authorized. Fixing the LDAP Injection Directory Searcher vulnerability requires untrusted data to be encoded using the Web Protection Library (aka AntiXSS) LDAP encoding method 'Encoder.LdapFilterEncode'. For more information checkout the CWE-90 (https://cwe.mitre.org/data/definitions/90.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP26,
UnsafeExample: SampleVulnerableHSCSHARP26,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(new DirectorySearcher\s*\(\s*.*\s*,\s*.*\s*,\s*)(([^E]|E[^n]|En[^c]|Enc[^o]|Enco[^d]|Encod[^e]|Encode[^r]|Encoder[^.]|Encoder\.[^L]|Encoder\.L[^d]|Encoder\.Ld[^a]|Encoder\.Lda[^p]|Encoder\.Ldap[^F]|Encoder\.LdapF[^i]|Encoder\.LdapFi[^l]|Encoder\.LdapFil[^t]|Encoder\.LdapFilt[^e]|Encoder\.LdapFilte[^r]|Encoder\.LdapFilter[^E]|Encoder\.LdapFilterE[^n]|Encoder\.LdapFilterEn[^c]|Encoder\.LdapFilterEnc[^o]|Encoder\.LdapFilterEnco[^d]|Encoder\.LdapFilterEncod[^e])*)(\);)`),
regexp.MustCompile(`new DirectoryEntry\(.*\)`),
},
}
}
func NewLdapInjectionPathAssignment() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-27",
Name: "Ldap Injection Path Assignment",
Description: "LDAP Injection vulnerabilities occur when untrusted data is concatenated into a LDAP Path or Filter expression without properly escaping control characters. This can allow attackers to change the meaning of an LDAP query and gain access to resources for which they are not authorized. Fixing the LDAP Injection Path Assignment vulnerability requires untrusted data to be encoded using the appropriate Web Protection Library (aka AntiXSS) LDAP encoding method 'Encoder.LdapDistinguishedNameEncode'. For more information checkout the CWE-90 (https://cwe.mitre.org/data/definitions/90.html) advisory.",
Severity: severities.Medium.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP27,
UnsafeExample: SampleVulnerableHSCSHARP27,
},
Type: text.AndMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(\.Path)(([^E]|E[^n]|En[^c]|Enc[^o]|Enco[^d]|Encod[^e]|Encode[^r]|Encoder[^.]|Encoder\.[^L]|Encoder\.L[^d]|Encoder\.Ld[^a]|Encoder\.Lda[^p]|Encoder\.Ldap[^D]|Encoder\.LdapD[^i]|Encoder\.LdapDi[^s]|Encoder\.LdapDis[^t]|Encoder\.LdapDist[^i]|Encoder\.LdapDisti[^n]|Encoder\.LdapDistin[^g]|Encoder\.LdapDisting[^u]|Encoder\.LdapDistingu[^i]|Encoder\.LdapDistingui[^s]|Encoder\.LdapDistinguis[^h]|Encoder\.LdapDistinguish[^e]|Encoder\.LdapDistinguishe[^d]|Encoder\.LdapDistinguished[^N]|Encoder\.LdapDistinguishedN[^a]|Encoder\.LdapDistinguishedNa[^m]|Encoder\.LdapDistinguishedNam[^e]|Encoder\.LdapDistinguishedName[^E]|Encoder\.LdapDistinguishedNameE[^n]|Encoder\.LdapDistinguishedNameEn[^c]|Encoder\.LdapDistinguishedNameEnc[^o]|Encoder\.LdapDistinguishedNameEnco[^d]|Encoder\.LdapDistinguishedNameEncod[^e])*)(\);)`),
regexp.MustCompile(`new DirectoryEntry\(\)`),
},
}
}
func NewLDAPInjection() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-28",
Name: "LDAP Injection",
Description: "The dynamic value passed to the LDAP query should be validated. Risk: If the user input is not properly filtered, a malicious user could extend the LDAP query. Solution: Use proper encoder (LdapFilterEncode or LdapDistinguishedNameEncode) from AntiXSS library:. For more information access: (https://security-code-scan.github.io/#SCS0031) or (https://security-code-scan.github.io/#SCS0026) or CWE-90 (https://cwe.mitre.org/data/definitions/90.html) advisory.",
Severity: severities.High.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP28,
UnsafeExample: SampleVulnerableHSCSHARP28,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(.Filter\s*=\s*)(([^E]|E[^n]|En[^c]|Enc[^o]|Enco[^d]|Encod[^e]|Encode[^r]|Encoder[^.]|Encoder\.[^L]|Encoder\.L[^d]|Encoder\.Ld[^a]|Encoder\.Lda[^p]|Encoder\.Ldap[^F]|Encoder\.LdapF[^i]|Encoder\.LdapFi[^l]|Encoder\.LdapFil[^t]|Encoder\.LdapFilt[^e]|Encoder\.LdapFilte[^r]|Encoder\.LdapFilter[^E]|Encoder\.LdapFilterE[^n]|Encoder\.LdapFilterEn[^c]|Encoder\.LdapFilterEnc[^o]|Encoder\.LdapFilterEnco[^d]|Encoder\.LdapFilterEncod[^e])*)(\)['|"])`),
regexp.MustCompile(`(.Path\s*=\s*)(([^E]|E[^n]|En[^c]|Enc[^o]|Enco[^d]|Encod[^e]|Encode[^r]|Encoder[^.]|Encoder\.[^L]|Encoder\.L[^d]|Encoder\.Ld[^a]|Encoder\.Lda[^p]|Encoder\.Ldap[^D]|Encoder\.LdapD[^i]|Encoder\.LdapDi[^s]|Encoder\.LdapDis[^t]|Encoder\.LdapDist[^i]|Encoder\.LdapDisti[^n]|Encoder\.LdapDistin[^g]|Encoder\.LdapDisting[^u]|Encoder\.LdapDistingu[^i]|Encoder\.LdapDistingui[^s]|Encoder\.LdapDistinguis[^h]|Encoder\.LdapDistinguish[^e]|Encoder\.LdapDistinguishe[^d]|Encoder\.LdapDistinguished[^N]|Encoder\.LdapDistinguishedN[^a]|Encoder\.LdapDistinguishedNa[^m]|Encoder\.LdapDistinguishedNam[^e]|Encoder\.LdapDistinguishedName[^E]|Encoder\.LdapDistinguishedNameE[^n]|Encoder\.LdapDistinguishedNameEn[^c]|Encoder\.LdapDistinguishedNameEnc[^o]|Encoder\.LdapDistinguishedNameEnco[^d]|Encoder\.LdapDistinguishedNameEncod[^e])*)(,.*['|"])`),
},
}
}
func NewSQLInjectionLinq() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-29",
Name: "SQL Injection LINQ",
Description: "Malicious user might get direct read and/or write access to the database. If the database is poorly configured the attacker might even get Remote Code Execution (RCE) on the machine running the database.. For more information access: (https://security-code-scan.github.io/#SCS0002).",
Severity: severities.High.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP29,
UnsafeExample: SampleVulnerableHSCSHARP29,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(?i)(var|ExecuteQuery).*(=|\().*(SELECT|UPDATE|DELETE|INSERT).*\++`),
},
}
}
func NewInsecureDeserialization() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-30",
Name: "Insecure Deserialization",
Description: `Arbitrary code execution, full application compromise or denial of service. An attacker may pass specially crafted serialized .NET object of specific class that will execute malicious code during the construction of the object.
Solution:
There is no simple fix. Do not deserialize untrusted data: user input, cookies or data that crosses trust boundaries.
In case it is unavoidable:
1) If serialization is done on the server side, then crosses trust boundary, but is not modified and is returned back (like cookie for example) - use signed cryptography (HMAC for instance) to ensure it wasn’t tampered.
2) Do not get the type to deserialize into from untrusted source: the serialized stream itself or other untrusted parameter. BinaryFormatter for example reads type information from serialized stream itself and can’t be used with untrusted streams:
// DO NOT DO THIS!
var thing = (MyType)new BinaryFormatter().Deserialize(untrustedStream);
JavaScriptSerializer for instance without a JavaScriptTypeResolver is safe because it doesn’t resolve types at all:
3) If the library supports implement a callback that verifies if the object and its properties are of expected type (don’t blacklist, use whitelist!)
4) Serialize simple Data Transfer Objects (DTO) only. Do not serialize/deserialize type information. For example, use only TypeNameHandling.None (the default) in Json.net
For more information access: (https://security-code-scan.github.io/#SCS0028).`,
Severity: severities.Low.ToString(),
Confidence: confidence.Low.ToString(),
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`new\sBinaryFormatter\(\)\.Deserialize\(.*\)`),
regexp.MustCompile(`new\sJavaScriptSerializer\(..*\)`),
},
}
}
func NewSQLInjectionEnterpriseLibraryData() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-31",
Name: "SQL Injection Enterprise Library Data",
Description: "Arbitrary code execution, full application compromise or denial of service. An attacker may pass specially crafted serialized .NET object of specific class that will execute malicious code during the construction of the object. For more information access: (https://security-code-scan.github.io/#SCS0036).",
Severity: severities.High.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP31,
UnsafeExample: SampleVulnerableHSCSHARP31,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(GetSqlStringCommand\(.*\))(([^A]|A[^d]|Ad[^d]|Add[^I]|AddI[^n]|AddIn[^P]|AddInP[^a]|AddInPa[^r]|AddInPar[^a]|AddInPara[^m]|AddInParam[^e]|AddInParame[^t]|AddInParamet[^e]|AddInParamete[^r])*)(ExecuteDataSet\(.*\))`),
regexp.MustCompile(`ExecuteDataSet\(CommandType.*, "(SELECT|select).*(FROM|from).*(WHERE|where).*"\)`),
},
}
}
func NewCQLInjectionCassandra() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-32",
Name: "CQL Injection Cassandra",
Description: "Arbitrary code execution, full application compromise or denial of service. An attacker may pass specially crafted serialized .NET object of specific class that will execute malicious code during the construction of the object. For more information access: (https://security-code-scan.github.io/#SCS0038).",
Severity: severities.High.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP32,
UnsafeExample: SampleVulnerableHSCSHARP32,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(?i)(PreparedStatement.*Prepare\(['|"]select.*from.*where.*\))(([^B]|B[^i]|Bi[^n]|Bin[^d])*)(Execute\(.*\))`),
regexp.MustCompile(`(?i)Execute\(['|"]select.*from.*where.*['|"]\s*\+.*\)`),
},
}
}
func NewPasswordComplexityDefault() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-33",
Name: "Password Complexity Default",
Description: "PasswordValidator should have at least two requirements for better security, the RequiredLength property must be set with a minimum value of 8. For more information access: (https://security-code-scan.github.io/#SCS0027).",
Severity: severities.Low.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP33,
UnsafeExample: SampleVulnerableHSCSHARP33,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`new\sPasswordValidator\(\)`),
regexp.MustCompile(`new\sPasswordValidator(\n?\s*{)(\n*.*=.*,?)(\s|\n)*[^a-z]}`),
},
}
}
func NewCookieWithoutSSLFlag() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-34",
Name: "Cookie Without SSL Flag",
Description: "It is recommended to specify the Secure flag to new cookie. The Secure flag is a directive to the browser to make sure that the cookie is not sent by unencrypted channel. For more information access: (https://security-code-scan.github.io/#SCS0008) and (https://cwe.mitre.org/data/definitions/614.html).",
Severity: severities.Low.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP34,
UnsafeExample: SampleVulnerableHSCSHARP34,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`requireSSL\s*=\s*['|"]false['|"]`),
regexp.MustCompile(`(\<forms)(([^r]|r[^e]|re[^q]|req[^u]|requ[^i]|requi[^r]|requir[^e]|require[^S]|requireS[^S]|requireSS[^L])*)(\/\>)`),
regexp.MustCompile(`(new\sHttpCookie\(.*\))(.*|\n)*(\.Secure\s*=\s*false)`),
regexp.MustCompile(`(new\sHttpCookie)(([^S]|S[^e]|Se[^c]|Sec[^u]|Secu[^r]|Secur[^e])*)(})`),
},
}
}
func NewCookieWithoutHttpOnlyFlag() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-35",
Name: "Cookie Without HttpOnly Flag",
Description: "It is recommended to specify the HttpOnly flag to new cookie. For more information access: (https://security-code-scan.github.io/#SCS0009) or (https://cwe.mitre.org/data/definitions/1004.html).",
Severity: severities.Low.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP35,
UnsafeExample: SampleVulnerableHSCSHARP35,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`httpOnlyCookies\s*=\s*['|"]false['|"]`),
regexp.MustCompile(`(new\sHttpCookie\(.*\))(.*|\n)*(\.HttpOnly\s*=\s*false)`),
regexp.MustCompile(`(new\sHttpCookie)(([^H]|H[^t]|Ht[^t]|Htt[^p]|Http[^O]|HttpO[^n]|HttpOn[^l]|HttpOnl[^y])*)(})`),
},
}
}
func NewNoInputVariable() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-36",
Name: "No input variable",
Description: "The application appears to allow XSS through an unencrypted / unauthorized input variable. https://owasp.org/www-community/attacks/xss/. For more information checkout the CWE-79 (https://cwe.mitre.org/data/definitions/79.html) advisory.",
Severity: severities.High.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP36,
UnsafeExample: SampleVulnerableHSCSHARP36,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`\s*var\s+\w+\s*=\s*"\s*\<\%\s*=\s*\w+\%\>";`),
regexp.MustCompile(`\.innerHTML\s*=\s*.+`),
},
}
}
func NewIdentityWeakPasswordComplexity() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-37",
Name: "Identity Weak Password Complexity",
Description: "Weak passwords can allow attackers to easily guess user passwords using wordlist or brute force attacks. Enforcing a strict password complexity policy mitigates these attacks by significantly increasing the time to guess a user’s valid password. For more information checkout the CWE-521 (https://cwe.mitre.org/data/definitions/521.html) advisory.",
Severity: severities.Critical.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP37,
UnsafeExample: SampleVulnerableHSCSHARP37,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`new PasswordValidator\(\)`),
regexp.MustCompile(`RequiredLength = \b([0-7])\b`),
regexp.MustCompile(`(new PasswordValidator)(([^R]|R[^e]|Re[^q]|Req[^u]|Requ[^i]|Requi[^r]|Requir[^e]|Require[^d]|Required[^L]|RequiredL[^e]|RequiredLe[^n]|RequiredLen[^g]|RequiredLeng[^t]|RequiredLengt[^h])*)(};)`),
regexp.MustCompile(`(new PasswordValidator)(([^R]|R[^e]|Re[^q]|Req[^u]|Requ[^i]|Requi[^r]|Requir[^e]|Require[^D]|RequireD[^i]|RequireDi[^g]|RequireDig[^i]|RequireDigi[^t]|RequireDigit[^ ]|RequireDigit [^=]|RequireDigit =[^ ]|RequireDigit = [^t]|RequireDigit = t[^r]|RequireDigit = tr[^u]|RequireDigit = tru[^e])*)(};)`),
regexp.MustCompile(`(new PasswordValidator)(([^R]|R[^e]|Re[^q]|Req[^u]|Requ[^i]|Requi[^r]|Requir[^e]|Require[^L]|RequireL[^o]|RequireLo[^w]|RequireLow[^e]|RequireLowe[^r]|RequireLower[^c]|RequireLowerc[^a]|RequireLowerca[^s]|RequireLowercas[^e]|RequireLowercase[^ ]|RequireLowercase [^=]|RequireLowercase =[^ ]|RequireLowercase = [^t]|RequireLowercase = t[^r]|RequireLowercase = tr[^u]|RequireLowercase = tru[^e])*)(};)`),
regexp.MustCompile(`(new PasswordValidator)(([^R]|R[^e]|Re[^q]|Req[^u]|Requ[^i]|Requi[^r]|Requir[^e]|Require[^N]|RequireN[^o]|RequireNo[^n]|RequireNon[^L]|RequireNonL[^e]|RequireNonLe[^t]|RequireNonLet[^t]|RequireNonLett[^e]|RequireNonLette[^r]|RequireNonLetter[^O]|RequireNonLetterO[^r]|RequireNonLetterOr[^D]|RequireNonLetterOrD[^i]|RequireNonLetterOrDi[^g]|RequireNonLetterOrDig[^i]|RequireNonLetterOrDigi[^t]|RequireNonLetterOrDigit[^ ]|RequireNonLetterOrDigit [^=]|RequireNonLetterOrDigit =[^ ]|RequireNonLetterOrDigit = [^t]|RequireNonLetterOrDigit = t[^r]|RequireNonLetterOrDigit = tr[^u]|RequireNonLetterOrDigit = tru[^e])*)(};)`),
regexp.MustCompile(`(new PasswordValidator)(([^R]|R[^e]|Re[^q]|Req[^u]|Requ[^i]|Requi[^r]|Requir[^e]|Require[^U]|RequireU[^p]|RequireUp[^p]|RequireUpp[^e]|RequireUppe[^r]|RequireUpper[^c]|RequireUpper[^c]|RequireUpperc[^a]|RequireUpperca[^s]|RequireUppercas[^e]|RequireUppercase[^ ]|RequireUppercase [^=]|RequireUppercase =[^ ]|RequireUppercase = [^t]|RequireUppercase = t[^r]|RequireUppercase = tr[^u]|RequireUppercase = tru[^e])*)(};)`),
},
}
}
func NewNoLogSensitiveInformationInConsole() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-38",
Name: "No Log Sensitive Information in console",
Description: "The App logs information. Sensitive information should never be logged. For more information checkout the CWE-532 (https://cwe.mitre.org/data/definitions/532.html) advisory.",
Severity: severities.Info.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP38,
UnsafeExample: SampleVulnerableHSCSHARP38,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(?i)(((Log|log).*\.(Verbose|Debug|Info|Warn|Erro|ForContext|FromLogContext|Seq))|(Console.Write))`),
},
}
}
func NewOutputCacheConflict() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-39",
Name: "OutputCache Conflict",
Description: "Having the annotation [OutputCache] will disable the annotation [Authorize] for the requests following the first one. For more information access: (https://security-code-scan.github.io/#SCS0019).",
Severity: severities.Medium.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP39,
UnsafeExample: SampleVulnerableHSCSHARP39,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(\[Authorize\])(.*|\n)*(\[OutputCache\])`),
},
}
}
func NewOpenRedirect() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-40",
Name: "Open Redirect",
Description: "Your site may be used in phishing attacks. An attacker may craft a trustworthy looking link to your site redirecting a victim to a similar looking malicious site: 'http://yourdomain.com?redirect=https://urdomain.com/login'. For more information access: (https://security-code-scan.github.io/#SCS0027).",
Severity: severities.Low.ToString(),
Confidence: confidence.Low.ToString(),
SafeExample: SampleSafeHSCSHARP40,
UnsafeExample: SampleVulnerableHSCSHARP40,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`String.IsNullOrEmpty.*\n?.*{?\n?.*return\sRedirect\(.*\);`),
},
}
}
func NewRequestValidationDisabledAttribute() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-41",
Name: "Request Validation Disabled (Attribute)",
Description: "Request validation is disabled. Request validation allows the filtering of some XSS patterns submitted to the application. For more information access: (https://security-code-scan.github.io/#SCS0017).",
Severity: severities.High.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP41,
UnsafeExample: SampleVulnerableHSCSHARP41,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`\[ValidateInput\(false\)\]`),
},
}
}
func NewSQLInjectionOLEDB() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-42",
Name: "SQL Injection OLE DB",
Description: "Malicious user might get direct read and/or write access to the database. If the database is poorly configured the attacker might even get Remote Code Execution (RCE) on the machine running the database. For more information access: (https://security-code-scan.github.io/#SCS0020).",
Severity: severities.High.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP42,
UnsafeExample: SampleVulnerableHSCSHARP42,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(new OleDbConnection\(.*\))(([^P]|P[^a]|Pa[^r]|Par[^a]|Para[^m]|Param[^e]|Parame[^t]|Paramet[^e]|Paramete[^r]|Parameter[^s]|Parameters[^.]|Parameters\.[^A]|Parameters\.A[^d]|Parameters\.Ad[^d])*)(\.ExecuteReader\(.*\))`),
},
}
}
func NewRequestValidationDisabledConfigurationFile() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-43",
Name: "Request Validation Disabled (Configuration File)",
Description: "The validateRequest which provides additional protection against XSS is disabled in configuration file. For more information access: (https://security-code-scan.github.io/#SCS0017) or (https://cwe.mitre.org/data/definitions/20.html).",
Severity: severities.High.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP43,
UnsafeExample: SampleVulnerableHSCSHARP43,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`validateRequest\s*=\s*['|"]false['|"]`),
},
}
}
func NewSQLInjectionMsSQLDataProvider() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-44",
Name: "SQL Injection MsSQL Data Provider",
Description: "Malicious user might get direct read and/or write access to the database. If the database is poorly configured the attacker might even get Remote Code Execution (RCE) on the machine running the database. For more information access: (https://security-code-scan.github.io/#SCS0026).",
Severity: severities.High.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP44,
UnsafeExample: SampleVulnerableHSCSHARP44,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(new SqlCommand\(.*\))(([^P]|P[^a]|Pa[^r]|Par[^a]|Para[^m]|Param[^e]|Parame[^t]|Paramet[^e]|Paramete[^r]|Parameter[^s]|Parameters[^.]|Parameters\.[^A]|Parameters\.A[^d]|Parameters\.Ad[^d]|Parameters\.Add[^W]|Parameters\.AddW[^i]|Parameters\.AddWi[^t]|Parameters\.AddWit[^h]|Parameters\.AddWith[^V]|Parameters\.AddWithV[^a]|Parameters\.AddWithVa[^l]|Parameters\.AddWithVal[^u]|F[^e])*)(Open\(\)|ExecuteReader\(\))`),
},
}
}
func NewRequestValidationIsEnabledOnlyForPages() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-45",
Name: "Request validation is enabled only for pages",
Description: "The requestValidationMode which provides additional protection against XSS is enabled only for pages, not for all HTTP requests in configuration file. For more information access: (https://security-code-scan.github.io/#SCS0030).",
Severity: severities.High.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP45,
UnsafeExample: SampleVulnerableHSCSHARP45,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`requestValidationMode\s*=\s*['|"][0-3][^\d].*['|"]`),
},
}
}
func NewSQLInjectionEntityFramework() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-46",
Name: "SQL Injection Entity Framework",
Description: "Malicious user might get direct read and/or write access to the database. If the database is poorly configured the attacker might even get Remote Code Execution (RCE) on the machine running the database, please use SqlParameter to create query with parameters. For more information access: (https://security-code-scan.github.io/#SCS0035) or (https://cwe.mitre.org/data/definitions/89.html) .",
Severity: severities.High.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP46,
UnsafeExample: SampleVulnerableHSCSHARP46,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(Database\.ExecuteSqlCommand)(([^S]|S[^q]|Sq[^l]|Sql[^P]|SqlP[^a]|SqlPa[^r]|SqlPar[^a]|SqlPara[^m]|SqlParam[^e]|SqlParame[^t]|SqlParamet[^e]|SqlParamete[^r])*)(\);)`),
},
}
}
func NewViewStateNotEncrypted() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-47",
Name: "View State Not Encrypted",
Description: "The viewStateEncryptionMode is not set to Always in configuration file. Web Forms controls use hidden base64 encoded fields to store state information. If sensitive information is stored there it may be leaked to the client side. For more information access: (https://security-code-scan.github.io/#SCS0023) or (https://cwe.mitre.org/data/definitions/200.html).",
Severity: severities.High.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP47,
UnsafeExample: SampleVulnerableHSCSHARP47,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`viewStateEncryptionMode\s*=\s*['|"](Auto|Never)['|"]`),
},
}
}
func NewSQLInjectionNhibernate() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-48",
Name: "SQL Injection Nhibernate",
Description: "Malicious user might get direct read and/or write access to the database. If the database is poorly configured the attacker might even get Remote Code Execution (RCE) on the machine running the database. For more information access: (https://security-code-scan.github.io/#SCS0037).",
Severity: severities.High.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP48,
UnsafeExample: SampleVulnerableHSCSHARP48,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(?i)(\.CreateQuery|\.CreateSqlQuery)(([^S]|S[^e]|Se[^t]|Set[^P]|SetP[^a]|SetPa[^r]|SetPar[^a]|SetPara[^m]|SetParam[^e]|SetParame[^t]|SetParamet[^e]|SetParamete[^r])*)((FirstOrDefault|open|execute|all|list).*\);)`),
},
}
}
func NewViewStateMacDisabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-49",
Name: "View State MAC Disabled",
Description: "The enableViewStateMac is disabled in configuration file. (This feature cannot be disabled starting .NET 4.5.1). The view state could be altered by an attacker. For more information access: (https://security-code-scan.github.io/#SCS0024) or (https://cwe.mitre.org/data/definitions/807.html).",
Severity: severities.High.ToString(),
Confidence: confidence.High.ToString(),
SafeExample: SampleSafeHSCSHARP49,
UnsafeExample: SampleVulnerableHSCSHARP49,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`enableViewStateMac\s*=\s*['|"]false['|"]`),
},
}
}
func NewSQLInjectionNpgsql() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-50",
Name: "SQL Injection Npgsql",
Description: "Malicious user might get direct read and/or write access to the database. If the database is poorly configured the attacker might even get Remote Code Execution (RCE) on the machine running the database. For more information access: (https://security-code-scan.github.io/#SCS0039).",
Severity: severities.High.ToString(),
Confidence: confidence.Medium.ToString(),
SafeExample: SampleSafeHSCSHARP50,
UnsafeExample: SampleVulnerableHSCSHARP50,
},
Type: text.OrMatch,
Expressions: []*regexp.Regexp{
regexp.MustCompile(`(NpgsqlCommand\()(([^P]|P[^a]|Pa[^r]|Par[^a]|Para[^m]|Param[^e]|Parame[^t]|Paramet[^e]|Paramete[^r]|Parameter[^s]|Parameters[^.]|Parameters\.[^A]|Parameters\.A[^d]|Parameters\.Ad[^d]|Parameters\.Add[^W]|Parameters\.AddW[^i]|Parameters\.AddWi[^t]|Parameters\.AddWit[^h]|Parameters\.AddWith[^V]|Parameters\.AddWithV[^a]|Parameters\.AddWithVa[^l]|Parameters\.AddWithVal[^u]|Parameters\.AddWithValu[^e])*)(ExecuteNonQuery\(.*\)|ExecuteReader\(.*\))`),
},
}
}
func NewCertificateValidationDisabled() *text.Rule {
return &text.Rule{
Metadata: engine.Metadata{
ID: "HS-CSHARP-51",
Name: "Certificate Validation Disabled",
Description: "Disabling certificate validation is often used to connect easily to a host that is not signed by a root certificate authority. As a consequence, this is vulnerable to Man-in-the-middle attacks since the client will trust any certificate. For more information access: (https://security-code-scan.github.io/#SCS0004).",