-
Notifications
You must be signed in to change notification settings - Fork 13
/
yara_test.go
355 lines (302 loc) · 8.44 KB
/
yara_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
package yapscan
import (
"errors"
"io"
"os"
"path/filepath"
"sort"
"strings"
"testing"
"time"
"github.com/yeka/zip"
"github.com/hillu/go-yara/v4"
. "github.com/smartystreets/goconvey/convey"
"github.com/stretchr/testify/mock"
)
func testDataDir(path ...string) string {
path = append([]string{"testdata", "yara"}, path...)
return filepath.Join(path...)
}
func TestNewYaraScanner(t *testing.T) {
Convey("Creating a new yara scanner", t, func() {
Convey("with a nil argument should fail.", func() {
ys, err := NewYaraScanner(nil)
So(ys, ShouldBeNil)
So(err, ShouldNotBeNil)
})
Convey("with a non-nil argument should be successful.", func() {
ys, err := NewYaraScanner(&yara.Rules{})
So(ys, ShouldNotBeNil)
So(err, ShouldBeNil)
})
})
}
func TestYaraScannerDelegatedMethods(t *testing.T) {
mockedRules := new(MockRules)
defer mockedRules.AssertExpectations(t)
Convey("A YaraScanner", t, func() {
ys, _ := NewYaraScanner(mockedRules)
Convey("should pass a ScanFile call on to the underlying scanner", func() {
filename := "some filename"
expextedErr := errors.New("some error")
mockedRules.
On("ScanFile", filename, yara.ScanFlags(0), time.Duration(0), mock.Anything).
Return(expextedErr).
Once()
_, err := ys.ScanFile(filename)
Convey("and return any errors.", func() {
So(err, ShouldEqual, expextedErr)
})
})
Convey("should pass a ScanMem call on to the underlying scanner", func() {
buf := []byte("some data")
expextedErr := errors.New("some error")
mockedRules.
On("ScanMem", buf, yara.ScanFlags(0), time.Duration(0), mock.Anything).
Return(expextedErr).
Once()
_, err := ys.ScanMem(buf)
Convey("and return any errors.", func() {
So(err, ShouldEqual, expextedErr)
})
})
})
}
func TestIsYaraRulesFile(t *testing.T) {
Convey("Files with any extension in YaraRulesFileExtensions", t, func() {
files := make([]string, 0)
for _, ext := range YaraRulesFileExtensions {
files = append(files, "someFile"+ext)
}
Convey("should be matched.", func() {
for _, file := range files {
So(IsYaraRulesFile(file), ShouldBeTrue)
}
})
})
Convey("Files with any extension in YaraRulesFileExtensions (other case)", t, func() {
files := make([]string, 0)
for _, ext := range YaraRulesFileExtensions {
files = append(files, "someFile"+strings.ToUpper(ext))
files = append(files, "someFile"+strings.ToUpper(ext[0:1])+ext[1:])
}
Convey("should be matched.", func() {
for _, file := range files {
So(IsYaraRulesFile(file), ShouldBeTrue)
}
})
})
Convey("Files with extensions different from those in YaraRulesFileExtensions", t, func() {
files := []string{
"someFile.exe",
"someFile.dll",
"someFile.notyara",
"someFileWithNoExt",
"someFile.a",
"x",
}
for _, ext := range YaraRulesFileExtensions {
files = append(files, "someFile"+ext+"not")
}
Convey("should be matched.", func() {
for _, file := range files {
So(IsYaraRulesFile(file), ShouldBeFalse)
}
})
})
}
func TestLoadYaraRules(t *testing.T) {
Convey("Loading yara rules from a non-existing directory", t, func() {
rules, err := LoadYaraRules(testDataDir("thisdoesnotexist"), false)
Convey("should error.", func() {
So(rules, ShouldBeNil)
So(err, ShouldNotBeNil)
})
})
Convey("Loading a single uncompiled yara rule", t, func() {
rules, err := LoadYaraRules(testDataDir("rules_uncompiled", "rule1.yara"), false)
So(rules, ShouldNotBeNil)
So(err, ShouldBeNil)
Convey("should yield the correct rule.", func() {
yRules := rules.GetRules()
So(len(yRules), ShouldEqual, 1)
So(yRules[0].Identifier(), ShouldEqual, "rule1")
})
})
Convey("Loading a single compiled yara rule", t, withCompiledRules1And2(func(rulesPath string) {
rules, err := LoadYaraRules(rulesPath, false)
So(rules, ShouldNotBeNil)
So(err, ShouldBeNil)
names := make([]string, 0)
for _, rule := range rules.GetRules() {
names = append(names, rule.Identifier())
}
sort.Strings(names)
Convey("should yield exactly the rules in this file.", func() {
So(names, ShouldResemble, []string{
"rule1", "rule2",
})
So(len(names), ShouldEqual, 2)
})
}))
Convey("Loading multiple uncompiled yara rules", t, func() {
Convey("non-recursively", func() {
rules, err := LoadYaraRules(testDataDir("rules_uncompiled"), false)
So(rules, ShouldNotBeNil)
So(err, ShouldBeNil)
names := make([]string, 0)
for _, rule := range rules.GetRules() {
names = append(names, rule.Identifier())
}
sort.Strings(names)
Convey("should yield exactly the rules in this file.", func() {
So(names, ShouldResemble, []string{
"rule1", "rule2",
})
So(len(names), ShouldEqual, 2)
})
})
Convey("recursively", func() {
rules, err := LoadYaraRules(testDataDir("rules_uncompiled"), true)
So(rules, ShouldNotBeNil)
So(err, ShouldBeNil)
names := make([]string, 0)
for _, rule := range rules.GetRules() {
names = append(names, rule.Identifier())
}
sort.Strings(names)
Convey("should yield exactly the rules in this file.", func() {
So(names, ShouldResemble, []string{
"rule1", "rule2", "rule3",
})
So(len(names), ShouldEqual, 3)
})
})
})
Convey("Loading an encrypted zip", t, func() {
Convey("with uncompiled yara rules", func() {
rules, err := LoadYaraRules(testDataDir("rules_zipped", "uncompiled.zip"), true)
So(rules, ShouldNotBeNil)
So(err, ShouldBeNil)
names := make([]string, 0)
for _, rule := range rules.GetRules() {
names = append(names, rule.Identifier())
}
sort.Strings(names)
Convey("should yield exactly the rules in this file.", func() {
So(names, ShouldResemble, []string{
"rule1", "rule2", "rule3",
})
So(len(names), ShouldEqual, 3)
})
})
Convey("with compiled yara rules", withCompiledAndZippedRules1And2And3(func(rulesPath string) {
rules, err := LoadYaraRules(rulesPath, true)
So(rules, ShouldNotBeNil)
So(err, ShouldBeNil)
names := make([]string, 0)
for _, rule := range rules.GetRules() {
names = append(names, rule.Identifier())
}
sort.Strings(names)
Convey("should yield exactly the rules in this file.", func() {
So(names, ShouldResemble, []string{
"rule1", "rule2", "rule3",
})
So(len(names), ShouldEqual, 3)
})
}))
})
}
func withCompiledRules1And2(inner func(rulesPath string)) func(c C) {
return func(c C) {
r1, err := os.ReadFile(testDataDir("rules_uncompiled", "rule1.yara"))
if err != nil {
panic(err)
}
r2, err := os.ReadFile(testDataDir("rules_uncompiled", "rule2.yara"))
if err != nil {
panic(err)
}
sb := &strings.Builder{}
sb.Write(r1)
sb.WriteString("\n")
sb.Write(r2)
rules := yara.MustCompile(sb.String(), nil)
tmpFile, err := os.CreateTemp(os.TempDir(), "rule1and2*.yarc")
if err != nil {
panic(err)
}
rulesPath := tmpFile.Name()
tmpFile.Close()
err = rules.Save(rulesPath)
if err != nil {
panic(err)
}
c.Reset(func() {
os.Remove(rulesPath)
})
inner(rulesPath)
}
}
func withCompiledAndZippedRules1And2And3(inner func(rulesPath string)) func(c C) {
return func(c C) {
r1, err := os.ReadFile(testDataDir("rules_uncompiled", "rule1.yara"))
if err != nil {
panic(err)
}
r2, err := os.ReadFile(testDataDir("rules_uncompiled", "rule2.yara"))
if err != nil {
panic(err)
}
r3, err := os.ReadFile(testDataDir("rules_uncompiled", "subdir", "rule3.yara"))
if err != nil {
panic(err)
}
sb := &strings.Builder{}
sb.Write(r1)
sb.WriteString("\n")
sb.Write(r2)
sb.WriteString("\n")
sb.Write(r3)
rules := yara.MustCompile(sb.String(), nil)
tmpFile, err := os.CreateTemp(os.TempDir(), "rule1and2and3*.yarc")
if err != nil {
panic(err)
}
compiledRulesPath := tmpFile.Name()
tmpFile.Close()
err = rules.Save(compiledRulesPath)
if err != nil {
panic(err)
}
rulesPath := func() string {
compiled, err := os.Open(compiledRulesPath)
if err != nil {
panic(err)
}
defer compiled.Close()
tmpFile, err = os.CreateTemp(os.TempDir(), "rule1and2and3*.zip")
if err != nil {
panic(err)
}
defer tmpFile.Close()
z := zip.NewWriter(tmpFile)
defer z.Close()
out, err := z.Encrypt("rules.yarc", RulesZIPPassword, zip.AES256Encryption)
if err != nil {
panic(err)
}
_, err = io.Copy(out, compiled)
if err != nil {
panic(err)
}
return tmpFile.Name()
}()
c.Reset(func() {
os.Remove(compiledRulesPath)
os.Remove(rulesPath)
})
inner(rulesPath)
}
}