-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifiers_test.go
More file actions
282 lines (256 loc) · 8.11 KB
/
Copy pathmodifiers_test.go
File metadata and controls
282 lines (256 loc) · 8.11 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
package sigma
import (
"encoding/base64"
"strings"
"testing"
)
func TestParseModifiers_NoModifier(t *testing.T) {
field, result := parseModifiers("CommandLine", []string{"test.exe"})
if field != "CommandLine" {
t.Errorf("expected field 'CommandLine', got %q", field)
}
if result.operator != "=" {
t.Errorf("expected operator '=', got %q", result.operator)
}
if result.allOf {
t.Error("expected allOf=false")
}
}
func TestParseModifiers_Contains(t *testing.T) {
field, result := parseModifiers("CommandLine|contains", []string{"mimikatz"})
if field != "CommandLine" {
t.Errorf("expected field 'CommandLine', got %q", field)
}
if result.operator != "contains" {
t.Errorf("expected operator 'contains', got %q", result.operator)
}
}
func TestParseModifiers_StartsWith(t *testing.T) {
_, result := parseModifiers("Image|startswith", []string{`C:\Windows\`})
if result.operator != "startswith" {
t.Errorf("expected operator 'startswith', got %q", result.operator)
}
}
func TestParseModifiers_EndsWith(t *testing.T) {
_, result := parseModifiers("Image|endswith", []string{".exe"})
if result.operator != "endswith" {
t.Errorf("expected operator 'endswith', got %q", result.operator)
}
}
func TestParseModifiers_Regex(t *testing.T) {
_, result := parseModifiers("CommandLine|re", []string{`.*mimikatz.*`})
if result.operator != "matches" {
t.Errorf("expected operator 'matches', got %q", result.operator)
}
}
func TestParseModifiers_CIDR(t *testing.T) {
_, result := parseModifiers("DestinationIp|cidr", []string{"10.0.0.0/8"})
if result.operator != "cidrmatch" {
t.Errorf("expected operator 'cidrmatch', got %q", result.operator)
}
}
func TestParseModifiers_Comparison(t *testing.T) {
tests := []struct {
mod string
op string
}{
{"gt", ">"},
{"gte", ">="},
{"lt", "<"},
{"lte", "<="},
}
for _, tt := range tests {
_, result := parseModifiers("EventID|"+tt.mod, []string{"10"})
if result.operator != tt.op {
t.Errorf("modifier %q: expected operator %q, got %q", tt.mod, tt.op, result.operator)
}
}
}
func TestParseModifiers_Exists(t *testing.T) {
_, result := parseModifiers("FieldName|exists", []string{"true"})
if result.operator != "exists" {
t.Errorf("expected operator 'exists', got %q", result.operator)
}
}
func TestParseModifiers_FieldRef(t *testing.T) {
_, result := parseModifiers("SubjectUserName|fieldref", []string{"TargetUserName"})
if result.operator != "fieldref" {
t.Errorf("expected operator 'fieldref', got %q", result.operator)
}
}
func TestParseModifiers_All(t *testing.T) {
_, result := parseModifiers("CommandLine|contains|all", []string{"-nop", "-w hidden"})
if result.operator != "contains" {
t.Errorf("expected operator 'contains', got %q", result.operator)
}
if !result.allOf {
t.Error("expected allOf=true")
}
}
func TestParseModifiers_Base64(t *testing.T) {
_, result := parseModifiers("CommandLine|base64", []string{"test"})
encoded := base64.StdEncoding.EncodeToString([]byte("test"))
found := false
for _, v := range result.values {
if v == encoded {
found = true
break
}
}
if !found {
t.Errorf("expected base64 encoded value %q in %v", encoded, result.values)
}
}
func TestParseModifiers_Base64Offset(t *testing.T) {
_, result := parseModifiers("CommandLine|base64offset", []string{"test"})
// Should have original + 3 offset variants
if len(result.values) < 4 {
t.Errorf("expected at least 4 values for base64offset, got %d: %v", len(result.values), result.values)
}
}
func TestParseModifiers_Wide(t *testing.T) {
_, result := parseModifiers("CommandLine|wide", []string{"test"})
if len(result.values) < 2 {
t.Errorf("expected at least 2 values for wide, got %d", len(result.values))
}
// Should contain the original value
if result.values[0] != "test" {
t.Errorf("expected first value 'test', got %q", result.values[0])
}
}
func TestParseModifiers_Windash(t *testing.T) {
_, result := parseModifiers("CommandLine|windash", []string{"-exec"})
found := false
for _, v := range result.values {
if v == "/exec" {
found = true
break
}
}
if !found {
t.Errorf("expected '/exec' variant in %v", result.values)
}
}
func TestParseModifiers_WindashSlash(t *testing.T) {
_, result := parseModifiers("CommandLine|windash", []string{"/exec"})
found := false
for _, v := range result.values {
if v == "-exec" {
found = true
break
}
}
if !found {
t.Errorf("expected '-exec' variant in %v", result.values)
}
}
func TestParseModifiers_ContainsAll(t *testing.T) {
_, result := parseModifiers("CommandLine|contains|all", []string{"a", "b", "c"})
if result.operator != "contains" {
t.Errorf("expected 'contains', got %q", result.operator)
}
if !result.allOf {
t.Error("expected allOf=true")
}
if len(result.values) != 3 {
t.Errorf("expected 3 values, got %d", len(result.values))
}
}
func TestParseModifiers_CaseInsensitive(t *testing.T) {
_, result := parseModifiers("Field|CONTAINS|ALL", []string{"test"})
if result.operator != "contains" {
t.Errorf("expected 'contains', got %q", result.operator)
}
if !result.allOf {
t.Error("expected allOf=true")
}
}
func TestParseModifiers_UTF16LE(t *testing.T) {
_, result := parseModifiers("Field|utf16le", []string{"A"})
if len(result.values) < 2 {
t.Errorf("expected at least 2 values, got %d", len(result.values))
}
// UTF16LE of "A" is 0x41 0x00
utf16Val := result.values[1]
if len(utf16Val) != 2 || utf16Val[0] != 0x41 || utf16Val[1] != 0x00 {
t.Errorf("expected UTF-16LE encoding of 'A', got %v", []byte(utf16Val))
}
}
func TestParseModifiers_UTF16BE(t *testing.T) {
_, result := parseModifiers("Field|utf16be", []string{"A"})
if len(result.values) < 2 {
t.Errorf("expected at least 2 values, got %d", len(result.values))
}
// UTF16BE of "A" is 0x00 0x41
utf16Val := result.values[1]
if len(utf16Val) != 2 || utf16Val[0] != 0x00 || utf16Val[1] != 0x41 {
t.Errorf("expected UTF-16BE encoding of 'A', got %v", []byte(utf16Val))
}
}
func TestParseModifiers_Expand(t *testing.T) {
// expand modifier passes through values unchanged
_, result := parseModifiers("CommandLine|expand", []string{"%APPDATA%\\test"})
if len(result.values) != 1 || result.values[0] != "%APPDATA%\\test" {
t.Errorf("expected value pass-through, got %v", result.values)
}
}
func TestParseModifiers_ChainedModifiers(t *testing.T) {
// base64 + contains
_, result := parseModifiers("CommandLine|base64|contains", []string{"test"})
if result.operator != "contains" {
t.Errorf("expected 'contains', got %q", result.operator)
}
// Should have original + base64 encoded
foundEncoded := false
for _, v := range result.values {
if strings.Contains(v, "=") || len(v) > len("test") {
foundEncoded = true
break
}
}
if !foundEncoded {
t.Log("Note: base64+contains chain produced:", result.values)
}
}
func TestParseModifiers_Cased(t *testing.T) {
field, result := parseModifiers("FieldName|cased", []string{"CasedValue"})
if field != "FieldName" {
t.Errorf("expected field 'FieldName', got %q", field)
}
if result.operator != "=" {
t.Errorf("expected operator '=', got %q", result.operator)
}
if !result.caseSensitive {
t.Error("expected caseSensitive=true for |cased modifier")
}
}
func TestParseModifiers_ContainsCased(t *testing.T) {
field, result := parseModifiers("CommandLine|contains|cased", []string{"Mimikatz"})
if field != "CommandLine" {
t.Errorf("expected field 'CommandLine', got %q", field)
}
if result.operator != "contains" {
t.Errorf("expected operator 'contains', got %q", result.operator)
}
if !result.caseSensitive {
t.Error("expected caseSensitive=true for |contains|cased chain")
}
}
func TestParseModifiers_CasedAll(t *testing.T) {
_, result := parseModifiers("Image|endswith|cased|all", []string{"cmd.exe", "powershell.exe"})
if result.operator != "endswith" {
t.Errorf("expected operator 'endswith', got %q", result.operator)
}
if !result.caseSensitive {
t.Error("expected caseSensitive=true")
}
if !result.allOf {
t.Error("expected allOf=true")
}
}
func TestParseModifiers_NoCased(t *testing.T) {
_, result := parseModifiers("CommandLine|contains", []string{"test"})
if result.caseSensitive {
t.Error("expected caseSensitive=false when |cased not present")
}
}