Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.17](backport #36173) [Winlogbeat] Add missing query while reading .evtx file #36255

Merged
merged 4 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Winlogbeat*

- Fix the ability to use filtering features (e.g. `ignore_older`, `event_id`, `provider`, `level`) while reading `.evtx` files. {issue}16826[16826] {pull}36173[36173]

*Functionbeat*

==== Bugfixes
Expand Down
2 changes: 1 addition & 1 deletion winlogbeat/eventlog/wineventlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (l *winEventLog) openChannel(bookmark win.EvtHandle) error {
func (l *winEventLog) openFile(state checkpoint.EventLogState, bookmark win.EvtHandle) error {
path := l.channelName

h, err := win.EvtQuery(0, path, "", win.EvtQueryFilePath|win.EvtQueryForwardDirection)
h, err := win.EvtQuery(0, path, l.query, win.EvtQueryFilePath|win.EvtQueryForwardDirection)
if err != nil {
return fmt.Errorf("failed to get handle to event log file %v: %w", path, err)
}
Expand Down
4 changes: 2 additions & 2 deletions winlogbeat/eventlog/wineventlog_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (l *winEventLogExp) openChannel(bookmark win.Bookmark) (win.EvtHandle, erro
func (l *winEventLogExp) openFile(state checkpoint.EventLogState, bookmark win.Bookmark) (win.EvtHandle, error) {
path := l.channelName

h, err := win.EvtQuery(0, path, "", win.EvtQueryFilePath|win.EvtQueryForwardDirection)
h, err := win.EvtQuery(0, path, l.query, win.EvtQueryFilePath|win.EvtQueryForwardDirection)
if err != nil {
return win.NilHandle, fmt.Errorf("failed to get handle to event log file %v: %w", path, err)
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func (l *winEventLogExp) openFile(state checkpoint.EventLogState, bookmark win.B
}

func (l *winEventLogExp) Read() ([]Record, error) {
var records []Record
var records []Record //nolint:prealloc // Avoid unnecessary preallocation for each reader every second when event log is inactive.

for h, ok := l.iterator.Next(); ok; h, ok = l.iterator.Next() {
record, err := l.processHandle(h)
Expand Down
29 changes: 29 additions & 0 deletions winlogbeat/eventlog/wineventlog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@
eventCreateMsgFile = "%SystemRoot%\\System32\\EventCreate.exe"
// services.exe is used by the Service Control Manager as its event message
// file; these tests use it to log messages with more than one parameter.
servicesMsgFile = "%SystemRoot%\\System32\\services.exe"

Check failure on line 58 in winlogbeat/eventlog/wineventlog_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

const `servicesMsgFile` is unused (unused)
// netevent.dll has messages that require no message parameters.
netEventMsgFile = "%SystemRoot%\\System32\\netevent.dll"

Check failure on line 60 in winlogbeat/eventlog/wineventlog_test.go

View workflow job for this annotation

GitHub Actions / lint (windows)

const `netEventMsgFile` is unused (unused)
)

func TestWinEventLogConfig_Validate(t *testing.T) {
Expand Down Expand Up @@ -276,6 +276,7 @@
assert.Equal(t, totalEvents, eventCount)
})

// Test reading .evtx file without any query filters
t.Run("evtx_file", func(t *testing.T) {
path, err := filepath.Abs("../sys/wineventlog/testdata/sysmon-9.01.evtx")
if err != nil {
Expand All @@ -301,6 +302,34 @@

assert.Len(t, records, 32)
})

// Test reading .evtx file with event_id filter
t.Run("evtx_file_with_query", func(t *testing.T) {
path, err := filepath.Abs("../sys/wineventlog/testdata/sysmon-9.01.evtx")
if err != nil {
t.Fatal(err)
}

log := openLog(t, map[string]interface{}{
"name": path,
"no_more_events": "stop",
"event_id": "3, 5",
})
defer log.Close()

records, err := log.Read()

// This implementation returns the EOF on the next call.
if err == nil && api == winEventLogAPIName {
_, err = log.Read()
}

if assert.Error(t, err, "no_more_events=stop requires io.EOF to be returned") {
assert.Equal(t, io.EOF, err)
}

assert.Len(t, records, 21)
})
}

// ---- Utility Functions -----
Expand Down
Loading