forked from yanivagman/tracee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
608 lines (534 loc) · 17.5 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
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"github.com/aquasecurity/libbpfgo/helpers"
embed "github.com/aquasecurity/tracee"
"github.com/aquasecurity/tracee/cmd/tracee-ebpf/internal/flags"
"github.com/aquasecurity/tracee/pkg/capabilities"
tracee "github.com/aquasecurity/tracee/pkg/ebpf"
"github.com/aquasecurity/tracee/pkg/metrics"
"github.com/aquasecurity/tracee/types/trace"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/syndtr/gocapability/capability"
cli "github.com/urfave/cli/v2"
)
var traceeInstallPath string
var listenMetrics bool
var metricsAddr string
var version string
func main() {
app := &cli.App{
Name: "Tracee",
Usage: "Trace OS events and syscalls using eBPF",
Version: version,
Action: func(c *cli.Context) error {
// tracee-ebpf does not support arguments, only flags
if c.NArg() > 0 {
return cli.ShowAppHelp(c)
}
if c.Bool("list") {
printList()
return nil
}
// enable debug mode if debug flag is passed
if c.Bool("debug") {
err := flags.EnableDebugMode()
if err != nil {
return fmt.Errorf("failed to start debug mode: %v", err)
}
}
// for the rest of execution, use this debug mode value
debug := flags.DebugModeEnabled()
cfg := tracee.Config{
PerfBufferSize: c.Int("perf-buffer-size"),
BlobPerfBufferSize: c.Int("blob-perf-buffer-size"),
Debug: debug,
}
cacheSlice := c.StringSlice("cache")
if checkCommandIsHelp(cacheSlice) {
fmt.Print(flags.CacheHelp())
return nil
}
cache, err := flags.PrepareCache(cacheSlice)
if err != nil {
return err
}
cfg.Cache = cache
if debug && cfg.Cache != nil {
fmt.Fprintf(os.Stdout, "Cache: cache type is \"%s\"\n", cfg.Cache)
}
captureSlice := c.StringSlice("capture")
if checkCommandIsHelp(captureSlice) {
fmt.Print(flags.CaptureHelp())
return nil
}
capture, err := flags.PrepareCapture(captureSlice)
if err != nil {
return err
}
cfg.Capture = &capture
traceSlice := c.StringSlice("trace")
if checkCommandIsHelp(traceSlice) {
fmt.Print(flags.FilterHelp())
return nil
}
filter, err := flags.PrepareFilter(traceSlice)
if err != nil {
return err
}
cfg.Filter = &filter
containerMode := (cfg.Filter.ContFilter.Enabled && cfg.Filter.ContFilter.Value) ||
(cfg.Filter.NewContFilter.Enabled && cfg.Filter.NewContFilter.Value) ||
cfg.Filter.ContIDFilter.Enabled
outputSlice := c.StringSlice("output")
if checkCommandIsHelp(outputSlice) {
fmt.Print(flags.OutputHelp())
return nil
}
output, printerConfig, err := flags.PrepareOutput(outputSlice)
if err != nil {
return err
}
cfg.Output = &output
// kernel lockdown check
lockdown, err := helpers.Lockdown()
if err == nil && lockdown == helpers.CONFIDENTIALITY {
return fmt.Errorf("kernel lockdown is set to 'confidentiality', can't load eBPF programs.")
}
if debug {
fmt.Fprintf(os.Stdout, "OSInfo: Security Lockdown is '%v'\n", lockdown)
}
// environment capabilities
selfCap, err := capabilities.Self()
if err != nil {
return err
}
if err = capabilities.CheckRequired(selfCap, []capability.Cap{capability.CAP_IPC_LOCK, capability.CAP_SYS_ADMIN}); err != nil {
return err
}
enabled, err := helpers.FtraceEnabled()
if err != nil {
return err
}
if !enabled {
fmt.Fprintf(os.Stderr, "ftrace_enabled: warning: ftrace is not enabled, kernel events won't be caught, make sure to enable it by executing echo 1 | sudo tee /proc/sys/kernel/ftrace_enabled")
}
// OS kconfig information
kernelConfig, err := helpers.InitKernelConfig()
if err == nil { // do not fail (yet ?) if we cannot init kconfig
kernelConfig.AddNeeded(helpers.CONFIG_BPF, helpers.BUILTIN)
kernelConfig.AddNeeded(helpers.CONFIG_BPF_SYSCALL, helpers.BUILTIN)
kernelConfig.AddNeeded(helpers.CONFIG_KPROBE_EVENTS, helpers.BUILTIN)
kernelConfig.AddNeeded(helpers.CONFIG_BPF_EVENTS, helpers.BUILTIN)
missing := kernelConfig.CheckMissing() // do fail if we found os-release file and it is not enough
if len(missing) > 0 {
return fmt.Errorf("missing kernel configuration options: %s\n", missing)
}
} else {
if debug {
fmt.Fprintf(os.Stderr, "KConfig: warning: could not check enabled kconfig features\n(%v)\n", err)
fmt.Fprintf(os.Stderr, "KConfig: warning: assuming kconfig values, might have unexpected behavior\n")
}
}
// OS release information
OSInfo, err := helpers.GetOSInfo()
if err != nil {
if debug {
fmt.Fprintf(os.Stderr, "OSInfo: warning: os-release file could not be found\n(%v)\n", err) // only to be enforced when BTF needs to be downloaded, later on
fmt.Fprintf(os.Stdout, "OSInfo: %v: %v\n", helpers.OS_KERNEL_RELEASE, OSInfo.GetOSReleaseFieldValue(helpers.OS_KERNEL_RELEASE))
}
} else if debug {
for k, v := range OSInfo.GetOSReleaseAllFieldValues() {
fmt.Fprintf(os.Stdout, "OSInfo: %v: %v\n", k, v)
}
}
// decide BTF & BPF files to use based on kconfig, release & environment
err = prepareBpfObject(&cfg, kernelConfig, OSInfo)
if err != nil {
return fmt.Errorf("failed preparing BPF object: %w", err)
}
cfg.ChanEvents = make(chan trace.Event)
cfg.ChanErrors = make(chan error)
t, err := tracee.New(cfg)
if err != nil {
return fmt.Errorf("error creating Tracee: %v", err)
}
if listenMetrics {
err := metrics.RegisterPrometheus(t.Stats())
if err != nil {
fmt.Fprintf(os.Stderr, "Error registering prometheus metrics: %v\n", err)
} else {
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
go func() {
if debug {
fmt.Fprintf(os.Stdout, "Serving metrics endpoint at %s\n", metricsAddr)
}
if err := http.ListenAndServe(metricsAddr, mux); err != http.ErrServerClosed {
fmt.Fprintf(os.Stderr, "Error serving metrics endpoint: %v\n", err)
}
}()
}
}
if err := os.MkdirAll(cfg.Capture.OutputPath, 0755); err != nil {
t.Close()
return fmt.Errorf("error creating output path: %v", err)
}
err = ioutil.WriteFile(path.Join(cfg.Capture.OutputPath, "tracee.pid"), []byte(strconv.Itoa(os.Getpid())+"\n"), 0640)
if err != nil {
t.Close()
return fmt.Errorf("error creating readiness file: %v", err)
}
if printerConfig.OutFile == nil {
printerConfig.OutFile, err = os.OpenFile(printerConfig.OutPath, os.O_WRONLY, 0755)
if err != nil {
return err
}
}
if printerConfig.ErrFile == nil {
printerConfig.ErrFile, err = os.OpenFile(printerConfig.ErrPath, os.O_WRONLY, 0755)
if err != nil {
return err
}
}
printer, err := newEventPrinter(printerConfig.Kind, containerMode, cfg.Output.RelativeTime, printerConfig.OutFile, printerConfig.ErrFile)
if err != nil {
return err
}
// create a context that is cancelled by SIGINT/SIGTERM
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
defer func() {
signal.Stop(sig)
cancel()
}()
go func() {
select {
case <-sig:
cancel()
case <-ctx.Done():
}
}()
go func() {
printer.Preamble()
for {
select {
case event := <-cfg.ChanEvents:
printer.Print(event)
case err := <-cfg.ChanErrors:
printer.Error(err)
case <-ctx.Done():
return
}
}
}()
// always print stats before exiting
defer func() {
stats := t.Stats()
printer.Epilogue(*stats)
printer.Close()
}()
// run until ctx is cancelled by signal
return t.Run(ctx)
},
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "list",
Aliases: []string{"l"},
Value: false,
Usage: "just list tracable events",
},
&cli.StringSliceFlag{
Name: "trace",
Aliases: []string{"t"},
Value: nil,
Usage: "select events to trace by defining trace expressions. run '--trace help' for more info.",
},
&cli.StringSliceFlag{
Name: "capture",
Aliases: []string{"c"},
Value: nil,
Usage: "capture artifacts that were written, executed or found to be suspicious. run '--capture help' for more info.",
},
&cli.StringSliceFlag{
Name: "output",
Aliases: []string{"o"},
Value: cli.NewStringSlice("format:table"),
Usage: "Control how and where output is printed. run '--output help' for more info.",
},
&cli.StringSliceFlag{
Name: "cache",
Aliases: []string{"a"},
Value: cli.NewStringSlice("none"),
Usage: "Control event caching queues. run '--cache help' for more info.",
},
&cli.IntFlag{
Name: "perf-buffer-size",
Aliases: []string{"b"},
Value: 1024, // 4 MB of contigous pages
Usage: "size, in pages, of the internal perf ring buffer used to submit events from the kernel",
},
&cli.IntFlag{
Name: "blob-perf-buffer-size",
Value: 1024, // 4 MB of contigous pages
Usage: "size, in pages, of the internal perf ring buffer used to send blobs from the kernel",
},
&cli.BoolFlag{
Name: "debug",
Value: false,
Usage: "write verbose debug messages to standard output and retain intermediate artifacts. enabling will output debug messages to stdout, which will likely break consumers which expect to receive machine-readable events from stdout",
},
&cli.StringFlag{
Name: "install-path",
Value: "/tmp/tracee",
Usage: "path where tracee will install or lookup it's resources",
Destination: &traceeInstallPath,
},
&cli.BoolFlag{
Name: "metrics",
Usage: "enable metrics endpoint",
Destination: &listenMetrics,
Value: false,
},
&cli.StringFlag{
Name: "metrics-addr",
Usage: "listening address of the metrics endpoint server",
Value: ":3366",
Destination: &metricsAddr,
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
func prepareBpfObject(config *tracee.Config, kConfig *helpers.KernelConfig, OSInfo *helpers.OSInfo) error {
var d = struct {
btfenv bool
bpfenv bool
btfvmlinux bool
}{
btfenv: false,
bpfenv: false,
btfvmlinux: helpers.OSBTFEnabled(),
}
debug := config.Debug
bpfFilePath, err := checkEnvPath("TRACEE_BPF_FILE")
if bpfFilePath != "" {
d.bpfenv = true
} else if bpfFilePath == "" && err != nil {
return err
}
btfFilePath, err := checkEnvPath("TRACEE_BTF_FILE")
if btfFilePath != "" {
d.btfenv = true
} else if btfFilePath == "" && err != nil {
return err
}
if debug {
fmt.Printf("BTF: bpfenv = %v, btfenv = %v, vmlinux = %v\n", d.bpfenv, d.btfenv, d.btfvmlinux)
}
var tVersion, kVersion string
var bpfBytes []byte
var unpackBTFFile string
// Decision ordering:
// (1) BPF file given & BTF (vmlinux or env) exists: always load BPF as CO-RE
// (2) BPF file given & if no BTF exists: it is a non CO-RE BPF
if d.bpfenv {
if debug {
fmt.Printf("BPF: using BPF object from environment: %v\n", bpfFilePath)
}
if d.btfvmlinux || d.btfenv { // (1)
if d.btfenv {
if debug {
fmt.Printf("BTF: using BTF file from environment: %v\n", btfFilePath)
}
config.BTFObjPath = btfFilePath
}
} // else {} (2)
if bpfBytes, err = ioutil.ReadFile(bpfFilePath); err != nil {
return err
}
goto out
}
// (3) no BPF file given & BTF (vmlinux or env) exists: load embedded BPF as CO-RE
if d.btfvmlinux || d.btfenv { // (3)
if debug {
fmt.Println("BPF: using embedded BPF object")
}
if d.btfenv {
if debug {
fmt.Printf("BTF: using BTF file from environment: %v\n", btfFilePath)
}
config.BTFObjPath = btfFilePath
}
bpfFilePath = "embedded-core"
bpfBytes, err = unpackCOREBinary()
if err != nil {
return fmt.Errorf("could not unpack embedded CO-RE eBPF object: %v", err)
}
goto out
}
// (4) no BPF file given & no BTF available: check embedded BTF files
unpackBTFFile = filepath.Join(traceeInstallPath, "/tracee.btf")
err = unpackBTFHub(unpackBTFFile, OSInfo)
if err == nil {
if debug {
fmt.Printf("BTF: using BTF file from embedded btfhub: %v\n", unpackBTFFile)
}
config.BTFObjPath = unpackBTFFile
bpfFilePath = "embedded-core"
bpfBytes, err = unpackCOREBinary()
if err != nil {
return fmt.Errorf("could not unpack embedded CO-RE eBPF object: %v", err)
}
goto out
}
// (5) no BPF file given & no BTF available & no embedded BTF: non CO-RE BPF
tVersion = strings.ReplaceAll(version, "\"", "")
tVersion = strings.ReplaceAll(tVersion, ".", "_")
kVersion = OSInfo.GetOSReleaseFieldValue(helpers.OS_KERNEL_RELEASE)
kVersion = strings.ReplaceAll(kVersion, ".", "_")
bpfFilePath = fmt.Sprintf("%s/tracee.bpf.%s.%s.o", traceeInstallPath, kVersion, tVersion)
if debug {
fmt.Printf("BPF: no BTF file was found or provided\n")
fmt.Printf("BPF: trying non CO-RE eBPF at %s\n", bpfFilePath)
}
if bpfBytes, err = ioutil.ReadFile(bpfFilePath); err != nil {
// tell entrypoint that eBPF non CO-RE obj compilation is needed
fmt.Printf("BPF: %v\n", err)
fmt.Printf("BPF: ATTENTION:\n")
fmt.Printf("BPF: It seems tracee-ebpf can't load CO-RE eBPF obj and could not find\n")
fmt.Printf("BPF: the non CO-RE object in %s. You may build a non CO-RE eBPF\n", traceeInstallPath)
fmt.Printf("BPF: obj by using the source tree and executing \"make install-bpf-nocore\".\n")
os.Exit(2)
}
out:
config.KernelConfig = kConfig
config.BPFObjPath = bpfFilePath
config.BPFObjBytes = bpfBytes
return nil
}
func checkCommandIsHelp(s []string) bool {
if len(s) == 1 && s[0] == "help" {
return true
}
return false
}
func getFormattedEventParams(eventID int32) string {
eventParams := tracee.EventsDefinitions[eventID].Params
var verboseEventParams string
verboseEventParams += "("
prefix := ""
for index, arg := range eventParams {
if index == 0 {
verboseEventParams += arg.Type + " " + arg.Name
prefix = ", "
continue
}
verboseEventParams += prefix + arg.Type + " " + arg.Name
}
verboseEventParams += ")"
return verboseEventParams
}
func getPad(padChar string, padLength int) (pad string) {
for i := 0; i < padLength; i++ {
pad += padChar
}
return
}
func printList() {
padChar, firstPadLen, secondPadLen := " ", 9, 36
titleHeaderPadFirst := getPad(padChar, firstPadLen)
titleHeaderPadSecond := getPad(padChar, secondPadLen)
var b strings.Builder
b.WriteString("System Calls: " + titleHeaderPadFirst + "Sets:" + titleHeaderPadSecond + "Arguments:\n")
b.WriteString("____________ " + titleHeaderPadFirst + "____ " + titleHeaderPadSecond + "_________" + "\n\n")
printEventGroup(&b, 0, int(tracee.SysEnterEventID))
printEventGroup(&b, tracee.Unique32BitSyscallsStartID, int(tracee.Unique32BitSyscallsEndID))
b.WriteString("\n\nOther Events: " + titleHeaderPadFirst + "Sets:" + titleHeaderPadSecond + "Arguments:\n")
b.WriteString("____________ " + titleHeaderPadFirst + "____ " + titleHeaderPadSecond + "_________\n\n")
printEventGroup(&b, int(tracee.SysEnterEventID), int(tracee.MaxCommonEventID))
printEventGroup(&b, int(tracee.InitNamespacesEventID), int(tracee.MaxUserSpaceEventID))
b.WriteString("\n\nNetwork Events: " + titleHeaderPadFirst + "Sets:" + titleHeaderPadSecond + "Arguments:\n")
b.WriteString("____________ " + titleHeaderPadFirst + "____ " + titleHeaderPadSecond + "_________\n\n")
printEventGroup(&b, int(tracee.NetPacket), int(tracee.MaxNetEventID))
fmt.Println(b.String())
}
func printEventGroup(b *strings.Builder, firstEventID, lastEventID int) {
for i := firstEventID; i < lastEventID; i++ {
index := int32(i)
event, ok := tracee.EventsDefinitions[index]
if !ok {
continue
}
if event.Sets != nil {
eventSets := fmt.Sprintf("%-22s %-40s %s\n", event.Name, fmt.Sprintf("%v", event.Sets), getFormattedEventParams(index))
b.WriteString(eventSets)
} else {
b.WriteString(event.Name + "\n")
}
}
}
func checkEnvPath(env string) (string, error) {
filePath, _ := os.LookupEnv(env)
if filePath != "" {
_, err := os.Stat(filePath)
if err != nil {
return "", fmt.Errorf("could not open %s %s", env, filePath)
}
return filePath, nil
}
return "", nil
}
func unpackCOREBinary() ([]byte, error) {
b, err := embed.BPFBundleInjected.ReadFile("dist/tracee.bpf.core.o")
if err != nil {
return nil, err
}
if flags.DebugModeEnabled() {
fmt.Println("unpacked CO:RE bpf object file into memory")
}
return b, nil
}
// unpackBTFHub unpacks tailored, to the compiled eBPF object, BTF files for kernel supported by BTFHub
func unpackBTFHub(outFilePath string, OSInfo *helpers.OSInfo) error {
var btfFilePath string
osId := OSInfo.GetOSReleaseFieldValue(helpers.OS_ID)
versionId := strings.Replace(OSInfo.GetOSReleaseFieldValue(helpers.OS_VERSION_ID), "\"", "", -1)
kernelRelease := OSInfo.GetOSReleaseFieldValue(helpers.OS_KERNEL_RELEASE)
arch := OSInfo.GetOSReleaseFieldValue(helpers.OS_ARCH)
if err := os.MkdirAll(filepath.Dir(outFilePath), 0755); err != nil {
return fmt.Errorf("could not create temp dir: %s", err.Error())
}
btfFilePath = fmt.Sprintf("dist/btfhub/%s/%s/%s/%s.btf", osId, versionId, arch, kernelRelease)
btfFile, err := embed.BPFBundleInjected.Open(btfFilePath)
if err != nil {
return fmt.Errorf("error opening embedded btfhub file: %s", err.Error())
}
defer btfFile.Close()
outFile, err := os.Create(outFilePath)
if err != nil {
return fmt.Errorf("could not create btf file: %s", err.Error())
}
defer outFile.Close()
if _, err := io.Copy(outFile, btfFile); err != nil {
return fmt.Errorf("error copying embedded btfhub file: %s", err.Error())
}
return nil
}