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

[Winlogbeat] Add missing query while reading .evtx file #36173

Merged
merged 18 commits into from
Aug 8, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
*Winlogbeat*

- Add "event.category" and "event.type" to Sysmon module for EventIDs 8, 9, 19, 20, 27, 28, 255 {pull}35193[35193]
- 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*

Expand Down
3 changes: 2 additions & 1 deletion winlogbeat/eventlog/wineventlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (l *winEventLog) Open(state checkpoint.EventLogState) 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 {
l.metrics.logError(err)
return fmt.Errorf("failed to get handle to event log file %v: %w", path, err)
Expand Down Expand Up @@ -424,6 +424,7 @@ func (l *winEventLog) Read() ([]Record, error) {
return nil, err
}

//nolint:prealloc // Avoid unnecessary preallocation for each reader every second when event log is inactive.
var records []Record
defer func() {
l.metrics.log(records)
Expand Down
3 changes: 2 additions & 1 deletion winlogbeat/eventlog/wineventlog_experimental.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (l *winEventLogExp) open(state checkpoint.EventLogState) (win.EvtHandle, er
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 @@ -256,6 +256,7 @@ func (l *winEventLogExp) openChannel(bookmark win.Bookmark) (win.EvtHandle, erro
}

func (l *winEventLogExp) Read() ([]Record, error) {
//nolint:prealloc // Avoid unnecessary preallocation for each reader every second when event log is inactive.
var records []Record
defer func() {
l.metrics.log(records)
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 @@ -270,6 +270,7 @@ func testWindowsEventLog(t *testing.T, api string) {
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 @@ -295,6 +296,34 @@ func testWindowsEventLog(t *testing.T, api string) {

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