-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
wineventlog_windows.go
548 lines (478 loc) · 15.4 KB
/
wineventlog_windows.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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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 wineventlog
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"reflect"
"runtime"
"sort"
"syscall"
"github.com/elastic/beats/v7/libbeat/common"
"golang.org/x/sys/windows"
"github.com/elastic/beats/v7/winlogbeat/sys"
)
// Errors
var (
// ErrorEvtVarTypeNull is an error that means the content of the EVT_VARIANT
// data is null.
ErrorEvtVarTypeNull = errors.New("null EVT_VARIANT data")
)
// bookmarkTemplate is a parameterized string that requires two parameters,
// the channel name and the record ID. The formatted string can be used to open
// a new event log subscription and resume from the given record ID.
const bookmarkTemplate = `<BookmarkList><Bookmark Channel="%s" RecordId="%d" ` +
`IsCurrent="True"/></BookmarkList>`
var providerNameContext EvtHandle
func init() {
if avail, _ := IsAvailable(); avail {
providerNameContext, _ = CreateRenderContext([]string{"Event/System/Provider/@Name"}, EvtRenderContextValues)
}
}
// IsAvailable returns true if the Windows Event Log API is supported by this
// operating system. If not supported then false is returned with the
// accompanying error.
func IsAvailable() (bool, error) {
err := modwevtapi.Load()
if err != nil {
return false, err
}
return true, nil
}
// Channels returns a list of channels that are registered on the computer.
func Channels() ([]string, error) {
handle, err := _EvtOpenChannelEnum(0, 0)
if err != nil {
return nil, err
}
defer _EvtClose(handle)
var channels []string
cpBuffer := make([]uint16, 512)
loop:
for {
var used uint32
err := _EvtNextChannelPath(handle, uint32(len(cpBuffer)), &cpBuffer[0], &used)
if err != nil {
errno, ok := err.(syscall.Errno)
if ok {
switch errno {
case ERROR_INSUFFICIENT_BUFFER:
// Grow buffer.
newLen := 2 * len(cpBuffer)
if int(used) > newLen {
newLen = int(used)
}
cpBuffer = make([]uint16, newLen)
continue
case ERROR_NO_MORE_ITEMS:
break loop
}
}
return nil, err
}
channels = append(channels, syscall.UTF16ToString(cpBuffer[:used]))
}
return channels, nil
}
// EvtOpenLog gets a handle to a channel or log file that you can then use to
// get information about the channel or log file.
func EvtOpenLog(session EvtHandle, path string, flags EvtOpenLogFlag) (EvtHandle, error) {
var err error
var pathPtr *uint16
if path != "" {
pathPtr, err = syscall.UTF16PtrFromString(path)
if err != nil {
return 0, err
}
}
return _EvtOpenLog(session, pathPtr, uint32(flags))
}
// EvtQuery runs a query to retrieve events from a channel or log file that
// match the specified query criteria.
func EvtQuery(session EvtHandle, path string, query string, flags EvtQueryFlag) (EvtHandle, error) {
var err error
var pathPtr *uint16
if path != "" {
pathPtr, err = syscall.UTF16PtrFromString(path)
if err != nil {
return 0, err
}
}
var queryPtr *uint16
if query != "" {
queryPtr, err = syscall.UTF16PtrFromString(query)
if err != nil {
return 0, err
}
}
return _EvtQuery(session, pathPtr, queryPtr, uint32(flags))
}
// Subscribe creates a new subscription to an event log channel.
func Subscribe(
session EvtHandle,
event windows.Handle,
channelPath string,
query string,
bookmark EvtHandle,
flags EvtSubscribeFlag,
) (EvtHandle, error) {
var err error
var cp *uint16
if channelPath != "" {
cp, err = syscall.UTF16PtrFromString(channelPath)
if err != nil {
return 0, err
}
}
var q *uint16
if query != "" {
q, err = syscall.UTF16PtrFromString(query)
if err != nil {
return 0, err
}
}
eventHandle, err := _EvtSubscribe(session, uintptr(event), cp, q, bookmark,
0, 0, flags)
if err != nil {
return 0, err
}
return eventHandle, nil
}
// EvtSeek seeks to a specific event in a query result set.
func EvtSeek(resultSet EvtHandle, position int64, bookmark EvtHandle, flags EvtSeekFlag) error {
_, err := _EvtSeek(resultSet, position, bookmark, 0, uint32(flags))
return err
}
// EventHandles reads the event handles from a subscription. It attempt to read
// at most maxHandles. ErrorNoMoreHandles is returned when there are no more
// handles available to return. Close must be called on each returned EvtHandle
// when finished with the handle.
func EventHandles(subscription EvtHandle, maxHandles int) ([]EvtHandle, error) {
if maxHandles < 1 {
return nil, fmt.Errorf("maxHandles must be greater than 0")
}
eventHandles := make([]EvtHandle, maxHandles)
var numRead uint32
err := _EvtNext(subscription, uint32(len(eventHandles)),
&eventHandles[0], 0, 0, &numRead)
if err != nil {
// Munge ERROR_INVALID_OPERATION to ERROR_NO_MORE_ITEMS when no handles
// were read. This happens you call the method and there are no events
// to read (i.e. polling).
if err == ERROR_INVALID_OPERATION && numRead == 0 {
return nil, ERROR_NO_MORE_ITEMS
}
return nil, err
}
return eventHandles[:numRead], nil
}
// RenderEvent reads the event data associated with the EvtHandle and renders
// the data as XML. An error and XML can be returned by this method if an error
// occurs while rendering the XML with RenderingInfo and the method is able to
// recover by rendering the XML without RenderingInfo.
func RenderEvent(
eventHandle EvtHandle,
lang uint32,
renderBuf []byte,
pubHandleProvider func(string) sys.MessageFiles,
out io.Writer,
) error {
providerName, err := evtRenderProviderName(renderBuf, eventHandle)
if err != nil {
return err
}
var publisherHandle uintptr
if pubHandleProvider != nil {
messageFiles := pubHandleProvider(providerName)
if messageFiles.Err == nil {
// There is only ever a single handle when using the Windows Event
// Log API.
publisherHandle = messageFiles.Handles[0].Handle
}
}
// Only a single string is returned when rendering XML.
err = FormatEventString(EvtFormatMessageXml,
eventHandle, providerName, EvtHandle(publisherHandle), lang, renderBuf, out)
// Recover by rendering the XML without the RenderingInfo (message string).
if err != nil {
// Do not try to recover from InsufficientBufferErrors because these
// can be retried with a larger buffer.
if _, ok := err.(sys.InsufficientBufferError); ok {
return err
}
err = RenderEventXML(eventHandle, renderBuf, out)
}
return err
}
// RenderEventXML renders the event as XML. If the event is already rendered, as
// in a forwarded event whose content type is "RenderedText", then the XML will
// include the RenderingInfo (message). If the event is not rendered then the
// XML will not include the message, and in this case RenderEvent should be
// used.
func RenderEventXML(eventHandle EvtHandle, renderBuf []byte, out io.Writer) error {
return renderXML(eventHandle, EvtRenderEventXml, renderBuf, out)
}
// RenderBookmarkXML renders a bookmark as XML.
func RenderBookmarkXML(bookmarkHandle EvtHandle, renderBuf []byte, out io.Writer) error {
return renderXML(bookmarkHandle, EvtRenderBookmark, renderBuf, out)
}
// CreateBookmarkFromRecordID creates a new bookmark pointing to the given recordID
// within the supplied channel. Close must be called on returned EvtHandle when
// finished with the handle.
func CreateBookmarkFromRecordID(channel string, recordID uint64) (EvtHandle, error) {
xml := fmt.Sprintf(bookmarkTemplate, channel, recordID)
p, err := syscall.UTF16PtrFromString(xml)
if err != nil {
return 0, err
}
h, err := _EvtCreateBookmark(p)
if err != nil {
return 0, err
}
return h, nil
}
// CreateBookmarkFromEvent creates a new bookmark pointing to the given event.
// Close must be called on returned EvtHandle when finished with the handle.
func CreateBookmarkFromEvent(handle EvtHandle) (EvtHandle, error) {
h, err := _EvtCreateBookmark(nil)
if err != nil {
return 0, err
}
if err = _EvtUpdateBookmark(h, handle); err != nil {
return 0, err
}
return h, nil
}
// CreateBookmarkFromXML creates a new bookmark from the serialised representation
// of an existing bookmark. Close must be called on returned EvtHandle when
// finished with the handle.
func CreateBookmarkFromXML(bookmarkXML string) (EvtHandle, error) {
xml, err := syscall.UTF16PtrFromString(bookmarkXML)
if err != nil {
return 0, err
}
return _EvtCreateBookmark(xml)
}
// CreateRenderContext creates a render context. Close must be called on
// returned EvtHandle when finished with the handle.
func CreateRenderContext(valuePaths []string, flag EvtRenderContextFlag) (EvtHandle, error) {
var paths []uintptr
for _, path := range valuePaths {
utf16, err := syscall.UTF16FromString(path)
if err != nil {
return 0, err
}
paths = append(paths, reflect.ValueOf(&utf16[0]).Pointer())
}
var pathsAddr uintptr
if len(paths) > 0 {
pathsAddr = reflect.ValueOf(&paths[0]).Pointer()
}
context, err := _EvtCreateRenderContext(uint32(len(paths)), pathsAddr, flag)
if err != nil {
return 0, err
}
return context, nil
}
// OpenPublisherMetadata opens a handle to the publisher's metadata. Close must
// be called on returned EvtHandle when finished with the handle.
func OpenPublisherMetadata(
session EvtHandle,
publisherName string,
lang uint32,
) (EvtHandle, error) {
p, err := syscall.UTF16PtrFromString(publisherName)
if err != nil {
return 0, err
}
h, err := _EvtOpenPublisherMetadata(session, p, nil, lang, 0)
if err != nil {
return 0, err
}
return h, nil
}
// Close closes an EvtHandle.
func Close(h EvtHandle) error {
return _EvtClose(h)
}
// FormatEventString formats part of the event as a string.
// messageFlag determines what part of the event is formatted as as string.
// eventHandle is the handle to the event.
// publisher is the name of the event's publisher.
// publisherHandle is a handle to the publisher's metadata as provided by
// EvtOpenPublisherMetadata.
// lang is the language ID.
// buffer is optional and if not provided it will be allocated. If the provided
// buffer is not large enough then an InsufficientBufferError will be returned.
func FormatEventString(
messageFlag EvtFormatMessageFlag,
eventHandle EvtHandle,
publisher string,
publisherHandle EvtHandle,
lang uint32,
buffer []byte,
out io.Writer,
) error {
// Open a publisher handle if one was not provided.
ph := publisherHandle
if ph == 0 {
ph, err := OpenPublisherMetadata(0, publisher, lang)
if err != nil {
return err
}
defer _EvtClose(ph)
}
// Create a buffer if one was not provided.
var bufferUsed uint32
if buffer == nil {
err := _EvtFormatMessage(ph, eventHandle, 0, 0, 0, messageFlag,
0, nil, &bufferUsed)
if err != nil && err != ERROR_INSUFFICIENT_BUFFER {
return err
}
bufferUsed *= 2
buffer = make([]byte, bufferUsed)
bufferUsed = 0
}
err := _EvtFormatMessage(ph, eventHandle, 0, 0, 0, messageFlag,
uint32(len(buffer)/2), &buffer[0], &bufferUsed)
bufferUsed *= 2
if err == ERROR_INSUFFICIENT_BUFFER {
return sys.InsufficientBufferError{err, int(bufferUsed)}
}
if err != nil {
return err
}
// This assumes there is only a single string value to read. This will
// not work to read keys (when messageFlag == EvtFormatMessageKeyword).
return common.UTF16ToUTF8Bytes(buffer[:bufferUsed], out)
}
// Publishers returns a sort list of event publishers on the local computer.
func Publishers() ([]string, error) {
publisherEnumerator, err := _EvtOpenPublisherEnum(NilHandle, 0)
if err != nil {
return nil, fmt.Errorf("failed in EvtOpenPublisherEnum: %w", err)
}
defer Close(publisherEnumerator)
var (
publishers []string
bufferUsed uint32
buffer = make([]uint16, 1024)
)
loop:
for {
if err = _EvtNextPublisherId(publisherEnumerator, uint32(len(buffer)), &buffer[0], &bufferUsed); err != nil {
switch err {
case ERROR_NO_MORE_ITEMS:
break loop
case ERROR_INSUFFICIENT_BUFFER:
buffer = make([]uint16, bufferUsed)
continue loop
default:
return nil, fmt.Errorf("failed in EvtNextPublisherId: %w", err)
}
}
provider := windows.UTF16ToString(buffer)
publishers = append(publishers, provider)
}
sort.Strings(publishers)
return publishers, nil
}
// offset reads a pointer value from the reader then calculates an offset from
// the start of the buffer to the pointer location. If the pointer value is
// NULL or is outside of the bounds of the buffer then an error is returned.
// reader will be advanced by the size of a uintptr.
func offset(buffer []byte, reader io.Reader) (uint64, error) {
// Handle 32 and 64-bit pointer size differences.
var dataPtr uint64
var err error
switch runtime.GOARCH {
default:
return 0, fmt.Errorf("Unhandled architecture: %s", runtime.GOARCH)
case "amd64":
err = binary.Read(reader, binary.LittleEndian, &dataPtr)
if err != nil {
return 0, err
}
case "386":
var p uint32
err = binary.Read(reader, binary.LittleEndian, &p)
if err != nil {
return 0, err
}
dataPtr = uint64(p)
}
if dataPtr == 0 {
return 0, ErrorEvtVarTypeNull
}
bufferPtr := uint64(reflect.ValueOf(&buffer[0]).Pointer())
offset := dataPtr - bufferPtr
if offset > uint64(len(buffer)) {
return 0, fmt.Errorf("Invalid pointer %x. Cannot dereference an "+
"address outside of the buffer [%x:%x].", dataPtr, bufferPtr,
bufferPtr+uint64(len(buffer)))
}
return offset, nil
}
// readString reads a pointer using the reader then parses the UTF-16 string
// that the pointer addresses within the buffer.
func readString(buffer []byte, reader io.Reader) (string, error) {
offset, err := offset(buffer, reader)
if err != nil {
// Ignore NULL values.
if err == ErrorEvtVarTypeNull {
return "", nil
}
return "", err
}
str, err := sys.UTF16BytesToString(buffer[offset:])
return str, err
}
// evtRenderProviderName renders the ProviderName of an event.
func evtRenderProviderName(renderBuf []byte, eventHandle EvtHandle) (string, error) {
var bufferUsed, propertyCount uint32
err := _EvtRender(providerNameContext, eventHandle, EvtRenderEventValues,
uint32(len(renderBuf)), &renderBuf[0], &bufferUsed, &propertyCount)
if err == ERROR_INSUFFICIENT_BUFFER {
return "", sys.InsufficientBufferError{err, int(bufferUsed)}
}
if err != nil {
return "", fmt.Errorf("evtRenderProviderName %v", err)
}
reader := bytes.NewReader(renderBuf)
return readString(renderBuf, reader)
}
func renderXML(eventHandle EvtHandle, flag EvtRenderFlag, renderBuf []byte, out io.Writer) error {
var bufferUsed, propertyCount uint32
err := _EvtRender(0, eventHandle, flag, uint32(len(renderBuf)),
&renderBuf[0], &bufferUsed, &propertyCount)
if err == ERROR_INSUFFICIENT_BUFFER {
return sys.InsufficientBufferError{err, int(bufferUsed)}
}
if err != nil {
return err
}
if int(bufferUsed) > len(renderBuf) {
return fmt.Errorf("Windows EvtRender reported that wrote %d bytes "+
"to the buffer, but the buffer can only hold %d bytes",
bufferUsed, len(renderBuf))
}
return common.UTF16ToUTF8Bytes(renderBuf[:bufferUsed], out)
}