/
output.go
419 lines (361 loc) 路 11.5 KB
/
output.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package output
import (
"encoding/json"
"fmt"
"path/filepath"
"github.com/deepfence/YaraHunter/utils"
pb "github.com/deepfence/agent-plugins-grpc/srcgo"
log "github.com/sirupsen/logrus"
// "github.com/fatih/color"
"os"
"strings"
"time"
"unicode/utf8"
tw "github.com/olekukonko/tablewriter"
)
const (
Indent = " " // Indentation for Json printing
)
// severity
const (
HIGH = "high"
MEDIUM = "medium"
LOW = "low"
)
var (
scanFilename = utils.GetDfInstallDir() + "/var/log/fenced/malware-scan/malware_scan.log"
scanStatusFilename = utils.GetDfInstallDir() + "/var/log/fenced/malware-scan-log/malware_scan_log.log"
)
type IOCFound struct {
LayerID string `json:"Image Layer ID,omitempty"`
RuleName string `json:"Matched Rule Name,omitempty"`
StringsToMatch []string `json:"Matched Part,omitempty"`
CategoryName []string `json:"Category,omitempty"`
Severity string `json:"Severity,omitempty"`
SeverityScore float64 `json:"Severity Score,omitempty"`
FileSeverity string `json:"FileSeverity,omitempty"`
FileSevScore float64 `json:"File Severity Score,omitempty"`
CompleteFilename string `json:"Full File Name,omitempty"`
Meta []string `json:"rule meta"`
MetaRules map[string]string `json:"rule metadata"`
Summary string `json:"Summary,omitempty"`
Class string `json:"Class,omitempty"`
}
type JSONDirIOCOutput struct {
Timestamp time.Time
DirName string `json:"Directory Name"`
IOC []IOCFound
}
type JSONImageIOCOutput struct {
Timestamp time.Time
ImageName string `json:"Image Name"`
ImageID string `json:"Image ID"`
ContainerID string `json:"Container ID"`
IOC []IOCFound
}
func (imageOutput *JSONImageIOCOutput) SetImageName(imageName string) {
imageOutput.ImageName = imageName
}
func (imageOutput *JSONImageIOCOutput) SetImageID(imageID string) {
imageOutput.ImageID = imageID
}
func (imageOutput *JSONImageIOCOutput) SetTime() {
imageOutput.Timestamp = time.Now()
}
func (imageOutput *JSONImageIOCOutput) SetIOC(ioc []IOCFound) {
imageOutput.IOC = ioc
}
func (imageOutput *JSONImageIOCOutput) GetIOC() []IOCFound {
return imageOutput.IOC
}
func (imageOutput JSONImageIOCOutput) WriteJSON() error {
return printIOCToJSON(imageOutput)
}
func (imageOutput JSONImageIOCOutput) WriteTable() error {
return WriteTableOutput(&imageOutput.IOC)
}
func (dirOutput *JSONDirIOCOutput) SetTime() {
dirOutput.Timestamp = time.Now()
}
func (dirOutput *JSONDirIOCOutput) SetIOC(ioc []IOCFound) {
dirOutput.IOC = ioc
}
func (dirOutput *JSONDirIOCOutput) GetIOC() []IOCFound {
return dirOutput.IOC
}
func (dirOutput JSONDirIOCOutput) WriteJSON() error {
return printIOCToJSON(dirOutput)
}
func (dirOutput JSONDirIOCOutput) WriteTable() error {
return WriteTableOutput(&dirOutput.IOC)
}
func printIOCToJSON(iocJSON interface{}) error {
file, err := json.MarshalIndent(iocJSON, "", Indent)
if err != nil {
log.Errorf("printIOCToJsonFile: Couldn't format json output: %s", err)
return err
}
fmt.Println(string(file))
return nil
}
func MalwaresToMalwareInfos(out []IOCFound) []*pb.MalwareInfo {
res := make([]*pb.MalwareInfo, 0)
// log.Error("reached everywhere here", out)
for _, v := range out {
// log.Error("did it reach to this point 1", v)
if MalwaresToMalwareInfo(v) != nil {
res = append(res, MalwaresToMalwareInfo(v))
}
// log.Error("did it reach to this point", v)
}
return res
}
func MalwaresToMalwareInfo(out IOCFound) *pb.MalwareInfo {
bool := true
if !(utf8.ValidString(out.LayerID) && utf8.ValidString(out.RuleName) &&
utf8.ValidString(out.Summary) && utf8.ValidString(out.Class) &&
utf8.ValidString(out.FileSeverity) && utf8.ValidString(out.CompleteFilename)) {
bool = false
}
meta := make([]string, 0)
metaRules := make(map[string]string)
stringsToMatch := make([]string, 0)
for i := range out.Meta {
if !utf8.ValidString(out.Meta[i]) && bool {
log.Debugf("reached the meta point %s : %t", out.Meta[i], utf8.ValidString(out.Meta[i]))
} else if len(out.Meta[i]) > 0 {
meta = append(meta, out.Meta[i])
}
}
out.Meta = meta
for k, v := range out.MetaRules {
if !utf8.ValidString(v) && bool {
log.Debugf("reached the meta point %s : %t", v, utf8.ValidString(v))
} else {
metaRules[k] = v
}
}
out.MetaRules = metaRules
for i := range out.StringsToMatch {
if !utf8.ValidString(out.StringsToMatch[i]) && bool {
log.Debugf("reached the meta point %s : %t", out.StringsToMatch[i], utf8.ValidString(out.StringsToMatch[i]))
} else {
stringsToMatch = append(stringsToMatch, out.StringsToMatch[i])
}
}
out.StringsToMatch = stringsToMatch
if bool {
return &pb.MalwareInfo{
ImageLayerId: out.LayerID,
RuleName: out.RuleName,
StringsToMatch: out.StringsToMatch,
SeverityScore: out.SeverityScore,
FileSeverity: out.FileSeverity,
FileSevScore: out.FileSevScore,
CompleteFilename: out.CompleteFilename,
Meta: out.Meta,
MetaRules: out.MetaRules,
Summary: out.Summary,
Class: out.Class,
}
} else {
return nil
}
}
func PrintColoredIOC(iocs []IOCFound, isFirstIOC *bool) {
for _, IOC := range iocs {
printColoredIOCJsonObject(IOC, isFirstIOC, IOC.FileSevScore, IOC.FileSeverity)
*isFirstIOC = false
}
}
// Function to print json object with the matches IOC string in color
// @parameters
// IOC - Structure with details of the IOC found
// isFirstIOC - indicates if some IOC are already printed, used to properly format json
func printColoredIOCJsonObject(ioc IOCFound, isFirstIOC *bool, fileScore float64, severity string) {
Indent3 := Indent + Indent + Indent
if *isFirstIOC {
fmt.Fprintf(os.Stdout, Indent+Indent+"{\n")
} else {
fmt.Fprintf(os.Stdout, ",\n"+Indent+Indent+"{\n")
}
if ioc.LayerID != "" {
fmt.Fprintf(os.Stdout, Indent3+"\"Image Layer ID\": %s,\n", jsonMarshal(ioc.LayerID))
}
fmt.Fprintf(os.Stdout, Indent3+"\"Matched Rule Name\": %s,\n", jsonMarshal(ioc.RuleName))
fmt.Fprintf(os.Stdout, Indent3+"\"Strings to match are\": [\n")
var count = 0
for _, c := range ioc.StringsToMatch {
if len(c) > 0 {
if count == 0 {
fmt.Fprintf(os.Stdout, Indent3+Indent3+jsonMarshal(c)+"\n")
} else {
fmt.Fprintf(os.Stdout, Indent3+Indent3+","+jsonMarshal(c)+"\n")
}
count++
}
}
fmt.Fprintf(os.Stdout, Indent3+"],\n")
summary := ""
class := "Undefined"
categoryName := "["
for i, c := range ioc.CategoryName {
if len(c) > 0 {
str := []string{"The file", ioc.CompleteFilename, "has a", c, "match."}
summary = strings.Join(str, " ")
categoryName += jsonMarshal(c)
if i == 0 && len(ioc.CategoryName) > 1 {
categoryName += ","
}
}
}
categoryName += "]"
// fmt.Fprintf(os.Stdout, Indent3+"\"String to Match\": %s,\n", IOC.StringsToMatch)
// fmt.Fprintf(os.Stdout, Indent3+"\"File Match Severity\": %s,\n", jsonMarshal(severity))
// fmt.Fprintf(os.Stdout, Indent3+"\"File Match Severity Score\": %.2f,\n", fileScore)
fmt.Fprintf(os.Stdout, Indent3+"\"Category\": %s,\n", categoryName)
fmt.Fprintf(os.Stdout, Indent3+"\"File Name\": %s,\n", jsonMarshal(ioc.CompleteFilename))
for _, c := range ioc.Meta {
var metaSplit = strings.Split(c, " : ")
if len(metaSplit) > 1 {
fmt.Fprintf(os.Stdout, Indent3+jsonMarshal(metaSplit[0])+":"+jsonMarshal(strings.ReplaceAll(metaSplit[1], "\n", ""))+",\n")
if metaSplit[0] == "description" {
str := []string{"The file has a rule match that ", strings.ReplaceAll(metaSplit[1], "\n", "") + "."}
summary += strings.Join(str, " ")
} else {
if metaSplit[0] == "info" {
class = strings.TrimSpace(strings.ReplaceAll(metaSplit[1], "\n", ""))
} else if len(metaSplit[0]) > 0 {
str := []string{"The matched rule file's ", metaSplit[0], " is", strings.ReplaceAll(metaSplit[1], "\n", "") + "."}
summary += strings.Join(str, " ")
}
}
}
}
fmt.Fprintf(os.Stdout, Indent3+"\"Summary\": %s\n", jsonMarshal(summary))
fmt.Fprintf(os.Stdout, Indent3+"\"Class\": %s\n", class)
fmt.Fprintf(os.Stdout, Indent+Indent+"}\n")
}
func jsonMarshal(input string) string {
output, _ := json.Marshal(input)
return string(output)
}
func writeToFile(malwareScanMsg string, filename string) error {
if err := os.MkdirAll(filepath.Dir(filename), 0755); err != nil {
return fmt.Errorf("os.MkdirAll: %w", err)
}
f, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer f.Close()
malwareScanMsg = strings.ReplaceAll(malwareScanMsg, "\n", " ")
if _, err = f.WriteString(malwareScanMsg + "\n"); err != nil {
return err
}
return nil
}
func WriteScanStatus(status, scanID, scanMessage string) {
var scanLogDoc = make(map[string]interface{})
scanLogDoc["scan_id"] = scanID
scanLogDoc["scan_status"] = status
scanLogDoc["scan_message"] = scanMessage
byteJSON, err := json.Marshal(scanLogDoc)
if err != nil {
log.Errorf("Error marshalling json for malware-logs-status: %s", err)
return
}
err = writeToFile(string(byteJSON), scanStatusFilename)
if err != nil {
log.Errorf("Error in sending data to malware-logs-status to mark in progress: %s", err)
return
}
}
type MalwareScanDoc struct {
*pb.MalwareInfo
ScanID string `json:"scan_id,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
}
func WriteScanData(malwares []*pb.MalwareInfo, scanID string) {
for _, malware := range malwares {
doc := MalwareScanDoc{
MalwareInfo: malware,
ScanID: scanID,
Timestamp: time.Now().UTC().Format("2006-01-02T15:04:05.000") + "Z",
}
byteJSON, err := json.Marshal(&doc)
if err != nil {
log.Errorf("Error marshalling json: %s", err)
continue
}
err = writeToFile(string(byteJSON), scanFilename)
if err != nil {
log.Errorf("Error in writing data to malware scan file: %s", err)
}
}
}
func WriteTableOutput(report *[]IOCFound) error {
table := tw.NewWriter(os.Stdout)
table.SetHeader([]string{"Rule Name", "Class", "Severity", "Matched Part", "File Name"})
table.SetHeaderLine(true)
table.SetBorder(true)
table.SetAutoWrapText(true)
table.SetAutoFormatHeaders(true)
table.SetColMinWidth(0, 10)
table.SetColMinWidth(1, 10)
table.SetColMinWidth(2, 10)
table.SetColMinWidth(3, 20)
table.SetColMinWidth(4, 20)
for _, r := range *report {
table.Append([]string{r.RuleName, r.Class, r.FileSeverity, strings.Join(r.StringsToMatch, ","), r.CompleteFilename})
}
table.Render()
return nil
}
type SevCount struct {
Total int
High int
Medium int
Low int
}
func CountBySeverity(report []IOCFound) SevCount {
detail := SevCount{}
for _, r := range report {
detail.Total += 1
switch r.FileSeverity {
case HIGH:
detail.High += 1
case MEDIUM:
detail.Medium += 1
case LOW:
detail.Low += 1
}
}
return detail
}
func ExitOnSeverity(severity string, count int, failOnCount int) {
log.Debugf("ExitOnSeverity severity=%s count=%d failOnCount=%d",
severity, count, failOnCount)
if count >= failOnCount {
if len(severity) > 0 {
msg := "Exit malware scan. Number of %s malwares (%d) reached/exceeded the limit (%d).\n"
log.Fatalf(msg, severity, count, failOnCount)
}
msg := "Exit malware scan. Number of malwares (%d) reached/exceeded the limit (%d).\n"
log.Fatalf(msg, count, failOnCount)
}
}
func FailOn(details SevCount, failOnHighCount int, failOnMediumCount int, failOnLowCount int, failOnCount int) {
if failOnHighCount > 0 {
ExitOnSeverity(HIGH, details.High, failOnHighCount)
}
if failOnMediumCount > 0 {
ExitOnSeverity(MEDIUM, details.Medium, failOnMediumCount)
}
if failOnLowCount > 0 {
ExitOnSeverity(LOW, details.Low, failOnLowCount)
}
if failOnCount > 0 {
ExitOnSeverity("", details.Total, failOnCount)
}
}