-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
ported_test.go
432 lines (408 loc) · 19.8 KB
/
ported_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
package yara
import (
"fmt"
"testing"
)
// This file contains tests that were ported from yara/yara-python/tests.py
var pe32file = []byte{
0x4d, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x50, 0x45, 0x00, 0x00, 0x4c, 0x01, 0x01, 0x00,
0x5d, 0xbe, 0x45, 0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xe0, 0x00, 0x03, 0x01, 0x0b, 0x01, 0x08, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x01, 0x00, 0x00,
0x60, 0x01, 0x00, 0x00, 0x64, 0x01, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x64, 0x01, 0x00, 0x00, 0x60, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0x00, 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x2e, 0x74, 0x65, 0x78, 0x74, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
0x60, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x60, 0x01, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0x00, 0x00, 0x60, 0x6a, 0x2a, 0x58, 0xc3,
}
var elf32file = []byte{
0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00,
0x60, 0x80, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00, 0xa8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x20, 0x00, 0x01, 0x00, 0x28, 0x00,
0x04, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x80, 0x04, 0x08, 0x00, 0x80, 0x04, 0x08, 0x6c, 0x00, 0x00, 0x00,
0x6c, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xb8, 0x01, 0x00, 0x00, 0x00, 0xbb, 0x2a, 0x00, 0x00, 0x00, 0xcd, 0x80,
0x00, 0x54, 0x68, 0x65, 0x20, 0x4e, 0x65, 0x74, 0x77, 0x69, 0x64, 0x65,
0x20, 0x41, 0x73, 0x73, 0x65, 0x6d, 0x62, 0x6c, 0x65, 0x72, 0x20, 0x32,
0x2e, 0x30, 0x35, 0x2e, 0x30, 0x31, 0x00, 0x00, 0x2e, 0x73, 0x68, 0x73,
0x74, 0x72, 0x74, 0x61, 0x62, 0x00, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x00,
0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00, 0x60, 0x80, 0x04, 0x08, 0x60, 0x00, 0x00, 0x00,
0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x6c, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x8b, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
}
var elf64file = []byte{
0x7f, 0x45, 0x4c, 0x46, 0x02, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x3e, 0x00, 0x01, 0x00, 0x00, 0x00,
0x80, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xc8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x38, 0x00, 0x01, 0x00, 0x40, 0x00,
0x04, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x8c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xb8, 0x01, 0x00, 0x00,
0x00, 0xbb, 0x2a, 0x00, 0x00, 0x00, 0xcd, 0x80, 0x00, 0x54, 0x68, 0x65,
0x20, 0x4e, 0x65, 0x74, 0x77, 0x69, 0x64, 0x65, 0x20, 0x41, 0x73, 0x73,
0x65, 0x6d, 0x62, 0x6c, 0x65, 0x72, 0x20, 0x32, 0x2e, 0x30, 0x35, 0x2e,
0x30, 0x31, 0x00, 0x00, 0x2e, 0x73, 0x68, 0x73, 0x74, 0x72, 0x74, 0x61,
0x62, 0x00, 0x2e, 0x74, 0x65, 0x78, 0x74, 0x00, 0x2e, 0x63, 0x6f, 0x6d,
0x6d, 0x65, 0x6e, 0x74, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0b, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xab, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
}
func TestBooleanOperators(t *testing.T) {
assertTrueRules(t, []string{
"rule test { condition: true }",
"rule test { condition: true or false }",
"rule test { condition: true and true }",
"rule test { condition: 0x1 and 0x2}",
}, []byte("dummy"))
assertFalseRules(t, []string{
"rule test { condition: false }",
"rule test { condition: true and false }",
"rule test { condition: false or false }",
}, []byte("dummy"))
}
func TestComparisonOperators(t *testing.T) {
assertTrueRules(t, []string{
"rule test { condition: 2 > 1 }",
"rule test { condition: 1 < 2 }",
"rule test { condition: 2 >= 1 }",
"rule test { condition: 1 <= 1 }",
"rule test { condition: 1 == 1 }",
"rule test { condition: 1.5 == 1.5}",
"rule test { condition: 1.0 == 1}",
"rule test { condition: 1.5 >= 1.0}",
"rule test { condition: 1.5 >= 1}",
"rule test { condition: 1.0 >= 1}",
"rule test { condition: 0.5 < 1}",
"rule test { condition: 0.5 <= 1}",
"rule rest { condition: 1.0 <= 1}",
"rule rest { condition: \"abc\" == \"abc\"}",
"rule rest { condition: \"abc\" <= \"abc\"}",
"rule rest { condition: \"abc\" >= \"abc\"}",
"rule rest { condition: \"ab\" < \"abc\"}",
"rule rest { condition: \"abc\" > \"ab\"}",
"rule rest { condition: \"abc\" < \"abd\"}",
"rule rest { condition: \"abd\" > \"abc\"}",
}, []byte("dummy"))
assertFalseRules(t, []string{
"rule test { condition: 1 != 1}",
"rule test { condition: 2 > 3}",
"rule test { condition: 2 > 3}",
"rule test { condition: 2.1 < 2}",
"rule test { condition: \"abc\" != \"abc\"}",
"rule test { condition: \"abc\" > \"abc\"}",
"rule test { condition: \"abc\" < \"abc\"}",
}, []byte("dummy"))
}
func TestArithmeticOperators(t *testing.T) {
assertTrueRules(t, []string{
"rule test { condition: (1 + 1) * 2 == (9 - 1) \\ 2 }",
"rule test { condition: 5 % 2 == 1 }",
"rule test { condition: 1.5 + 1.5 == 3}",
"rule test { condition: 3 \\ 2 == 1}",
"rule test { condition: 3.0 \\ 2 == 1.5}",
"rule test { condition: 1 + -1 == 0}",
"rule test { condition: -1 + -1 == -2}",
"rule test { condition: 4 --2 * 2 == 8}",
"rule test { condition: -1.0 * 1 == -1.0}",
"rule test { condition: 1-1 == 0}",
"rule test { condition: -2.0-3.0 == -5}",
"rule test { condition: --1 == 1}",
"rule test { condition: 1--1 == 2}",
"rule test { condition: -0x01 == -1}",
}, []byte("dummy"))
}
func TestBitwiseOperators(t *testing.T) {
assertTrueRules(t, []string{
"rule test { condition: 0x55 | 0xAA == 0xFF }",
"rule test { condition: ~0xAA ^ 0x5A & 0xFF == (~0xAA) ^ (0x5A & 0xFF) }",
"rule test { condition: ~0x55 & 0xFF == 0xAA }",
"rule test { condition: 8 >> 2 == 2 }",
"rule test { condition: 1 << 3 == 8 }",
"rule test { condition: 1 | 3 ^ 3 == 1 | (3 ^ 3) }",
}, []byte("dummy"))
assertFalseRules(t, []string{
"rule test { condition: ~0xAA ^ 0x5A & 0xFF == 0x0F }",
"rule test { condition: 1 | 3 ^ 3 == (1 | 3) ^ 3}",
}, []byte("dummy"))
}
func TestStrings(t *testing.T) {
assertTrueRules(t, []string{
"rule test { strings: $a = \"a\" condition: $a }",
"rule test { strings: $a = \"ab\" condition: $a }",
"rule test { strings: $a = \"abc\" condition: $a }",
"rule test { strings: $a = \"xyz\" condition: $a }",
"rule test { strings: $a = \"abc\" nocase fullword condition: $a }",
"rule test { strings: $a = \"aBc\" nocase condition: $a }",
"rule test { strings: $a = \"abc\" fullword condition: $a }",
}, []byte("---- abc ---- xyz"))
assertFalseRules(t, []string{
"rule test { strings: $a = \"a\" fullword condition: $a }",
"rule test { strings: $a = \"ab\" fullword condition: $a }",
"rule test { strings: $a = \"abc\" wide fullword condition: $a }",
}, []byte("---- abc ---- xyz"))
assertTrueRules(t, []string{
"rule test { strings: $a = \"a\" wide condition: $a }",
"rule test { strings: $a = \"a\" wide ascii condition: $a }",
"rule test { strings: $a = \"ab\" wide condition: $a }",
"rule test { strings: $a = \"ab\" wide ascii condition: $a }",
"rule test { strings: $a = \"abc\" wide condition: $a }",
"rule test { strings: $a = \"abc\" wide nocase fullword condition: $a }",
"rule test { strings: $a = \"aBc\" wide nocase condition: $a }",
"rule test { strings: $a = \"aBc\" wide ascii nocase condition: $a }",
"rule test { strings: $a = \"---xyz\" wide nocase condition: $a }",
}, []byte("---- a\x00b\x00c\x00 -\x00-\x00-\x00-\x00x\x00y\x00z\x00"))
assertTrueRules(t, []string{
"rule test { strings: $a = \"abc\" fullword condition: $a }",
}, []byte("abc"))
assertFalseRules(t, []string{
"rule test { strings: $a = \"abc\" fullword condition: $a }",
}, []byte("xabcx"))
assertFalseRules(t, []string{
"rule test { strings: $a = \"abc\" fullword condition: $a }",
}, []byte("xabc"))
assertFalseRules(t, []string{
"rule test { strings: $a = \"abc\" fullword condition: $a }",
}, []byte("abcx"))
assertFalseRules(t, []string{
"rule test { strings: $a = \"abc\" ascii wide fullword condition: $a }",
}, []byte("abcx"))
assertTrueRules(t, []string{
"rule test { strings: $a = \"abc\" ascii wide fullword condition: $a }",
}, []byte("a\x00abc"))
assertTrueRules(t, []string{
"rule test { strings: $a = \"abc\" wide fullword condition: $a }",
}, []byte("a\x00b\x00c\x00"))
assertFalseRules(t, []string{
"rule test { strings: $a = \"abc\" wide fullword condition: $a }",
}, []byte("x\x00a\x00b\x00c\x00x\x00"))
assertFalseRules(t, []string{
"rule test { strings: $a = \"ab\" wide fullword condition: $a }",
}, []byte("x\x00a\x00b\x00"))
assertFalseRules(t, []string{
"rule test { strings: $a = \"abc\" wide fullword condition: $a }",
}, []byte("x\x00a\x00b\x00c\x00"))
assertTrueRules(t, []string{
"rule test { strings: $a = \"abc\" wide fullword condition: $a }",
}, []byte("x\x01a\x00b\x00c\x00"))
assertTrueRules(t, []string{"" +
"rule test {\n" +
" strings:\n" +
" $a = \"abcdef\"\n" +
" $b = \"cdef\"\n" +
" $c = \"ef\"\n" +
" condition:\n" +
" all of them\n" +
"}",
}, []byte("abcdef"))
}
func TestWildcardStrings(t *testing.T) {
assertTrueRules(t, []string{"" +
"rule test {\n" +
" strings:\n" +
" $s1 = \"abc\"\n" +
" $s2 = \"xyz\"\n" +
" condition:\n" +
" for all of ($*) : ($)\n" +
"}",
}, []byte("---- abc ---- A\x00B\x00C\x00 ---- xyz"))
}
func TestHexStrings(t *testing.T) {
assertTrueRules(t, []string{
"rule test { strings: $a = { 64 01 00 00 60 01 } condition: $a }",
"rule test { strings: $a = { 64 0? 00 00 ?0 01 } condition: $a }",
"rule test { strings: $a = { 64 01 [1-3] 60 01 } condition: $a }",
"rule test { strings: $a = { 64 01 [1-3] (60|61) 01 } condition: $a }",
"rule test { strings: $a = { 4D 5A [-] 6A 2A [-] 58 C3} condition: $a }",
"rule test { strings: $a = { 4D 5A [300-] 6A 2A [-] 58 C3} condition: $a }",
}, pe32file)
assertFalseRules(t, []string{
"rule test { strings: $a = { 4D 5A [0-300] 6A 2A } condition: $a }",
}, pe32file)
assertTrueRules(t, []string{
"rule test { strings: $a = { 31 32 [-] 38 39 } condition: $a }",
"rule test { strings: $a = { 31 32 [-] 33 34 [-] 38 39 } condition: $a }",
"rule test { strings: $a = { 31 32 [1] 34 35 [2] 38 39 } condition: $a }",
"rule test { strings: $a = { 31 32 [1-] 34 35 [1-] 38 39 } condition: $a }",
"rule test { strings: $a = { 31 32 [0-3] 34 35 [1-] 38 39 } condition: $a }",
}, []byte("123456789"))
assertTrueRules(t, []string{
"rule test { strings: $a = { 31 32 [-] 38 39 } condition: all of them }",
}, []byte("123456789"))
assertFalseRules(t, []string{
"rule test { strings: $a = { 31 32 [-] 32 33 } condition: $a }",
"rule test { strings: $a = { 35 36 [-] 31 32 } condition: $a }",
"rule test { strings: $a = { 31 32 [2-] 34 35 } condition: $a }",
"rule test { strings: $a = { 31 32 [0-3] 37 38 } condition: $a }",
}, []byte("123456789"))
rules := makeRules(t, "rule test { strings: $a = { 61 [0-3] (62|63) } condition: $a }")
var matches MatchRules
rules.ScanMem([]byte("abbb"), 0, 0, &matches)
if matches[0].Strings[0].Name != "$a" ||
matches[0].Strings[0].Offset != 0 ||
string(matches[0].Strings[0].Data) != "ab" {
t.Error("wrong match")
}
}
// TODO: TestCount
// TODO: TestAt
// TODO: TestOffset
// TODO: TestOf
// TODO: TestFor
// TODO: TestRE
func TestEntrypoint(t *testing.T) {
assertTrueRules(t, []string{
"rule test { strings: $a = { 6a 2a 58 c3 } condition: $a at entrypoint }",
}, pe32file)
assertTrueRules(t, []string{
"rule test { strings: $a = { b8 01 00 00 00 bb 2a } condition: $a at entrypoint }",
}, elf32file)
assertTrueRules(t, []string{
"rule test { strings: $a = { b8 01 00 00 00 bb 2a } condition: $a at entrypoint }",
}, elf64file)
assertFalseRules(t, []string{
"rule test { condition: entrypoint >= 0 }",
}, []byte("dummy"))
}
func TestFilesize(t *testing.T) {
assertTrueRules(t, []string{
fmt.Sprintf("rule test { condition: filesize == %d }", len(pe32file)),
}, pe32file)
}
// TODO: TestCompileFile
// TODO: TestCompileFiles
// TODO: TestIncludeFiles
type params map[string]interface{}
func TestExternals(t *testing.T) {
for _, sample := range []struct {
rule string
params
predicate bool
}{
{"rule test { condition: ext_int == 15 }", params{"ext_int": 15}, true},
{"rule test { condition: ext_int == -15}", params{"ext_int": -15}, true},
{"rule test { condition: ext_float == 3.14 }", params{"ext_float": 3.14}, true},
{"rule test { condition: ext_float == -0.5 }", params{"ext_float": -0.5}, true},
{"rule test { condition: ext_bool }", params{"ext_bool": true}, true},
{"rule test { condition: ext_str }", params{"ext_str": ""}, false},
{"rule test { condition: ext_str }", params{"ext_str": "foo"}, true},
{"rule test { condition: ext_bool }", params{"ext_bool": false}, false},
{"rule test { condition: ext_str contains \"ssi\" }", params{"ext_str": "mississippi"}, true},
{"rule test { condition: ext_str matches /foo/ }", params{"ext_str": ""}, false},
{"rule test { condition: ext_str matches /foo/ }", params{"ext_str": "FOO"}, false},
{"rule test { condition: ext_str matches /foo/i }", params{"ext_str": "FOO"}, true},
{"rule test { condition: ext_str matches /ssi(s|p)/ }", params{"ext_str": "mississippi"}, true},
{"rule test { condition: ext_str matches /ppi$/ }", params{"ext_str": "mississippi"}, true},
{"rule test { condition: ext_str matches /ssi$/ }", params{"ext_str": "mississippi"}, false},
{"rule test { condition: ext_str matches /^miss/ }", params{"ext_str": "mississippi"}, true},
{"rule test { condition: ext_str matches /^iss/ }", params{"ext_str": "mississippi"}, false},
{"rule test { condition: ext_str matches /ssi$/ }", params{"ext_str": "mississippi"}, false},
} {
r, err := Compile(sample.rule, sample.params)
if err != nil {
t.Errorf("rule=%s params=%+v: Compile error: %s", sample.rule, sample.params, err)
continue
}
var m MatchRules
r.ScanMem([]byte("dummy"), 0, 0, &m)
if sample.predicate != (len(m) > 0) {
t.Errorf("rule=%s params=%+v: expected %t, got %t",
sample.rule, sample.params, sample.predicate, (len(m) > 0))
}
}
}
// TODO: TestCallback
// TODO: TestCompare
// TODO: TestComments
func TestModules(t *testing.T) {
assertTrueRules(t, []string{
"import \"tests\" rule test { condition: tests.constants.one + 1 == tests.constants.two }",
"import \"tests\" rule test { condition: tests.constants.foo == \"foo\" }",
"import \"tests\" rule test { condition: tests.struct_array[1].i == 1 }",
"import \"tests\" rule test { condition: tests.struct_array[0].i == 1 or true}",
"import \"tests\" rule test { condition: tests.integer_array[0] == 0}",
"import \"tests\" rule test { condition: tests.integer_array[1] == 1}",
"import \"tests\" rule test { condition: tests.string_array[0] == \"foo\"}",
"import \"tests\" rule test { condition: tests.string_array[2] == \"baz\"}",
"import \"tests\" rule test { condition: tests.string_dict[\"foo\"] == \"foo\"}",
"import \"tests\" rule test { condition: tests.string_dict[\"bar\"] == \"bar\"}",
"import \"tests\" rule test { condition: tests.isum(1,2) == 3}",
"import \"tests\" rule test { condition: tests.isum(1,2,3) == 6}",
"import \"tests\" rule test { condition: tests.fsum(1.0,2.0) == 3.0}",
"import \"tests\" rule test { condition: tests.fsum(1.0,2.0,3.0) == 6.0}",
"import \"tests\" rule test { condition: tests.length(\"dummy\") == 5}",
}, []byte("dummy"))
assertFalseRules(t, []string{
"import \"tests\" rule test { condition: tests.struct_array[0].i == 1 }",
"import \"tests\" rule test { condition: tests.isum(1,1) == 3}",
"import \"tests\" rule test { condition: tests.fsum(1.0,1.0) == 3.0}",
}, []byte("dummy"))
}
func TestIntegerFunctions(t *testing.T) {
assertTrueRules(t, []string{
"rule test { condition: uint8(0) == 0xAA}",
"rule test { condition: uint16(0) == 0xBBAA}",
"rule test { condition: uint32(0) == 0xDDCCBBAA}",
"rule test { condition: uint8be(0) == 0xAA}",
"rule test { condition: uint16be(0) == 0xAABB}",
"rule test { condition: uint32be(0) == 0xAABBCCDD}",
}, []byte("\xAA\xBB\xCC\xDD"))
}