-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_test.go
More file actions
384 lines (354 loc) · 13.1 KB
/
Copy pathfuzz_test.go
File metadata and controls
384 lines (354 loc) · 13.1 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
package leql
import (
"strings"
"testing"
)
func FuzzLEQLParser(f *testing.F) {
seeds := []string{
// --- Basic comparison operators ---
`where(field=value)`,
`where(field!=value)`,
`where(field>10)`,
`where(field>=10)`,
`where(field<10)`,
`where(field<=10)`,
// --- Numeric field names (the grammar supports NUMBER in fieldName) ---
`where(1=1)`,
`where(3=3)`,
`where(0=false)`,
`where(status=200 AND 1=1)`,
`where(1=1 AND 3=3 AND field=value)`,
`where(1=1 OR 2=2)`,
`where(NOT 1=1)`,
`where(42>10)`,
// --- String operators ---
`where(field CONTAINS error)`,
`where(field ICONTAINS error)`,
`where(field STARTS-WITH prefix)`,
`where(field ISTARTS-WITH prefix)`,
`where(message CONTAINS "critical error")`,
`where(path STARTS-WITH "/usr/local/bin")`,
`where(host ICONTAINS "PROD")`,
`where(log ISTARTS-WITH "WARN")`,
// --- Set operators ---
`where(field IN [a, b, c])`,
`where(field IIN ["A", "B"])`,
`where(status IN [200, 301, 404, 500])`,
`where(user IIN ["Admin", "root", "system"])`,
`where(field NOT IN [a, b])`,
`where(NOT field IN [x, y, z])`,
// --- List string operators ---
`where(field CONTAINS-ANY ["error", "fail"])`,
`where(field ICONTAINS-ANY ["error", "fail"])`,
`where(field CONTAINS-ALL ["error", "critical"])`,
`where(field ICONTAINS-ALL ["error", "critical"])`,
`where(field STARTS-WITH-ANY ["/var", "/tmp"])`,
`where(field ISTARTS-WITH-ANY ["/var", "/tmp"])`,
`where(message CONTAINS-ANY ["login", "logout", "timeout"])`,
`where(path STARTS-WITH-ANY ["/etc", "/usr", "/var", "/tmp"])`,
`where(log ICONTAINS-ALL ["error", "database", "timeout"])`,
// --- Boolean logic ---
`where(a=1 AND b=2)`,
`where(a=1 OR b=2)`,
`where(NOT field=value)`,
`where((a=1 OR b=2) AND c=3)`,
`where(a=1 AND b=2 AND c=3)`,
`where(a=1 OR b=2 OR c=3)`,
`where((a=1 AND b=2) OR (c=3 AND d=4))`,
`where(NOT (a=1 OR b=2))`,
`where(a=1 AND NOT b=2)`,
`where(NOT a=1 AND NOT b=2)`,
`where((NOT a=1) OR b=2)`,
`where(a=1 AND (b=2 OR c=3) AND d=4)`,
// --- Implicit AND (space-separated) ---
`where(a=1 b=2)`,
`where(a=1 b=2 c=3)`,
`where(status=200 user=admin)`,
// --- Complex boolean logic ---
`where((status=200 OR status=301) AND user!=anonymous AND NOT path STARTS-WITH "/health")`,
`where(severity="HIGH" AND (action=block OR action=drop) AND NOT source_ip=IP(10.0.0.0/8))`,
// --- Nested where clauses ---
`where(where(field=value))`,
`where(where(a=1 AND b=2))`,
`where(where(where(deep=nested)))`,
`where(where(status=200) AND user=admin)`,
`where(where("access_types" = "ReadData"))`,
`where(where("geoip_country_code" NOT IN ["PL", "UK"] and "result" = "SUCCESS"))`,
// --- NOCASE wrapper ---
`where(field=NOCASE(value))`,
`where(user=NOCASE(admin))`,
`where(hostname=NOCASE("web-server-01"))`,
`where(status=NOCASE(success))`,
// --- IP/CIDR matching ---
`where(ip=IP(10.0.0.0/8))`,
`where(source_address=IP(192.168.1.0/24))`,
`where(destination_ip=IP(172.16.0.0/12))`,
`where(source_address=IP(10.0.0.1))`,
// --- Regex patterns ---
`where(/regex/i)`,
`where(field=/pattern/)`,
`where(/error|warning|critical/i)`,
`where(field=/^admin.*/)`,
`where(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/)`,
`where(user=/^(admin|root|system)$/)`,
`where(/powershell|cmd\.exe|wscript/i)`,
// --- Dotted field names ---
`where(source_json.Workload=SharePoint)`,
`where(process.name=powershell.exe)`,
`where(source_json.Operation=FileUploaded)`,
`where(event.category=authentication)`,
`where(source_json.ClientIP STARTS-WITH "10.")`,
`where(source_json.properties.status.errorCode!=0)`,
`where("env_vars.7.parent_val" = "C:\ProgramData")`,
// --- Quoted field names ---
`where("source_address" = "111.161.118.40")`,
`where("geoip_country_code" != "US")`,
`where("result" = "SUCCESS")`,
// --- Keyword search ---
`where("Amazon Boardman")`,
`where('single quoted keyword')`,
// --- Multiple clauses ---
`groupby(field) calculate(count)`,
`groupby(user, status) calculate(count)`,
`where(x=1) groupby(y) calculate(sum:z) sort(desc) limit(10)`,
`where(result=FAILED) groupby(user) having(count>5)`,
`calculate(count) timeslice(30m)`,
`calculate(count) timeslice(1h)`,
`select(hostname, process.name) where(status=200)`,
`where(status>=300) groupby(status) calculate(count) sort(desc) limit(350)`,
`where(severity="HIGH") groupby(asset) calculate(unique:signature)`,
`where(direction=OUTBOUND) groupby(destination_address) calculate(sum:outgoing_bytes)`,
`where(connection_status=DENY) groupby(source_address)`,
`where(geoip_country_code!="US") groupby(geoip_country_name)`,
`where(result=SUCCESS) groupby(service, geoip_country_name, user_agent)`,
`where(status>=400) groupby(url) calculate(count) sort(desc) limit(50)`,
`where(action=ALLOW) groupby(source_address, destination_port) calculate(count) sort(desc) limit(100)`,
`where(result=FAILED) groupby(user) calculate(count) having(count>10) sort(desc) limit(20)`,
`calculate(bytes) timeslice(5m)`,
`groupby(user) calculate(unique:source_address)`,
`where(severity IN ["HIGH", "CRITICAL"]) groupby(signature) calculate(count) sort(desc)`,
// --- Real InsightIDR detection patterns ---
`where(source_json.Workload=SharePoint AND source_json.Operation=FileUploaded) groupby(source_json.UserId) calculate(count)`,
`where(geoip_country_code!="US" AND result=SUCCESS) groupby(user, geoip_country_name, service)`,
`where(connection_status=DENY AND destination_port=445) groupby(source_address) calculate(count) sort(desc)`,
`where(process.name CONTAINS-ANY ["powershell.exe", "cmd.exe", "wscript.exe", "cscript.exe"])`,
`where(source_json.Operation IN ["Add member to role.", "Add member to group."] AND source_json.Workload=AzureActiveDirectory)`,
`where(result=FAILED) groupby(user) calculate(count) having(count>5) sort(desc) limit(10)`,
`where(NOT geoip_country_code IN ["US", "CA", "GB"] AND result=SUCCESS AND service=sslvpn)`,
`where(destination_port IN [4444, 5555, 6666, 8080, 8443]) groupby(source_address, destination_address)`,
`where(source_json.properties.status.errorCode!=0 AND source_json.category=SignInLogs)`,
`where(EventCode=4688 AND process.name ICONTAINS "mimikatz")`,
`where(EventCode=4625) groupby(user) calculate(count) having(count>10)`,
`where(source_json.Operation CONTAINS-ANY ["MailItemsAccessed", "Send", "SearchQueryInitiatedExchange"])`,
// --- Postfix negation ---
`where(field NOT IN [a, b, c])`,
`where(user NOT CONTAINS admin)`,
`where(status NOT IN [200, 301])`,
`where(path NOT STARTS-WITH "/health")`,
// --- Edge cases ---
``,
`where()`,
`groupby()`,
`calculate()`,
`where(=)`,
`where(field=)`,
`where(=value)`,
`where(field field)`,
`where(field=value AND)`,
`where(AND field=value)`,
`where(OR)`,
`where(NOT)`,
`where(NOT NOT field=value)`,
`where((()))`,
`where(field IN [])`,
`where(field CONTAINS "")`,
`where(field="")`,
`where( field = value )`,
`sort(asc)`,
`sort(desc)`,
`limit(0)`,
`limit(999999)`,
`timeslice(1s)`,
`timeslice(24h)`,
`from(logsource) where(field=value)`,
`calculate(average:response_time)`,
`calculate(min:latency)`,
`calculate(max:bytes)`,
`calculate(standarddeviation:response_time)`,
`calculate(sd:latency)`,
`calculate(unique:user)`,
// --- Variable conditions ---
`where($variable=value)`,
// --- Triple-quoted strings ---
`where(field="""triple quoted""")`,
`where(field='''triple single''')`,
// --- Strict equality ---
`where(field==value)`,
`where(field!==value)`,
// --- ALL() field list ---
`where(ALL(field1, field2)=value)`,
`where(ALL(a, b, c) IN [x, y])`,
// --- Multi-field conditions ---
`where(field1, field2 = value)`,
`where(src_ip, dst_ip = "10.0.0.1")`,
}
for _, s := range seeds {
f.Add(s)
}
f.Fuzz(func(t *testing.T, query string) {
// Must not panic -- that's the only requirement
result := ExtractConditions(query)
_ = result
})
}
// TestConditionCompleteness verifies that ExtractConditions extracts all expected
// field names from a variety of LEQL query patterns. This is a deterministic test
// that catches regressions in field extraction across all supported syntax forms.
func TestConditionCompleteness(t *testing.T) {
tests := []struct {
name string
query string
expectedFields []string
}{
{
name: "simple equality",
query: `where(status=200)`,
expectedFields: []string{"status"},
},
{
name: "numeric field name standalone",
query: `where(1=1)`,
expectedFields: []string{"1"},
},
{
name: "numeric field names with surrounding conditions",
query: `where(status=200 AND 1=1 AND user=admin)`,
expectedFields: []string{"status", "1", "user"},
},
{
name: "multiple AND conditions",
query: `where(action=block AND severity=HIGH AND protocol=TCP)`,
expectedFields: []string{"action", "severity", "protocol"},
},
{
name: "multiple OR conditions",
query: `where(status=200 OR status=301 OR status=404)`,
expectedFields: []string{"status"},
},
{
name: "mixed AND/OR conditions",
query: `where((status=200 OR status=404) AND user=admin)`,
expectedFields: []string{"status", "user"},
},
{
name: "NOT conditions",
query: `where(NOT status=404 AND user=admin)`,
expectedFields: []string{"status", "user"},
},
{
name: "CONTAINS operator",
query: `where(message CONTAINS error AND severity=HIGH)`,
expectedFields: []string{"message", "severity"},
},
{
name: "STARTS-WITH operator",
query: `where(path STARTS-WITH "/var" AND host=webserver)`,
expectedFields: []string{"path", "host"},
},
{
name: "ICONTAINS operator",
query: `where(user ICONTAINS admin)`,
expectedFields: []string{"user"},
},
{
name: "IN set operator",
query: `where(status IN [200, 301, 404])`,
expectedFields: []string{"status"},
},
{
name: "IIN set operator",
query: `where(user IIN ["Admin", "root"])`,
expectedFields: []string{"user"},
},
{
name: "CONTAINS-ANY list operator",
query: `where(message CONTAINS-ANY ["error", "fail", "critical"])`,
expectedFields: []string{"message"},
},
{
name: "CONTAINS-ALL list operator",
query: `where(log CONTAINS-ALL ["error", "database"])`,
expectedFields: []string{"log"},
},
{
name: "NOCASE wrapper",
query: `where(hostname=NOCASE(webserver) AND status=200)`,
expectedFields: []string{"hostname", "status"},
},
{
name: "IP CIDR matching",
query: `where(source_address=IP(10.0.0.0/8) AND destination_port=443)`,
expectedFields: []string{"source_address", "destination_port"},
},
{
name: "dotted field names",
query: `where(source_json.Workload=SharePoint AND source_json.Operation=FileUploaded)`,
expectedFields: []string{"source_json.Workload", "source_json.Operation"},
},
{
name: "regex field condition",
query: `where(field=/pattern/ AND status=200)`,
expectedFields: []string{"field", "status"},
},
{
name: "implicit AND (space-separated)",
query: `where(status=200 user=admin)`,
expectedFields: []string{"status", "user"},
},
{
name: "postfix NOT IN",
query: `where(geoip_country_code NOT IN ["US", "CA"] AND result=SUCCESS)`,
expectedFields: []string{"geoip_country_code", "result"},
},
{
name: "real InsightIDR brute force detection",
query: `where(result=FAILED) groupby(user) calculate(count)`,
expectedFields: []string{"result"},
},
{
name: "real InsightIDR geo anomaly",
query: `where(geoip_country_code!="US" AND result=SUCCESS AND service=sslvpn)`,
expectedFields: []string{"geoip_country_code", "result", "service"},
},
{
name: "real InsightIDR process monitoring",
query: `where(process.name CONTAINS-ANY ["powershell.exe", "cmd.exe", "wscript.exe"])`,
expectedFields: []string{"process.name"},
},
{
name: "complex nested boolean with NOT and OR",
query: `where((action=block OR action=drop) AND NOT source_address=IP(10.0.0.0/8) AND severity=HIGH)`,
expectedFields: []string{"action", "source_address", "severity"},
},
{
name: "quoted field names",
query: `where("source_address" = "10.0.0.1" AND "result" = "SUCCESS")`,
expectedFields: []string{"source_address", "result"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ExtractConditions(tt.query)
extractedFields := make(map[string]bool)
for _, c := range result.Conditions {
extractedFields[strings.ToLower(c.Field)] = true
}
for _, expected := range tt.expectedFields {
if !extractedFields[strings.ToLower(expected)] {
t.Errorf("field %q not extracted\n query: %s\n extracted: %v\n errors: %v",
expected, tt.query, extractedFields, result.Errors)
}
}
})
}
}