forked from chainguard-dev/bincapz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bincapz.go
220 lines (192 loc) · 6.08 KB
/
bincapz.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
// Copyright 2024 Chainguard, Inc.
// SPDX-License-Identifier: Apache-2.0
// bincapz returns information about a binaries capabilities
package main
import (
"context"
"flag"
"fmt"
"io/fs"
"log/slog"
"os"
"strings"
"github.com/chainguard-dev/bincapz/pkg/action"
"github.com/chainguard-dev/bincapz/pkg/bincapz"
"github.com/chainguard-dev/bincapz/pkg/compile"
"github.com/chainguard-dev/bincapz/pkg/profile"
"github.com/chainguard-dev/bincapz/pkg/render"
"github.com/chainguard-dev/bincapz/pkg/version"
"github.com/chainguard-dev/bincapz/rules"
thirdparty "github.com/chainguard-dev/bincapz/third_party"
"github.com/chainguard-dev/clog"
)
var (
// Exit codes based on diff(1) and https://man.freebsd.org/cgi/man.cgi?errno(2)
ExitOK = 0
ExitActionFailed = 2
ExitProfilerError = 3
ExitInputOutput = 5
ExitRenderFailed = 11
ExitInvalidRules = 14
ExitInvalidArgument = 22
)
// parse risk levels.
func parseRisk(s string) int {
levels := map[string]int{
"0": 0,
"any": 0,
"all": 0,
"1": 1,
"low": 1,
"2": 2,
"medium": 2,
"3": 3,
"high": 3,
"4": 4,
"crit": 4,
"critical": 4,
}
return levels[strings.ToLower(s)]
}
func main() {
allFlag := flag.Bool("all", false, "Ignore nothing, show all")
diffFlag := flag.Bool("diff", false, "Show capability drift between two files")
formatFlag := flag.String("format", "terminal", "Output type -- valid values are: json, markdown, simple, terminal, yaml")
ignoreSelfFlag := flag.Bool("ignore-self", true, "Ignore the bincapz binary")
ignoreTagsFlag := flag.String("ignore-tags", "", "Rule tags to ignore")
outputFlag := flag.String("o", "", "write output to this path instead of stdout")
includeDataFilesFlag := flag.Bool("data-files", false, "Include files that are detected as non-program (binary or source) files")
minFileLevelFlag := flag.Int("min-file-level", -1, "Obsoleted by --min-file-risk")
minLevelFlag := flag.Int("min-level", -1, "Obsoleted by --min-risk")
minFileRiskFlag := flag.String("min-file-risk", "low", "Only show results for files that meet this risk level (any,low,medium,high,critical")
minRiskFlag := flag.String("min-risk", "low", "Minimum risk level to show results for (any,low,medium,high,critical)")
errFirstMissFlag := flag.Bool("err-first-miss", false, "exit with error if scan source has no matching capabilities")
errFirstHitFlag := flag.Bool("err-first-hit", false, "exit with error if scan source has matching capabilities")
ociFlag := flag.Bool("oci", false, "Scan an OCI image")
omitEmptyFlag := flag.Bool("omit-empty", false, "Omit files that contain no matches")
profileFlag := flag.Bool("profile", false, "Generate profile and trace files")
statsFlag := flag.Bool("stats", false, "Show statistics about the scan")
thirdPartyFlag := flag.Bool("third-party", true, "Include third-party rules, which may have licensing restrictions")
verboseFlag := flag.Bool("verbose", false, "Emit verbose logging messages to stderr")
versionFlag := flag.Bool("version", false, "Show version information")
flag.Parse()
args := flag.Args()
returnCode := ExitOK
defer func() { os.Exit(returnCode) }()
logLevel := new(slog.LevelVar)
logLevel.Set(slog.LevelError)
logOpts := &slog.HandlerOptions{Level: logLevel, AddSource: true}
log := clog.New(slog.NewTextHandler(os.Stderr, logOpts))
var stop func()
if *profileFlag {
var err error
stop, err = profile.Profile()
if err != nil {
log.Error("profiling failed", slog.Any("error", err))
returnCode = ExitProfilerError
return
}
}
if len(args) == 0 && !*versionFlag {
fmt.Printf("usage: bincapz [flags] <directories>")
returnCode = ExitInvalidArgument
return
}
if *verboseFlag {
logOpts.AddSource = true
logLevel.Set(slog.LevelDebug)
}
if *versionFlag {
ver, err := version.Version()
if err != nil {
fmt.Printf("bincapz unknown version\n")
}
fmt.Printf("%s\n", ver)
return
}
ctx := clog.WithLogger(context.Background(), log)
clog.FromContext(ctx).Info("bincapz starting")
ignoreTags := strings.Split(*ignoreTagsFlag, ",")
includeDataFiles := *includeDataFilesFlag
minRisk := parseRisk(*minRiskFlag)
// Backwards compatibility
if *minLevelFlag != -1 {
minRisk = *minLevelFlag
}
minFileRisk := parseRisk(*minFileRiskFlag)
// Backwards compatibility
if *minFileLevelFlag != -1 {
minFileRisk = *minFileLevelFlag
}
stats := *statsFlag
if *allFlag {
ignoreTags = []string{}
minRisk = -1
includeDataFiles = true
*ignoreSelfFlag = false
}
outFile := os.Stdout
var err error
if *outputFlag != "" {
outFile, err = os.OpenFile(*outputFlag, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err != nil {
log.Error("open file", slog.Any("error", err), slog.String("path", *outputFlag))
returnCode = ExitInputOutput
return
}
}
defer func() {
outFile.Close()
}()
renderer, err := render.New(*formatFlag, outFile)
if err != nil {
log.Error("invalid format", slog.Any("error", err), slog.String("format", *formatFlag))
returnCode = ExitInvalidArgument
return
}
rfs := []fs.FS{rules.FS}
if *thirdPartyFlag {
rfs = append(rfs, thirdparty.FS)
}
yrs, err := compile.Recursive(ctx, rfs)
if err != nil {
log.Error("YARA rule compilation", slog.Any("error", err))
returnCode = ExitInvalidRules
return
}
bc := action.Config{
IgnoreSelf: *ignoreSelfFlag,
IgnoreTags: ignoreTags,
IncludeDataFiles: includeDataFiles,
MinFileRisk: minFileRisk,
MinRisk: minRisk,
OCI: *ociFlag,
OmitEmpty: *omitEmptyFlag,
Renderer: renderer,
Rules: yrs,
ScanPaths: args,
Stats: stats,
ErrFirstHit: *errFirstHitFlag,
ErrFirstMiss: *errFirstMissFlag,
}
var res *bincapz.Report
if *diffFlag {
res, err = action.Diff(ctx, bc)
} else {
res, err = action.Scan(ctx, bc)
}
if err != nil {
returnCode = ExitActionFailed
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
err = renderer.Full(ctx, res)
if err != nil {
returnCode = ExitRenderFailed
log.Error("render failed", slog.Any("error", err))
return
}
if *profileFlag {
stop()
}
}