-
Notifications
You must be signed in to change notification settings - Fork 74
/
r0aketw.c
213 lines (189 loc) · 6.12 KB
/
r0aketw.c
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
/*++
Copyright (c) Alex Ionescu. All rights reserved.
Module Name:
r0aketw.c
Abstract:
This module implements the ETW tracing support routines for r0ak
Author:
Alex Ionescu (@aionescu) 21-Jul-2018 - First public version
Environment:
User mode only.
--*/
#include "r0ak.h"
//
// Tracks tracing data between calls
//
typedef struct _ETW_DATA
{
TRACEHANDLE SessionHandle;
TRACEHANDLE ParserHandle;
PEVENT_TRACE_PROPERTIES Properties;
PVOID WorkItemRoutine;
} ETW_DATA, *PETW_DATA;
DEFINE_GUID(g_EtwTraceGuid,
0x53636210,
0xbe24,
0x1264,
0xc6, 0xa5, 0xf0, 0x9c, 0x59, 0x88, 0x1e, 0xbd);
WCHAR g_EtwTraceName[] = L"r0ak-etw";
VOID
EtpEtwEventCallback(
_In_ PEVENT_RECORD EventRecord
)
{
PETW_DATA etwData;
//
// Look for an "end of work item execution event"
//
if (EventRecord->EventHeader.EventDescriptor.Opcode ==
(PERFINFO_LOG_TYPE_WORKER_THREAD_ITEM_END & 0xFF))
{
//
// Grab our context and check if the work routine matches ours
//
etwData = (PETW_DATA)EventRecord->UserContext;
if (*(PVOID*)EventRecord->UserData == etwData->WorkItemRoutine)
{
//
// Stop the trace -- this callback will run a few more times
//
printf("[+] Kernel finished executing work item at 0x%.16p\n",
etwData->WorkItemRoutine);
ControlTrace(etwData->SessionHandle,
NULL,
etwData->Properties,
EVENT_TRACE_CONTROL_STOP);
}
}
}
_Success_(return != 0)
BOOL
EtwParseSession (
_In_ PETW_DATA EtwData
)
{
ULONG errorCode;
//
// Process the trace until the right work item is found
//
errorCode = ProcessTrace(&EtwData->ParserHandle, 1, NULL, NULL);
if (errorCode != ERROR_SUCCESS)
{
printf("[-] Failed to process trace: %lX\n", errorCode);
ControlTrace(EtwData->SessionHandle,
NULL,
EtwData->Properties,
EVENT_TRACE_CONTROL_STOP);
}
//
// All done -- cleanup
//
CloseTrace(EtwData->ParserHandle);
HeapFree(GetProcessHeap(), 0, EtwData->Properties);
HeapFree(GetProcessHeap(), 0, EtwData);
return errorCode == ERROR_SUCCESS;
}
_Success_(return != 0)
BOOL
EtwStartSession (
_Outptr_ PETW_DATA* EtwData,
_In_ PVOID WorkItemRoutine
)
{
ULONG errorCode;
ULONG traceFlags[8] = { 0 };
EVENT_TRACE_LOGFILEW logFile = { 0 };
ULONG bufferSize;
//
// Initialize context
//
*EtwData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(**EtwData));
if (*EtwData == NULL)
{
printf("[-] Out of memory allocating ETW state\n");
return FALSE;
}
//
// Allocate memory for our session descriptor
//
bufferSize = sizeof(EVENT_TRACE_PROPERTIES) + sizeof(g_EtwTraceName);
(*EtwData)->Properties = HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY,
bufferSize);
if ((*EtwData)->Properties == NULL)
{
printf("[-] Failed to allocate memory for the ETW trace\n");
HeapFree(GetProcessHeap(), 0, *EtwData);
return FALSE;
}
//
// Create a real-time session using the system logger, tracing nothing
//
(*EtwData)->Properties->Wnode.BufferSize = bufferSize;
(*EtwData)->Properties->Wnode.Guid = g_EtwTraceGuid;
(*EtwData)->Properties->Wnode.ClientContext = 1;
(*EtwData)->Properties->Wnode.Flags = WNODE_FLAG_TRACED_GUID;
(*EtwData)->Properties->MinimumBuffers = 1;
(*EtwData)->Properties->LogFileMode = EVENT_TRACE_REAL_TIME_MODE |
EVENT_TRACE_SYSTEM_LOGGER_MODE;
(*EtwData)->Properties->FlushTimer = 1;
(*EtwData)->Properties->LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES);
errorCode = StartTrace(&(*EtwData)->SessionHandle,
g_EtwTraceName,
(*EtwData)->Properties);
if (errorCode != ERROR_SUCCESS)
{
printf("[-] Failed to create the event trace session: %lX\n",
errorCode);
HeapFree(GetProcessHeap(), 0, (*EtwData)->Properties);
HeapFree(GetProcessHeap(), 0, *EtwData);
return FALSE;
}
//
// Open a consumer handle to it
//
logFile.LoggerName = g_EtwTraceName;
logFile.ProcessTraceMode = PROCESS_TRACE_MODE_REAL_TIME |
PROCESS_TRACE_MODE_EVENT_RECORD;
logFile.EventRecordCallback = EtpEtwEventCallback;
logFile.Context = *EtwData;
(*EtwData)->ParserHandle = OpenTrace(&logFile);
if ((*EtwData)->ParserHandle == INVALID_PROCESSTRACE_HANDLE)
{
printf("[-] Failed open a consumer handle for the trace session: %lX\n",
GetLastError());
ControlTrace((*EtwData)->SessionHandle,
NULL,
(*EtwData)->Properties,
EVENT_TRACE_CONTROL_STOP);
HeapFree(GetProcessHeap(), 0, (*EtwData)->Properties);
HeapFree(GetProcessHeap(), 0, *EtwData);
return FALSE;
}
//
// Trace worker thread events
//
traceFlags[2] = PERF_WORKER_THREAD;
errorCode = TraceSetInformation((*EtwData)->SessionHandle,
TraceSystemTraceEnableFlagsInfo,
traceFlags,
sizeof(traceFlags));
if (errorCode != ERROR_SUCCESS)
{
printf("[-] Failed to set flags for event trace session: %lX\n",
errorCode);
ControlTrace((*EtwData)->SessionHandle,
NULL,
(*EtwData)->Properties,
EVENT_TRACE_CONTROL_STOP);
CloseTrace((*EtwData)->ParserHandle);
HeapFree(GetProcessHeap(), 0, (*EtwData)->Properties);
HeapFree(GetProcessHeap(), 0, *EtwData);
return FALSE;
}
//
// Remember which work routine we'll be looking for
//
(*EtwData)->WorkItemRoutine = WorkItemRoutine;
return TRUE;
}