-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyarascanner.go
83 lines (71 loc) · 1.88 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
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
package main
import (
// standard
"fmt"
"github.com/hillu/go-yara/v4"
)
type YaraScanner struct {
rulesets []*RuleSet
}
// To implement an interface in Go all you need to do is just define all the functions in the interface
func (self *YaraScanner) Scan(data [] byte) (*ScanReport, error) {
var matches []ScanMatch
response := new(ScanReport)
response.Filename = "stream"
response.Matches = matches
for _, ruleset := range self.rulesets {
var m yara.MatchRules
err := ruleset.Rules.ScanMem(data, 0, 300, &m)
if err != nil {
response.Status = "ERROR"
return response,err
}
for _, resp := range m {
var match ScanMatch
match.Rule = resp.Rule
match.Namespace = resp.Namespace
match.Tags = resp.Tags
matches = append(matches, match)
}
}
if len(matches) > 0 {
response.Status = "INFECTED"
} else {
response.Status = "CLEAN"
matches = [] ScanMatch{}
}
response.Matches = matches
return response,nil
}
func (self *YaraScanner) LoadIndex(indexPath string) error {
ruleset, err := NewRuleSet(indexPath)
if err != nil {
return err
}
self.rulesets = append(self.rulesets, ruleset)
return nil
}
func (self *YaraScanner) ListRuleSets() (*RuleSetResponseObject, error) {
response := new(RuleSetResponseObject)
for _, ruleset := range self.rulesets {
response.Names = append(response.Names, ruleset.Name)
}
return response,nil
}
func (self *YaraScanner) ListRules(rulesetname string) (*RuleListResponseObject, error) {
response := new(RuleListResponseObject)
fmt.Printf("listRules called, %s\n", rulesetname)
for _, ruleset := range self.rulesets {
fmt.Printf("Looking for %s, looking at %s\n", rulesetname, ruleset.Name)
if ruleset.Name == rulesetname {
rules, err := ruleset.ListRules()
if err != nil {
return nil, err
}
for _, rule := range rules {
response.Rules = append(response.Rules, rule)
}
}
}
return response, nil
}