-
Notifications
You must be signed in to change notification settings - Fork 7
/
EtwKernelRoutines.js
299 lines (276 loc) · 11.7 KB
/
EtwKernelRoutines.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
"use strict";
function initializeScript()
{
return [new host.functionAlias(AllRegisteredLoggersWithConsumers, "EtwLoggersAndConsumers"),
new host.functionAlias(EtwConsumersForProcess, "EtwConsumersForProcess"),
new host.functionAlias(EtwConsumersForAllProcesses, "EtwConsumersForAllProcesses"),
new host.functionAlias(EtwRegisteredGuids, "EtwRegisteredGuids"),
new host.functionAlias(EtwProvidersForProcess, "EtwProvidersForProcess"),
new host.functionAlias(EtwProvidersForAllProcesses, "EtwProvidersForAllProcesses"),
new host.apiVersionSupport(1, 7)];
}
function invokeScript()
{
//
// Insert your script content here. This method will be called whenever the script is
// invoked from a client.
//
// See the following for more details:
//
// https://aka.ms/JsDbgExt
//
}
function GetGuidsForLoggerId(loggerId, guidType)
{
let dbgOutput = host.diagnostics.debugLog;
let guidArray = new Array();
let hostSiloGlobals = host.getModuleSymbolAddress("nt", "PspHostSiloGlobals");
let typedhostSiloGlobals = host.createTypedObject(hostSiloGlobals, "nt", "_ESERVERSILO_GLOBALS");
let guidHashTable = typedhostSiloGlobals.EtwSiloState.EtwpGuidHashTable;
// As a performance optimization, since Windows 8 the reigstered GUIDs are
// organized in 64 hash buckets instead of one big list.
// Each hash bucket contains a list of all GUIDs with a matching hash.
for (let bucket of guidHashTable)
{
// There are three GUID types in each bucket, deinfed in ETW_GUID_TYPE enum:
// - EtwTraceGuidType = 0
// - EtwNotificationGuidType = 1
// - EtwGroupGuidType = 2
// There is a linked list for each type in every bucket.
// Collect GUIDs of the type requested by the caller.
// Most common type is EtwTraceGuidType.
let guidEntries = host.namespace.Debugger.Utility.Collections.FromListEntry(bucket.ListHead[guidType], "nt!_ETW_GUID_ENTRY", "GuidList");
if (guidEntries.Count() != 0)
{
for (let guid of guidEntries)
{
if (loggerId === -1)
{
guidArray.push(guid);
}
else
{
for (let enableInfo of guid.EnableInfo)
{
if (enableInfo.LoggerId === loggerId)
{
guidArray.push(guid);
break;
}
}
}
}
}
}
return guidArray;
}
function EtwRegisteredGuids()
{
let dbgOutput = host.diagnostics.debugLog;
let guidsArrays = new Array();
guidsArrays[0] = GetGuidsForLoggerId(-1, 0);
guidsArrays[1] = GetGuidsForLoggerId(-1, 1);
guidsArrays[2] = GetGuidsForLoggerId(-1, 2);
let guidTypes;
guidTypes = [
"EtwTraceGuidType",
"EtwNotificationGuidType",
"EtwGroupGuidType"
];
for (let i in guidsArrays)
{
if (guidsArrays[i].length != 0)
{
dbgOutput("Printing GUIDs for type ", guidTypes[i], ":\n");
for (let guid of guidsArrays[i])
{
if (guid.LastEnable.Enabled) // only print enabled GUIDs
{
dbgOutput("\tETW Guid Entry: ", guid.address, "\n");
dbgOutput("\tGuid: ", guid.Guid, "\n");
dbgOutput("\tSecurity Descriptor: ", guid.SecurityDescriptor.address, "\n");
dbgOutput("\tLogger ID: ", guid.LastEnable.LoggerId, "\n");
let regEntryLinkField = "RegList";
if (i == 2)
{
// group GUIDs registration entries are linked through the GroupRegList field
regEntryLinkField = "GroupRegList";
}
let regEntries = host.namespace.Debugger.Utility.Collections.FromListEntry(guid.RegListHead, "nt!_ETW_REG_ENTRY", regEntryLinkField);
if (regEntries.Count() != 0)
{
dbgOutput("\tRegistration entries: \n");
for (let regEntry of regEntries)
{
dbgOutput("\t\tETW_REG_ENTRY: ", regEntry.address, "\n");
if ((regEntry.DbgUserRegistration != 0) && (host.parseInt64(regEntry.Process.address, 16).compareTo(host.parseInt64(0)) != 0))
{
try {
dbgOutput("\t\t\tProcess: ", regEntry.Process.SeAuditProcessCreationInfo.ImageFileName.Name, " ID: ", host.parseInt64(regEntry.Process.UniqueProcessId.address, 16).toString(10), "\n");
} catch (e) {
}
}
if (host.parseInt64(regEntry.Callback.address, 16).compareTo(host.parseInt64(0)) != 0)
{
if (regEntry.DbgKernelRegistration != 0)
{
let callback_sym = host.namespace.Debugger.Utility.Control.ExecuteCommand(".printf \"%y\"," + host.parseInt64(regEntry.Callback.address, 16).toString());
dbgOutput("\t\t\tKernel registration. Callback: ", callback_sym.First(), "\n");
}
else
{
dbgOutput("\t\t\t\tCallback: ", regEntry.Callback.address, "\n");
}
}
}
}
dbgOutput("\n");
}
}
}
}
}
function AllRegisteredLoggersWithConsumers()
{
let dbgOutput = host.diagnostics.debugLog;
let hostSiloGlobals = host.getModuleSymbolAddress("nt", "PspHostSiloGlobals");
let typedhostSiloGlobals = host.createTypedObject(hostSiloGlobals, "nt", "_ESERVERSILO_GLOBALS");
let maxLoggers = typedhostSiloGlobals.EtwSiloState.MaxLoggers;
for (let i = 0; i < maxLoggers; i++)
{
let logger = typedhostSiloGlobals.EtwSiloState.EtwpLoggerContext[i];
if (host.parseInt64(logger.address, 16).compareTo(host.parseInt64("0x1")) != 0)
{
dbgOutput("WMI Logger Context: ", logger.address, "\n");
dbgOutput("\tName: ", logger.LoggerName, "\n");
dbgOutput("\tLoggerId: ", logger.LoggerId, "\n");
dbgOutput("\tInstance Guid: ", logger.InstanceGuid, "\n");
dbgOutput("\tRealtime Log File Name: ", logger.RealtimeLogfileName, "\n");
if ((logger.LoggerMode & 0x2000000) == 0x2000000)
{
dbgOutput("\t** This is a system trace logger **\n");
}
let consumers = host.namespace.Debugger.Utility.Collections.FromListEntry(logger.Consumers, "nt!_ETW_REALTIME_CONSUMER", "Links");
if (consumers.Count() != 0)
{
dbgOutput("\tConsumers:\n");
for (let consumer of consumers)
{
dbgOutput("\t\tName: ", consumer.ProcessObject.SeAuditProcessCreationInfo.ImageFileName.Name, "\n");
dbgOutput("\t\tId: ", host.parseInt64(consumer.ProcessObject.UniqueProcessId.address, 16).toString(10), "\n");
}
}
// we only care about trace guids in this case
let guidArray = GetGuidsForLoggerId(logger.LoggerId, 0);
if (guidArray.length != 0)
{
dbgOutput("\tGuids:\n");
for (let guid of guidArray)
{
for (let enableInfo of guid.EnableInfo)
{
if ((enableInfo.LoggerId === logger.LoggerId) && (enableInfo.IsEnabled))
{
dbgOutput("\t\t", guid.Guid, "\n");
}
}
}
}
dbgOutput("\n");
}
}
}
function EtwConsumersForProcess(process)
{
let dbgOutput = host.diagnostics.debugLog;
let hostSiloGlobals = host.getModuleSymbolAddress("nt", "PspHostSiloGlobals");
let typedhostSiloGlobals = host.createTypedObject(hostSiloGlobals, "nt", "_ESERVERSILO_GLOBALS");
dbgOutput("ETW consumers for process ", process.Name, " with ID ", process.Id, ":\n");
let handles = process.Io.Handles;
try
{
for (let handle of handles)
{
try
{
let objType = handle.Object.ObjectType;
if (objType === "EtwConsumer")
{
let consumer = host.createTypedObject(handle.Object.Body.address, "nt", "_ETW_REALTIME_CONSUMER");
let loggerId = consumer.LoggerId;
let logger = typedhostSiloGlobals.EtwSiloState.EtwpLoggerContext[loggerId];
dbgOutput("\tETW Realtime Consumer: ", consumer.address, "\n");
dbgOutput("\t\tWMI Logger Context: ", logger.address, "\n");
dbgOutput("\t\tName: ", logger.LoggerName, "\n");
dbgOutput("\t\tLoggerId: ", loggerId, "\n");
dbgOutput("\t\tInstance Guid: ", logger.InstanceGuid, "\n");
dbgOutput("\t\tRealtime Log File Name: ", logger.RealtimeLogfileName, "\n");
let guidArray = GetGuidsForLoggerId(loggerId, 0);
if (guidArray.length != 0)
{
dbgOutput("\t\tProvider Guids:\n");
for (let guid of guidArray)
{
if (guid.LastEnable.Enabled) // only show enabled GUIDs
{
dbgOutput("\t\t\t", guid.Guid, "\n");
}
}
}
else
{
dbgOutput("\t\t** No provider GUIDs are registered for this logger **\n");
}
dbgOutput("\n");
}
} catch (e) {
dbgOutput("\tException parsing handle ", handle.Handle, "in process ", process.Name, "!\n");
}
}
} catch (e) {
}
}
function EtwProvidersForProcess(process)
{
let dbgOutput = host.diagnostics.debugLog;
dbgOutput("ETW providers for process ", process.Name, " with ID ", process.Id, ":\n");
let handles = process.Io.Handles;
try
{
for (let handle of handles)
{
try
{
let objType = handle.Object.ObjectType;
if (objType === "EtwRegistration")
{
let regEntry = host.createTypedObject(handle.Object.Body.address, "nt", "_ETW_REG_ENTRY");
dbgOutput("\t", regEntry.GuidEntry.Guid, "\n");
}
} catch (e) {
dbgOutput("\tException parsing handle ", handle.Handle, "in process ", process.Name, "!\n");
}
}
} catch (e) {
}
}
function EtwConsumersForAllProcesses()
{
let dbgOutput = host.diagnostics.debugLog;
let processes = host.currentSession.Processes;
for (let process of processes)
{
EtwConsumersForProcess(process);
dbgOutput("\n");
}
}
function EtwProvidersForAllProcesses()
{
let dbgOutput = host.diagnostics.debugLog;
let processes = host.currentSession.Processes;
for (let process of processes)
{
EtwProvidersForProcess(process);
dbgOutput("\n");
}
}