forked from taviso/ctftool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctftool.c
339 lines (267 loc) · 10.8 KB
/
ctftool.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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#define WIN32_LEAN_AND_MEAN
#define WIN32_NO_STATUS
#include <windows.h>
#include <winternl.h>
#include <initguid.h>
#include <stdio.h>
#include <stdlib.h>
#include <msctf.h>
#undef WIN32_NO_STATUS
#include <ntstatus.h>
#include "ntdll.h"
#include "ntalpctyp.h"
#include "ntalpc.h"
#include "wineditline/src/editline/readline.h"
#include "ctfinternal.h"
#include "ctftool.h"
#include "marshal.h"
#include "util.h"
#include "messages.h"
#include "command.h"
#pragma warning(disable : 4090 6011)
FARPROC AlpcInitializeMessageAttribute;
FARPROC AlpcGetMessageAttribute;
FARPROC GUIDFromString;
BOOL InitializeAlpcRoutines()
{
HMODULE NtDll = GetModuleHandle("NTDLL");
HMODULE Shell32 = LoadLibrary("SHELL32");
if (NtDll == NULL)
return FALSE;
if (Shell32 == NULL)
return FALSE;
AlpcInitializeMessageAttribute = GetProcAddress(NtDll, "AlpcInitializeMessageAttribute");
AlpcGetMessageAttribute = GetProcAddress(NtDll, "AlpcGetMessageAttribute");
AlpcGetMessageAttribute = GetProcAddress(NtDll, "AlpcGetMessageAttribute");
GUIDFromString = GetProcAddress(Shell32, MAKEINTRESOURCE(704));
return TRUE;
}
BOOL GetServerPortName(PWCHAR Name, SIZE_T NameMax)
{
WCHAR DesktopName[MAX_PATH];
HDESK ThreadDesktop;
DWORD NameLength;
DWORD SessionId;
// Note that it is not necessary to close this handle.
ThreadDesktop = GetThreadDesktop(GetCurrentThreadId());
// Find the SessionId from the PEB.
SessionId = NtCurrentTeb()->ProcessEnvironmentBlock->SessionId;
// Find the name of the current desktop.
if (GetUserObjectInformationW(ThreadDesktop,
UOI_NAME,
DesktopName,
sizeof DesktopName,
&NameLength)) {
_snwprintf(Name, NameMax / sizeof(WCHAR), L"\\BaseNamedObjects\\msctf.server%s%u",
DesktopName,
SessionId);
return TRUE;
}
return FALSE;
}
HANDLE OpenAlpcPort(PWCHAR AlpcPortName, PPORT_MESSAGE ConnectMessage, SIZE_T MessageSize)
{
OBJECT_ATTRIBUTES ObjectAttributes;
UNICODE_STRING PortName;
ALPC_PORT_ATTRIBUTES PortAttributes;
HANDLE AlpcHandle = INVALID_HANDLE_VALUE;
ULONG BufferLength = 64;
NTSTATUS Result;
RtlInitUnicodeString(&PortName, AlpcPortName);
InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
InitializeMessageHeader(ConnectMessage, MessageSize, LPC_CONNECTION_REQUEST);
ZeroMemory(&PortAttributes, sizeof PortAttributes);
PortAttributes.SecurityQos.Length = sizeof(PortAttributes.SecurityQos);
PortAttributes.SecurityQos.ContextTrackingMode = TRUE;
PortAttributes.MaxMessageLength = 512;
PortAttributes.DupObjectTypes = 0x88000000;
Result = NtAlpcConnectPort(&AlpcHandle,
&PortName,
&ObjectAttributes,
&PortAttributes,
0,
NULL,
ConnectMessage,
&BufferLength,
NULL,
NULL,
NULL);
if (!NonInteractive) {
LogMessage(stdout, "NtAlpcConnectPort(\"%S\") => %#x", AlpcPortName, Result);
}
return AlpcHandle;
}
NTSTATUS SendReceiveMarshalData(HANDLE AlpcHandle,
ULONG TypeFlags,
PCTF_MARSHAL_PARAM Params,
ULONG ParamCount,
DWORD DestinationThread)
{
NTSTATUS Result;
SIZE_T BufferLength;
PCTF_MSGBASE SendReceiveBuffer;
// We need enough space to append the Params, it has to be a contiguous buffer.
BufferLength = sizeof(CTF_MSGBASE) + GetParamsSize(Params, ParamCount);
// Allocate and initialize.
SendReceiveBuffer = calloc(BufferLength, 1);
// Append marshal parameters.
memcpy(&SendReceiveBuffer[1], Params, GetParamsSize(Params, ParamCount));
// Configure Message.
SendReceiveBuffer->Message = TypeFlags;
SendReceiveBuffer->SrcThreadId = ClientThreadId;
SendReceiveBuffer->DstThreadId = DestinationThread;
SendReceiveBuffer->ulNumParams = ParamCount;
SendReceiveBuffer->ulDataLength = GetParamsSize(Params, ParamCount);
// Send the data.
Result = SendReceivePortMessage(AlpcHandle,
&SendReceiveBuffer->Header,
BufferLength,
NULL);
// Check if the send worked.
if (Result != 0) {
goto cleanup;
}
// Copy the RPC result.
Result = SendReceiveBuffer->Result;
// The monitor leaks a stack pointer here.
LeakedStackPointer = SendReceiveBuffer->pData;
// Restore data so that caller can see any returned data.
memcpy(Params, &SendReceiveBuffer[1], GetParamsSize(Params, ParamCount));
cleanup:
// All done with our copy.
free(SendReceiveBuffer);
return Result;
}
// I don't know what these are, so make them accessible via set.
UINT64 ProxyExtra1;
UINT64 ProxyExtra2;
UINT64 ProxyExtra3;
NTSTATUS SendReceiveProxyData(HANDLE AlpcHandle,
ULONG TypeFlags,
PCTF_MARSHAL_PARAM Params,
ULONG ParamCount,
PCTF_MARSHAL_COMSTUB Stub,
DWORD FunctionIndex,
DWORD DestinationThread)
{
NTSTATUS Result;
SIZE_T BufferLength;
PCTF_MSGBASE SendReceiveBuffer;
PCTF_MARSHAL_PARAM ParamPtr;
CTF_PROXY_SIGNATURE ProxySignature = {
.FunctionIndex = FunctionIndex,
.StubId = Stub->StubId,
.Timestamp = Stub->Timestamp,
.field_1C = ProxyExtra1,
.field_20 = ProxyExtra2,
.field_24 = ProxyExtra3,
};
// Copy the correct GUID over from the Stub.
memcpy(&ProxySignature.Interface, &Stub->Interface, sizeof(GUID));
// We need enough space to append the Params, it has to be a contiguous buffer.
BufferLength = sizeof(CTF_MSGBASE) + sizeof(CTF_PROXY_SIGNATURE) + GetParamsSize(Params, ParamCount);
// Allocate and initialize.
SendReceiveBuffer = calloc(BufferLength, 1);
// Append proxy parameters.
ParamPtr = mempcpy(&SendReceiveBuffer[1], &ProxySignature, sizeof(CTF_PROXY_SIGNATURE));
// Append marshalled parameters.
memcpy(ParamPtr, Params, GetParamsSize(Params, ParamCount));
// When parameters are Proxied, their base offset needs to adjusted because of the Proxy header.
for (int i = 0; i < ParamCount; i++) {
ParamPtr[i].Start += sizeof(CTF_PROXY_SIGNATURE);
}
// Configure Message.
SendReceiveBuffer->Message = TypeFlags;
SendReceiveBuffer->SrcThreadId = ClientThreadId;
SendReceiveBuffer->DstThreadId = DestinationThread;
// As far as I can tell, only this parameter is used.
SendReceiveBuffer->ulDataLength = sizeof(ProxySignature) + GetParamsSize(Params, ParamCount);
SendReceiveBuffer->ulNumParams = ParamCount;
// Send the data.
Result = SendReceivePortMessage(AlpcHandle,
&SendReceiveBuffer->Header,
BufferLength,
NULL);
if (Result != 0) {
goto cleanup;
}
Result = SendReceiveBuffer->Result;
// Restore the adjusted Base
for (int i = 0; i < ParamCount; i++) {
ParamPtr[i].Start -= sizeof(CTF_PROXY_SIGNATURE);
}
// Restore data so that caller can see any returned data.
memcpy(Params, ParamPtr, GetParamsSize(Params, ParamCount));
cleanup:
// All done with our copy.
free(SendReceiveBuffer);
return Result;
}
NTSTATUS SendReceivePortMessage(HANDLE AlpcHandle,
PPORT_MESSAGE PortMessage,
ULONG BufferLength,
PLARGE_INTEGER Timeout)
{
NTSTATUS Result;
ULONG MessageAttributeSize;
PALPC_MESSAGE_ATTRIBUTES ReceiveMessageAttributes = NULL;
Result = AlpcInitializeMessageAttribute(0x60000000, NULL, 0, &MessageAttributeSize);
if (Result != STATUS_BUFFER_TOO_SMALL) {
LogMessage(stderr, "unexpected result from AlpcInitializeMessageAttribute()");
goto cleanup;
}
ReceiveMessageAttributes = calloc(1, MessageAttributeSize);
if (AlpcInitializeMessageAttribute(0x60000000,
ReceiveMessageAttributes,
MessageAttributeSize,
&MessageAttributeSize) < 0) {
LogMessage(stderr, "AlpcInitializeMessageAttribute failed");
goto cleanup;
}
ReceiveMessageAttributes->ValidAttributes = 0;
InitializeMessageHeader(PortMessage, BufferLength, 0);
Result = NtAlpcSendWaitReceivePort(AlpcHandle,
ALPC_MSGFLG_SYNC_REQUEST,
PortMessage,
NULL,
PortMessage,
&BufferLength,
ReceiveMessageAttributes,
Timeout);
cleanup:
free(ReceiveMessageAttributes);
return Result;
}
int main(int argc, char **argv)
{
PCHAR CommandLine;
ULONG Result = 1;
HANDLE MessageThread;
InitializeAlpcRoutines();
MessageThread = CreateThread(NULL, 0, MessageHandlerThread, NULL, 0, NULL);
if (MessageThread) {
ClientThreadId = GetThreadId(MessageThread);
}
LogMessage(stdout, "An interactive ctf exploration tool by @taviso.");
LogMessage(stdout, "Type \"help\" for available commands.");
LogMessage(stdout, "Most commands require a connection, see \"help connect\".");
read_history(".ctfhistory");
do {
CommandLine = readline("ctf> ");
// Check for EOF.
if (CommandLine == NULL)
break;
// Save this to the history so that arrow keys work.
add_history(CommandLine);
Result = DispatchCommand(CommandLine);
rl_free(CommandLine);
} while (Result != 0);
// Record all our commands.
write_history(".ctfhistory");
// Cleanup.
if (PortHandle)
CloseHandle(PortHandle);
if (MessageWindow)
DestroyWindow(MessageWindow);
return 0;
}