-
-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathfilter_test.go
More file actions
1481 lines (1369 loc) · 58.6 KB
/
Copy pathfilter_test.go
File metadata and controls
1481 lines (1369 loc) · 58.6 KB
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 2019-2020 by Nedim Sabic Sabic
* https://www.fibratus.io
* All Rights Reserved.
*
* 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.
*/
package filter
import (
"github.com/rabbitstack/fibratus/internal/etw/processors"
"github.com/rabbitstack/fibratus/pkg/callstack"
"github.com/rabbitstack/fibratus/pkg/config"
"github.com/rabbitstack/fibratus/pkg/event"
"github.com/rabbitstack/fibratus/pkg/event/params"
"github.com/rabbitstack/fibratus/pkg/filter/fields"
"github.com/rabbitstack/fibratus/pkg/fs"
"github.com/rabbitstack/fibratus/pkg/pe"
"github.com/rabbitstack/fibratus/pkg/ps"
pstypes "github.com/rabbitstack/fibratus/pkg/ps/types"
"github.com/rabbitstack/fibratus/pkg/sys"
"github.com/rabbitstack/fibratus/pkg/util/signature"
"github.com/rabbitstack/fibratus/pkg/util/va"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/windows"
"net"
"os"
"path/filepath"
"testing"
"time"
"unsafe"
)
var cfg = &config.Config{
EventSource: config.EventSourceConfig{
EnableHandleEvents: true,
EnableNetEvents: true,
EnableRegistryEvents: true,
EnableFileIOEvents: true,
EnableImageEvents: true,
EnableThreadEvents: true,
EnableMemEvents: true,
EnableDNSEvents: true,
EnableThreadpoolEvents: true,
},
Filters: &config.Filters{},
PE: pe.Config{Enabled: true},
}
func TestFilterCompile(t *testing.T) {
f := New(`ps.name = 'cmd.exe'`, cfg)
require.NoError(t, f.Compile())
f = New(`'cmd.exe'`, cfg)
require.EqualError(t, f.Compile(), "expected at least one field or operator but zero found")
f = New(`ps.name`, cfg)
require.EqualError(t, f.Compile(), "expected at least one field or operator but zero found")
f = New(`pe.is_exec`, cfg)
require.NoError(t, f.Compile())
f = New(`length(pe.imphash) > 0`, cfg)
require.NoError(t, f.Compile())
f = New(`ps.name =`, cfg)
require.EqualError(t, f.Compile(), "ps.name =\n╭─────────^\n|\n|\n╰─────────────────── expected field, bound field, string, number, bool, ip, function")
}
func TestSeqFilterCompile(t *testing.T) {
f := New(`sequence
|evt.name = 'CreateProcess'| by ps.exe
|evt.name = 'CreateFile' and file.operation = 'create'| by file.name
`, cfg)
require.NoError(t, f.Compile())
require.NotNil(t, f.GetSequence())
assert.Len(t, f.GetSequence().Expressions, 2)
assert.NotNil(t, f.GetSequence().Expressions[0].By)
assert.True(t, len(f.GetStringFields()) > 0)
}
func TestSeqFilterInvalidBoundRefs(t *testing.T) {
f := New(`sequence
|evt.name = 'CreateProcess'| as e1
|evt.name = 'CreateFile' and file.name = $e.ps.exe |
`, cfg)
require.Error(t, f.Compile())
f1 := New(`sequence
|evt.name = 'CreateProcess'| as e1
|evt.name = 'CreateFile' and file.name = $e1.ps.exe |
`, cfg)
require.NoError(t, f1.Compile())
}
func TestStringFields(t *testing.T) {
f := New(`ps.name = 'cmd.exe' and evt.name = 'CreateProcess' or evt.name in ('TerminateProcess', 'CreateFile')`, cfg)
require.NoError(t, f.Compile())
assert.Len(t, f.GetStringFields(), 2)
assert.Len(t, f.GetStringFields()[fields.EvtName], 3)
assert.Len(t, f.GetStringFields()[fields.PsName], 1)
}
func TestProcFilter(t *testing.T) {
ps1 := &pstypes.PS{
Name: "wininit.exe",
Username: "SYSTEM",
Domain: "NT AUTHORITY",
SID: "S-1-5-18",
PID: 5042,
Parent: &pstypes.PS{
Name: "services.exe",
SID: "S-1-5-8",
PID: 2034,
Parent: &pstypes.PS{
Name: "System",
},
},
IsWOW64: false,
IsProtected: true,
IsPackaged: false,
TokenIntegrityLevel: "SYSTEM",
IsTokenElevated: false,
TokenElevationType: "DEFAULT",
}
evt := &event.Event{
Type: event.CreateProcess,
Category: event.Process,
Params: event.Params{
params.Cmdline: {Name: params.Cmdline, Type: params.UnicodeString, Value: "C:\\Windows\\system32\\svchost-fake.exe -k RPCSS"},
params.ProcessName: {Name: params.ProcessName, Type: params.AnsiString, Value: "svchost-fake.exe"},
params.ProcessID: {Name: params.ProcessID, Type: params.PID, Value: uint32(1234)},
params.ProcessParentID: {Name: params.ProcessParentID, Type: params.PID, Value: uint32(345)},
params.UserSID: {Name: params.UserSID, Type: params.WbemSID, Value: []byte{224, 8, 226, 31, 15, 167, 255, 255, 0, 0, 0, 0, 15, 167, 255, 255, 1, 1, 0, 0, 0, 0, 0, 5, 18, 0, 0, 0}},
params.Username: {Name: params.Username, Type: params.UnicodeString, Value: "loki"},
params.Domain: {Name: params.Domain, Type: params.UnicodeString, Value: "TITAN"},
params.ProcessFlags: {Name: params.ProcessFlags, Type: params.Flags, Value: uint32(0x000000E)},
params.ProcessTokenIntegrityLevel: {Name: params.ProcessTokenIntegrityLevel, Type: params.AnsiString, Value: "SYSTEM"},
params.ProcessTokenIsElevated: {Name: params.ProcessTokenIsElevated, Type: params.Bool, Value: true},
params.ProcessTokenElevationType: {Name: params.ProcessTokenElevationType, Type: params.AnsiString, Value: "FULL"},
},
Name: "CreateProcess",
PID: 1023,
PS: &pstypes.PS{
Name: "svchost.exe",
Cmdline: "C:\\Windows\\System32\\svchost.exe",
Parent: ps1,
Ppid: 345,
Username: "SYSTEM",
Domain: "NT AUTHORITY",
SID: "S-1-5-18",
Args: []string{"-k", "DcomLaunch", "-p", "-s", "LSM"},
Envs: map[string]string{"ALLUSERSPROFILE": "C:\\ProgramData", "OS": "Windows_NT", "ProgramFiles(x86)": "C:\\Program Files (x86)"},
Modules: []pstypes.Module{
{Name: "C:\\Windows\\System32\\kernel32.dll", Size: 12354, Checksum: 23123343, BaseAddress: va.Address(4294066175), DefaultBaseAddress: va.Address(4293993725)},
{Name: "C:\\Windows\\System32\\user32.dll", Size: 212354, Checksum: 33123343, BaseAddress: va.Address(4277288959), DefaultBaseAddress: va.Address(4293993725)},
},
Threads: map[uint32]pstypes.Thread{
3453: {Tid: 3453, StartAddress: va.Address(144229524944769), IOPrio: 2, PagePrio: 5, KstackBase: va.Address(18446677035730165760), KstackLimit: va.Address(18446677035730137088), UstackLimit: va.Address(86376448), UstackBase: va.Address(86372352)},
3455: {Tid: 3455, StartAddress: va.Address(140729524944768), IOPrio: 3, PagePrio: 5, KstackBase: va.Address(18446687035730165760), KstackLimit: va.Address(18446698035730165760), UstackLimit: va.Address(86376448), UstackBase: va.Address(46375352)},
},
Mmaps: []pstypes.Mmap{
{Size: 34545, BaseAddress: va.Address(144229524944769), Protection: 4653056, File: "C:\\Windows\\System32\\ucrtbase.dll", Type: "IMAGE"}, //EXECUTE_READWRITE|READONLY
{Size: 4096, BaseAddress: va.Address(145229445447666), Protection: 12845056, Type: "PAGEFILE"}, // READWRITE 12845056
},
IsProtected: false,
IsPackaged: true,
IsWOW64: false,
TokenIntegrityLevel: "SYSTEM",
IsTokenElevated: false,
TokenElevationType: "DEFAULT",
},
}
evt.Timestamp, _ = time.Parse(time.RFC3339, "2011-05-03T15:04:05.323Z")
evt1 := &event.Event{
Type: event.OpenProcess,
Category: event.Process,
Params: event.Params{
params.DesiredAccess: {Name: params.DesiredAccess, Type: params.Flags, Value: uint32(0x1400), Flags: event.PsAccessRightFlags},
},
Name: "OpenProcess",
PID: 1023,
PS: &pstypes.PS{
Name: "svchost.exe",
Parent: ps1,
Ppid: 345,
Envs: map[string]string{"ALLUSERSPROFILE": "C:\\ProgramData", "OS": "Windows_NT", "ProgramFiles(x86)": "C:\\Program Files (x86)"},
Modules: []pstypes.Module{
{Name: "C:\\Windows\\System32\\kernel32.dll", Size: 12354, Checksum: 23123343, BaseAddress: va.Address(4294066175), DefaultBaseAddress: va.Address(4293993725)},
{Name: "C:\\Windows\\System32\\user32.dll", Size: 212354, Checksum: 33123343, BaseAddress: va.Address(4277288959), DefaultBaseAddress: va.Address(4293993725)},
},
},
}
var tests = []struct {
filter string
matches bool
}{
{`ps.name = 'svchost.exe'`, true},
{`ps.name = 'svchot.exe'`, false},
{`ps.name = 'mimikatz.exe' or ps.name contains 'svc'`, true},
{`ps.name ~= 'SVCHOST.exe'`, true},
{`ps.cmdline = 'C:\\Windows\\System32\\svchost.exe'`, true},
{`ps.child.cmdline = 'C:\\Windows\\system32\\svchost-fake.exe -k RPCSS'`, true},
{`ps.username = 'SYSTEM'`, true},
{`ps.domain = 'NT AUTHORITY'`, true},
{`ps.sid = 'S-1-5-18'`, true},
{`ps.pid = 1023`, true},
{`ps.child.sid = 'S-1-5-18'`, true},
{`ps.sibling.pid = 1234`, true},
{`ps.child.pid = 1234`, true},
{`ps.child.uuid > 0`, true},
{`ps.parent.pid = 5042`, true},
{`ps.sibling.name = 'svchost-fake.exe'`, true},
{`ps.child.name = 'svchost-fake.exe'`, true},
{`ps.sibling.username = 'loki'`, true},
{`ps.child.username = 'loki'`, true},
{`ps.sibling.domain = 'TITAN'`, true},
{`ps.child.domain = 'TITAN'`, true},
{`ps.parent.username = 'SYSTEM'`, true},
{`ps.parent.domain = 'NT AUTHORITY'`, true},
{`ps.envs[ALLUSERSPROFILE] = 'C:\\ProgramData'`, true},
{`ps.envs[ALLUSER] = 'C:\\ProgramData'`, true},
{`ps.envs[ProgramFiles] = 'C:\\Program Files (x86)'`, true},
{`ps.envs[windir] = 'C:\\WINDOWS'`, false},
{`ps.envs in ('ALLUSERSPROFILE:C:\\ProgramData')`, true},
{`foreach(ps.envs, $env, substr($env, 0, indexof($env, ':')) = 'OS')`, true},
{`ps.child.is_wow64`, true},
{`ps.child.is_packaged`, true},
{`ps.child.is_protected`, true},
{`ps.is_wow64`, false},
{`ps.is_packaged`, true},
{`ps.is_protected`, false},
{`ps.parent.is_wow64`, false},
{`ps.parent.is_packaged`, false},
{`ps.parent.is_protected`, true},
{`ps.token.integrity_level = 'SYSTEM'`, true},
{`ps.token.is_elevated = false`, true},
{`ps.token.elevation_type = 'DEFAULT'`, true},
{`ps.child.token.integrity_level = 'SYSTEM'`, true},
{`ps.child.token.is_elevated = true`, true},
{`ps.child.token.elevation_type = 'FULL'`, true},
{`ps.parent.token.integrity_level = 'SYSTEM'`, true},
{`ps.parent.token.is_elevated = false`, true},
{`ps.parent.token.elevation_type = 'DEFAULT'`, true},
{`evt.name = 'CreateProcess' and ps.name contains 'svchost'`, true},
{`ps.modules IN ('kernel32.dll')`, true},
{`evt.name = 'CreateProcess' and evt.pid != ps.ppid`, true},
{`ps.parent.name = 'wininit.exe'`, true},
{`ps.ancestor[0] = 'svchost.exe'`, false},
{`ps.ancestor[0] = 'wininit.exe'`, true},
{`ps.ancestor[1] = 'services.exe'`, true},
{`ps.ancestor[2] = 'System'`, true},
{`ps.ancestor[3] = ''`, true},
{`ps.ancestor intersects ('wininit.exe', 'services.exe', 'System')`, true},
{`foreach(ps._ancestors, $proc, $proc.name in ('wininit.exe', 'services.exe', 'System'))`, true},
{`foreach(ps._ancestors, $proc, $proc.name in ('wininit.exe', 'services.exe', 'System') and ps.is_packaged, ps.is_packaged)`, true},
{`foreach(ps._ancestors, $proc, $proc.name not in ('svchost.exe', 'WmiPrvSE.exe'))`, true},
{`foreach(ps._ancestors, $proc, $proc.sid = 'S-1-5-8'))`, true},
{`foreach(ps._ancestors, $proc, $proc.name endswith 'ices.exe'))`, true},
{`foreach(ps._ancestors, $proc, $proc.pid in (2034, 343) and $proc.name = 'services.exe')`, true},
{`foreach(ps._ancestors, $proc, $proc.username = 'SYSTEM')`, true},
{`foreach(ps._ancestors, $proc, $proc.domain = 'NT AUTHORITY')`, true},
{`foreach(ps._ancestors, $proc, $proc.username = upper('system'))`, true},
{`foreach(ps._ancestors, $proc, $proc.token.integrity_level = 'SYSTEM' and $proc.token.is_elevated = false and $proc.token.elevation_type = 'DEFAULT')`, true},
{`ps.args intersects ('-k', 'DcomLaunch')`, true},
{`ps.args intersects ('-w', 'DcomLaunch')`, false},
{`ps.args iintersects ('-K', 'DComLaunch')`, true},
{`ps.args iintersects ('-W', 'DcomLaunch')`, false},
{`foreach(ps.modules, $mod, $mod imatches 'us?r32.dll')`, true},
{`foreach(ps._modules, $mod, $mod.path imatches '?:\\Windows\\System32\\us?r32.dll')`, true},
{`foreach(ps._modules, $mod, $mod.name imatches 'USER32.*')`, true},
{`foreach(ps._modules, $mod, $mod.name imatches 'USER32.*' and $mod.size >= 212354)`, true},
{`foreach(ps._modules, $mod, ($mod.name imatches 'USER32.*' and $mod.size >= 212354) or $mod.name imatches '*winhttp.dll')`, true},
{`foreach(ps._modules, $mod, ($mod.name imatches 'winhttp.dll' and $mod.size >= 11212354) or $mod.name matches 'user32.dll')`, true},
{`foreach(ps._modules, $mod, $mod.checksum = 23123343)`, true},
{`foreach(ps._modules, $mod, $mod.address = 'fff23fff')`, true},
{`foreach(ps._threads, $t, $t.start_address = '7ffe2557ff80')`, true},
{`foreach(ps._threads, $t, $t.start_address = '7ffe2557ff80' or $t.user_stack_base = '2251760466')`, true},
{`foreach(ps._threads, $t, $t.tid = 3453)`, true},
{`foreach(ps._threads, $t, $t.start_address = '7ffe2557ff80' or $t.user_stack_base = '2251760466')`, true},
{`foreach(ps._threads, $t, $t.kernel_stack_base = 'ffffcc1fcf800000' and $t.kernel_stack_limit = 'ffffd620f297b000')`, true},
{`foreach(ps._mmaps, $mmap, $mmap.protection = 'RW')`, true},
{`foreach(ps._mmaps, $mmap, $mmap.path = 'C:\\Windows\\System32\\ucrtbase.dll' and $mmap.type = 'IMAGE')`, true},
{`foreach(ps._mmaps, $mmap, $mmap.address = '8415dd81bff2')`, true},
{`foreach(ps._mmaps, $mmap, $mmap.size = 4096)`, true},
}
psnap := new(ps.SnapshotterMock)
psnap.On("FindAndPut", uint32(1234)).Return(ps1)
for i, tt := range tests {
f := New(tt.filter, cfg, WithPSnapshotter(psnap))
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt)
if matches != tt.matches {
t.Errorf("%d. %q ps filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
var tests1 = []struct {
filter string
matches bool
}{
{`ps.access.mask.names in ('QUERY_INFORMATION', 'QUERY_LIMITED_INFORMATION')`, true},
{`ps.access.mask.names in ('ALL_ACCESS')`, false},
}
for i, tt := range tests1 {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt1)
if matches != tt.matches {
t.Errorf("%d. %q ps filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
}
func TestThreadFilter(t *testing.T) {
pars := event.Params{
params.ProcessID: {Name: params.ProcessID, Type: params.PID, Value: uint32(os.Getpid())},
params.ThreadID: {Name: params.ThreadID, Type: params.TID, Value: uint32(3453)},
params.BasePrio: {Name: params.BasePrio, Type: params.Uint8, Value: uint8(13)},
params.StartAddress: {Name: params.StartAddress, Type: params.Address, Value: uint64(140729524944768)},
params.TEB: {Name: params.TEB, Type: params.Address, Value: uint64(614994620416)},
params.IOPrio: {Name: params.IOPrio, Type: params.Uint8, Value: uint8(2)},
params.KstackBase: {Name: params.KstackBase, Type: params.Address, Value: uint64(18446677035730165760)},
params.KstackLimit: {Name: params.KstackLimit, Type: params.Address, Value: uint64(18446677035730137088)},
params.PagePrio: {Name: params.PagePrio, Type: params.Uint8, Value: uint8(5)},
params.UstackBase: {Name: params.UstackBase, Type: params.Address, Value: uint64(86376448)},
params.UstackLimit: {Name: params.UstackLimit, Type: params.Address, Value: uint64(86372352)},
params.StartAddressSymbol: {Name: params.StartAddressSymbol, Type: params.UnicodeString, Value: "LoadImage"},
params.StartAddressModule: {Name: params.StartAddressModule, Type: params.UnicodeString, Value: "C:\\Windows\\System32\\kernel32.dll"},
}
evt := &event.Event{
Type: event.CreateThread,
Params: pars,
Name: "CreateThread",
PID: windows.GetCurrentProcessId(),
Category: event.Thread,
PS: &pstypes.PS{
Name: "svchost.exe",
Envs: map[string]string{"ALLUSERSPROFILE": "C:\\ProgramData", "OS": "Windows_NT", "ProgramFiles(x86)": "C:\\Program Files (x86)"},
Modules: []pstypes.Module{
{Name: "C:\\Windows\\System32\\kernel32.dll", Size: 2312354, Checksum: 23123343, BaseAddress: va.Address(0x7ffb5c1d0396), DefaultBaseAddress: va.Address(0x7ffb5c1d0396)},
{Name: "C:\\Windows\\System32\\user32.dll", Size: 32212354, Checksum: 33123343, BaseAddress: va.Address(0x7ffb313953b2), DefaultBaseAddress: va.Address(0x7ffb313953b2)},
{Name: "C:\\Program Files\\JetBrains\\GoLand 2021.2.3\\jbr\\bin\\java.dll", Size: 32212354, Checksum: 33123343, BaseAddress: va.Address(0x7ffb3138592e), DefaultBaseAddress: va.Address(0x7ffb3138592e)},
},
},
}
// append the module signature
cert := &sys.Cert{Subject: "US, Washington, Redmond, Microsoft Corporation, Microsoft Windows", Issuer: "US, Washington, Redmond, Microsoft Corporation, Microsoft Windows Production PCA 2011"}
signature.GetSignatures().PutSignature(0x7ffb3138592e, &signature.Signature{Filename: "C:\\Program Files\\JetBrains\\GoLand 2021.2.3\\jbr\\bin\\java.dll", Level: 4, Type: 1, Cert: cert})
// simulate unbacked RWX frame
base, err := windows.VirtualAlloc(0, 1024, windows.MEM_COMMIT|windows.MEM_RESERVE, windows.PAGE_EXECUTE_READWRITE)
require.NoError(t, err)
defer func() {
_ = windows.VirtualFree(base, 1024, windows.MEM_DECOMMIT)
}()
insns := []byte{
0x4C, 0x8B, 0xD1, // mov r10, rcx
0xB8, 0x55, 0x0, 0x0, 0x0, // mov eax, 55h
0xF6, 0x04, 0x25, 0x08, 0x03, 0xFE, 0x7F, 0x01, // test byte ptr[7FFE0308h]
0x0F, 0x05, // syscall
0xC3, // ret
}
require.NoError(t, windows.WriteProcessMemory(windows.CurrentProcess(), base, &insns[0], uintptr(len(insns)), nil))
evt.Callstack.Init(8)
evt.Callstack.PushFrame(callstack.Frame{PID: evt.PID, Addr: 0x2638e59e0a5, Offset: 0, Symbol: "?", Module: "unbacked"})
evt.Callstack.PushFrame(callstack.Frame{PID: evt.PID, Addr: va.Address(base), Offset: 0, Symbol: "?", Module: "unbacked"})
evt.Callstack.PushFrame(callstack.Frame{PID: evt.PID, Addr: 0x7ffb313853b2, Offset: 0x10a, Symbol: "Java_java_lang_ProcessImpl_create", Module: "C:\\Program Files\\JetBrains\\GoLand 2021.2.3\\jbr\\bin\\java.dll"})
evt.Callstack.PushFrame(callstack.Frame{PID: evt.PID, Addr: 0x7ffb3138592e, ModuleAddress: 0x7ffb3138592e, Offset: 0x3a2, Symbol: "Java_java_lang_ProcessImpl_waitForTimeoutInterruptibly", Module: "C:\\Program Files\\JetBrains\\GoLand 2021.2.3\\jbr\\bin\\java.dll"})
evt.Callstack.PushFrame(callstack.Frame{PID: evt.PID, Addr: 0x7ffb5d8e61f4, Offset: 0x54, Symbol: "CreateProcessW", Module: "C:\\WINDOWS\\System32\\KERNEL32.DLL"})
evt.Callstack.PushFrame(callstack.Frame{PID: evt.PID, Addr: 0x7ffb5c1d0396, Offset: 0x66, Symbol: "CreateProcessW", Module: "C:\\WINDOWS\\System32\\KERNELBASE.dll"})
evt.Callstack.PushFrame(callstack.Frame{PID: evt.PID, Addr: 0xfffff8072ebc1f6f, Offset: 0x4ef, Symbol: "FltRequestFileInfoOnCreateCompletion", Module: "C:\\WINDOWS\\System32\\drivers\\FLTMGR.SYS"})
evt.Callstack.PushFrame(callstack.Frame{PID: evt.PID, Addr: 0xfffff8072eb8961b, Offset: 0x20cb, Symbol: "FltGetStreamContext", Module: "C:\\WINDOWS\\System32\\drivers\\FLTMGR.SYS"})
var tests = []struct {
filter string
matches bool
}{
{`thread.ustack.base = '5260000'`, true},
{`thread.ustack.limit = '525f000'`, true},
{`thread.kstack.base = 'ffffc307810d6000'`, true},
{`thread.kstack.limit = 'ffffc307810cf000'`, true},
{`thread.start_address = '7ffe2557ff80'`, true},
{`thread.teb_address = '8f30893000'`, true},
{`thread.start_address.symbol = 'LoadImage'`, true},
{`thread.start_address.module = 'C:\\Windows\\System32\\kernel32.dll'`, true},
{`thread.callstack.summary = 'KERNELBASE.dll|KERNEL32.DLL|java.dll|unbacked'`, true},
{`thread.callstack.detail icontains 'C:\\WINDOWS\\System32\\KERNELBASE.dll!CreateProcessW+0x66'`, true},
{`thread.callstack.modules in ('C:\\WINDOWS\\System32\\KERNELBASE.dll', 'C:\\Program Files\\JetBrains\\GoLand 2021.2.3\\jbr\\bin\\java.dll')`, true},
{`thread.callstack.modules[5] = 'C:\\WINDOWS\\System32\\KERNELBASE.dll'`, true},
{`thread.callstack.modules[7] = 'C:\\WINDOWS\\System32\\drivers\\FLTMGR.SYS'`, true},
{`thread.callstack.modules[8] = ''`, true},
{`thread.callstack.symbols imatches ('KERNELBASE.dll!CreateProcess*', 'Java_java_lang_ProcessImpl_create')`, true},
{`thread.callstack.symbols[2] = 'Java_java_lang_ProcessImpl_create'`, true},
{`thread.callstack.symbols[8] = ''`, true},
{`thread.callstack.protections in ('RWX')`, true},
{`thread.callstack.allocation_sizes > 0`, false},
{`length(thread.callstack.callsite_leading_assembly) > 0`, true},
{`thread.callstack.callsite_trailing_assembly matches ('*mov r10, rcx|mov eax, 0x*|syscall*')`, true},
{`thread.callstack.is_unbacked`, true},
{`thread.callstack.addresses intersects ('7ffb5d8e61f4', 'fffff8072eb8961b')`, true},
{`thread.callstack.final_user_module.name = 'java.dll'`, true},
{`thread.callstack.final_user_module.path = 'C:\\Program Files\\JetBrains\\GoLand 2021.2.3\\jbr\\bin\\java.dll'`, true},
{`thread.callstack.final_user_symbol.name = 'Java_java_lang_ProcessImpl_waitForTimeoutInterruptibly'`, true},
{`thread.callstack.final_kernel_module.name = 'FLTMGR.SYS'`, true},
{`thread.callstack.final_kernel_module.path = 'C:\\WINDOWS\\System32\\drivers\\FLTMGR.SYS'`, true},
{`thread.callstack.final_kernel_symbol.name = 'FltGetStreamContext'`, true},
{`thread.callstack.final_user_module.signature.is_signed = true`, true},
{`thread.callstack.final_user_module.signature.is_trusted = true`, true},
{`thread.callstack.final_user_module.signature.cert.issuer imatches '*Microsoft Corporation*'`, true},
{`thread.callstack.final_user_module.signature.cert.subject imatches '*Microsoft Windows*'`, true},
{`foreach(thread._callstack, $frame, $frame.address = '2638e59e0a5' or $frame.address = '7ffb5c1d0396')`, true},
{`foreach(thread._callstack, $frame, $frame.address = 'fffff8072ebc1f6f' or $frame.address = 'fffff8072eb8961b')`, true},
{`foreach(thread._callstack, $frame, $frame.address = 'ffffffffff')`, false},
{`foreach(thread._callstack, $frame, $frame.symbol imatches '?:\\Program Files\\*java.dll!Java_java_lang_ProcessImpl_create')`, true},
{`foreach(thread._callstack, $frame, $frame.symbol imatches '*CreateProcessW')`, true},
{`foreach(thread._callstack, $frame, $frame.module = 'C:\\Program Files\\JetBrains\\GoLand 2021.2.3\\jbr\\bin\\java.dll')`, true},
{`foreach(thread._callstack, $frame, base($frame.module) = 'java.dll' and $frame.symbol imatches '*Java_java_lang_ProcessImpl_create')`, true},
{`foreach(thread._callstack, $frame, $frame.offset = 266)`, true},
{`foreach(thread._callstack, $frame, $frame.is_unbacked = true)`, true},
{`ps.name = 'svchost.exe' and not foreach(thread._callstack, $frame, $frame.symbol imatches '*LoadLibrary')`, true},
{`foreach(thread._callstack, $frame, $frame.allocation_size = 0)`, true},
{`foreach(thread._callstack, $frame, $frame.protection = 'RWX')`, true},
{`foreach(thread._callstack, $frame, $frame.callsite_trailing_assembly matches '*mov r10, rcx|mov eax, 0x*|syscall*' and $frame.module = 'unbacked')`, true},
{`foreach(thread._callstack, $frame, $frame.module.signature.is_signed and $frame.module.signature.is_trusted)`, true},
{`foreach(thread._callstack, $frame, $frame.module.signature.cert.issuer imatches '*Microsoft Corporation*')`, true},
{`foreach(thread._callstack, $frame, $frame.module.signature.cert.subject imatches '*Microsoft Windows*')`, true},
}
for i, tt := range tests {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt)
if matches != tt.matches {
t.Errorf("%d. %q thread filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
// spawn a new process
var si windows.StartupInfo
si.Flags = windows.STARTF_USESHOWWINDOW
var pi windows.ProcessInformation
argv := windows.StringToUTF16Ptr(filepath.Join(os.Getenv("windir"), "regedit.exe"))
err = windows.CreateProcess(
nil,
argv,
nil,
nil,
true,
0,
nil,
nil,
&si,
&pi)
require.NoError(t, err)
for {
if sys.IsProcessRunning(pi.Process) {
break
}
time.Sleep(time.Millisecond * 100)
log.Infof("%d pid not yet ready", pi.ProcessId)
}
defer windows.TerminateProcess(pi.Process, 0)
evt.PID = pi.ProcessId
// try until a valid address is returned
// or fail if max attempts are exhausted
j := 50
ntdll := getNtdllAddress(pi.ProcessId)
for ntdll == 0 && j > 0 {
ntdll = getNtdllAddress(pi.ProcessId)
time.Sleep(time.Millisecond * 250)
j--
}
// overwrite ntdll address with dummy bytes
// to reproduce module stomping technique
var protect uint32
require.NoError(t, windows.VirtualProtectEx(pi.Process, ntdll, uintptr(len(insns)), windows.PAGE_EXECUTE_READWRITE, &protect))
var n uintptr
require.NoError(t, windows.WriteProcessMemory(pi.Process, ntdll, &insns[0], uintptr(len(insns)), &n))
evt.Callstack[0] = callstack.Frame{PID: evt.PID, Addr: va.Address(ntdll), Offset: 0, Symbol: "?", Module: "C:\\Windows\\System32\\ntdll.dll"}
var tests1 = []struct {
filter string
matches bool
}{
{`thread.callstack.allocation_sizes > 0`, true},
{`foreach(thread._callstack, $frame, $frame.allocation_size > 2048 and $frame.protection = 'RWXC')`, true},
}
for i, tt := range tests1 {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt)
if matches != tt.matches {
t.Errorf("%d. %q thread filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
}
func TestFileFilter(t *testing.T) {
evt := &event.Event{
Type: event.CreateFile,
Tid: 2484,
PID: 859,
CPU: 1,
Seq: 2,
Name: "CreateFile",
Category: event.File,
Host: "archrabbit",
Description: "Creates or opens a new file, directory, I/O device, pipe, console",
Params: event.Params{
params.FileObject: {Name: params.FileObject, Type: params.Uint64, Value: uint64(12456738026482168384)},
params.FilePath: {Name: params.FilePath, Type: params.UnicodeString, Value: "C:\\Windows\\system32\\user32.dll"},
params.FileType: {Name: params.FileType, Type: params.AnsiString, Value: "file"},
params.FileOperation: {Name: params.FileOperation, Type: params.AnsiString, Value: "open"},
},
Metadata: map[event.MetadataKey]any{"foo": "bar", "fooz": "barzz"},
}
var tests = []struct {
filter string
matches bool
}{
{`file.name = 'user32.dll'`, true},
{`file.path = 'C:\\Windows\\system32\\user32.dll'`, true},
{`file.extension = '.dll'`, true},
{`file.extension not contains '.exe'`, true},
{`file.extension contains '.exe' or (file.extension contains '.dll' and file.name endswith 'user32.dll')`, true},
{`file.extension = '.dll' or (file.extension contains '.exe' and file.name endswith 'kernel32.dll')`, true},
{`file.extension not contains '.exe' and file.extension contains '.dll'`, true},
{`file.extension not contains '.exe' and file.extension not contains '.com'`, true},
{`file.extension not contains '.exe' and file.extension not contains '.com' and file.extension not in ('.vba', '.exe')`, true},
{`file.extension not in ('.exe', '.com')`, true},
{`file.extension not in ('.exe', '.dll')`, false},
{`file.path matches 'C:\\*\\user32.dll'`, true},
{`file.path not matches 'C:\\*.exe'`, true},
{`file.path imatches 'C:\\*\\USER32.dll'`, true},
{`file.path matches ('C:\\*\\user3?.dll', 'C:\\*\\user32.*')`, true},
{`file.path contains ('C:\\Windows\\system32\\kernel32.dll', 'C:\\Windows\\system32\\user32.dll')`, true},
{`file.path not matches ('C:\\*.exe', 'C:\\Windows\\*.com')`, true},
{`file.path endswith ('.exe', 'kernel32.dll', 'user32.dll')`, true},
{`file.path iendswith ('.EXE', 'KERNEL32.dll', 'user32.dll')`, true},
{`file.path istartswith ('C:\\WINDOWS', 'KERNEL32.dll', 'user32.dll')`, true},
{`file.path iin ('C:\\WINDOWS\\system32\\user32.dll')`, true},
{`file.path fuzzy 'C:\\Windows\\system32\\ser3ll'`, true},
{`file.path ifuzzy 'C:\\WINDOWS\\sYS\\ser3ll'`, true},
{`file.path ifuzzy 'C:\\WINDOWS\\sYS\\32dll'`, true},
{`file.path fuzzy ('C:\\Windows\\system32\\kernel', 'C:\\Windows\\system32\\ser3ll')`, true},
{`file.path ifuzzynorm 'C:\\WINDOWS\\sÝS\\32dll'`, true},
{`base(file.path) = 'user32.dll'`, true},
{`ext(base(file.path)) = '.dll'`, true},
{`base(file.path, false) = 'user32'`, true},
{`dir(file.path) = 'C:\\Windows\\system32'`, true},
{`ext(file.path) = '.dll'`, true},
{`ext(file.path, false) = 'dll'`, true},
{`is_abs(file.path)`, true},
{`is_abs(base(file.path))`, false},
{`file.path iin glob('C:\\Windows\\System32\\*.dll')`, true},
{`volume(file.path) = 'C:'`, true},
}
for i, tt := range tests {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt)
if matches != tt.matches {
t.Errorf("%d. %q file filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
}
func TestFileInfoFilter(t *testing.T) {
var tests = []struct {
f string
e *event.Event
matches bool
}{
{
`file.info_class = 'Allocation'`,
&event.Event{
Category: event.File,
Type: event.SetFileInformation,
Name: "SetFileInformation",
Params: event.Params{
params.FileInfoClass: {Name: params.FileInfoClass, Type: params.Enum, Value: fs.AllocationClass, Enum: fs.FileInfoClasses},
},
},
true,
},
{
`file.info.allocation_size = 64500`,
&event.Event{
Category: event.File,
Type: event.SetFileInformation,
Name: "SetFileInformation",
Params: event.Params{
params.FileInfoClass: {Name: params.FileInfoClass, Type: params.Enum, Value: fs.AllocationClass, Enum: fs.FileInfoClasses},
params.FileExtraInfo: {Name: params.FileExtraInfo, Type: params.Uint64, Value: uint64(64500)},
},
},
true,
},
{
`file.info.eof_size = 64500`,
&event.Event{
Category: event.File,
Type: event.SetFileInformation,
Name: "SetFileInformation",
Params: event.Params{
params.FileInfoClass: {Name: params.FileInfoClass, Type: params.Enum, Value: fs.EOFClass, Enum: fs.FileInfoClasses},
params.FileExtraInfo: {Name: params.FileExtraInfo, Type: params.Uint64, Value: uint64(64500)},
},
},
true,
},
{
`file.info.eof_size = 64500`,
&event.Event{
Category: event.File,
Type: event.SetFileInformation,
Name: "SetFileInformation",
Params: event.Params{
params.FileInfoClass: {Name: params.FileInfoClass, Type: params.Enum, Value: fs.DispositionClass, Enum: fs.FileInfoClasses},
params.FileExtraInfo: {Name: params.FileExtraInfo, Type: params.Uint64, Value: uint64(1)},
},
},
false,
},
{
`file.info.is_disposition_delete_file = true`,
&event.Event{
Category: event.File,
Type: event.DeleteFile,
Name: "DeleteFile",
Params: event.Params{
params.FileInfoClass: {Name: params.FileInfoClass, Type: params.Enum, Value: fs.DispositionClass, Enum: fs.FileInfoClasses},
params.FileExtraInfo: {Name: params.FileExtraInfo, Type: params.Uint64, Value: uint64(1)},
},
},
true,
},
}
for _, tt := range tests {
t.Run(tt.f, func(t *testing.T) {
f := New(tt.f, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tt.matches, f.Run(tt.e))
})
}
}
func TestEventFilter(t *testing.T) {
evt := &event.Event{
Type: event.CreateFile,
Tid: 2484,
PID: 859,
CPU: 1,
Seq: 2,
Name: "CreateFile",
Category: event.File,
Host: "archrabbit",
Description: "Creates or opens a new file, directory, I/O device, pipe, console",
Params: event.Params{
params.ProcessID: {Name: params.ProcessID, Type: params.PID, Value: uint32(3434)},
params.FileObject: {Name: params.FileObject, Type: params.Uint64, Value: uint64(12456738026482168384)},
params.FilePath: {Name: params.FilePath, Type: params.UnicodeString, Value: "\\Device\\HarddiskVolume2\\Windows\\system32\\user32.dll"},
params.FileType: {Name: params.FileType, Type: params.AnsiString, Value: "file"},
params.FileOperation: {Name: params.FileOperation, Type: params.AnsiString, Value: "open"},
},
Metadata: map[event.MetadataKey]any{"foo": "bar", "fooz": "barz"},
}
evt.Timestamp, _ = time.Parse(time.RFC3339, "2011-05-03T15:04:05.323Z")
var tests = []struct {
filter string
matches bool
}{
{`evt.seq = 2`, true},
{`evt.pid = 859`, true},
{`evt.tid = 2484`, true},
{`evt.cpu = 1`, true},
{`evt.name = 'CreateFile'`, true},
{`evt.category = 'file'`, true},
{`evt.host = 'archrabbit'`, true},
{`evt.nparams = 5`, true},
{`evt.arg[file_path] = '\\Device\\HarddiskVolume2\\Windows\\system32\\user32.dll'`, true},
{`evt.arg[type] = 'file'`, true},
{`evt.arg[pid] = 3434`, true},
{`evt.desc contains 'Creates or opens a new file'`, true},
{`evt.date.d = 3 AND evt.date.m = 5 AND evt.time.s = 5 AND evt.time.m = 4 and evt.time.h = 15`, true},
{`evt.time = '15:04:05'`, true},
{`concat(evt.name, evt.host, evt.nparams) = 'CreateFilearchrabbit5'`, true},
{`ltrim(evt.host, 'arch') = 'rabbit'`, true},
{`concat(ltrim(evt.name, 'Create'), evt.host) = 'Filearchrabbit'`, true},
{`lower(rtrim(evt.name, 'File')) = 'create'`, true},
{`upper(rtrim(evt.name, 'File')) = 'CREATE'`, true},
{`replace(evt.host, 'rabbit', '_bunny') = 'arch_bunny'`, true},
{`replace(evt.host, 'rabbit', '_bunny', '_bunny', 'bunny') = 'archbunny'`, true},
{`split(file.path, '\\') IN ('windows', 'system32')`, true},
{`length(file.path) = 51`, true},
{`indexof(file.path, '\\') = 0`, true},
{`indexof(file.path, '\\', 'last') = 40`, true},
{`indexof(file.path, 'h2', 'any') = 22`, true},
{`substr(file.path, indexof(file.path, '\\'), indexof(file.path, '\\Hard')) = '\\Device'`, true},
{`substr(evt.desc, indexof(evt.desc, '\\'), indexof(evt.desc, 'NOT')) = 'Creates or opens a new file, directory, I/O device, pipe, console'`, true},
{`entropy(file.path) > 120`, true},
{`regex(file.path, '\\\\Device\\\\HarddiskVolume[2-9]+\\\\.*')`, true},
}
for i, tt := range tests {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt)
if matches != tt.matches {
t.Errorf("%d. %q evt filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
}
func TestNetFilter(t *testing.T) {
evt := &event.Event{
Type: event.SendTCPv4,
Tid: 2484,
PID: 859,
PS: &pstypes.PS{
Name: "cmd.exe",
},
Category: event.Net,
Params: event.Params{
params.NetDport: {Name: params.NetDport, Type: params.Uint16, Value: uint16(443)},
params.NetSport: {Name: params.NetSport, Type: params.Uint16, Value: uint16(43123)},
params.NetSIP: {Name: params.NetSIP, Type: params.IPv4, Value: net.ParseIP("127.0.0.1")},
params.NetDIP: {Name: params.NetDIP, Type: params.IPv4, Value: net.ParseIP("216.58.201.174")},
},
}
var tests = []struct {
filter string
matches bool
}{
{`net.dip = 216.58.201.174`, true},
{`net.dip != 216.58.201.174`, false},
{`net.dip != 116.58.201.174`, true},
{`net.dip startswith '216.58'`, true},
{`net.dip endswith '.174'`, true},
{`net.dport = 443`, true},
{`net.dport in (123, 443)`, true},
{`net.dip != 116.58.201.174`, true},
{`net.dip not in ('116.58.201.172', '16.58.201.176')`, true},
{`net.dip not in (116.58.201.172, 16.58.201.176)`, true},
{`ps.name = 'cmd.exe' and not cidr_contains(net.sip, '227.0.0.1/12', '8.2.3.0/4')`, true},
{`ps.name = 'cmd.exe' and not ((net.sip in (222.1.1.1)) or (net.sip in (12.3.4.5)))`, true},
{`cidr_contains(net.dip, '216.58.201.1/24') = true`, true},
{`cidr_contains(net.dip, '226.58.201.1/24') = false`, true},
{`cidr_contains(net.dip, '216.58.201.1/24', '216.58.201.10/24') = true and evt.pid = 859`, true},
{`evt.name not in ('CreateProcess', 'Connect') and cidr_contains(net.dip, '216.58.201.1/24') = true`, true},
}
for i, tt := range tests {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt)
if matches != tt.matches {
t.Errorf("%d. %q net filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
evt1 := &event.Event{
Type: event.SendTCPv4,
Tid: 2484,
PID: 859,
PS: &pstypes.PS{
Name: "cmd.exe",
},
Category: event.Net,
Params: event.Params{
params.NetDport: {Name: params.NetDport, Type: params.Uint16, Value: uint16(53)},
params.NetSport: {Name: params.NetSport, Type: params.Uint16, Value: uint16(43123)},
params.NetSIP: {Name: params.NetSIP, Type: params.IPv4, Value: net.ParseIP("127.0.0.1")},
params.NetDIP: {Name: params.NetDIP, Type: params.IPv4, Value: net.ParseIP("8.8.8.8")},
},
}
var tests1 = []struct {
filter string
matches bool
}{
{`net.dip.names in ('dns.google.')`, true},
{`length(net.sip.names) > 0`, true},
}
for i, tt := range tests1 {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt1)
if matches != tt.matches {
t.Errorf("%d. %q net filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
}
func TestRegistryFilter(t *testing.T) {
evt := &event.Event{
Type: event.RegSetValue,
Tid: 2484,
PID: 859,
Category: event.Registry,
Params: event.Params{
params.RegPath: {Name: params.RegPath, Type: params.UnicodeString, Value: `HKEY_LOCAL_MACHINE\SYSTEM\Setup\Pid`},
params.RegData: {Name: params.RegData, Type: params.Uint32, Value: uint32(10234)},
params.RegValueType: {Name: params.RegValueType, Type: params.AnsiString, Value: "DWORD"},
params.NTStatus: {Name: params.NTStatus, Type: params.AnsiString, Value: "success"},
params.RegKeyHandle: {Name: params.RegKeyHandle, Type: params.Address, Value: uint64(18446666033449935464)},
},
}
var tests = []struct {
filter string
matches bool
}{
{`registry.status startswith ('key not', 'succ')`, true},
{`registry.path = 'HKEY_LOCAL_MACHINE\\SYSTEM\\Setup\\Pid'`, true},
{`registry.key.name icontains ('Setup', 'setup')`, true},
{`registry.value = 'Pid'`, true},
{`registry.value.type in ('DWORD', 'QWORD')`, true},
{`registry.data = '10234'`, true},
{`MD5(registry.path) = 'eab870b2a516206575d2ffa2b98d8af5'`, true},
}
for i, tt := range tests {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(evt)
if matches != tt.matches {
t.Errorf("%d. %q registry filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
}
func TestImageFilter(t *testing.T) {
e1 := &event.Event{
Type: event.LoadImage,
Category: event.Image,
Params: event.Params{
params.ImagePath: {Name: params.ImagePath, Type: params.UnicodeString, Value: filepath.Join(os.Getenv("windir"), "System32", "kernel32.dll")},
params.ProcessID: {Name: params.ProcessID, Type: params.PID, Value: uint32(1023)},
params.ImageCheckSum: {Name: params.ImageCheckSum, Type: params.Uint32, Value: uint32(2323432)},
params.ImageBase: {Name: params.ImageBase, Type: params.Address, Value: uint64(0x7ffb313833a3)},
params.ImageSignatureType: {Name: params.ImageSignatureType, Type: params.Enum, Value: uint32(1), Enum: signature.Types},
params.ImageSignatureLevel: {Name: params.ImageSignatureLevel, Type: params.Enum, Value: uint32(4), Enum: signature.Levels},
params.FileIsDotnet: {Name: params.FileIsDotnet, Type: params.Bool, Value: false},
},
}
var tests = []struct {
filter string
matches bool
}{
{`image.signature.type = 'EMBEDDED'`, true},
{`image.signature.level = 'AUTHENTICODE'`, true},
{`image.pid = 1023`, true},
{`image.path endswith 'System32\\kernel32.dll'`, true},
{`image.name = 'kernel32.dll'`, true},
{`image.checksum = 2323432`, true},
{`image.base.address = '7ffb313833a3'`, true},
{`image.cert.issuer icontains 'Microsoft Windows'`, true},
{`image.cert.subject icontains 'Microsoft Corporation'`, true},
{`image.is_dotnet`, false},
}
for i, tt := range tests {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {
t.Fatal(err)
}
matches := f.Run(e1)
if matches != tt.matches {
t.Errorf("%d. %q image filter mismatch: exp=%t got=%t", i, tt.filter, tt.matches, matches)
}
}
// check signatures expectations
sig := signature.GetSignatures().GetSignature(0x7ffb313833a3)
assert.NotNil(t, sig)
assert.Equal(t, filepath.Join(os.Getenv("windir"), "System32", "kernel32.dll"), sig.Filename)
assert.Equal(t, signature.Embedded, sig.Type)
assert.Equal(t, signature.AuthenticodeLevel, sig.Level)
// now exercise unsigned/unchecked signature
e2 := &event.Event{
Type: event.LoadImage,
Category: event.Image,
Params: event.Params{
params.ImagePath: {Name: params.ImagePath, Type: params.UnicodeString, Value: filepath.Join(os.Getenv("windir"), "System32", "kernel32.dll")},
params.ProcessID: {Name: params.ProcessID, Type: params.PID, Value: uint32(1023)},
params.ImageCheckSum: {Name: params.ImageCheckSum, Type: params.Uint32, Value: uint32(2323432)},
params.ImageBase: {Name: params.ImageBase, Type: params.Address, Value: uint64(0x7ccb313833a3)},
params.ImageSignatureType: {Name: params.ImageSignatureType, Type: params.Enum, Value: uint32(0), Enum: signature.Types},
params.ImageSignatureLevel: {Name: params.ImageSignatureLevel, Type: params.Enum, Value: uint32(0), Enum: signature.Levels},
params.FileIsDotnet: {Name: params.FileIsDotnet, Type: params.Bool, Value: false},
},
}
var tests1 = []struct {
filter string
matches bool
}{
{`image.signature.type = 'EMBEDDED'`, true},
{`image.signature.level = 'AUTHENTICODE'`, true},
{`image.pid = 1023`, true},
{`image.name endswith 'kernel32.dll'`, true},
{`image.checksum = 2323432`, true},
{`image.base.address = '7ccb313833a3'`, true},
{`image.cert.issuer icontains 'Microsoft Windows'`, true},
{`image.cert.subject icontains 'Microsoft Corporation'`, true},
}
for i, tt := range tests1 {
f := New(tt.filter, cfg)
err := f.Compile()
if err != nil {