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

Cherry-pick #11357 to 5.6: Fix Winlogbeat escaping CRLF sequences #11372

Merged
merged 2 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ https://github.com/elastic/beats/compare/v5.6.16...5.6[Check the HEAD diff]
*Winlogbeat*

- Prevent Winlogbeat from dropping events with invalid XML. {pull}11006{11006}
- Fix Winlogbeat escaping CR, LF and TAB characters. {issue}11328[11328] {pull}11357[11357]

==== Added

Expand Down
21 changes: 7 additions & 14 deletions winlogbeat/sys/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -151,21 +152,13 @@ func TestXML(t *testing.T) {
}
}

// Tests that control characters other than CR and LF are escaped
// when the event is decoded.
func TestInvalidXML(t *testing.T) {
eventXML := fmt.Sprintf(`
<Event>
<UserData>
<Operation_ClientFailure xmlns='http://manifests.microsoft.com/win/2006/windows/WMI'>
<Id>{00000000-0000-0000-0000-000000000000}</Id>
<Message>じゃあ宇宙カウボーイ。。。%s</Message>
</Operation_ClientFailure>
</UserData>
</Event>
`, "\x1b")
_, err := UnmarshalEventXML([]byte(eventXML))
if !assert.NoError(t, err) {
assert.Equal(t, err.Error(), "XML syntax error on line 6: illegal character code U+001B")
}
evXML := strings.Replace(allXML, "%1", "\t&#xD;\n\x1b", -1)
ev, err := UnmarshalEventXML([]byte(evXML))
assert.Equal(t, nil, err)
assert.Equal(t, "Creating WSMan shell on server with ResourceUri: \t\r\n\\u001b", ev.Message)
}

func BenchmarkXMLUnmarshal(b *testing.B) {
Expand Down
2 changes: 1 addition & 1 deletion winlogbeat/sys/xmlreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (r *xmlSafeReader) Read(d []byte) (n int, err error) {
}
for i := 0; i < len(r.buf); {
code, size := utf8.DecodeRune(r.buf[i:])
if unicode.IsControl(code) {
if !unicode.IsSpace(code) && unicode.IsControl(code) {
n = copy(d, r.buf[:i])
r.buf = r.buf[n+1:]
r.code = []byte(fmt.Sprintf("\\u%04x", code))
Expand Down
24 changes: 24 additions & 0 deletions winlogbeat/tests/system/test_eventlogging.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,27 @@ def test_utf16_characters(self):
})
self.assertTrue(len(evts), 1)
self.assertEqual(evts[0]["message"], msg)

def test_multiline_events(self):
"""
eventlogging - Event with newlines and control characters
"""
msg = """
A trusted logon process has been registered with the Local Security Authority.
This logon process will be trusted to submit logon requests.

Subject:

Security ID: SYSTEM
Account Name: MS4\x1e$
Account Domain: WORKGROUP
Logon ID: 0x3e7
Logon Process Name: IKE"""
self.write_event_log(msg)
evts = self.read_events()
self.assertTrue(len(evts), 1)
self.assertEqual(unicode(self.api), evts[0]["winlog.api"], evts[0])
self.assertNotIn("event.original", evts[0], msg=evts[0])
self.assertIn("message", evts[0], msg=evts[0])
self.assertNotIn("\\u000a", evts[0]["message"], msg=evts[0])
self.assertEqual(unicode(msg), evts[0]["message"].decode('unicode-escape'), msg=evts[0])
24 changes: 24 additions & 0 deletions winlogbeat/tests/system/test_wineventlog.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,27 @@ def test_utf16_characters(self):
})
self.assertTrue(len(evts), 1)
self.assertEqual(evts[0]["message"], msg)

def test_multiline_events(self):
"""
wineventlog - Event with newlines and control characters
"""
msg = """
A trusted logon process has been registered with the Local Security Authority.
This logon process will be trusted to submit logon requests.

Subject:

Security ID: SYSTEM
Account Name: MS4\x1e$
Account Domain: WORKGROUP
Logon ID: 0x3e7
Logon Process Name: IKE"""
self.write_event_log(msg)
evts = self.read_events()
self.assertTrue(len(evts), 1)
self.assertEqual(unicode(self.api), evts[0]["winlog.api"], msg=evts[0])
self.assertNotIn("event.original", evts[0], msg=evts[0])
self.assertIn("message", evts[0], msg=evts[0])
self.assertNotIn("\\u000a", evts[0]["message"], msg=evts[0])
self.assertEqual(unicode(msg), evts[0]["message"].decode('unicode-escape'), msg=evts[0])