Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/action/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ func scanSinglePath(ctx context.Context, c malcontent.Config, path string, ruleF
// This is a short-circuit that avoids any report generation logic
risk := report.HighestMatchRisk(mrs)
threshold := max(3, c.MinFileRisk, c.MinRisk)
if c.Scan && risk < threshold {
if c.Scan && risk < threshold && !c.QuantityIncreasesRisk {
fr := &malcontent.FileReport{Skipped: "overall risk too low for scan", Path: path}
if isArchive {
os.RemoveAll(path)
}
return fr, nil
}

fr, err := report.Generate(ctx, path, mrs, c, archiveRoot, logger, fc, kind)
fr, err := report.Generate(ctx, path, mrs, c, archiveRoot, logger, fc, kind, risk)
if err != nil {
return nil, NewFileReportError(err, path, TypeGenerateError)
}
Expand Down
31 changes: 15 additions & 16 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func fileMatchesRule(meta []yarax.Metadata, ext string) bool {
return true
}

func Generate(ctx context.Context, path string, mrs *yarax.ScanResults, c malcontent.Config, expath string, _ *clog.Logger, fc []byte, kind *programkind.FileType) (*malcontent.FileReport, error) {
func Generate(ctx context.Context, path string, mrs *yarax.ScanResults, c malcontent.Config, expath string, _ *clog.Logger, fc []byte, kind *programkind.FileType, highestRisk int) (*malcontent.FileReport, error) {
if ctx.Err() != nil {
return &malcontent.FileReport{}, ctx.Err()
}
Expand Down Expand Up @@ -408,7 +408,6 @@ func Generate(ctx context.Context, path string, mrs *yarax.ScanResults, c malcon
risk := 0
riskCounts := make(map[int]int, 0)

highestRisk := HighestMatchRisk(mrs)
// Store match rules in a map for future override operations
mrsMap := createMatchRulesMap(mrs, matchCount)

Expand All @@ -435,9 +434,9 @@ func Generate(ctx context.Context, path string, mrs *yarax.ScanResults, c malcon
switch {
case risk == -1:
continue
case risk < minScore && !ignoreMalcontent && !override:
case !c.Scan && risk < minScore && !ignoreMalcontent && !override:
continue
case c.Scan && risk < highestRisk && !ignoreMalcontent && !override:
case c.Scan && risk < highestRisk && !c.QuantityIncreasesRisk && !ignoreMalcontent && !override:
continue
}

Expand Down Expand Up @@ -483,13 +482,15 @@ func Generate(ctx context.Context, path string, mrs *yarax.ScanResults, c malcon
}

// Update the behaviors to account for overrides
fr.Behaviors = handleOverrides(fr.Behaviors, fr.Overrides, minScore)
fr.Behaviors = handleOverrides(fr.Behaviors, fr.Overrides, minScore, c.Scan, c.QuantityIncreasesRisk)

// Adjust the overall risk if we deviated from overallRiskScore
// Scans will still need to drop <= medium results
newRisk := highestBehaviorRisk(fr)
if overallRiskScore != newRisk {
overallRiskScore = newRisk
overallRiskScore = highestBehaviorRisk(fr)

// If something has a lot of high, it's probably critical
if c.QuantityIncreasesRisk && upgradeRisk(ctx, overallRiskScore, riskCounts, size) {
overallRiskScore = CRITICAL
}

if c.Scan && overallRiskScore < HIGH {
Expand All @@ -503,11 +504,6 @@ func Generate(ctx context.Context, path string, mrs *yarax.ScanResults, c malcon
fr.Skipped = "ignoring malcontent binary"
}

// If something has a lot of high, it's probably critical
if c.QuantityIncreasesRisk && upgradeRisk(ctx, overallRiskScore, riskCounts, size) {
overallRiskScore = 4
}

slices.Sort(pledges)
slices.Sort(syscalls)
slices.Sort(caps)
Expand Down Expand Up @@ -732,8 +728,6 @@ func upgradeRisk(ctx context.Context, riskScore int, riskCounts map[int]int, siz
upgrade = true
case highCount > 6:
upgrade = true
case !upgrade:
upgrade = false
}

clog.DebugContextf(ctx, "upgrading risk: high=%d, size=%d", highCount, size)
Expand Down Expand Up @@ -779,7 +773,7 @@ func highestBehaviorRisk(fr *malcontent.FileReport) int {
}

// handleOverrides modifies the behavior slice based on the contents of the override slice.
func handleOverrides(original, override []*malcontent.Behavior, minScore int) []*malcontent.Behavior {
func handleOverrides(original, override []*malcontent.Behavior, minScore int, scan, quantityIncreasesRisk bool) []*malcontent.Behavior {
behaviorMap := make(map[string]*malcontent.Behavior, len(original))
for _, b := range original {
behaviorMap[b.RuleName] = b
Expand All @@ -798,6 +792,11 @@ func handleOverrides(original, override []*malcontent.Behavior, minScore int) []

modified := make([]*malcontent.Behavior, 0, len(behaviorMap))
for _, b := range behaviorMap {
// if running a scan and using quantityIncreasesRisk,
// append every behavior so we can handle filtering correctly
if scan && quantityIncreasesRisk && b.RiskScore >= HIGH {
modified = append(modified, b)
}
if b.RiskScore >= minScore {
modified = append(modified, b)
}
Expand Down
Loading