2222package yara
2323
2424import (
25+ "os"
26+ "path/filepath"
27+ "syscall"
28+ "testing"
29+ "time"
30+
31+ "github.com/hillu/go-yara/v4"
32+ "github.com/rabbitstack/fibratus/pkg/kevent"
33+ "github.com/rabbitstack/fibratus/pkg/kevent/ktypes"
34+
2535 "github.com/rabbitstack/fibratus/pkg/alertsender"
2636 htypes "github.com/rabbitstack/fibratus/pkg/handle/types"
2737 "github.com/rabbitstack/fibratus/pkg/kevent/kparams"
@@ -34,11 +44,6 @@ import (
3444 "github.com/stretchr/testify/assert"
3545 "github.com/stretchr/testify/mock"
3646 "github.com/stretchr/testify/require"
37- "os"
38- "path/filepath"
39- "syscall"
40- "testing"
41- "time"
4247)
4348
4449var yaraAlert * alertsender.Alert
@@ -167,8 +172,19 @@ func TestScan(t *testing.T) {
167172 time .Sleep (time .Millisecond * 100 )
168173 }
169174
175+ kevt := & kevent.Kevent {
176+ Type : ktypes .CreateProcess ,
177+ Name : "CreateProcess" ,
178+ Tid : 2484 ,
179+ PID : 859 ,
180+ Kparams : kevent.Kparams {
181+ kparams .ProcessName : {Name : kparams .ProcessName , Type : kparams .UnicodeString , Value : "svchost.exe" },
182+ },
183+ Metadata : make (map [string ]string ),
184+ }
185+
170186 // test attaching on pid
171- require .NoError (t , s .ScanProc (pi .ProcessId ))
187+ require .NoError (t , s .ScanProc (pi .ProcessId , kevt ))
172188 require .NotNil (t , yaraAlert )
173189
174190 assert .Equal (t , "YARA alert on process notepad.exe" , yaraAlert .Title )
@@ -177,10 +193,35 @@ func TestScan(t *testing.T) {
177193
178194 // test file scanning on DLL that merely contains
179195 // the fmt.Println("Go Yara DLL Test") statement
180- require .NoError (t , s .ScanFile ("_fixtures/yara-test.dll" ))
196+ require .NoError (t , s .ScanFile ("_fixtures/yara-test.dll" , kevt ))
181197 require .NotNil (t , yaraAlert )
182198
183199 assert .Equal (t , "YARA alert on file _fixtures/yara-test.dll" , yaraAlert .Title )
184200 assert .Contains (t , yaraAlert .Tags , "dll" )
185201
186202}
203+
204+ func TestMatchesMeta (t * testing.T ) {
205+ yaraMatches := []yara.MatchRule {
206+ {Rule : "test" , Namespace : "ns1" },
207+ {Rule : "test2" , Namespace : "ns2" , Tags : []string {"dropper" }, Metas : []yara.Meta {{Identifier : "author" , Value : "rabbit" }}},
208+ }
209+
210+ kevt := & kevent.Kevent {
211+ Type : ktypes .CreateProcess ,
212+ Name : "CreateProcess" ,
213+ Tid : 2484 ,
214+ PID : 859 ,
215+ Kparams : kevent.Kparams {
216+ kparams .ProcessName : {Name : kparams .ProcessName , Type : kparams .UnicodeString , Value : "svchost.exe" },
217+ },
218+ Metadata : make (map [string ]string ),
219+ }
220+
221+ assert .Empty (t , kevt .Metadata )
222+
223+ putMatchesMeta (yaraMatches , kevt )
224+
225+ assert .NotEmpty (t , kevt .Metadata )
226+ assert .Contains (t , kevt .Metadata , matchesMeta )
227+ }
0 commit comments