-
Notifications
You must be signed in to change notification settings - Fork 1
/
scanner.go
145 lines (112 loc) · 3.94 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
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
package main
import (
"time"
"encoding/json"
"os"
"strconv"
)
// struct to hold compiler and channels
type Scanner struct {
yarascanner YaraScanner
clamscanner ClamScanner
healthcheckrequests chan *HealthCheckRequest
scanstreamrequests chan *ScanStreamRequest
namerequests chan *RuleSetRequest
rulerequests chan *RuleListRequest
}
func (self *Scanner) healthcheck() (*HealthCheckResponse) {
healthCheckResponse := new (HealthCheckResponse)
clamDHealth := self.clamscanner.isClamdReady()
if clamDHealth {
healthCheckResponse.health = "OK"
} else {
healthCheckResponse.health = "ERROR"
}
return healthCheckResponse
}
func (self *Scanner) scanstream(data []byte) (*ScanResponse) {
info.Println("Running yarascan")
scanResponse := new (ScanResponse)
yaraScannerResponse,yaraerr := ScanStream(&self.yarascanner, data)
scanResponse.data = yaraScannerResponse
scanResponse.err = yaraerr
yaraRespJson, _ := json.Marshal(yaraScannerResponse)
info.Println( time.Now().Format(time.RFC3339) + " yarascan scan result " + string(yaraRespJson))
if (yaraerr == nil) && len(yaraScannerResponse.Matches) > 0 {
info.Println( time.Now().Format(time.RFC3339) + " Found matches with yara " + string(yaraRespJson))
}
info.Println("Running clamscan on addr: "+ clamdaddr)
clamScannerResponse,clamerr := ScanStream(&self.clamscanner, data)
clamRespJson, _ := json.Marshal(clamScannerResponse)
info.Println( time.Now().Format(time.RFC3339) + " clamav scan result " + string(clamRespJson))
if (clamerr == nil) && len(clamScannerResponse.Matches) > 0 {
info.Println( time.Now().Format(time.RFC3339) + " Found matches with clamav" + string(clamRespJson))
scanResponse.data = clamScannerResponse
}
if clamerr != nil {
scanResponse.err = clamerr
}
return scanResponse
}
func (self *Scanner) warmUp() {
info.Println("Warming Up")
var yaraHealth = bool(false)
var clamDHealth = bool(false)
yaraScannerResponse,yaraerr := ScanStream(&self.yarascanner, eicar)
if (yaraerr == nil) && len(yaraScannerResponse.Matches) > 0 {
yaraHealth = true
}
clamDHealth = self.clamscanner.warmUp()
if yaraHealth && clamDHealth {
info.Println("Warmed Up")
} else {
info.Println( time.Now().Format(time.RFC3339) + " Warm up failed exiting.. Yara Health" + strconv.FormatBool(yaraHealth) + "ClamD Health" + strconv.FormatBool(clamDHealth))
os.Exit(1)
}
}
func (self *Scanner) LoadIndex(indexPath string) error {
return self.yarascanner.LoadIndex(indexPath)
}
func (self *Scanner) listRuleSets() *RuleSetResponse {
response,err := self.yarascanner.ListRuleSets()
ruleSetResponse := new (RuleSetResponse)
ruleSetResponse.err = err
ruleSetResponse.data = response
return ruleSetResponse
}
func (self *Scanner) listRules(rulesetname string) (*RuleListResponse) {
response, err := self.yarascanner.ListRules(rulesetname)
ruleListResponse := new (RuleListResponse)
ruleListResponse.err = err
ruleListResponse.data = response
return ruleListResponse
}
func (self *Scanner) Run() {
info.Println("Waiting for scan requests")
for {
select {
case healthcheckmsg := <-healthcheckrequests:
response := self.healthcheck()
healthcheckmsg.ResponseChan <- response
case scanstreammsg := <-scanstreamrequests:
response := self.scanstream(scanstreammsg.data)
scanstreammsg.ResponseChan <- response
case setmsg := <-namerequests:
response := self.listRuleSets()
setmsg.ResponseChan <- response
case rulemsg := <-rulerequests:
response := self.listRules(rulemsg.RuleSet)
rulemsg.ResponseChan <- response
}
}
}
func NewScanner(healthcheckreq chan *HealthCheckRequest, scanstream chan *ScanStreamRequest, name chan *RuleSetRequest, list chan *RuleListRequest) (*Scanner, error) {
scanner := new(Scanner)
scanner.healthcheckrequests = healthcheckreq
scanner.scanstreamrequests = scanstream
scanner.namerequests = name
scanner.rulerequests = list
scanner.yarascanner = YaraScanner{}
scanner.clamscanner = ClamScanner{clamdaddr}
return scanner, nil
}