A Primer On Event Tracing For Windows (ETW)

Introduction

What is ETW?

It’s a general-purpose, high-speed tracing facility provided by the operating system. Using a buffering and logging mechanism implemented in the kernel, ETW provides a tracing mechanism for events raised by both user-mode applications and kernel-mode device drivers — MSDN-Magazine

Architecture

https://web.archive.org/web/20200725154736/https://docs.microsoft.com/en-us/archive/blogs/ntdebugging/part-1-etw-introduction-and-overview

Controllers

Providers

Event Viewer
<channel
chid="c1"
name="Microsoft-Windows-Windows Defender/Operational"
symbol="CHANNEL_DEFENDER_OPERATIONAL"
type="Operational"
/>
Image from “winmeta.xml”
...
<task name="ProcessStart" message="ProcessStart" value="1"/>
<task name="ProcessStop" message="ProcessStop" value="2"/><task name="ThreadStart" message="ThreadStart" value="3"/><task name="ThreadStop" message="ThreadStop" value="4"/>...
<task name="Connect" message="Connect" value="1">     <opcodes>        <opcode name="Open(async)" message="Open(async)" value="10"/>        <opcode name="tobeusedwhenanobjectisconstructed" message="tobeusedwhenanobjectisconstructed" value="16"/>        <opcode name="Tobeusedwhenoperationisjustexecutingamethod" message="Tobeusedwhenoperationisjustexecutingamethod" value="20"/>     </opcodes></task>
...<keywords>   <keyword name="WINEVENT_KEYWORD_PROCESS" message="WINEVENT_KEYWORD_PROCESS" mask="0x10"/>   <keyword name="WINEVENT_KEYWORD_THREAD" message="WINEVENT_KEYWORD_THREAD" mask="0x20"/>   <keyword name="WINEVENT_KEYWORD_IMAGE" message="WINEVENT_KEYWORD_IMAGE" mask="0x40"/>   <keyword name="WINEVENT_KEYWORD_CPU_PRIORITY" message="WINEVENT_KEYWORD_CPU_PRIORITY" mask="0x80"/></keywords>...
<maps><valueMap name="DnsIpTypeMap"><map value="0x0" message="static"/><map value="0x1" message="dynamic"/></valueMap></maps>
Message : Correlating activity id's. %n %t CurrentActivityId: %1 %n %t ParentActivityId: %2
<template tid="T_CorrelationEvent"><data inType="win:GUID" name="currentActivityId" /><data inType="win:GUID" name="parentActivityId" /></template>
<event value="1" symbol="ProcessStart" version="0" task="ProcessStart" opcode="win:Start" level="win:Informational" keywords="WINEVENT_KEYWORD_PROCESS" template="ProcessStartArgs"/>
<task name="ProcessStart" message="$(string.task_ProcessStart)" value="1"/>
<localization><resources culture="en-GB"><stringTable><string id="keyword_WINEVENT_KEYWORD_PROCESS" value="WINEVENT_KEYWORD_PROCESS"/><string id="keyword_WINEVENT_KEYWORD_THREAD" value="WINEVENT_KEYWORD_THREAD"/><string id="keyword_WINEVENT_KEYWORD_IMAGE" value="WINEVENT_KEYWORD_IMAGE"/>...</stringTable></resources></localization>

Trace Sessions / Trace Files (.etl)

Consumers

Interacting With ETW

# logman.exe query providers[Provider]               [GUID]--------------------------------------------------------------------...Microsoft-Windows-Crypto-CNG  {E3E0E2F0-C9C5-11E0-8AB9-9EBC4824019B}Microsoft-Windows-Crypto-RNG  {54D5AC20-E14F-4FDA-92DA-EBF7556FF176}Microsoft-Windows-D3D9        {783ACA0A-790E-4D7F-8451-AA850511C6B9}Microsoft-Windows-Data-Pdf    {B97561FE-B27A-4C48-AA3E-7D3ADDC105B1}Microsoft-Windows-DCLocator   {CFAA5446-C6C4-4F5C-866F-31C9B55B962D}Microsoft-Windows-DDisplay    {75051C9D-2833-4A29-8923-046DB7A432CA}...
# logman.exe query providers "Microsoft-Windows-WMI-Activity"Provider                                 GUID
--------------------------------------------------------------------

Microsoft-Windows-WMI-Activity {1418EF04-B0B4-4623-BF7E-D74AB47BBDAA}
Value Keyword Description
--------------------------------------------------------------------

0x8000000000000000 Microsoft-Windows-WMI-Activity/Trace
0x4000000000000000 Microsoft-Windows-WMI-Activity/Operational
0x2000000000000000 Microsoft-Windows-WMI-Activity/Debug
Value Level Description
--------------------------------------------------------------------

0x02 win:Error Error
0x04 win:Informational Information
PID Image
--------------------------------------------------------------------

