This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
/
analyzers.go
224 lines (187 loc) · 5.19 KB
/
analyzers.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package cortex
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"sync"
)
const analyzersURL = "/api/analyzer"
// Analyzer defines a specific Cortex Analyzer
//
// More info: https://github.com/CERT-BDF/CortexDocs/blob/master/api/get-analyzer.md
type Analyzer struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
DataTypeList []string `json:"dataTypeList"`
}
// ListAnalyzers retrieves all analyzers that are available.
// Analyzers can be filtered by a datatype parameter.
// When "*" is used as a parameter, function returns all analyzers.
func (c *Client) ListAnalyzers(datatype string) ([]Analyzer, error) {
requestURL := analyzersURL
if datatype != "*" {
requestURL = analyzersURL + "/type/" + datatype
}
r, err := c.sendRequest("GET", requestURL, nil)
if err != nil {
return nil, err
}
var a []Analyzer
if err := json.NewDecoder(r.Body).Decode(&a); err != nil {
return nil, err
}
r.Body.Close()
return a, nil
}
// GetAnalyzer retrieves an Analyzer by its' ID
func (c *Client) GetAnalyzer(id string) (*Analyzer, error) {
r, err := c.sendRequest("GET", analyzersURL+"/"+id, nil)
if err != nil {
return nil, err
}
if r.StatusCode == 404 {
return nil, fmt.Errorf("Can't find the analyzer with an id %s", id)
}
a := &Analyzer{}
if err := json.NewDecoder(r.Body).Decode(a); err != nil {
return nil, err
}
r.Body.Close()
return a, nil
}
// RunAnalyzer runs a selected analyzer for a specified job
func (c *Client) RunAnalyzer(id string, obs Observable) (*Job, error) {
var resp *http.Response
var err error
switch obs.Type() {
case "file":
far := obs.(*FileArtifact)
obsData, err := json.Marshal(far.FileArtifactMeta)
if err != nil {
return nil, err
}
resp, err = c.sendFileRequest(obsData, analyzersURL+"/"+id+"/run", far.FileName, far.Reader)
if err != nil {
return nil, err
}
default:
obsData, err := json.Marshal(obs.(*Artifact))
if err != nil {
return nil, err
}
resp, err = c.sendRequest("POST", analyzersURL+"/"+id+"/run", &obsData)
if err != nil {
return nil, err
}
}
j := &Job{}
if err = json.NewDecoder(resp.Body).Decode(j); err != nil {
return nil, err
}
// resp.Body.Close()
return j, nil
}
// RunAnalyzerThenGetReport is a helper function that combines multiple
// functions to return JobReport providing more clear API
func (c *Client) RunAnalyzerThenGetReport(id string, obs Observable, timeout string) (*JobReport, error) {
j, err := c.RunAnalyzer(id, obs)
if err != nil {
c.log(fmt.Sprintf("Failed to run the analyzer %s", id))
return nil, err
}
w, err := c.WaitForJob(j.ID, timeout)
if err != nil {
c.log(fmt.Sprintf("Failed to wait for the job %s", j.ID))
return nil, err
}
r, err := c.GetJobReport(w.ID)
if err != nil {
c.log(fmt.Sprintf("Failed to get the job report %s", w.ID))
return nil, err
}
return r, nil
}
// AnalyzeData runs all analyzers suitable for a specified job and returns a channel with reports
func (c *Client) AnalyzeData(obs Observable, timeout string) (<-chan *JobReport, error) {
var wg sync.WaitGroup
reports := make(chan *JobReport)
analyzers, err := c.ListAnalyzers(obs.Type())
if err != nil {
return nil, err
}
log.Println(analyzers)
wg.Add(len(analyzers))
switch obs.(type) {
case *FileArtifact:
reports, err = c.analyzeFile(analyzers, obs.(*FileArtifact), &wg, timeout, reports)
if err != nil {
return nil, err
}
case *Artifact:
reports, err = c.analyzeString(analyzers, obs, &wg, timeout, reports)
if err != nil {
return nil, err
}
}
go func() {
wg.Wait()
close(reports)
}()
return reports, nil
}
func (c *Client) analyzeFile(analyzers []Analyzer, fa *FileArtifact, wg *sync.WaitGroup, timeout string, rch chan *JobReport) (chan *JobReport, error) {
var readPipes []*io.PipeReader
var writePipes []*io.PipeWriter
for _, a := range analyzers {
fr, fw := io.Pipe()
readPipes = append(readPipes, fr)
writePipes = append(writePipes, fw)
o := &FileArtifact{
FileName: fa.FileName,
Reader: fr,
FileArtifactMeta: fa.FileArtifactMeta,
}
go func(an Analyzer, f io.Reader) {
defer wg.Done()
c.log(fmt.Sprintf("Starting %s analyzer", an.Name))
report, err := c.RunAnalyzerThenGetReport(an.ID, o, timeout)
if err == nil && report != nil {
rch <- report
} else {
c.log(fmt.Sprintf("Failed to process %s with %s", o.Description(), an.Name))
}
}(a, fr)
}
wr := make([]io.Writer, len(writePipes))
for i := range writePipes {
wr[i] = writePipes[i]
}
mw := io.MultiWriter(wr...)
go func() {
if _, err := io.Copy(mw, fa.Reader); err != nil {
log.Fatal(err)
}
for i := range writePipes {
writePipes[i].Close()
}
}()
return rch, nil
}
func (c *Client) analyzeString(analyzers []Analyzer, obs Observable, wg *sync.WaitGroup, timeout string, rch chan *JobReport) (chan *JobReport, error) {
for _, a := range analyzers {
go func(an Analyzer) {
defer wg.Done()
report, err := c.RunAnalyzerThenGetReport(an.ID, obs, timeout)
if err == nil {
rch <- report
} else {
c.log(fmt.Sprintf("Failed to process %s with %s", obs.Description(), an.Name))
}
}(a)
}
return rch, nil
}