forked from hillu/go-yara
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_test.go
206 lines (181 loc) · 5.16 KB
/
scanner_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
// Copyright © 2015-2020 Hilko Bengen <[email protected]>
// All rights reserved.
//
// Use of this source code is governed by the license that can be
// found in the LICENSE file.
package yara
import (
"errors"
"io/ioutil"
"os"
"runtime"
"testing"
)
func makeScanner(t *testing.T, rule string) *Scanner {
c, err := NewCompiler()
if c == nil || err != nil {
t.Fatal("NewCompiler():", err)
}
if err = c.AddString(rule, ""); err != nil {
t.Fatal("AddString():", err)
}
r, err := c.GetRules()
if err != nil {
t.Fatal("GetRules:", err)
}
s, err := NewScanner(r)
if err != nil {
t.Fatal("NewScanner:", err)
}
return s
}
func TestScannerSimpleMatch(t *testing.T) {
s := makeScanner(t,
"rule test : tag1 { meta: author = \"Matt Blewitt\" strings: $a = \"abc\" fullword condition: $a }")
var m MatchRules
if err := s.SetCallback(&m).ScanMem([]byte(" abc ")); err != nil {
t.Errorf("ScanMem: %s", err)
} else if len(m) != 1 {
t.Errorf("ScanMem: wanted 1 match, got %d", len(m))
}
t.Logf("Matches: %+v", m)
}
func TestScannerSimpleFileMatch(t *testing.T) {
s := makeScanner(t,
"rule test : tag1 { meta: author = \"Matt Blewitt\" strings: $a = \"abc\" fullword condition: $a }")
tf, _ := ioutil.TempFile("", "TestScannerSimpleFileMatch")
defer os.Remove(tf.Name())
tf.Write([]byte(" abc "))
tf.Close()
var m MatchRules
if err := s.SetCallback(&m).ScanFile(tf.Name()); err != nil {
t.Errorf("ScanFile(%s): %s", tf.Name(), err)
} else if len(m) != 1 {
t.Errorf("ScanFile: wanted 1 match, got %d", len(m))
}
t.Logf("Matches: %+v", m)
}
func TestScannerSimpleFileDescriptorMatch(t *testing.T) {
s := makeScanner(t,
"rule test : tag1 { meta: author = \"Matt Blewitt\" strings: $a = \"abc\" fullword condition: $a }")
tf, _ := ioutil.TempFile("", "TestScannerSimpleFileDescriptorMatch")
defer os.Remove(tf.Name())
tf.Write([]byte(" abc "))
tf.Seek(0, os.SEEK_SET)
var m MatchRules
if err := s.SetCallback(&m).ScanFileDescriptor(tf.Fd()); err != nil {
t.Errorf("ScanFileDescriptor(%v): %s", tf.Fd(), err)
} else if len(m) != 1 {
t.Errorf("ScanFileDescriptor: wanted 1 match, got %d", len(m))
}
t.Logf("Matches: %+v", m)
}
func TestScannerEmptyCallback(t *testing.T) {
s := makeScanner(t,
"rule test : tag1 { meta: author = \"Matt Blewitt\" strings: $a = \"abc\" fullword condition: $a }")
if err := s.ScanMem([]byte(" abc ")); err != nil {
t.Errorf("ScanMem: %s", err)
}
if m, ok := s.Callback.(*MatchRules); !ok {
t.Error("no *MatchRules set")
} else if len(*m) != 1 {
t.Errorf("length of MatchRules: %d (expected 1)", len(*m))
}
}
// TestScannerIndependence tests that two scanners can
// execute with different external variables and the same ruleset
func TestScannerIndependence(t *testing.T) {
rulesStr := `
rule test {
condition: bool_var and int_var == 1 and str_var == "foo"
}
`
c, err := NewCompiler()
if c == nil || err != nil {
t.Fatal("NewCompiler():", err)
}
c.DefineVariable("bool_var", false)
c.DefineVariable("int_var", 0)
c.DefineVariable("str_var", "")
if err = c.AddString(rulesStr, ""); err != nil {
t.Fatal("AddString():", err)
}
r, err := c.GetRules()
if err != nil {
t.Fatal("GetRules:", err)
}
s1, err := NewScanner(r)
if err != nil {
t.Fatal("NewScanner:", err)
}
s2, err := NewScanner(r)
if err != nil {
t.Fatal("NewScanner:", err)
}
s1.DefineVariable("bool_var", true)
s1.DefineVariable("int_var", 1)
s1.DefineVariable("str_var", "foo")
s2.DefineVariable("bool_var", false)
s2.DefineVariable("int_var", 2)
s2.DefineVariable("str_var", "bar")
var m1, m2 MatchRules
if err := s1.SetCallback(&m1).ScanMem([]byte("")); err != nil {
t.Fatal(err)
}
if err := s2.SetCallback(&m2).ScanMem([]byte("")); err != nil {
t.Fatal(err)
}
if !(len(m1) > 0) {
t.Errorf("wanted >0 matches, got %d", len(m1))
}
if len(m2) != 0 {
t.Errorf("wanted 0 matches, got %d", len(m2))
}
t.Logf("Matches 1: %+v", m1)
t.Logf("Matches 2: %+v", m2)
}
func TestScannerImportDataCallback(t *testing.T) {
cb := newTestCallback(t)
s := makeScanner(t, `
import "tests"
import "pe"
rule t1 { condition: true }
rule t2 { condition: false }
rule t3 {
condition: tests.module_data == "callback-data-for-tests-module"
}`)
if err := s.SetCallback(cb).ScanMem([]byte("")); err != nil {
t.Error(err)
}
for _, module := range []string{"tests", "pe"} {
if _, ok := cb.modules[module]; !ok {
t.Errorf("ImportModule was not called for %s", module)
}
}
for _, rule := range []string{"t1", "t3"} {
if _, ok := cb.matched["t1"]; !ok {
t.Errorf("RuleMatching was not called for %s", rule)
}
}
if _, ok := cb.notMatched["t2"]; !ok {
t.Errorf("RuleNotMatching was not called for %s", "t2")
}
if !cb.finished {
t.Errorf("ScanFinished was not called")
}
runtime.GC()
}
type failingScanCallback struct{}
func (*failingScanCallback) RuleMatching(*ScanContext, *Rule) (bool, error) {
return true, errors.New("go away")
}
func TestScannerError(t *testing.T) {
s := makeScanner(t, `
rule test { condition: true }
`)
var err error
if err = s.SetCallback(&failingScanCallback{}).ScanMem([]byte{0, 0, 0, 0}); err == nil {
t.Fatal("ScanMem: did not fail")
}
t.Logf("ScanMem: got expected error, %s", err)
}