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
|
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"time"
"gopkg.in/yaml.v3"
"github.com/elastic/go-libaudit/v2"
"github.com/elastic/go-libaudit/v2/aucoalesce"
"github.com/elastic/go-libaudit/v2/auparse"
)
var (
fs = flag.NewFlagSet("auparse", flag.ExitOnError)
in = fs.String("in", "-", "input file (defaults to stdin)")
out = fs.String("out", "-", "output file (defaults to stdout)")
interpret = fs.Bool("i", false, "interpret and normalize messages")
idLookup = fs.Bool("id", true, "lookup uid and gid values in messages (requires -i)")
format = fs.String("format", "", "output format, possible values - json, yaml, text (default)")
)
func main() {
if err := fs.Parse(os.Args[1:]); err != nil {
log.Fatal(err)
}
if err := processLogs(); err != nil {
log.Fatalf("error: %v", err)
}
}
func input() (io.ReadCloser, error) {
if *in == "-" {
return os.Stdin, nil
}
return os.Open(*in)
}
func output() (io.WriteCloser, error) {
if *out == "-" {
return os.Stdout, nil
}
return os.OpenFile(*out, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o600)
}
func processLogs() error {
input, err := input()
if err != nil {
return err
}
defer input.Close()
output, err := output()
if err != nil {
return err
}
defer output.Close()
reassembler, err := libaudit.NewReassembler(5, 2*time.Second, &streamHandler{output})
if err != nil {
return fmt.Errorf("failed to create reassmbler: %w", err)
}
defer reassembler.Close()
// Start goroutine to periodically purge timed-out events.
go func() {
t := time.NewTicker(500 * time.Millisecond)
defer t.Stop()
for range t.C {
if reassembler.Maintain() != nil {
return
}
}
}()
// Process lines from the input.
s := bufio.NewScanner(input)
for s.Scan() {
line := s.Text()
auditMsg, err := auparse.ParseLogLine(line)
if err != nil {
log.Printf("failed to parse message header: %v", err)
}
reassembler.PushMessage(auditMsg)
}
return nil
}
type streamHandler struct {
output io.Writer
}
func (s *streamHandler) ReassemblyComplete(msgs []*auparse.AuditMessage) {
if err := s.outputMultipleMessages(msgs); err != nil {
log.Printf("[WARN] failed writing message to output: %v", err)
}
}
func (*streamHandler) EventsLost(count int) {
log.Printf("detected the loss of %v sequences.", count)
}
func (s *streamHandler) outputMultipleMessages(msgs []*auparse.AuditMessage) error {
var err error
if !*interpret {
if _, err = s.output.Write([]byte("---\n")); err != nil {
return err
}
for _, m := range msgs {
if err = s.outputSingleMessage(m); err != nil {
return err
}
}
return nil
}
event, err := aucoalesce.CoalesceMessages(msgs)
if err != nil {
log.Printf("failed to coalesce messages: %v", err)
return nil
}
if *idLookup {
aucoalesce.ResolveIDs(event)
}
switch *format {
case "json":
if err := s.printJSON(event); err != nil {
log.Printf("failed to marshal event to JSON: %v", err)
}
case "yaml":
if _, err := s.output.Write([]byte("---\n")); err != nil {
return err
}
if err := s.printYAML(event); err != nil {
log.Printf("failed to marshal message to YAML: %v", err)
}
default:
sm := event.Summary
if _, err := s.output.Write([]byte("---\n")); err != nil {
return err
}
_, err := fmt.Fprintf(
s.output,
`time="%v" sequence=%v category=%v type=%v actor=%v/%v action=%v thing=%v/%v how=%v tags=%v`+"\n",
event.Timestamp, event.Sequence, event.Category, event.Type, sm.Actor.Primary, sm.Actor.Secondary,
sm.Action, sm.Object.Primary, sm.Object.Secondary, sm.How, event.Tags,
)
if err != nil {
return err
}
}
return nil
}
func (s *streamHandler) outputSingleMessage(m *auparse.AuditMessage) error {
switch *format {
case "json":
if err := s.printJSON(m.ToMapStr()); err != nil {
log.Printf("failed to marshal message to JSON: %v", err)
}
case "yaml":
if err := s.printYAML(m.ToMapStr()); err != nil {
log.Printf("failed to marshal message to YAML: %v", err)
}
default:
if _, err := fmt.Fprintf(
s.output,
"type=%v msg=%v\n",
m.RecordType, m.RawData,
); err != nil {
return err
}
}
return nil
}
func (s *streamHandler) printJSON(v interface{}) error {
jsonBytes, err := json.Marshal(v)
if err != nil {
return err
}
if _, err = s.output.Write(jsonBytes); err != nil {
return err
}
if _, err = s.output.Write([]byte("\n")); err != nil {
return err
}
return nil
}
func (s *streamHandler) printYAML(v interface{}) error {
yamlBytes, err := yaml.Marshal(v)
if err != nil {
return err
}
if _, err = s.output.Write(yamlBytes); err != nil {
return err
}
if _, err = s.output.Write([]byte("\n")); err != nil {
return err
}
return nil
}
|