/
config.go
337 lines (299 loc) · 12.3 KB
/
config.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
/*
* Copyright 2019-2020 by Nedim Sabic Sabic
* https://www.fibratus.io
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"encoding/json"
"fmt"
"github.com/rabbitstack/fibratus/pkg/aggregator"
"github.com/rabbitstack/fibratus/pkg/aggregator/transformers"
removet "github.com/rabbitstack/fibratus/pkg/aggregator/transformers/remove"
replacet "github.com/rabbitstack/fibratus/pkg/aggregator/transformers/replace"
tagst "github.com/rabbitstack/fibratus/pkg/aggregator/transformers/tags"
"github.com/rabbitstack/fibratus/pkg/kevent"
"github.com/rabbitstack/fibratus/pkg/outputs/amqp"
"github.com/rabbitstack/fibratus/pkg/outputs/elasticsearch"
"github.com/rabbitstack/fibratus/pkg/util/log"
"github.com/rabbitstack/fibratus/pkg/util/multierror"
yara "github.com/rabbitstack/fibratus/pkg/yara/config"
"gopkg.in/yaml.v2"
"io/ioutil"
"time"
renamet "github.com/rabbitstack/fibratus/pkg/aggregator/transformers/rename"
trimt "github.com/rabbitstack/fibratus/pkg/aggregator/transformers/trim"
"github.com/rabbitstack/fibratus/pkg/alertsender"
mailsender "github.com/rabbitstack/fibratus/pkg/alertsender/mail"
slacksender "github.com/rabbitstack/fibratus/pkg/alertsender/slack"
"github.com/rabbitstack/fibratus/pkg/outputs"
"github.com/rabbitstack/fibratus/pkg/outputs/console"
"github.com/rabbitstack/fibratus/pkg/pe"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"os"
"path/filepath"
"strings"
)
const (
kcapFile = "kcap.file"
configFile = "config-file"
debugPrivilege = "debug-privilege"
initHandleSnapshot = "handle.init-snapshot"
serializeThreads = "kevent.serialize-threads"
serializeImages = "kevent.serialize-images"
serializeHandles = "kevent.serialize-handles"
serializePE = "kevent.serialize-pe"
serializeEnvs = "kevent.serialize-envs"
)
// Config stores configuration options for fine tuning the behaviour of Fibratus.
type Config struct {
// Kstream stores different configuration options for fine tuning kstream consumer/controller settings.
Kstream KstreamConfig `json:"kstream" yaml:"kstream"`
// Filament contains filament settings
Filament FilamentConfig `json:"filament" yaml:"filament"`
// PE contains the settings that influences the behaviour of the PE (Portable Executable) reader.
PE pe.Config `json:"pe" yaml:"pe"`
// Output stores the currently active output config
Output outputs.Config
// InitHandleSnapshot indicates whether initial handle snapshot is built
InitHandleSnapshot bool `json:"init-handle-snapshot" yaml:"init-handle-snapshot"`
DebugPrivilege bool `json:"debug-privilege" yaml:"debug-privilege"`
KcapFile string
// API stores global HTTP API preferences
API APIConfig `json:"api" yaml:"api"`
// Yara contains configuration that influences the behaviour of the Yara engine
Yara yara.Config `json:"yara" yaml:"yara"`
// Aggregator stores event aggregator configuration
Aggregator aggregator.Config `json:"aggregator" yaml:"aggregator"`
// Log contains log-specific configuration options
Log log.Config `json:"logging" yaml:"logging"`
// Transformers stores transformer configurations
Transformers []transformers.Config
// Alertsenders stores alert sender configurations
Alertsenders []alertsender.Config
flags *pflag.FlagSet
viper *viper.Viper
opts *Options
}
// Options determines which config flags are toggled depending on the command type.
type Options struct {
capture bool
replay bool
run bool
list bool
stats bool
}
// Option is the type alias for the config option.
type Option func(*Options)
// WithCapture determines the capture command is executed.
func WithCapture() Option {
return func(o *Options) {
o.capture = true
}
}
// WithReplay determines the replay command is executed.
func WithReplay() Option {
return func(o *Options) {
o.replay = true
}
}
// WithRun determines the main command is executed.
func WithRun() Option {
return func(o *Options) {
o.run = true
}
}
// WithList determines the list command is executed.
func WithList() Option {
return func(o *Options) {
o.list = true
}
}
// WithStats determines the stats command is executed.
func WithStats() Option {
return func(o *Options) {
o.stats = true
}
}
// NewWithOpts builds a new configuration store from a variety of sources such as configuration files,
// environment variables or command line flags.
func NewWithOpts(options ...Option) *Config {
opts := &Options{}
for _, opt := range options {
opt(opts)
}
v := viper.New()
v.AutomaticEnv()
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
flagSet := new(pflag.FlagSet)
c := &Config{
Kstream: KstreamConfig{},
Filament: FilamentConfig{},
API: APIConfig{},
PE: pe.Config{},
Log: log.Config{},
Aggregator: aggregator.Config{},
viper: v,
flags: flagSet,
opts: opts,
}
if opts.run || opts.replay {
aggregator.AddFlags(flagSet)
console.AddFlags(flagSet)
amqp.AddFlags(flagSet)
elasticsearch.AddFlags(flagSet)
removet.AddFlags(flagSet)
replacet.AddFlags(flagSet)
renamet.AddFlags(flagSet)
trimt.AddFlags(flagSet)
tagst.AddFlags(flagSet)
mailsender.AddFlags(flagSet)
slacksender.AddFlags(flagSet)
yara.AddFlags(flagSet)
}
if opts.run || opts.capture {
pe.AddFlags(flagSet)
}
c.addFlags()
return c
}
// GetConfigFile gets the path of the configuration file from Viper value.
func (c Config) GetConfigFile() string {
return c.viper.GetString(configFile)
}
// MustViperize adds the flag set to the Cobra command and binds them within the Viper flags.
func (c *Config) MustViperize(cmd *cobra.Command) {
cmd.PersistentFlags().AddFlagSet(c.flags)
if err := c.viper.BindPFlags(cmd.PersistentFlags()); err != nil {
panic(err)
}
if c.opts.capture || c.opts.replay {
if err := cmd.MarkPersistentFlagRequired(kcapFile); err != nil {
panic(err)
}
}
}
// Init setups the configuration state from Viper.
func (c *Config) Init() error {
c.Kstream.initFromViper(c.viper)
c.Filament.initFromViper(c.viper)
c.API.initFromViper(c.viper)
c.PE.InitFromViper(c.viper)
c.Aggregator.InitFromViper(c.viper)
c.Log.InitFromViper(c.viper)
c.Yara.InitFromViper(c.viper)
c.InitHandleSnapshot = c.viper.GetBool(initHandleSnapshot)
c.DebugPrivilege = c.viper.GetBool(debugPrivilege)
c.KcapFile = c.viper.GetString(kcapFile)
kevent.SerializeThreads = c.viper.GetBool(serializeThreads)
kevent.SerializeImages = c.viper.GetBool(serializeImages)
kevent.SerializeHandles = c.viper.GetBool(serializeHandles)
kevent.SerializePE = c.viper.GetBool(serializePE)
kevent.SerializeEnvs = c.viper.GetBool(serializeEnvs)
if c.opts.run || c.opts.replay {
if err := c.tryLoadOutput(); err != nil {
return err
}
if err := c.tryLoadTransformers(); err != nil {
return err
}
if err := c.tryLoadAlertSenders(); err != nil {
return err
}
}
return nil
}
// TryLoadFile attempts to load the configuration file from specified path on the file system.
func (c *Config) TryLoadFile(file string) error {
c.viper.SetConfigFile(file)
return c.viper.ReadInConfig()
}
// Validate ensures that all configuration options provided by user have the expected values. It returns
// a list of validation errors prefixed with the offending configuration property/flag.
func (c *Config) Validate() error {
// we'll first validate the structure and values of the config file
file := c.viper.GetString(configFile)
var out interface{}
b, err := ioutil.ReadFile(file)
if err != nil {
return err
}
switch filepath.Ext(file) {
case ".yaml", ".yml":
err = yaml.Unmarshal(b, &out)
case ".json":
err = json.Unmarshal(b, &out)
default:
return fmt.Errorf("%s is not a supported config file extension", filepath.Ext(file))
}
if err != nil {
return fmt.Errorf("couldn't read the config file: %v", err)
}
// validate config file content
valid, errs := validate(out)
if !valid || len(errs) > 0 {
return fmt.Errorf("invalid config: %v", multierror.Wrap(errs...))
}
// now validate the Viper config flags
valid, errs = validate(c.viper.AllSettings())
if !valid || len(errs) > 0 {
return fmt.Errorf("invalid config: %v", multierror.Wrap(errs...))
}
return nil
}
// File returns the config file path.
func (c *Config) File() string { return c.viper.GetString(configFile) }
func (c *Config) addFlags() {
c.flags.String(configFile, filepath.Join(os.Getenv("PROGRAMFILES"), "fibratus", "config", "fibratus.yml"), "Indicates the location of the configuration file")
if c.opts.run || c.opts.replay {
c.flags.StringP(filamentName, "f", "", "Specifies the filament to execute")
}
if c.opts.capture {
c.flags.StringP(kcapFile, "o", "", "The path of the output kcap file")
}
if c.opts.replay {
c.flags.StringP(kcapFile, "k", "", "The path of the input kcap file")
}
if c.opts.run || c.opts.replay || c.opts.list {
c.flags.String(filamentPath, filepath.Join(os.Getenv("PROGRAMFILES"), "fibratus", "filaments"), "Denotes the directory where filaments are located")
}
if c.opts.run || c.opts.replay || c.opts.capture || c.opts.stats {
c.flags.String(transport, `localhost:8080`, "Specifies the underlying transport protocol for the API HTTP server")
c.flags.Duration(timeout, time.Second*15, "Determines the timeout for the API server responses")
}
if c.opts.run || c.opts.capture {
c.flags.Bool(initHandleSnapshot, true, "Indicates whether initial handle snapshot is built. This implies scanning the system handles table and producing an entry for each handle object")
c.flags.Bool(enableThreadKevents, true, "Determines whether thread kernel events are collected by Kernel Logger provider")
c.flags.Bool(enableRegistryKevents, true, "Determines whether registry kernel events are collected by Kernel Logger provider")
c.flags.Bool(enableNetKevents, true, "Determines whether network (TCP/UDP) kernel events are collected by Kernel Logger provider")
c.flags.Bool(enableFileIOKevents, true, "Determines whether disk I/O kernel events are collected by Kernel Logger provider")
c.flags.Bool(enableImageKevents, true, "Determines whether file I/O kernel events are collected by Kernel Logger provider")
c.flags.Bool(enableHandleKevents, false, "Determines whether object manager kernel events (handle creation/destruction) are collected by Kernel Logger provider")
c.flags.Int(bufferSize, int(maxBufferSize), "Represents the amount of memory allocated for each event tracing session buffer, in kilobytes. The buffer size affects the rate at which buffers fill and must be flushed (small buffer size requires less memory but it increases the rate at which buffers must be flushed)")
c.flags.Int(minBuffers, int(defaultMinBuffers), "Determines the minimum number of buffers allocated for the event tracing session's buffer pool")
c.flags.Int(maxBuffers, int(defaultMaxBuffers), "Determines the maximum number of buffers allocated for the event tracing session's buffer pool")
c.flags.Duration(flushInterval, defaultFlushInterval, "Specifies how often the trace buffers are forcibly flushed")
c.flags.StringSlice(blacklistEvents, []string{}, "A list of symbolical kernel event names that will be dropped from the kernel event stream. By default all events are accepted")
c.flags.StringSlice(blacklistImages, []string{"System"}, "A list of image names that will be dropped from the kernel event stream. Image names are case insensitive")
c.flags.Bool(serializeThreads, false, "Indicates if threads are serialized as part of the process state")
c.flags.Bool(serializeImages, false, "Indicates if images are serialized as part of the process state")
c.flags.Bool(serializeHandles, false, "Indicates if handles are serialized as part of the process state")
c.flags.Bool(serializePE, false, "Indicates if the PE metadata are serialized as part of the process state")
c.flags.Bool(serializeEnvs, true, "Indicates if environment variables are serialized as part of the process state")
}
c.Log.AddFlags(c.flags)
}