-
Notifications
You must be signed in to change notification settings - Fork 8
/
scanner.go
118 lines (99 loc) · 2.56 KB
/
scanner.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
package main
import (
// standard
"fmt"
// external
"github.com/jheise/yaramsg"
)
// struct to hold compiler and channels
type Scanner struct {
rulesets []*RuleSet
scanrequests chan *ScanRequest
namerequests chan *RuleSetRequest
rulerequests chan *RuleListRequest
}
func (self *Scanner) scan(filename string) (*ScanResponse, error) {
response := new(ScanResponse)
response.Filename = filename
var err error
var matches []*yaramsg.Match
filepath := uploads_dir + "/" + filename
for _, ruleset := range self.rulesets {
output, err := ruleset.Rules.ScanFile(filepath, 0, 300)
if err != nil {
return response, err
}
for _, resp := range output {
match := new(yaramsg.Match)
match.Rule = resp.Rule
match.Namespace = resp.Namespace
match.Tags = resp.Tags
matches = append(matches, match)
}
}
response.Matches = matches
return response, err
}
func (self *Scanner) LoadIndex(indexPath string) error {
ruleset, err := NewRuleSet(indexPath)
if err != nil {
return err
}
self.rulesets = append(self.rulesets, ruleset)
return nil
}
func (self *Scanner) listRuleSets() *RuleSetResponse {
response := new(RuleSetResponse)
for _, ruleset := range self.rulesets {
response.Names = append(response.Names, ruleset.Name)
}
return response
}
func (self *Scanner) listRules(rulesetname string) (*RuleListResponse, error) {
response := new(RuleListResponse)
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
}
func (self *Scanner) Run() {
info.Println("Waiting for scan requests")
for {
select {
case scanmsg := <-scanrequests:
response, err := self.scan(scanmsg.Filename)
if err != nil {
elog.Println(err)
return
}
scanmsg.ResponseChan <- response
case setmsg := <-namerequests:
response := self.listRuleSets()
setmsg.ResponseChan <- response
case rulemsg := <-rulerequests:
response, err := self.listRules(rulemsg.RuleSet)
if err != nil {
elog.Println(err)
return
}
rulemsg.ResponseChan <- response
}
}
}
func NewScanner(scan chan *ScanRequest, name chan *RuleSetRequest, list chan *RuleListRequest) (*Scanner, error) {
scanner := new(Scanner)
scanner.scanrequests = scan
scanner.namerequests = name
scanner.rulerequests = list
return scanner, nil
}