0x000012d4 C:\Windows\System32\wbem\WmiPrvSE.exe
0x00000dd4 C:\Windows\System32\svchost.exe
0x00000dd4 C:\Windows\System32\svchost.exe
0x00000cb4 C:\Windows\System32\wbem\WmiPrvSE.exe
0x00000a30 C:\Windows\System32\svchost.exe
# logman.exe query -etsData Collector Set              Type                  Status
--------------------------------------------------------------------
...Circular Kernel Context Logger Trace RunningEventlog-Security Trace RunningDiagLog Trace RunningDiagtrack-Listener Trace RunningEventLog-Application Trace RunningEventLog-System Trace RunningLwtNetLog Trace RunningNetCore Trace RunningNtfsLog Trace Running...
# logman.exe query "EventLog-System" -etsName:                 EventLog-System
Status: Running
Root Path: %systemdrive%\PerfLogs\Admin
Segment: Off
Schedules: On
Segment Max Size: 100 MB
Name: EventLog-System\EventLog-System
Type: Trace
Append: Off
Circular: Off
Overwrite: Off
Buffer Size: 64
Buffers Lost: 0
Buffers Written: 1345
Buffer Flush Timer: 1
Clock Type: System
File Mode: Real-time
Provider:
Name: Microsoft-Windows-FunctionDiscoveryHost
Provider Guid: {538CBBAD-4877-4EB2-B26E-7CAEE8F0F8CB}
Level: 255
KeywordsAll: 0x0
KeywordsAny: 0x8000000000000000 (System)
Properties: 65
Filter Type: 0
Provider:
Name: Microsoft-Windows-Subsys-SMSS
Provider Guid: {43E63DA5-41D1-4FBF-ADED-1BBED98FDD1D}
Level: 255
KeywordsAll: 0x0
KeywordsAny: 0x4000000000000000 (System)
Properties: 65
Filter Type: 0
Provider:
Name: Microsoft-Windows-Kernel-General
Provider Guid: {A68CA8B7-004F-D7B6-A698-07E2DE0F1F5D}
Level: 255
KeywordsAll: 0x0
KeywordsAny: 0x8000000000000000 (System)
Properties: 65
Filter Type: 0
...
Perf Monitor
[System.Diagnostics.Eventing.Reader.EventLogSession]::GlobalSession.GetProviderNames()
# [System.Diagnostics.Eventing.Reader.ProviderMetadata]("Microsoft-Windows-Powershell")Name              : Microsoft-Windows-PowershellId                : a0c1853b-5c40-4b15-8766-3cf1c58f985aMessageFilePath   : C:\Windows\system32\WindowsPowerShell\v1.0\PSEvents.dllResourceFilePath  : C:\Windows\system32\WindowsPowerShell\v1.0\PSEvents.dllParameterFilePath :HelpLink          : https://go.microsoft.com/fwlink/events.asp?CoName=Microsoft Corporation&ProdName=Microsoft® Windows® Operating System&ProdVer=10.0.19041.1&FileName=PSEvents.dll&FileVer=10.0.19041.1DisplayName       :LogLinks          : {Microsoft-Windows-PowerShell/Operational, Microsoft-Windows-PowerShell/Analytic, Microsoft-Windows-PowerShell/Debug, Microsoft-Windows-PowerShell/Admin}Levels            : {win:Error, win:Warning, win:Informational, win:Verbose...}Opcodes           : {win:Start, win:Stop, Open, Close...}Keywords          : {Runspace, Pipeline, Protocol, Transport...}Tasks             : {CreateRunspace, ExecuteCommand, Serialization, Powershell-Console-Startup...}Events            : {4097, 4098, 4099, 4100...}
# [System.Diagnostics.Eventing.Reader.ProviderMetadata]("Microsoft-Windows-Powershell") | Select-Object -ExpandProperty OpcodesName                  Value DisplayName
---- ----- -----------
win:Start 1 Start
win:Stop 2 Stop
Open 10 Open (async)
Close 11 Close (Async)
Connect 12 connect
Disconnect 13 Disconnect
Negotiate 14 Negotiate
Create 15 On create calls
Constructor 16 to be used when an object is constructed
Dispose 17 To be used when an object is disposed
EventHandler 18 To be used when an event handler is raised
Exception 19 To be used when an exception is raised
Method 20 To be used when operation is just executing a method
Send 21 Send (Async)
Receive 22 Receive (Async)
Rehydration 23 Rehydration
SerializationSettings 24 Serialization settings
ShuttingDown 25 Shutting down
Message Analyzer — 1
Message Analyzer — 2
Message Analyzer — 3

Extracting ETW Providers

RegisteredTraceEventParser.GetManifestForRegisteredProvider
ETW Explorer — Summary
ETW Explorer — Events
ETW Explorer — Manifest
DumpRegisteredManifest providerName [outputFileName]
WEPExplorer Output

Script ETW

More ETW

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WINEVT\Publishers\
Registry location

Conclusion

--

--

I write about #Detection #ThreatHunting #WindowsInternals #Malware #DFIR and occasionally #Python.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nasreddine Bencherchali

I write about #Detection #ThreatHunting #WindowsInternals #Malware #DFIR and occasionally #Python.