/
goevtx.go
421 lines (377 loc) · 9.75 KB
/
goevtx.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package evtx
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"time"
)
type GoEvtxElement interface{}
type GoEvtxMap map[string]interface{}
type GoEvtxPath []string
func (p GoEvtxPath) String() string {
return strings.Join(p, "/")
}
type ErrEvtxEltNotFound struct {
path GoEvtxPath
}
func (e *ErrEvtxEltNotFound) Error() string {
return fmt.Sprintf("Element at path %v not found", e.path)
}
// Path : helper function that converts a path string to a table of strings
// @s : path string, has to be in form of /correct/path/string with (correct,
// path, string) being keys to look for recursively
func Path(s string) GoEvtxPath {
return strings.Split(strings.Trim(s, PathSeparator), PathSeparator)
}
// HasKeys : determines whether this map is in a key value form
// return bool
func (pg *GoEvtxMap) HasKeys(keys ...string) bool {
for _, k := range keys {
if _, ok := (*pg)[k]; !ok {
return false
}
}
return true
}
// Add : concatenate two GoEvtxMap together
// @other: other map to concatenate with
func (pg *GoEvtxMap) Add(other GoEvtxMap) {
for k, v := range other {
if _, ok := (*pg)[k]; ok {
panic("Duplicated key")
}
(*pg)[k] = v
}
}
// GetMap : Get the full map containing the path
// @path : path to search for
func (pg *GoEvtxMap) GetMap(path *GoEvtxPath) (*GoEvtxMap, error) {
if len(*path) > 0 {
if ge, ok := (*pg)[(*path)[0]]; ok {
if len(*path) == 1 {
return pg, nil
}
switch ge.(type) {
case GoEvtxMap:
p := ge.(GoEvtxMap)
np := (*path)[1:]
return p.GetMap(&np)
}
}
}
return nil, &ErrEvtxEltNotFound{*path}
}
func (pg *GoEvtxMap) GetMapStrict(path *GoEvtxPath) *GoEvtxMap {
pg, err := pg.GetMap(path)
if err != nil {
panic(err)
}
return pg
}
func (pg *GoEvtxMap) GetMapWhere(path *GoEvtxPath, value interface{}) (*GoEvtxMap, error) {
m, err := pg.GetMap(path)
if err != nil {
return nil, err
}
if m != nil && len(*path) > 0 {
np := (*path)[len(*path)-1:]
if m.Equal(&np, value) {
return m, nil
}
}
return nil, &ErrEvtxEltNotFound{*path}
}
func (pg *GoEvtxMap) GetMapWhereStrict(path *GoEvtxPath, value interface{}) *GoEvtxMap {
pg, err := pg.GetMapWhere(path, value)
if err != nil {
panic(err)
}
return pg
}
// Recursive search in a GoEvtxMap according to a given path
// @path : path to search for
// return *GoEvtxElement, error : pointer to the element found at path
func (pg *GoEvtxMap) Get(path *GoEvtxPath) (*GoEvtxElement, error) {
if len(*path) > 0 {
if i, ok := (*pg)[(*path)[0]]; ok {
if len(*path) == 1 {
cge := GoEvtxElement(i)
return &cge, nil
}
switch i.(type) {
case GoEvtxMap:
p := i.(GoEvtxMap)
np := (*path)[1:]
return p.Get(&np)
case map[string]interface{}:
p := GoEvtxMap(i.(map[string]interface{}))
np := (*path)[1:]
return p.Get(&np)
}
}
}
return nil, &ErrEvtxEltNotFound{*path}
}
func (pg *GoEvtxMap) GetStrict(path *GoEvtxPath) *GoEvtxElement {
gee, err := pg.Get(path)
if err != nil {
panic(err)
}
return gee
}
// GetUint returns the GoEvtxElement at path as a string
// @path : path to search for
// return string, error
func (pg *GoEvtxMap) GetString(path *GoEvtxPath) (string, error) {
pE, err := pg.Get(path)
if err != nil {
return "", err
}
if s, ok := (*pE).(string); ok {
return s, nil
}
return "", fmt.Errorf("Bad type expect string got %T", (*pE))
}
func (pg *GoEvtxMap) GetStringStrict(path *GoEvtxPath) string {
s, err := pg.GetString(path)
if err != nil {
panic(err)
}
return s
}
// GetBool returns the GoEvtxElement at path as a bool
// @path : path to search for
// return (bool, error)
func (pg *GoEvtxMap) GetBool(path *GoEvtxPath) (bool, error) {
s, err := pg.GetString(path)
if err != nil {
return false, &ErrEvtxEltNotFound{*path}
}
b, err := strconv.ParseBool(s)
if err != nil {
return false, err
}
return b, err
}
func (pg *GoEvtxMap) GetBoolStrict(path *GoEvtxPath) bool {
b, err := pg.GetBool(path)
if err != nil {
panic(err)
}
return b
}
// GetInt returns the GoEvtxElement at path as a int64
// @path : path to search for
// return int64, error
func (pg *GoEvtxMap) GetInt(path *GoEvtxPath) (int64, error) {
s, err := pg.GetString(path)
if err != nil {
return 0, &ErrEvtxEltNotFound{*path}
}
i, err := strconv.ParseInt(s, 0, 64)
if err != nil {
return 0, err
}
return i, nil
}
func (pg *GoEvtxMap) GetIntStrict(path *GoEvtxPath) int64 {
i, err := pg.GetInt(path)
if err != nil {
panic(err)
}
return i
}
// GetUint returns the GoEvtxElement at path as a uint64
// @path : path to search for
// return uint64
func (pg *GoEvtxMap) GetUint(path *GoEvtxPath) (uint64, error) {
s, err := pg.GetString(path)
if err != nil {
return 0, &ErrEvtxEltNotFound{*path}
}
u, err := strconv.ParseUint(s, 0, 64)
if err != nil {
return 0, err
}
return u, nil
}
func (pg *GoEvtxMap) GetUintStrict(path *GoEvtxPath) uint64 {
u, err := pg.GetUint(path)
if err != nil {
panic(err)
}
return u
}
// GetUint returns the GoEvtxElement at path as a Time struct
// @path : path to search for
// return Time
func (pg *GoEvtxMap) GetTime(path *GoEvtxPath) (time.Time, error) {
t, err := pg.Get(path)
if err != nil {
return time.Time{}, &ErrEvtxEltNotFound{*path}
}
// If the value was extracted from raw BinXML (not a template) it may happen
// that the value stored at path is a string since in raw BinXML everything
// seems to be ValueText
switch (*t).(type) {
case time.Time:
return (*t).(time.Time), nil
case UTCTime:
return time.Time((*t).(UTCTime)), nil
case string:
return time.Parse(time.RFC3339Nano, (*t).(string))
default:
return time.Time{}, fmt.Errorf("Cannot convert %T to time.Time", *t)
}
}
func (pg *GoEvtxMap) GetTimeStrict(path *GoEvtxPath) time.Time {
t, err := pg.GetTime(path)
if err != nil {
panic(err)
}
return t
}
// EventID returns the EventID of the Event as a int64
// return int64 : EventID
func (pg *GoEvtxMap) EventID() int64 {
eid, err := pg.GetInt(&EventIDPath)
if err != nil {
eid, err = pg.GetInt(&EventIDPath2)
if err != nil {
panic(err)
}
return eid
}
return eid
}
// Channel returns the Channel attribute of the event
// return string : Channel attribute
func (pg *GoEvtxMap) Channel() string {
return pg.GetStringStrict(&ChannelPath)
}
// EventRecordID returns the EventRecordID of the the event. It panics if the
// attribute is not found in the event.
func (pg *GoEvtxMap) EventRecordID() int64 {
return pg.GetIntStrict(&EventRecordIDPath)
}
// TimeCreated returns the creation time of the event. It panics if the attribute
// is not in the event
func (pg *GoEvtxMap) TimeCreated() time.Time {
return pg.GetTimeStrict(&SystemTimePath)
}
// UserID retrieves the UserID attribute located at /Event/System/Security/UserID
// if present. If not present the ok flag will be false
func (pg *GoEvtxMap) UserID() (userID string, ok bool) {
userID, err := pg.GetString(&UserIDPath)
if err == nil {
ok = true
}
return
}
func (pg *GoEvtxMap) Before(t time.Time) bool {
return pg.GetTimeStrict(&SystemTimePath).Before(t)
}
func (pg *GoEvtxMap) After(t time.Time) bool {
return pg.GetTimeStrict(&SystemTimePath).After(t)
}
func (pg *GoEvtxMap) At(t time.Time) bool {
return pg.GetTimeStrict(&SystemTimePath).Equal(t)
}
func (pg *GoEvtxMap) Between(t1, t2 time.Time) bool {
return (pg.After(t1) || pg.At(t1)) && (pg.Before(t2) || pg.At(t2))
}
// Equal returns true if element at path is equal to i
// @path : path at witch GoEvtxElement is located
// @i : interface to test equality with
// return bool : true if equality is verified
func (pg *GoEvtxMap) Equal(path *GoEvtxPath, i interface{}) bool {
t, err := pg.Get(path)
if err != nil {
return false
}
return reflect.DeepEqual(*t, i)
}
// Equal returns true if element at path is equal to any object
// @path : path at witch GoEvtxElement is located
// @is : slice of interface to test equality with
// return bool : true if equality is verified
func (pg *GoEvtxMap) AnyEqual(path *GoEvtxPath, is []interface{}) bool {
t, err := pg.Get(path)
if err != nil {
return false
}
for _, i := range is {
if reflect.DeepEqual(i, *t) {
return true
}
}
return false
}
// RegexMatch returns true if GoEvtxElement located at path matches a regexp
// @path : path at witch GoEvtxElement is located
// @pattern : regexp to test
// return bool
func (pg *GoEvtxMap) RegexMatch(path *GoEvtxPath, pattern *regexp.Regexp) bool {
s, err := pg.GetString(path)
if err != nil {
return false
}
return pattern.MatchString(s)
}
// IsEventID returns true if pg is one of the EventID number specified in parameter
// @eids : EventID numbers to test against
// return bool
func (pg *GoEvtxMap) IsEventID(eids ...interface{}) bool {
return pg.AnyEqual(&EventIDPath, eids)
}
// Set sets a new GoEvtxElement at path
// @path... : path to look for
// @new : new value
// return error if any
func (pg *GoEvtxMap) Set(path *GoEvtxPath, new GoEvtxElement) error {
if len(*path) > 0 {
i := (*pg)[(*path)[0]]
if len(*path) == 1 {
(*pg)[(*path)[0]] = new
return nil
}
switch i.(type) {
case GoEvtxMap:
p := i.(GoEvtxMap)
np := (*path)[1:]
return p.Set(&np, new)
case map[string]interface{}:
p := GoEvtxMap(i.(map[string]interface{}))
np := (*path)[1:]
return p.Set(&np, new)
}
}
return &ErrEvtxEltNotFound{*path}
}
// Del deletes the object referenced by path
func (pg *GoEvtxMap) Del(path *GoEvtxPath) {
if len(*path) > 0 {
if ge, ok := (*pg)[(*path)[0]]; ok {
if len(*path) == 1 {
delete((*pg), (*path)[0])
}
switch ge.(type) {
case GoEvtxMap:
p := ge.(GoEvtxMap)
np := (*path)[1:]
p.Del(&np)
case map[string]interface{}:
p := GoEvtxMap(ge.(map[string]interface{}))
np := (*path)[1:]
p.Del(&np)
}
}
}
}
// DelXmlns : utility function to delete useless xlmns entry found in every
// GoEvtxMap
func (pg *GoEvtxMap) DelXmlns() {
pg.Del(&XmlnsPath)
}