/
common.go
260 lines (222 loc) · 5.74 KB
/
common.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
package pcaps
import (
"fmt"
"math"
"os"
"strings"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
"github.com/aquasecurity/tracee/pkg/config"
"github.com/aquasecurity/tracee/pkg/errfmt"
"github.com/aquasecurity/tracee/pkg/logger"
"github.com/aquasecurity/tracee/pkg/utils"
"github.com/aquasecurity/tracee/types/trace"
)
var outputDirectory *os.File
var fake pcapgo.NgInterface
const (
pcapDir string = "pcap/"
pcapSingleDir string = pcapDir
pcapProcDir string = pcapDir + "processes/"
pcapContDir string = pcapDir + "containers/"
pcapCommDir string = pcapDir + "commands/"
)
const (
pcapsToCache = 100 // number of pcaps to keep cached in LRU cache
flushAtPackets = 1 // flush pcap file after X number of packets
)
// TODO: flush per interval as well (so we can grow flushAtPackets higher)
const AF_INET = 2
const AF_INET6 = 10
//
// Functions
//
func initializeGlobalVars(output *os.File) {
outputDirectory = output // where to save pcap files
// fake interface to be added to each pcap file (needed)
fake = pcapgo.NgInterface{ // https://www.tcpdump.org/linktypes.html
Name: "tracee",
Comment: "trace fake interface",
Description: "non-existing interface",
LinkType: layers.LinkTypeNull, // layer2 is 4 bytes (or 32bit)
SnapLength: uint32(math.MaxUint32),
}
}
// getItemIndexFromEvent returns correct trace.Event variable according to
// given PcapType
func getItemIndexFromEvent(event *trace.Event, itemType PcapType) string {
switch itemType {
case Single:
return itemType.String()
case Process:
return fmt.Sprint(event.HostThreadID)
case Container:
return event.Container.ID
case Command:
// index by using container_id and comm (to avoid having all
// captured packets in command/host/XXX, as it would occur if
// indexing by container_id only).
ret := fmt.Sprintf("%s:%s", event.Container.ID, event.ProcessName)
return ret
}
return ""
}
// getPcapFileName returns a string used to create a pcap file under the
// capture output directory.
func getPcapFileName(event *trace.Event, pcapType PcapType) (string, error) {
var err error
contID := getContainerID(event.Container.ID)
// create needed dirs
err = mkdirForPcapType(outputDirectory, contID, pcapType)
if err != nil {
return "", errfmt.WrapError(err)
}
// return filename in format according to pcap type
return getFileStringFormat(event, contID, pcapType), nil
}
// getContainerID returns the container string to be used in pcap files or dirs
func getContainerID(contID string) string {
if contID == "" {
return "host"
}
if len(contID) > 11 {
contID = contID[0:11]
}
return contID
}
// getFileStringFormat creates the string that will hold the pcap filename
func getFileStringFormat(e *trace.Event, c string, t PcapType) string {
var format string
switch t {
case Single:
format = fmt.Sprintf(
"%s%s.pcap", // single.pcap
pcapSingleDir,
strings.ToLower(t.String()),
)
case Process:
format = fmt.Sprintf(
pcapProcDir+"%v/%v_%v_%v.pcap",
c,
e.ProcessName,
e.HostThreadID,
e.ThreadStartTime,
)
case Container:
format = fmt.Sprintf(
pcapContDir+"%v.pcap",
c,
)
case Command:
format = fmt.Sprintf(
pcapCommDir+"%v/%v.pcap",
c,
e.ProcessName,
)
}
return format
}
// mkdirForPcapType creates the dir that will hold the pcap file(s)
func mkdirForPcapType(o *os.File, c string, t PcapType) error {
var e error
s := "pcap"
e = utils.MkdirAtExist(o, s, os.ModePerm)
if e != nil {
return errfmt.WrapError(e)
}
switch t {
case Single:
e = utils.MkdirAtExist(o, pcapSingleDir, os.ModePerm)
if e != nil {
return errfmt.WrapError(e)
}
case Process:
e = utils.MkdirAtExist(o, pcapProcDir, os.ModePerm)
if e != nil {
return errfmt.WrapError(e)
}
e = utils.MkdirAtExist(o, pcapProcDir+c, os.ModePerm)
if e != nil {
return errfmt.WrapError(e)
}
case Container:
e = utils.MkdirAtExist(o, pcapContDir, os.ModePerm)
if e != nil {
return errfmt.WrapError(e)
}
case Command:
e = utils.MkdirAtExist(o, pcapCommDir, os.ModePerm)
if e != nil {
return errfmt.WrapError(e)
}
e = utils.MkdirAtExist(o, pcapCommDir+c, os.ModePerm)
if e != nil {
return errfmt.WrapError(e)
}
}
return nil
}
// getPcapFileAndWriter returns a file descriptor and and its associated pcap
// writer depending on the type "t" given (a Pcap interface implementation).
func getPcapFileAndWriter(event *trace.Event, t PcapType) (
*os.File,
*pcapgo.NgWriter,
error,
) {
pcapFilePath, err := getPcapFileName(event, t)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
file, err := utils.OpenAt(
outputDirectory,
pcapFilePath,
os.O_APPEND|os.O_WRONLY|os.O_CREATE,
0644,
)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
logger.Debugw("pcap file (re)opened", "filename", pcapFilePath)
writer, err := pcapgo.NewNgWriterInterface(
file,
fake,
pcapgo.DefaultNgWriterOptions,
)
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
err = writer.Flush()
if err != nil {
return nil, nil, errfmt.WrapError(err)
}
return file, writer, nil
}
// configToPcapType converts a simple bool like config struct to internal config
func configToPcapType(simple config.PcapsConfig) PcapType {
var cfg PcapType
if simple.CaptureSingle {
cfg |= Single
}
if simple.CaptureProcess {
cfg |= Process
}
if simple.CaptureContainer {
cfg |= Container
}
if simple.CaptureCommand {
cfg |= Command
}
return cfg
}
// PcapsEnabled checks if the simple config has any bool value set
func PcapsEnabled(simple config.PcapsConfig) bool {
cfg := configToPcapType(simple)
return cfg != None
}
func GetPcapOptions(c config.PcapsConfig) PcapOption {
var options PcapOption
if c.CaptureFiltered {
options |= Filtered
}
return options
}