-
Notifications
You must be signed in to change notification settings - Fork 17
/
parser_test.go
792 lines (732 loc) · 17.4 KB
/
parser_test.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
package sigma
import (
"testing"
"github.com/gobwas/glob"
"gopkg.in/yaml.v2"
)
var detection1 = `
detection:
condition: "selection1 and not selection3"
selection1:
Image:
- '*\schtasks.exe'
- '*\nslookup.exe'
- '*\certutil.exe'
- '*\bitsadmin.exe'
- '*\mshta.exe'
ParentImage:
- '*\mshta.exe'
- '*\powershell.exe'
- '*\cmd.exe'
- '*\rundll32.exe'
- '*\cscript.exe'
- '*\wscript.exe'
- '*\wmiprvse.exe'
selection3:
CommandLine: "+R +H +S +A *.cui"
`
var detection1_positive = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +A asd.cui",
"ParentImage": "C:\\test\\wmiprvse.exe",
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "aaa",
"ParentImage": "C:\\test\\wmiprvse.exe"
}
`
var detection1_negative1 = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +S +A lll.cui",
"ParentImage": "C:\\test\\mshta.exe"
}
`
var detection1_negative2 = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +S +A lll.cui"
}
`
var detection2 = `
detection:
condition: "(selection1 and selection2) and not selection3"
selection1:
Image:
- '*\schtasks.exe'
- '*\nslookup.exe'
- '*\certutil.exe'
- '*\bitsadmin.exe'
- '*\mshta.exe'
selection2:
ParentImage:
- '*\mshta.exe'
- '*\powershell.exe'
- '*\cmd.exe'
- '*\rundll32.exe'
- '*\cscript.exe'
- '*\wscript.exe'
- '*\wmiprvse.exe'
selection3:
CommandLine: "+R +H +S +A *.cui"
`
var detection3 = `
detection:
condition: "(selection1 or selection2) and not selection3"
selection1:
Image:
- '*\schtasks.exe'
- '*\nslookup.exe'
- '*\certutil.exe'
- '*\bitsadmin.exe'
- '*\mshta.exe'
selection2:
ParentImage:
- '*\mshta.exe'
- '*\powershell.exe'
- '*\cmd.exe'
- '*\rundll32.exe'
- '*\cscript.exe'
- '*\wscript.exe'
- '*\wmiprvse.exe'
selection3:
CommandLine: "+R +H +S +A *.cui"
`
var detection3_positive1 = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +A asd.cui",
"ParentImage": "C:\\test\\custom.exe",
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "aaa",
"ParentImage": "C:\\test\\wmiprvse.exe"
}
`
var detection3_positive2 = `
{
"Image": "C:\\test\\custom.exe",
"CommandLine": "+R +H +A asd.cui",
"ParentImage": "C:\\test\\wmiprvse.exe",
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "aaa",
"ParentImage": "C:\\test\\wmiprvse.exe"
}
`
var detection3_negative = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +S +A lll.cui",
"ParentImage": "C:\\test\\mshta.exe"
}
`
var detection4 = `
detection:
condition: "all of selection* and not filter"
selection1:
Image:
- '*\schtasks.exe'
- '*\nslookup.exe'
- '*\certutil.exe'
- '*\bitsadmin.exe'
- '*\mshta.exe'
selection2:
ParentImage:
- '*\mshta.exe'
- '*\powershell.exe'
- '*\cmd.exe'
- '*\rundll32.exe'
- '*\cscript.exe'
- '*\wscript.exe'
- '*\wmiprvse.exe'
filter:
CommandLine: "+R +H +S +A *.cui"
`
var detection5 = `
detection:
condition: "1 of selection* and not filter"
selection1:
Image:
- '*\schtasks.exe'
- '*\nslookup.exe'
- '*\certutil.exe'
- '*\bitsadmin.exe'
- '*\mshta.exe'
selection2:
ParentImage:
- '*\mshta.exe'
- '*\powershell.exe'
- '*\cmd.exe'
- '*\rundll32.exe'
- '*\cscript.exe'
- '*\wscript.exe'
- '*\wmiprvse.exe'
filter:
CommandLine: "+R +H +S +A *.cui"
`
var detection6 = `
detection:
condition: "all of them"
selection_images:
Image:
- '*\schtasks.exe'
- '*\nslookup.exe'
- '*\certutil.exe'
- '*\bitsadmin.exe'
- '*\mshta.exe'
selection_parent_images:
ParentImage:
- '*\mshta.exe'
- '*\powershell.exe'
- '*\cmd.exe'
- '*\rundll32.exe'
- '*\cscript.exe'
- '*\wscript.exe'
- '*\wmiprvse.exe'
`
var detection6_positive = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +A asd.cui",
"ParentImage": "C:\\test\\wmiprvse.exe",
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "aaa",
"ParentImage": "C:\\test\\wmiprvse.exe"
}
`
var detection6_negative = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +S +A lll.cui",
"ParentImage": "C:\\test\\mshta\\lll.exe"
}
`
var detection7 = `
detection:
condition: "1 of them"
selection_images:
Image:
- '*\schtasks.exe'
- '*\nslookup.exe'
- '*\certutil.exe'
- '*\bitsadmin.exe'
- '*\mshta.exe'
selection_parent_images:
ParentImage:
- '*\mshta.exe'
- '*\powershell.exe'
- '*\cmd.exe'
- '*\rundll32.exe'
- '*\cscript.exe'
- '*\wscript.exe'
- '*\wmiprvse.exe'
`
var detection7_negative1 = `
{
"Image": "C:\\test\\bytesadmin.exe",
"CommandLine": "+R +H +S +A lll.cui",
"ParentImage": "E:\\go\\bin\\gofmt"
}
`
var detection7_negative2 = `
{
"Image": "C:\\test\\bytesadmin.exe",
"CommandLine": "+R +H +S +A lll.cui"
}
`
var detection8 = `
detection:
condition: "selection1 and not selection3"
selection1:
Image:
- '*\schtasks.exe'
- '*\nslookup.exe'
- '*\certutil.exe'
- '*\bitsadmin.exe'
- '*\mshta.exe'
ParentImage:
- '*\mshta.exe'
- '*\powershell.exe'
- '*\cmd.exe'
- '*\rundll32.exe'
- '*\cscript.exe'
- '*\wscript.exe'
- '*\wmiprvse.exe'
selection3:
CommandLine: "+R +H +S +A *.cui"
`
var detection8_positive = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +A asd.cui",
"ParentImage": "C:\\test\\wmiprvse.exe",
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "aaa",
"ParentImage": "C:\\test\\wmiprvse.exe"
}
`
var detection8_negative1 = `
{
"Image": "C:\\test\\bitsadmin.exe",
"CommandLine": "+R +H +S +A lll.cui",
"ParentImage": "C:\\test\\mshta.exe"
}
`
var detection8_negative2 = `
{
"Image": "C:\\test\\bitsadmin.exe",
"ParentImage": "C:\\test\\mshta.exe"
}
`
var detection9 = `
detection:
condition: "selection"
selection:
- PipeName|re: '\\\\SomePipeName[0-9a-f]{2}'
- PipeName2|re: '\\\\AnotherPipe[0-9a-f]*Name'
`
var detection9_positive = `
{
"PipeName": "\\\\SomePipeNamea4",
"PipeName2": "\\\\AnotherPipe0af3Name"
}
`
var detection9_negative = `
{
"PipeName": "\\\\SomePipeNameZZ",
"PipeName2": "\\\\AnotherPipe01zzName"
}
`
var detection10 = `
detection:
condition: "selection1 and selection2"
selection1:
- SomeName|startswith: 'TestStart'
selection2:
- SomeName|endswith: 'TestEnd'
`
var detection10_positive = `
{
"SomeName": "TestStart-Value-TestEnd"
}
`
var detection10_negative = `
{
"SomeName": "TestStart-Value"
}
`
var detection11 = `
detection:
condition: "selection1 and selection2"
selection1:
SomeName|contains|all:
- 'mark1'
- 'mark2'
selection2:
SomeName|contains:
- 'version1'
- 'version2'
`
var detection11_positive = `
{
"SomeName": "Some mark1 mark2 String version2"
}
`
var detection11_negative = `
{
"SomeName": "mark1 mark2 mark3 non-matching string"
}
`
var detection12 = `
detection:
condition: "selection1 and selection2"
selection1:
SomeKey|contains|all:
- 'val1'
- 'val2'
selection2:
SomeKey2:
- 'mustMatch1'
- 'mustMatch2'
`
var detection12_positive = `
{
"SomeKey": "val1 val2",
"SomeKey2": "mustMatch1"
}
`
var detection12_negative = `
{
"SomeKey": "val1 val2",
"SomeKey2": "mustMatch3"
}
`
// this test is a bit tricky:
// the '*\bits\*admin.exe' is looking to match '[wildCard]\bits*admin.exe' (one wildcard at head, one escaped wildcard)
// the '\\\\DoubleBackslash\\some*.exe' is looking to match '\\DoubleBackslash\some[wildCard].exe' (multiple backslashes, one wildcard)
// the '\leadingBackslash\\*.exe' is looking to match '\leadingBackslash\[wildCard].exe' (one wildcard and leading backslash)
// the 'full\\\*plaintext.exe' is looking to match 'full\*plaintext.exe' (no wildcards exact match)
var detection13 = `
detection:
condition: "all of them"
selection_images:
Image:
- '*\bits\*admin.exe'
- '\\\\DoubleBackslash\\some*.exe'
- '[Windows-*]\image.???'
selection_parent_images:
ParentImage:
- '\leadingBackslash\\*.exe'
- 'full\\\*plaintext.exe'
- '{000-aaa-*}\\*.exe'
`
var detection13_positive = `
{
"Image": "C:\\test\\bits*admin.exe",
"ParentImage": "\\leadingBackslash\\something.exe"
}
`
var detection13_positive2 = `
{
"Image": "\\\\DoubleBackslash\\someOther.exe",
"ParentImage": "full\\*plaintext.exe"
}
`
var detection13_positive3 = `
{
"Image": "C:\\test\\bits*admin.exe",
"ParentImage": "full\\*plaintext.exe"
}
`
var detection13_positive4 = `
{
"Image": "[Windows-Security]\\image.cmd",
"ParentImage": "{000-aaa-123}\\evil.exe"
}
`
// won't match as Image is looking for '*\bits*admin.exe' witha leading wildcard and an escaped '*' between bits and admin
// this provides 'C:\test\bitsadmin.exe', which matches the leading wildcard but fails to present the escaped '*'
var detection13_negative = `
{
"Image": "C:\\test\\bitsadmin.exe",
"ParentImage": "\\leadingBackslash\\something.exe"
}
`
// won't match as the ParentImage is looking for '\leadingBackslash\*.exe' with a wildcard
// this provides 'leadingBackslash\something.exe', missing the leading backslash
var detection13_negative2 = `
{
"Image": "C:\\test\\bits*admin.exe",
"ParentImage": "leadingBackslash\\something.exe"
}
`
// won't match as the ParentImage is looking for an exact match (no wildcards) to 'full\*plaintext.exe'
// this provides 'full\\*plaintext', the extra backslash kills it
var detection13_negative3 = `
{
"Image": "C:\\test\\bits*admin.exe",
"ParentImage": "full\\\\*plaintext"
}
`
// shouldn't match on either of these (Image is missing 'Windows' in the bracket, ParentImage is missing the
// a vaule of 000-aaa in the brackets)
var detection13_negative4 = `
{
"Image": "[-Security]\\image.cmd",
"ParentImage": "{000-aaa}\\evil.exe"
}
`
// this has a hacky test; we set 'noCollapseWSNeg' in the parseTestCast struct for this case specifically
// doing so will turn off collapsing the whitespace for the negative test and cause this to fail detection
var detection14 = `
detection:
condition: "selection"
selection:
SomeName|contains:
- 'whitespace collapse testing'
`
var detection14_case = `
{
"SomeName": "whitespace\t\tcollapse testing"
}
`
var detection15 = `
detection:
condition: "all of selection_* and 1 of option_*"
selection_images:
Image:
- '*bits*admin.exe'
selection_parent_images:
ParentImage:
- '*.exe'
selection_bar:
Baz:
- '*bar*'
option_1:
Bar|contains:
- 'Asdf'
option_2:
Test:
- 123
`
var detection15_positive1 = `
{
"Image": "C:\\test\\bits\\aaa-admin.exe",
"ParentImage": "\\leadingBackslash\\something.exe",
"Baz": "foo bar baz",
"Bar": "lalala Asdf [124]"
}
`
var detection15_negative1 = `
{
"Image": "C:\\test\\bits\\aaa-admin.exe",
"ParentImage": "\\leadingBackslash\\something.exe",
"Baz": "foo bar baz",
"Bar": "lalala Asd [124]"
}
`
var detection15_negative2 = `
{
"Image": "C:\\test\\bits\\aaa-admin.exe",
"ParentImage": "\\leadingBackslash\\something.exe",
"Baz": "foo baz",
"Bar": "lalala Asdf [124]"
}
`
var detection15_positive2 = `
{
"Image": "C:\\test\\bits\\aaa-admin.exe",
"ParentImage": "\\leadingBackslash\\something.exe",
"Baz": "foo bar baz",
"Test": 123
}
`
var detection15_negative3 = `
{
"Image": "C:\\test\\bits\\aaa-admin.exe",
"ParentImage": "\\leadingBackslash\\something.exe",
"Baz": "foo bar baz",
"Test": 124
}
`
var detection15_negative4 = `
{
"Image": "C:\\test\\bits\\aaa-admin.exe",
"ParentImage": "\\leadingBackslash\\something.exe",
"Baz": "foo baz",
"Test": 123
}
`
type parseTestCase struct {
ID int
Rule string
Pos, Neg []string
noCollapseWSNeg bool
}
var parseTestCases = []parseTestCase{
{
ID: 1,
Rule: detection1,
Pos: []string{detection1_positive},
Neg: []string{detection1_negative1, detection1_negative2},
},
{
ID: 2,
Rule: detection2,
Pos: []string{detection1_positive},
Neg: []string{detection1_negative1, detection1_negative2},
},
{
ID: 3,
Rule: detection3,
Pos: []string{detection3_positive1, detection3_positive2},
Neg: []string{detection3_negative},
},
{
ID: 4,
Rule: detection4,
Pos: []string{detection1_positive},
Neg: []string{detection1_negative1, detection1_negative2},
},
{
ID: 5,
Rule: detection5,
Pos: []string{detection3_positive1, detection3_positive2},
Neg: []string{detection3_negative},
},
{
ID: 6,
Rule: detection6,
Pos: []string{detection6_positive},
Neg: []string{detection6_negative},
},
{
ID: 7,
Rule: detection7,
Pos: []string{detection3_positive1, detection3_positive2},
Neg: []string{detection7_negative1, detection7_negative2},
},
{
ID: 8,
Rule: detection8,
Pos: []string{detection8_positive},
Neg: []string{detection8_negative1, detection8_negative2},
},
{
ID: 9,
Rule: detection9,
Pos: []string{detection9_positive},
Neg: []string{detection9_negative},
},
{
ID: 10,
Rule: detection10,
Pos: []string{detection10_positive},
Neg: []string{detection10_negative},
},
{
ID: 11,
Rule: detection11,
Pos: []string{detection11_positive},
Neg: []string{detection11_negative},
},
{
ID: 12,
Rule: detection12,
Pos: []string{detection12_positive},
Neg: []string{detection12_negative},
},
{
ID: 13,
Rule: detection13,
Pos: []string{detection13_positive, detection13_positive2, detection13_positive3, detection13_positive4},
Neg: []string{detection13_negative, detection13_negative2, detection13_negative3, detection13_negative4},
},
{
ID: 14,
Rule: detection14,
Pos: []string{detection14_case},
noCollapseWSNeg: false, // ensures whitespace is collapsed and everything matches
},
{
ID: 14,
Rule: detection14,
Neg: []string{detection14_case},
noCollapseWSNeg: true, // turns off whitespace collapsing and causing a non-match
},
{
ID: 15,
Rule: detection15,
Pos: []string{detection15_positive1, detection15_positive2},
Neg: []string{detection15_negative1, detection15_negative2, detection15_negative3, detection15_negative4},
},
}
func TestTokenCollect(t *testing.T) {
for _, c := range LexPosCases {
p := &parser{
lex: lex(c.Expr),
}
if err := p.collect(); err != nil {
switch err.(type) {
case ErrUnsupportedToken:
default:
t.Fatal(err)
}
}
}
}
func TestParse(t *testing.T) {
for _, c := range parseTestCases {
var rule Rule
if err := yaml.Unmarshal([]byte(c.Rule), &rule); err != nil {
t.Fatalf("rule parse case %d failed to unmarshal yaml, %s", c.ID, err)
}
expr := rule.Detection["condition"].(string)
p := &parser{
lex: lex(expr),
sigma: rule.Detection,
noCollapseWS: c.noCollapseWSNeg,
}
if err := p.collect(); err != nil {
t.Fatalf("rule parser case %d failed to collect lexical tokens, %s", c.ID, err)
}
if err := p.parse(); err != nil {
switch err.(type) {
case ErrWip:
t.Fatalf("WIP")
default:
t.Fatalf("rule parser case %d failed to parse lexical tokens, %s", c.ID, err)
}
}
}
}
func TestSigmaEscape(t *testing.T) {
tests := []struct {
name string
input string
expected string
validMatch string
skip bool
}{
{
name: "No_Change",
input: `\\leadingBackslash\\*.exe`,
expected: `\\leadingBackslash\\*.exe`,
validMatch: `\leadingBackslash\testing.exe`,
},
{
name: "Leading_Single_Backslash_Wildcard_After_Slash",
input: `\leadingBackslash\\*.exe`,
expected: `\\leadingBackslash\\*.exe`,
validMatch: `\leadingBackslash\testing.exe`,
},
{
name: "Leading_Wildcard_Single_Backslash_Esc_Wildcard",
input: `*\bits\*admin.exe`,
expected: `*\\bits\*admin.exe`,
validMatch: `leading\bits*admin.exe`,
},
{
name: "Double_Leading_Backslash_Single_Backslash_Wildcard",
input: `\\\\DoubleBackslash\some*.exe`,
expected: `\\\\DoubleBackslash\\some*.exe`,
validMatch: `\\DoubleBackslash\sometMatch.exe`,
},
{
name: "Plaintext_Only_Esc_Wildcard",
input: `some\full\\\*plaintext.exe`,
expected: `some\\full\\\*plaintext.exe`,
validMatch: `some\full\*plaintext.exe`,
},
{
name: "Double_Leading_Backslash_Complex_Mix_Esc",
input: `\\\\DoubleBackslash\?\some*Other\\*test.\\???`,
expected: `\\\\DoubleBackslash\?\\some*Other\\*test.\\???`,
validMatch: `\\DoubleBackslash?\someMixOther\wildcardtest.\cmd`,
},
{
name: "Mixed_Wildcards_Single_Backslash_Brackets",
input: `[*]\*\aSetof\\\sigma{rule?}here*`,
expected: `\[*\]\*\\aSetof\\\\sigma\{rule?\}here*`,
validMatch: `[testing]*\aSetof\\sigma{rules}hereWeGo`,
},
}
for _, curTest := range tests {
t.Run(curTest.name, func(t *testing.T) {
if curTest.skip {
t.Skip("test marked as skip")
}
escStr := escapeSigmaForGlob(curTest.input)
if escStr != curTest.expected {
t.Errorf("failed to validate escaped input; got: %s - expected: %s", escStr, curTest.expected)
}
// test as a glob to be sure
globT, err := glob.Compile(escStr)
if err != nil {
t.Fatalf("failed to compile glob: %+v", err)
}
if !globT.Match(curTest.validMatch) {
t.Errorf("compiled glob did NOT match valid input; glob: %s -- data: %s", escStr, curTest.validMatch)
}
})
}
}