forked from slackhq/go-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audit.go
215 lines (176 loc) · 5.2 KB
/
audit.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
//There is one rule here. "thou shall not block"
//Slack Technologies, Inc 2015
//Ryan Huber
package main
import (
"flag"
"github.com/pkg/profile"
"github.com/spf13/viper"
"log"
"log/syslog"
"os"
"os/exec"
"regexp"
"strconv"
"strings"
)
var l = log.New(os.Stdout, "", 0)
var el = log.New(os.Stderr, "", 0)
func loadConfig(config *viper.Viper) {
config.SetDefault("message_tracking.enabled", true)
config.SetDefault("message_tracking.log_out_of_order", false)
config.SetDefault("message_tracking.max_out_of_order", 500)
config.SetDefault("output.syslog.enabled", false)
config.SetDefault("output.syslog.priority", int(syslog.LOG_LOCAL0|syslog.LOG_WARNING))
config.SetDefault("output.syslog.tag", "go-audit")
config.SetDefault("output.syslog.attempts", "3")
config.SetDefault("log.flags", 0)
err := config.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
el.Printf("Config file has an error: %s\n", err)
os.Exit(1)
}
}
func setRules(config *viper.Viper) {
// Clear existing rules
err := exec.Command("auditctl", "-D").Run()
if err != nil {
el.Fatalf("Failed to flush existing audit rules. Error: %s\n", err)
}
l.Println("Flushed existing audit rules")
// Add ours in
if rules := config.GetStringSlice("rules"); len(rules) != 0 {
for i, v := range rules {
// Skip rules with no content
if v == "" {
continue
}
err := exec.Command("auditctl", strings.Fields(v)...).Run()
if err != nil {
el.Fatalf("Failed to add rule #%d. Error: %s \n", i+1, err)
}
l.Printf("Added audit rule #%d\n", i+1)
}
} else {
el.Fatalln("No audit rules found. exiting")
}
}
func createOutput(config *viper.Viper) *AuditWriter {
if config.GetBool("output.syslog.enabled") == false {
el.Fatalln("No outputs have been enabled")
}
attempts := config.GetInt("output.syslog.attempts")
if attempts < 1 {
el.Fatalln("output attempts for syslog must be at least 1", attempts, "provided")
}
syslogWriter, err := syslog.Dial(
config.GetString("output.syslog.network"),
config.GetString("output.syslog.address"),
syslog.Priority(config.GetInt("output.syslog.priority")),
config.GetString("output.syslog.tag"),
)
if err != nil {
el.Fatalln("Failed to open syslog writer. Error:", err)
}
return NewAuditWriter(syslogWriter, attempts)
}
func createFilters(config *viper.Viper) []AuditFilter {
var err error
var ok bool
fs := config.Get("filters")
filters := []AuditFilter{}
if fs == nil {
return filters
}
ft, ok := fs.([]interface{})
if !ok {
return filters
}
for i, f := range ft {
f2, ok := f.(map[interface{}]interface{})
if !ok {
el.Fatal("Could not parse filter ", i+1, f)
}
af := AuditFilter{}
for k, v := range f2 {
switch k {
case "message_type":
if ev, ok := v.(string); ok {
fv, err := strconv.ParseUint(ev, 10, 64)
if err != nil {
el.Fatal("`message_type` in filter ", i+1, " could not be parsed ", v, " ", err)
}
af.messageType = uint16(fv)
} else if ev, ok := v.(int); ok {
if !ok {
el.Fatal("`message_type` in filter ", i+1, " could not be parsed ", v)
}
af.messageType = uint16(ev)
} else {
el.Fatal("`message_type` in filter ", i+1, " could not be parsed ", v)
}
case "regex":
re, ok := v.(string)
if !ok {
el.Fatal("`regex` in filter ", i+1, " could not be parsed ", v)
}
if af.regex, err = regexp.Compile(re); err != nil {
el.Fatal("`regex` in filter ", i+1, " could not be parsed ", v, " ", err)
}
case "syscall":
if af.syscall, ok = v.(string); ok {
el.Fatal("`syscall` in filter ", i+1, " could not be parsed ", v)
} else if ev, ok := v.(int); ok {
af.syscall = strconv.Itoa(ev)
} else {
el.Fatal("`syscall` in filter ", i+1, " could not be parsed ", v)
}
}
}
filters = append(filters, af)
l.Printf("Ignoring syscall `%v` containing message type `%v` matching string `%s`\n", af.syscall, af.messageType, af.regex.String())
}
return filters
}
func main() {
config := viper.New()
configFile := flag.String("config", "", "Config file location")
cpuProfile := flag.Bool("cpuprofile", false, "Enable cpu profiling")
flag.Parse()
if *configFile == "" {
el.Println("A config file must be provided")
flag.Usage()
os.Exit(1)
}
config.SetConfigFile(*configFile)
loadConfig(config)
l.SetFlags(config.GetInt("log.flags"))
el.SetFlags(config.GetInt("log.flags"))
setRules(config)
if *cpuProfile {
l.Println("Enabling CPU profile ./cpu.pprof")
defer profile.Start(profile.Quiet, profile.ProfilePath(".")).Stop()
}
writer := createOutput(config)
nlClient := NewNetlinkClient(config.GetInt("socket_buffer.receive"))
marshaller := NewAuditMarshaller(
writer,
config.GetBool("message_tracking.enabled"),
config.GetBool("message_tracking.log_out_of_order"),
config.GetInt("message_tracking.max_out_of_order"),
createFilters(config),
)
l.Println("Started processing events")
//Main loop. Get data from netlink and send it to the json lib for processing
for {
msg, err := nlClient.Receive()
if err != nil {
el.Printf("Error during message receive: %+v\n", err)
continue
}
if msg == nil {
continue
}
marshaller.Consume(msg)
}
}