Skip to content

Commit

Permalink
use omitempty on a uint8 pointer to allow absence of opcode raw to be…
Browse files Browse the repository at this point in the history
… expressed
  • Loading branch information
efd6 committed Mar 30, 2022
1 parent b286188 commit bc88df8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 17 deletions.
22 changes: 8 additions & 14 deletions winlogbeat/sys/winevent/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ var (
const (
keywordAuditFailure = 0x10000000000000
keywordAuditSuccess = 0x20000000000000

// keywordClassic indicates the log was published with the "classic" event
// logging API.
// https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.eventing.reader.standardeventkeywords?view=netframework-4.8
keywordClassic = 0x80000000000000
)

// UnmarshalXML unmarshals the given XML into a new Event.
Expand All @@ -67,7 +62,7 @@ type Event struct {
Version Version `xml:"System>Version"`
LevelRaw uint8 `xml:"System>Level"`
TaskRaw uint16 `xml:"System>Task"`
OpcodeRaw uint8 `xml:"System>Opcode"`
OpcodeRaw *uint8 `xml:"System>Opcode,omitempty"`
KeywordsRaw HexInt64 `xml:"System>Keywords"`
TimeCreated TimeCreated `xml:"System>TimeCreated"`
RecordID uint64 `xml:"System>EventRecordID"`
Expand Down Expand Up @@ -343,20 +338,19 @@ func (v *HexInt64) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
func EnrichRawValuesWithNames(publisherMeta *WinMeta, event *Event) {
// Keywords. Each bit in the value can represent a keyword.
rawKeyword := int64(event.KeywordsRaw)
isClassic := keywordClassic&rawKeyword > 0

if len(event.Keywords) == 0 {
for mask, keyword := range defaultWinMeta.Keywords {
if rawKeyword&mask > 0 {
if rawKeyword&mask != 0 {
event.Keywords = append(event.Keywords, keyword)
rawKeyword -= mask
rawKeyword &^= mask
}
}
if publisherMeta != nil {
for mask, keyword := range publisherMeta.Keywords {
if rawKeyword&mask > 0 {
if rawKeyword&mask != 0 {
event.Keywords = append(event.Keywords, keyword)
rawKeyword -= mask
rawKeyword &^= mask
}
}
}
Expand All @@ -365,10 +359,10 @@ func EnrichRawValuesWithNames(publisherMeta *WinMeta, event *Event) {
var found bool
if event.Opcode == "" {
// Opcode (search in defaultWinMeta first).
if !isClassic {
event.Opcode, found = defaultWinMeta.Opcodes[event.OpcodeRaw]
if event.OpcodeRaw != nil {
event.Opcode, found = defaultWinMeta.Opcodes[*event.OpcodeRaw]
if !found && publisherMeta != nil {
event.Opcode = publisherMeta.Opcodes[event.OpcodeRaw]
event.Opcode = publisherMeta.Opcodes[*event.OpcodeRaw]
}
}
}
Expand Down
1 change: 1 addition & 0 deletions winlogbeat/sys/winevent/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func TestXML(t *testing.T) {
EventIdentifier: EventIdentifier{ID: 91},
LevelRaw: 4,
TaskRaw: 9,
OpcodeRaw: new(uint8), // The value in the XML is 0.
KeywordsRaw: 0x8020000000000000,
TimeCreated: TimeCreated{allXMLTimeCreated},
RecordID: 100,
Expand Down
5 changes: 4 additions & 1 deletion winlogbeat/sys/wineventlog/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,10 @@ func (r *Renderer) renderSystem(handle EvtHandle, event *winevent.Event) error {
case EvtSystemTask:
event.TaskRaw = data.(uint16)
case EvtSystemOpcode:
event.OpcodeRaw = data.(uint8)
if event.OpcodeRaw == nil {
event.OpcodeRaw = new(uint8)
}
*event.OpcodeRaw = data.(uint8)
case EvtSystemKeywords:
event.KeywordsRaw = winevent.HexInt64(data.(hexInt64))
case EvtSystemTimeCreated:
Expand Down
5 changes: 3 additions & 2 deletions winlogbeat/sys/wineventlog/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ func TestRenderer(t *testing.T) {

assert.Equal(t, e.Keywords, []string{"Audit Success"})

assert.EqualValues(t, 0, e.OpcodeRaw)
assert.NotNil(t, 0, e.OpcodeRaw)
assert.EqualValues(t, 0, *e.OpcodeRaw)
assert.Equal(t, "Info", e.Opcode)

assert.EqualValues(t, 0, e.LevelRaw)
Expand Down Expand Up @@ -131,7 +132,7 @@ func TestRenderer(t *testing.T) {

assert.Equal(t, e.Keywords, []string{"Classic"})

assert.EqualValues(t, 0, e.OpcodeRaw)
assert.EqualValues(t, (*uint8)(nil), e.OpcodeRaw)
assert.Equal(t, "", e.Opcode)

assert.EqualValues(t, 4, e.LevelRaw)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Event xmlns='http://schemas.microsoft.com/win/2004/08/events/event'><System><Provider Name='Windows Error Reporting'/><EventID Qualifiers='0'>1001</EventID><Level>4</Level><Task>0</Task><Keywords>0x80000000000000</Keywords><TimeCreated SystemTime='2020-02-18T02:52:37.1211986Z'/><EventRecordID>420107</EventRecordID><Channel>Application</Channel><Computer>vagrant</Computer><Security/></System><EventData><Data></Data><Data>0</Data><Data>WindowsWcpOtherFailure3</Data><Data>Not available</Data><Data>0</Data><Data>10.0.17763.850:3</Data><Data>inc\auto_hive.h</Data><Data>Windows::Rtl::AutoHive::Unload</Data><Data>358</Data><Data>c0000121</Data><Data>0xaad0d4fb</Data><Data></Data><Data></Data><Data></Data><Data></Data><Data>
\\?\C:\Windows\Logs\CBS\CBS.log
\\?\C:\Windows\Logs\CBS\CbsPersist_20200212163557.log
\\?\C:\Windows\Logs\CBS\CbsPersist_20200211235949.log
\\?\C:\Windows\Logs\CBS\CbsPersist_20200211033558.cab
\\?\C:\Windows\Logs\CBS\CbsPersist_20200210020038.cab
\\?\C:\Windows\Logs\CBS\CbsPersist_20200209082850.cab
\\?\C:\Windows\servicing\Sessions\Sessions.xml
\\?\C:\Windows\WinSxs\pending.xml
\\?\C:\Windows\WinSxs\poqexec.log
\\?\C:\Windows\Logs\Cbs\FilterList.log
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERC5A1.tmp.WERInternalMetadata.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERC7D5.tmp.xml
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERC7F3.tmp.csv
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERC9F8.tmp.txt
\\?\C:\ProgramData\Microsoft\Windows\WER\Temp\WERCA08.tmp.mdmp
\\?\C:\ProgramData\Microsoft\Windows\WER\ReportQueue\Critical_10.0.17763.850_3_b785171a54ee6e13bf912aeeb5bef5d9105e314b_00000000_cab_0c38cad1\memory.hdmp
\\?\C:\Windows\Temp\WERCAD4.tmp.WERDataCollectionStatus.txt</Data><Data>\\?\C:\ProgramData\Microsoft\Windows\WER\ReportQueue\Critical_10.0.17763.850_3_b785171a54ee6e13bf912aeeb5bef5d9105e314b_00000000_cab_0c38cad1</Data><Data></Data><Data>0</Data><Data>5e9de0ad-0fa4-4daa-aec1-8127dc88e6c7</Data><Data>100</Data><Data></Data><Data>0</Data></EventData></Event>
1 change: 1 addition & 0 deletions winlogbeat/sys/wineventlog/wineventlog_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestWinEventLog(t *testing.T) {
path string
events int
}{
{path: "application-windows-error-reporting.evtx", events: 1},
{path: "sysmon-9.01.evtx", events: 32},
{path: "ec1.evtx", events: 1}, // eventcreate /id 1000 /t error /l application /d "My custom error event for the application log"
{path: "ec2.evtx", events: 1}, // eventcreate /id 999 /t error /l application /so WinWord /d "Winword event 999 happened due to low diskspace"
Expand Down

0 comments on commit bc88df8

Please sign in to comment.