-
Notifications
You must be signed in to change notification settings - Fork 0
/
YaraScanner.go
57 lines (52 loc) · 1.27 KB
/
YaraScanner.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
package YaraStream
import (
"github.com/hillu/go-yara/v4"
"io/fs"
"log"
"os"
"path/filepath"
)
type YaraScanner struct {
rules *yara.Rules
}
type RuleDirectory struct {
Namespace string
Path string
}
// NewYaraScanner Will return a new scanner with rules compiled
func NewYaraScanner(ruleDirectories ...RuleDirectory) (*YaraScanner, error) {
scanner := &YaraScanner{}
compiler, err := yara.NewCompiler()
if err != nil {
return nil, err
}
for _, ruleDirectory := range ruleDirectories {
err = scanner.addDirectory(compiler, ruleDirectory)
if err != nil {
return nil, err
}
}
if scanner.rules, err = compiler.GetRules(); err != nil {
return nil, err
}
return scanner, nil
}
func (s *YaraScanner) addDirectory(compiler *yara.Compiler, ruleDirectory RuleDirectory) error {
return filepath.Walk(ruleDirectory.Path, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && (filepath.Ext(path) == ".yar" || filepath.Ext(path) == ".yara") {
ruleFile, err := os.Open(path)
if err != nil {
return err
}
err = compiler.AddFile(ruleFile, ruleDirectory.Namespace)
if err != nil {
return err
}
log.Printf("Loaded %s in namespace %s", path, ruleDirectory.Namespace)
}
return nil
})
}