-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
196 lines (186 loc) · 5.65 KB
/
main.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
package main
import (
"embed"
"flag"
"fmt"
"os"
"time"
)
//go:embed report_template.html
var f embed.FS
var Conf struct {
URL string
UserID string
Password string
IgnoreTLSErrors bool
}
func Configure() {
flag.StringVar(&Conf.URL, "url", "", "Smart Check/Container Security URL")
flag.StringVar(&Conf.UserID, "user", "administrator", "User name")
flag.StringVar(&Conf.Password, "password", "", "Password")
flag.BoolVar(&Conf.IgnoreTLSErrors, "ignore_tls", false, "Ignore TLS Errors")
flag.Parse()
if Conf.URL == "" {
fmt.Fprintf(os.Stderr, "missing required -%s argument/flag\n", "url")
os.Exit(2)
}
if Conf.Password == "" {
fmt.Fprintf(os.Stderr, "missing required -%s argument/flag\n", "password")
os.Exit(2)
}
fmt.Println(Conf)
}
func main() {
Configure()
//URL := "https://192.168.184.18:31616"
sc := NewSmartCheck(Conf.URL, Conf.IgnoreTLSErrors)
request := RequestCreateSessionUser{
User: RequestCreateSessionUserCredentials{
UserID: Conf.UserID,
Password: Conf.Password,
},
}
//fmt.Println("Create Session")
session, err := sc.CreateSession(&request)
if err != nil {
panic(err)
}
defer func() {
err = session.Delete()
if err != nil {
panic(err)
}
}()
template, err := f.ReadFile("report_template.html")
if err != nil {
panic(err)
}
rep := NewReportGenerator(string(template))
rep.Report.GeneratedOn = time.Now()
rep.Report.Registries = make([]RegistryReport, 0)
for r := range session.ListRegistries() {
registry := RegistryReport{
Name: r.Name,
Images: make([]ImageReport, 0),
}
//fmt.Println("Registry:", r.ID)
for im := range session.ListRegistryImages(r.ID) {
//fmt.Println("Image:", im.ID) //, im.Tag, im.Registry, im.Repository, im.Status)
scan := session.ImageLastScan(im)
image := ImageReport{
Name: scan.Name,
Layers: make([]LayerReport, 0),
}
rep.Report.Completed = scan.Details.Completed
for _, layer := range scan.Details.Results {
layerReport := LayerReport{
ID: layer.ID,
CreatedAt: layer.CreatedAt,
CreatedBy: layer.CreatedBy,
}
//fmt.Println("Result:")
if layer.Malware+layer.Vulnerabilities+layer.Contents == "" {
continue
}
fmt.Println("==========================")
fmt.Println("Scan competed:", scan.Details.Completed)
fmt.Println("Image:", scan.Name)
fmt.Println("Layer ID:", layer.ID)
fmt.Println("Created at:", layer.CreatedAt)
fmt.Println("Create with:", layer.CreatedBy)
fmt.Println("Findings:")
if layer.Malware != "" {
layerReport.Malware = make([]MalwareReport, 0)
/*
JSON, err := json.MarshalIndent(layer, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Layer:\n%s\n", string(JSON))
*/
for malware := range session.ListMalwareFindings(layer.Malware) {
name := malware.Icrc.Name
url := malware.Icrc.URL
if malware.Trendx.Found.Name != "" {
name = fmt.Sprintf("%s (Confidence %s%%)",
malware.Trendx.Found.Name,
malware.Trendx.Confidence)
url = malware.Trendx.Found.URL
}
malwareReport := MalwareReport{
Filename: malware.Filename,
Name: name,
URL: url,
}
fmt.Printf("Malware: %s %s (%s)\n", malware.Filename, name, url)
// Fixed in ohter layres?!
layerReport.Malware = append(layerReport.Malware, malwareReport)
}
}
if layer.Vulnerabilities != "" {
layerReport.Package = make([]PackageReport, 0)
for v := range session.ListVulnerabilitiesFindings(layer.Vulnerabilities) {
packageReport := PackageReport{
Name: v.Name,
Version: v.Version,
Vulnerabilities: make([]VulnerabilityReport, 0),
}
fmt.Println("Module/Package:", v.Name)
fmt.Println("Version:", v.Version)
for _, cve := range v.Vulnerabilities {
vulnerabilityReport := VulnerabilityReport{
Name: cve.Name,
Link: cve.Link,
Severity: cve.Severity,
}
fmt.Println("CVE:", cve.Name)
fmt.Println("URL:", cve.Link)
fmt.Println("Severity:", cve.Severity)
// cve.Description
// Overriden!!!
packageReport.Vulnerabilities = append(packageReport.Vulnerabilities, vulnerabilityReport)
}
layerReport.Package = append(layerReport.Package, packageReport)
}
}
if layer.Contents != "" {
layerReport.Contents = make([]ContentsReport, 0)
for contents := range session.ListContentsFindings(layer.Contents) {
contentsReport := ContentsReport{
Severity: contents.Severity,
Rule: contents.Rule,
Description: contents.Description,
Filename: contents.Filename,
}
/*JSON, err := json.MarshalIndent(contents, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Content:\n%s\n", string(JSON))
*/
fmt.Println("Severity: ", contents.Severity)
fmt.Println("Rule: ", contents.Rule)
fmt.Println("Description: ", contents.Description)
fmt.Println("File: ", contents.Filename)
layerReport.Contents = append(layerReport.Contents, contentsReport)
}
}
image.Layers = append(image.Layers, layerReport)
}
registry.Images = append(registry.Images, image)
}
rep.Report.Registries = append(rep.Report.Registries, registry)
}
rep.Report.Difference = rep.Report.GeneratedOn.Sub(rep.Report.Completed).Round(time.Second)
filename := rep.Report.GeneratedOn.Format("report_20060102.html")
//filename := "report.html"
f, err := os.Create(filename)
if err != nil {
panic(err)
}
err = rep.Generate(f)
if err != nil {
panic(err)
}
fmt.Printf("Output writted to: %s\n", filename)
}