27 _In_ PEVENT_TRACE_LOGFILE Buffer
31 _In_ PEVENT_RECORD EventRecord
43 _In_ PEVENT_TRACE_LOGFILE Buffer
47 _In_ PEVENT_RECORD EventRecord
54 static GUID ProcessHackerGuid = { 0x1288c53b, 0xaf35, 0x481b, { 0xb6, 0xb5, 0xa0, 0x5c, 0x39, 0x87, 0x2e, 0xd } };
55 static GUID SystemTraceControlGuid_I = { 0x9e814aad, 0x3204, 0x11d2, { 0x9a, 0x82, 0x00, 0x60, 0x08, 0xa8, 0x69, 0x39 } };
56 static GUID KernelRundownGuid_I = { 0x3b9c9951, 0x3480, 0x4220, { 0x93, 0x77, 0x9c, 0x8e, 0x51, 0x84, 0xf5, 0xcd } };
57 static GUID DiskIoGuid_I = { 0x3d6fa8d4, 0xfe05, 0x11d0, { 0x9d, 0xda, 0x00, 0xc0, 0x4f, 0xd7, 0xba, 0x7c } };
58 static GUID FileIoGuid_I = { 0x90cbdc39, 0x4a3e, 0x11d1, { 0x84, 0xf4, 0x00, 0x00, 0xf8, 0x04, 0x64, 0xe3 } };
59 static GUID TcpIpGuid_I = { 0x9a280ac0, 0xc8e0, 0x11d1, { 0x84, 0xe2, 0x00, 0xc0, 0x4f, 0xb9, 0x98, 0xa2 } };
60 static GUID UdpIpGuid_I = { 0xbf3a50c5, 0xa9c9, 0x4988, { 0xa0, 0x05, 0x2d, 0xf0, 0xb7, 0xc8, 0x0f, 0x80 } };
67 static TRACEHANDLE EtpSessionHandle;
69 static PGUID EtpActualSessionGuid;
70 static PEVENT_TRACE_PROPERTIES EtpTraceProperties;
71 static BOOLEAN EtpEtwActive;
72 static BOOLEAN EtpStartedSession;
73 static BOOLEAN EtpEtwExiting;
74 static HANDLE EtpEtwMonitorThreadHandle;
79 static TRACEHANDLE EtpRundownSessionHandle;
80 static PEVENT_TRACE_PROPERTIES EtpRundownTraceProperties;
81 static BOOLEAN EtpRundownActive;
82 static HANDLE EtpRundownEtwMonitorThreadHandle;
103 EtpEtwExiting =
TRUE;
107 if (EtpRundownActive)
122 EtpActualKernelLoggerName = &EtpPrivateKernelLoggerName;
123 EtpActualSessionGuid = &ProcessHackerGuid;
127 EtpActualKernelLoggerName = &EtpSharedKernelLoggerName;
128 EtpActualSessionGuid = &SystemTraceControlGuid_I;
131 bufferSize =
sizeof(EVENT_TRACE_PROPERTIES) + EtpActualKernelLoggerName->
Length +
sizeof(WCHAR);
133 if (!EtpTraceProperties)
134 EtpTraceProperties = PhAllocate(bufferSize);
136 memset(EtpTraceProperties, 0,
sizeof(EVENT_TRACE_PROPERTIES));
138 EtpTraceProperties->Wnode.BufferSize = bufferSize;
139 EtpTraceProperties->Wnode.Guid = *EtpActualSessionGuid;
140 EtpTraceProperties->Wnode.ClientContext = 1;
141 EtpTraceProperties->Wnode.Flags = WNODE_FLAG_TRACED_GUID;
142 EtpTraceProperties->MinimumBuffers = 1;
143 EtpTraceProperties->LogFileMode = EVENT_TRACE_REAL_TIME_MODE;
144 EtpTraceProperties->FlushTimer = 1;
145 EtpTraceProperties->EnableFlags = EVENT_TRACE_FLAG_DISK_IO | EVENT_TRACE_FLAG_DISK_FILE_IO | EVENT_TRACE_FLAG_NETWORK_TCPIP;
146 EtpTraceProperties->LogFileNameOffset = 0;
147 EtpTraceProperties->LoggerNameOffset =
sizeof(EVENT_TRACE_PROPERTIES);
150 EtpTraceProperties->LogFileMode |= EVENT_TRACE_SYSTEM_LOGGER_MODE;
152 result = StartTrace(&EtpSessionHandle, EtpActualKernelLoggerName->Buffer, EtpTraceProperties);
154 if (result == ERROR_SUCCESS)
158 EtpStartedSession =
TRUE;
160 else if (result == ERROR_ALREADY_EXISTS)
164 EtpStartedSession =
FALSE;
170 EtpEtwActive =
FALSE;
171 EtpStartedSession =
FALSE;
176 _In_ ULONG ControlCode
181 EtpTraceProperties->LogFileNameOffset = 0;
184 EtpStartedSession ? EtpSessionHandle : 0,
185 EtpStartedSession ? NULL : EtpActualKernelLoggerName->Buffer,
208 _In_ PEVENT_TRACE_LOGFILE Buffer
211 return !EtpEtwExiting;
215 _In_ PEVENT_RECORD EventRecord
218 if (memcmp(&EventRecord->EventHeader.ProviderId, &DiskIoGuid_I,
sizeof(GUID)) == 0)
227 switch (EventRecord->EventHeader.EventDescriptor.Opcode)
229 case EVENT_TRACE_TYPE_IO_READ:
232 case EVENT_TRACE_TYPE_IO_WRITE:
239 if (diskEvent.
Type != -1)
250 if (EventRecord->EventHeader.ProcessId != -1)
266 else if (memcmp(&EventRecord->EventHeader.ProviderId, &FileIoGuid_I,
sizeof(GUID)) == 0)
275 switch (EventRecord->EventHeader.EventDescriptor.Opcode)
290 if (fileEvent.
Type != -1)
301 memcmp(&EventRecord->EventHeader.ProviderId, &TcpIpGuid_I,
sizeof(GUID)) == 0 ||
302 memcmp(&EventRecord->EventHeader.ProviderId, &UdpIpGuid_I,
sizeof(GUID)) == 0
310 networkEvent.
Type = -1;
312 switch (EventRecord->EventHeader.EventDescriptor.Opcode)
314 case EVENT_TRACE_TYPE_SEND:
318 case EVENT_TRACE_TYPE_RECEIVE:
322 case EVENT_TRACE_TYPE_SEND + 16:
326 case EVENT_TRACE_TYPE_RECEIVE + 16:
332 if (memcmp(&EventRecord->EventHeader.ProviderId, &TcpIpGuid_I,
sizeof(GUID)) == 0)
337 if (networkEvent.
Type != -1)
351 source.
Port = _byteswap_ushort(data->
sport);
354 destination.
Port = _byteswap_ushort(data->
dport);
365 source.
Port = _byteswap_ushort(data->
sport);
368 destination.
Port = _byteswap_ushort(data->
dport);
386 EVENT_TRACE_LOGFILE logFile;
387 TRACEHANDLE traceHandle;
389 memset(&logFile, 0,
sizeof(EVENT_TRACE_LOGFILE));
390 logFile.LoggerName = EtpActualKernelLoggerName->Buffer;
391 logFile.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_EVENT_RECORD;
397 result = ERROR_SUCCESS;
398 traceHandle = OpenTrace(&logFile);
400 if (traceHandle != INVALID_PROCESSTRACE_HANDLE)
402 while (!EtpEtwExiting && (result = ProcessTrace(&traceHandle, 1, NULL, NULL)) == ERROR_SUCCESS)
405 CloseTrace(traceHandle);
411 if (result == ERROR_WMI_INSTANCE_NOT_FOUND)
423 return STATUS_SUCCESS;
433 bufferSize =
sizeof(EVENT_TRACE_PROPERTIES) + EtpRundownLoggerName.
Length +
sizeof(WCHAR);
435 if (!EtpRundownTraceProperties)
436 EtpRundownTraceProperties = PhAllocate(bufferSize);
438 memset(EtpRundownTraceProperties, 0,
sizeof(EVENT_TRACE_PROPERTIES));
440 EtpRundownTraceProperties->Wnode.BufferSize = bufferSize;
441 EtpRundownTraceProperties->Wnode.ClientContext = 1;
442 EtpRundownTraceProperties->Wnode.Flags = WNODE_FLAG_TRACED_GUID;
443 EtpRundownTraceProperties->MinimumBuffers = 1;
444 EtpRundownTraceProperties->LogFileMode = EVENT_TRACE_REAL_TIME_MODE;
445 EtpRundownTraceProperties->FlushTimer = 1;
446 EtpRundownTraceProperties->LogFileNameOffset = 0;
447 EtpRundownTraceProperties->LoggerNameOffset =
sizeof(EVENT_TRACE_PROPERTIES);
449 result = StartTrace(&EtpRundownSessionHandle, EtpRundownLoggerName.Buffer, EtpRundownTraceProperties);
451 if (result == ERROR_ALREADY_EXISTS)
455 EtpRundownTraceProperties->Wnode.BufferSize = bufferSize;
456 EtpRundownTraceProperties->LogFileNameOffset = 0;
457 EtpRundownTraceProperties->LoggerNameOffset =
sizeof(EVENT_TRACE_PROPERTIES);
458 result = StartTrace(&EtpRundownSessionHandle, EtpRundownLoggerName.Buffer, EtpRundownTraceProperties);
461 if (result != ERROR_SUCCESS)
464 result = EnableTraceEx(&KernelRundownGuid_I, NULL, EtpRundownSessionHandle, 1, 0, 0x10, 0, 0, NULL);
466 if (result != ERROR_SUCCESS)
472 EtpRundownActive =
TRUE;
482 EtpRundownTraceProperties->LogFileNameOffset = 0;
483 return ControlTrace(0, EtpRundownLoggerName.Buffer, EtpRundownTraceProperties, EVENT_TRACE_CONTROL_STOP);
487 _In_ PEVENT_TRACE_LOGFILE Buffer
490 return !EtpEtwExiting;
494 _In_ PEVENT_RECORD EventRecord
500 if (memcmp(&EventRecord->EventHeader.ProviderId, &FileIoGuid_I,
sizeof(GUID)) == 0)
509 switch (EventRecord->EventHeader.EventDescriptor.Opcode)
518 if (fileEvent.
Type != -1)
534 EVENT_TRACE_LOGFILE logFile;
535 TRACEHANDLE traceHandle;
537 memset(&logFile, 0,
sizeof(EVENT_TRACE_LOGFILE));
538 logFile.LoggerName = EtpRundownLoggerName.Buffer;
539 logFile.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME | PROCESS_TRACE_MODE_EVENT_RECORD;
542 logFile.Context = &traceHandle;
544 traceHandle = OpenTrace(&logFile);
546 if (traceHandle != INVALID_PROCESSTRACE_HANDLE)
548 ProcessTrace(&traceHandle, 1, NULL, NULL);
550 if (traceHandle != 0)
551 CloseTrace(traceHandle);
554 NtClose(EtpRundownEtwMonitorThreadHandle);
555 EtpRundownEtwMonitorThreadHandle = NULL;
557 return STATUS_SUCCESS;