-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
scan.go
475 lines (410 loc) · 14.8 KB
/
scan.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package local
import (
"context"
"errors"
"fmt"
"sort"
"strings"
"github.com/google/wire"
"github.com/samber/lo"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
dbTypes "github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/applier"
ftypes "github.com/aquasecurity/trivy/pkg/fanal/types"
"github.com/aquasecurity/trivy/pkg/licensing"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/scanner/langpkg"
"github.com/aquasecurity/trivy/pkg/scanner/ospkg"
"github.com/aquasecurity/trivy/pkg/scanner/post"
"github.com/aquasecurity/trivy/pkg/types"
"github.com/aquasecurity/trivy/pkg/vulnerability"
_ "github.com/aquasecurity/trivy/pkg/fanal/analyzer/all"
_ "github.com/aquasecurity/trivy/pkg/fanal/handler/all"
)
// SuperSet binds dependencies for Local scan
var SuperSet = wire.NewSet(
vulnerability.SuperSet,
applier.NewApplier,
ospkg.NewScanner,
langpkg.NewScanner,
NewScanner,
)
// Scanner implements the OspkgDetector and LibraryDetector
type Scanner struct {
applier applier.Applier
osPkgScanner ospkg.Scanner
langPkgScanner langpkg.Scanner
vulnClient vulnerability.Client
}
// NewScanner is the factory method for Scanner
func NewScanner(a applier.Applier, osPkgScanner ospkg.Scanner, langPkgScanner langpkg.Scanner,
vulnClient vulnerability.Client) Scanner {
return Scanner{
applier: a,
osPkgScanner: osPkgScanner,
langPkgScanner: langPkgScanner,
vulnClient: vulnClient,
}
}
// Scan scans the artifact and return results.
func (s Scanner) Scan(ctx context.Context, targetName, artifactKey string, blobKeys []string, options types.ScanOptions) (
types.Results, ftypes.OS, error) {
detail, err := s.applier.ApplyLayers(artifactKey, blobKeys)
switch {
case errors.Is(err, analyzer.ErrUnknownOS):
log.Logger.Debug("OS is not detected.")
// Packages may contain OS-independent binary information even though OS is not detected.
if len(detail.Packages) != 0 {
detail.OS = ftypes.OS{Family: "none"}
}
// If OS is not detected and repositories are detected, we'll try to use repositories as OS.
if detail.Repository != nil {
log.Logger.Debugf("Package repository: %s %s", detail.Repository.Family, detail.Repository.Release)
log.Logger.Debugf("Assuming OS is %s %s.", detail.Repository.Family, detail.Repository.Release)
detail.OS = ftypes.OS{
Family: detail.Repository.Family,
Name: detail.Repository.Release,
}
}
case errors.Is(err, analyzer.ErrNoPkgsDetected):
log.Logger.Warn("No OS package is detected. Make sure you haven't deleted any files that contain information about the installed packages.")
log.Logger.Warn(`e.g. files under "/lib/apk/db/", "/var/lib/dpkg/" and "/var/lib/rpm"`)
case err != nil:
return nil, ftypes.OS{}, xerrors.Errorf("failed to apply layers: %w", err)
}
target := types.ScanTarget{
Name: targetName,
OS: detail.OS,
Repository: detail.Repository,
Packages: mergePkgs(detail.Packages, detail.ImageConfig.Packages, options),
Applications: detail.Applications,
Misconfigurations: mergeMisconfigurations(targetName, detail),
Secrets: mergeSecrets(targetName, detail),
Licenses: detail.Licenses,
CustomResources: detail.CustomResources,
}
return s.ScanTarget(ctx, target, options)
}
func (s Scanner) ScanTarget(ctx context.Context, target types.ScanTarget, options types.ScanOptions) (types.Results, ftypes.OS, error) {
var eosl bool
var results, pkgResults types.Results
var err error
// By default, we need to remove dev dependencies from the result
// IncludeDevDeps option allows you not to remove them
excludeDevDeps(target.Applications, options.IncludeDevDeps)
// Fill OS packages and language-specific packages
if options.ListAllPackages {
if res := s.osPkgScanner.Packages(target, options); len(res.Packages) != 0 {
pkgResults = append(pkgResults, res)
}
pkgResults = append(pkgResults, s.langPkgScanner.Packages(target, options)...)
}
// Scan packages for vulnerabilities
if options.Scanners.Enabled(types.VulnerabilityScanner) {
var vulnResults types.Results
vulnResults, eosl, err = s.scanVulnerabilities(ctx, target, options)
if err != nil {
return nil, ftypes.OS{}, xerrors.Errorf("failed to detect vulnerabilities: %w", err)
}
target.OS.Eosl = eosl
// Merge package results into vulnerability results
mergedResults := s.fillPkgsInVulns(pkgResults, vulnResults)
results = append(results, mergedResults...)
} else {
// If vulnerability scanning is not enabled, it just adds package results.
results = append(results, pkgResults...)
}
// Store misconfigurations
results = append(results, s.misconfsToResults(target.Misconfigurations, options)...)
// Store secrets
results = append(results, s.secretsToResults(target.Secrets, options)...)
// Scan licenses
results = append(results, s.scanLicenses(target, options)...)
// For WASM plugins and custom analyzers
if len(target.CustomResources) != 0 {
results = append(results, types.Result{
Class: types.ClassCustom,
CustomResources: target.CustomResources,
})
}
for i := range results {
// Fill vulnerability details
s.vulnClient.FillInfo(results[i].Vulnerabilities)
}
// Post scanning
results, err = post.Scan(ctx, results)
if err != nil {
return nil, ftypes.OS{}, xerrors.Errorf("post scan error: %w", err)
}
return results, target.OS, nil
}
func (s Scanner) scanVulnerabilities(ctx context.Context, target types.ScanTarget, options types.ScanOptions) (
types.Results, bool, error) {
var eosl bool
var results types.Results
if slices.Contains(options.VulnType, types.VulnTypeOS) {
vuln, detectedEOSL, err := s.osPkgScanner.Scan(ctx, target, options)
if err != nil {
return nil, false, xerrors.Errorf("unable to scan OS packages: %w", err)
} else if vuln.Target != "" {
results = append(results, vuln)
}
eosl = detectedEOSL
}
if slices.Contains(options.VulnType, types.VulnTypeLibrary) {
vulns, err := s.langPkgScanner.Scan(target, options)
if err != nil {
return nil, false, xerrors.Errorf("failed to scan application libraries: %w", err)
}
results = append(results, vulns...)
}
return results, eosl, nil
}
func (s Scanner) fillPkgsInVulns(pkgResults, vulnResults types.Results) types.Results {
var results types.Results
if len(pkgResults) == 0 { // '--list-all-pkgs' == false or packages not found
return vulnResults
}
for _, result := range pkgResults {
if r, found := lo.Find(vulnResults, func(r types.Result) bool {
return r.Class == result.Class && r.Target == result.Target && r.Type == result.Type
}); found {
r.Packages = result.Packages
results = append(results, r)
} else { // when package result has no vulnerabilities we still need to add it to result(for 'list-all-pkgs')
results = append(results, result)
}
}
return results
}
func (s Scanner) misconfsToResults(misconfs []ftypes.Misconfiguration, options types.ScanOptions) types.Results {
if !ShouldScanMisconfigOrRbac(options.Scanners) &&
!options.ImageConfigScanners.Enabled(types.MisconfigScanner) {
return nil
}
return s.MisconfsToResults(misconfs)
}
// MisconfsToResults is exported for trivy-plugin-aqua purposes only
func (s Scanner) MisconfsToResults(misconfs []ftypes.Misconfiguration) types.Results {
log.Logger.Infof("Detected config files: %d", len(misconfs))
var results types.Results
for _, misconf := range misconfs {
log.Logger.Debugf("Scanned config file: %s", misconf.FilePath)
var detected []types.DetectedMisconfiguration
for _, f := range misconf.Failures {
detected = append(detected, toDetectedMisconfiguration(f, dbTypes.SeverityCritical, types.MisconfStatusFailure, misconf.Layer))
}
for _, w := range misconf.Warnings {
detected = append(detected, toDetectedMisconfiguration(w, dbTypes.SeverityMedium, types.MisconfStatusFailure, misconf.Layer))
}
for _, w := range misconf.Successes {
detected = append(detected, toDetectedMisconfiguration(w, dbTypes.SeverityUnknown, types.MisconfStatusPassed, misconf.Layer))
}
for _, w := range misconf.Exceptions {
detected = append(detected, toDetectedMisconfiguration(w, dbTypes.SeverityUnknown, types.MisconfStatusException, misconf.Layer))
}
results = append(results, types.Result{
Target: misconf.FilePath,
Class: types.ClassConfig,
Type: misconf.FileType,
Misconfigurations: detected,
})
}
sort.Slice(results, func(i, j int) bool {
return results[i].Target < results[j].Target
})
return results
}
func (s Scanner) secretsToResults(secrets []ftypes.Secret, options types.ScanOptions) types.Results {
if !options.Scanners.Enabled(types.SecretScanner) {
return nil
}
var results types.Results
for _, secret := range secrets {
log.Logger.Debugf("Secret file: %s", secret.FilePath)
results = append(results, types.Result{
Target: secret.FilePath,
Class: types.ClassSecret,
Secrets: lo.Map(secret.Findings, func(secret ftypes.SecretFinding, index int) types.DetectedSecret {
return types.DetectedSecret(secret)
}),
})
}
return results
}
func (s Scanner) scanLicenses(target types.ScanTarget, options types.ScanOptions) types.Results {
if !options.Scanners.Enabled(types.LicenseScanner) {
return nil
}
var results types.Results
scanner := licensing.NewScanner(options.LicenseCategories)
// License - OS packages
var osPkgLicenses []types.DetectedLicense
for _, pkg := range target.Packages {
for _, license := range pkg.Licenses {
category, severity := scanner.Scan(license)
osPkgLicenses = append(osPkgLicenses, types.DetectedLicense{
Severity: severity,
Category: category,
PkgName: pkg.Name,
Name: license,
Confidence: 1.0,
})
}
}
results = append(results, types.Result{
Target: "OS Packages",
Class: types.ClassLicense,
Licenses: osPkgLicenses,
})
// License - language-specific packages
for _, app := range target.Applications {
var langLicenses []types.DetectedLicense
for _, lib := range app.Libraries {
for _, license := range lib.Licenses {
category, severity := scanner.Scan(license)
langLicenses = append(langLicenses, types.DetectedLicense{
Severity: severity,
Category: category,
PkgName: lib.Name,
Name: license,
Confidence: 1.0,
})
}
}
targetName := app.FilePath
if t, ok := langpkg.PkgTargets[app.Type]; ok && targetName == "" {
// When the file path is empty, we will overwrite it with the pre-defined value.
targetName = t
}
results = append(results, types.Result{
Target: targetName,
Class: types.ClassLicense,
Licenses: langLicenses,
})
}
// License - file header or license file
var fileLicenses []types.DetectedLicense
for _, license := range target.Licenses {
for _, finding := range license.Findings {
category, severity := scanner.Scan(finding.Name)
fileLicenses = append(fileLicenses, types.DetectedLicense{
Severity: severity,
Category: category,
FilePath: license.FilePath,
Name: finding.Name,
Confidence: finding.Confidence,
Link: finding.Link,
})
}
}
results = append(results, types.Result{
Target: "Loose File License(s)",
Class: types.ClassLicenseFile,
Licenses: fileLicenses,
})
return results
}
func toDetectedMisconfiguration(res ftypes.MisconfResult, defaultSeverity dbTypes.Severity,
status types.MisconfStatus, layer ftypes.Layer) types.DetectedMisconfiguration {
severity := defaultSeverity
sev, err := dbTypes.NewSeverity(res.Severity)
if err != nil {
log.Logger.Warnf("severity must be %s, but %s", dbTypes.SeverityNames, res.Severity)
} else {
severity = sev
}
msg := strings.TrimSpace(res.Message)
if msg == "" {
msg = "No issues found"
}
var primaryURL string
// empty namespace implies a go rule from defsec, "builtin" refers to a built-in rego rule
// this ensures we don't generate bad links for custom policies
if res.Namespace == "" || strings.HasPrefix(res.Namespace, "builtin.") {
primaryURL = fmt.Sprintf("https://avd.aquasec.com/misconfig/%s", strings.ToLower(res.ID))
res.References = append(res.References, primaryURL)
}
if primaryURL == "" && len(res.References) > 0 {
primaryURL = res.References[0]
}
return types.DetectedMisconfiguration{
ID: res.ID,
AVDID: res.AVDID,
Type: res.Type,
Title: res.Title,
Description: res.Description,
Message: msg,
Resolution: res.RecommendedActions,
Namespace: res.Namespace,
Query: res.Query,
Severity: severity.String(),
PrimaryURL: primaryURL,
References: res.References,
Status: status,
Layer: layer,
Traces: res.Traces,
CauseMetadata: ftypes.CauseMetadata{
Resource: res.Resource,
Provider: res.Provider,
Service: res.Service,
StartLine: res.StartLine,
EndLine: res.EndLine,
Code: res.Code,
Occurrences: res.Occurrences,
},
}
}
func ShouldScanMisconfigOrRbac(scanners types.Scanners) bool {
return scanners.AnyEnabled(types.MisconfigScanner, types.RBACScanner)
}
// excludeDevDeps removes development dependencies from the list of applications
func excludeDevDeps(apps []ftypes.Application, include bool) {
if include {
return
}
for i := range apps {
apps[i].Libraries = lo.Filter(apps[i].Libraries, func(lib ftypes.Package, index int) bool {
return !lib.Dev
})
}
}
func mergePkgs(pkgs, pkgsFromCommands []ftypes.Package, options types.ScanOptions) []ftypes.Package {
if !options.ScanRemovedPackages || len(pkgsFromCommands) == 0 {
return pkgs
}
// pkg has priority over pkgsFromCommands
uniqPkgs := make(map[string]struct{})
for _, pkg := range pkgs {
uniqPkgs[pkg.Name] = struct{}{}
}
for _, pkg := range pkgsFromCommands {
if _, ok := uniqPkgs[pkg.Name]; ok {
continue
}
pkgs = append(pkgs, pkg)
}
return pkgs
}
// mergeMisconfigurations merges misconfigurations on container image config
func mergeMisconfigurations(targetName string, detail ftypes.ArtifactDetail) []ftypes.Misconfiguration {
if detail.ImageConfig.Misconfiguration == nil {
return detail.Misconfigurations
}
// Append misconfigurations on container image config
misconf := detail.ImageConfig.Misconfiguration
misconf.FilePath = targetName // Set the target name to the file path as container image config is not a real file.
return append(detail.Misconfigurations, *misconf)
}
// mergeSecrets merges secrets on container image config.
func mergeSecrets(targetName string, detail ftypes.ArtifactDetail) []ftypes.Secret {
if detail.ImageConfig.Secret == nil {
return detail.Secrets
}
// Append secrets on container image config
secret := detail.ImageConfig.Secret
secret.FilePath = targetName // Set the target name to the file path as container image config is not a real file.
return append(detail.Secrets, *secret)
}