This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
parser.go
292 lines (273 loc) · 7.3 KB
/
parser.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package libaudit
import (
"fmt"
"strconv"
"strings"
)
type record struct {
syscallNum string
arch string
a0 int
a1 int
}
// ErrorAuditParse is an implementation of the error interface that is returned by
// ParseAuditEvent. msg will contain a description of the error, and the raw audit event
// which failed parsing is returned in raw for inspection by the calling program.
type ErrorAuditParse struct {
Msg string
Raw string
}
// Error returns a string representation of ErrorAuditParse e
func (e ErrorAuditParse) Error() string {
return e.Msg
}
// newErrorAuditParse returns a new ErrorAuditParse type with the fields populated
func newErrorAuditParse(raw string, f string, v ...interface{}) ErrorAuditParse {
ret := ErrorAuditParse{
Raw: raw,
Msg: fmt.Sprintf(f, v...),
}
return ret
}
// ParseAuditEvent parses an incoming audit message from kernel and returns an AuditEvent.
//
// msgType is supposed to come from the calling function which holds the msg header indicating header
// type of the messages. It uses simple string parsing techniques and provider better performance than
// the regex parser, idea taken from parse_up_record(rnode* r) in ellist.c (libauparse).
func ParseAuditEvent(str string, msgType auditConstant, interpret bool) (*AuditEvent, error) {
var r record
var event = AuditEvent{
Raw: str,
}
// Create the map which will store the audit record fields for this event, note we
// provide an allocation hint here based on the average number of fields we would come
// across in an audit event
m := make(map[string]string, 24)
if strings.HasPrefix(str, "audit(") {
str = str[6:]
} else {
return nil, newErrorAuditParse(event.Raw, "malformed, missing audit prefix")
}
index := strings.Index(str, ":")
if index == -1 {
return nil, newErrorAuditParse(event.Raw, "malformed, can't locate start of fields")
}
// determine timestamp
timestamp := str[:index]
// move further on string, skipping ':'
str = str[index+1:]
index = strings.Index(str, ")")
if index == -1 {
return nil, newErrorAuditParse(event.Raw, "malformed, can't locate end of prefix")
}
serial := str[:index]
if strings.HasPrefix(str, serial+"): ") {
str = str[index+3:]
} else {
return nil, newErrorAuditParse(event.Raw, "malformed, prefix termination unexpected")
}
var (
nBytes string
orig = len(str)
n int
key string
value string
av bool
)
for n < orig {
getSpaceSlice(&str, &nBytes, &n)
var newIndex int
newIndex = strings.Index(nBytes, "=")
if newIndex == -1 {
// check type for special cases of AVC and USER_AVC
if msgType == AUDIT_AVC || msgType == AUDIT_USER_AVC {
if nBytes == "avc:" && strings.HasPrefix(str, "avc:") {
// skip over 'avc:'
str = str[len(nBytes)+1:]
av = true
continue
}
if av {
key = "seresult"
value = nBytes
if interpret {
var err error
value, err = interpretField(key, value, msgType, r)
if err != nil {
return nil, newErrorAuditParse(event.Raw, "interpretField: %v", err)
}
}
m[key] = value
av = false
if len(str) == len(nBytes) {
break
} else {
str = str[len(nBytes)+1:]
}
continue
}
if strings.HasPrefix(nBytes, "{") {
key = "seperms"
str = str[len(nBytes)+1:]
var v string
getSpaceSlice(&str, &nBytes, &n)
for nBytes != "}" {
if len(v) != 0 {
v += ","
}
v += nBytes
str = str[len(nBytes)+1:]
getSpaceSlice(&str, &nBytes, &n)
}
value = v
if interpret {
var err error
value, err = interpretField(key, value, msgType, r)
if err != nil {
return nil, newErrorAuditParse(event.Raw, "interpretField: %v", err)
}
}
m[key] = value
fixPunctuations(&value)
if len(str) == len(nBytes) {
//reached the end of message
break
} else {
str = str[len(nBytes)+1:]
}
continue
} else {
// We might get values with space, add it to prev key
// skip 'for' in avc message (special case)
if nBytes == "for" {
str = str[len(nBytes)+1:]
continue
}
value += " " + nBytes
fixPunctuations(&value)
if interpret {
var err error
value, err = interpretField(key, value, msgType, r)
if err != nil {
return nil, newErrorAuditParse(event.Raw, "interpretField: %v", err)
}
}
m[key] = value
}
} else {
// We might get values with space, add it to prev key
value += " " + nBytes
fixPunctuations(&value)
if interpret {
var err error
value, err = interpretField(key, value, msgType, r)
if err != nil {
return nil, newErrorAuditParse(event.Raw, "interpretField: %v", err)
}
}
m[key] = value
}
} else {
key = nBytes[:newIndex]
value = nBytes[newIndex+1:]
// for cases like msg='
// we look again for key value pairs
if strings.HasPrefix(value, "'") && key == "msg" {
newIndex = strings.Index(value, "=")
if newIndex == -1 {
// special case USER_AVC messages, start of: msg='avc:
if strings.HasPrefix(str, "msg='avc") {
str = str[5:]
}
continue
}
key = value[1:newIndex]
value = value[newIndex+1:]
}
fixPunctuations(&value)
if key == "arch" {
// determine machine type
}
if key == "a0" {
val, err := strconv.ParseInt(value, 16, 64)
if err != nil {
r.a0 = -1
} else {
r.a0 = int(val)
}
}
if key == "a1" {
val, err := strconv.ParseInt(value, 16, 64)
if err != nil {
r.a1 = -1
} else {
r.a1 = int(val)
}
}
if key == "syscall" {
r.syscallNum = value
}
if interpret {
var err error
value, err = interpretField(key, value, msgType, r)
if err != nil {
return nil, newErrorAuditParse(event.Raw, "interpretField: %v", err)
}
}
m[key] = value
}
if len(str) == len(nBytes) {
// Reached the end of message
break
} else {
str = str[len(nBytes)+1:]
}
}
event.Timestamp = timestamp
event.Serial = serial
event.Data = m
event.Type = msgType.String()[6:]
return &event, nil
}
// getSpaceSlice checks the index of the next space and put the string up to that space into
// the second string, total number of characters processed is updated with each call to the function
func getSpaceSlice(str *string, b *string, v *int) {
index := strings.Index(*str, " ")
if index != -1 {
if index == 0 {
// Found space on the first location only, just forward on the orig
// string and try again
*str = (*str)[1:]
getSpaceSlice(str, b, v)
} else {
*b = (*str)[:index]
// Keep updating total characters processed
*v += len(*b)
}
} else {
*b = (*str)
// Keep updating total characters processed
*v += len(*b)
}
}
func fixPunctuations(value *string) {
// Remove trailing punctuation
l := len(*value)
if l > 0 && strings.HasSuffix(*value, "'") {
*value = (*value)[:l-1]
l--
}
if l > 0 && strings.HasSuffix(*value, ",") {
*value = (*value)[:l-1]
l--
}
if l > 0 && strings.HasSuffix(*value, ")") {
if *value != "(none)" && *value != "(null)" {
*value = (*value)[:l-1]
l--
}
}
}