-
Notifications
You must be signed in to change notification settings - Fork 0
/
chwp.cpp
531 lines (386 loc) · 13.4 KB
/
chwp.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
// chwp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
BOOL GetUserSid(HANDLE, PSID*);
int EnablePriv(LPCWSTR);
BOOL GrantDaclPermissions(HANDLE, PSID, ACCESS_MASK);
BOOL GetOwnerLogonName(HANDLE, wchar_t**, wchar_t**);
int _tmain(int argc, _TCHAR* argv[])
{
HWINSTA hws;
DWORD wssz = 0;
wchar_t *pwsname;
HANDLE hselftok = NULL;
PSID pselfsid = NULL;
HDESK hdesktop;
HWND hpmwnd;
DWORD dwpmproc = 0;
HANDLE hpmproc;
HANDLE hpmtok = NULL;
HANDLE hnewtok = NULL;
SECURITY_DESCRIPTOR *psd = NULL;
DWORD dwsdsz = 0;
wchar_t *logonname;
wchar_t *domain;
if (argc != 2) {
wprintf(L"USAGE: chwp.exe <bitmap file>\n");
return 0;
}
/* get the current logon SID */
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hselftok)) {
wprintf(L"Failed to obtain current process token (%d)\n", GetLastError());
return 1;
}
if (!GetUserSid(hselftok, &pselfsid)) {
wprintf(L"Failed to get the current logon SID (%d)\n", GetLastError());
CloseHandle(hselftok);
return 1;
}
CloseHandle(hselftok);
/* attach to the interactive desktop if we're not already there */
hws = GetProcessWindowStation();
GetUserObjectInformationW(hws, UOI_NAME, NULL, 0, &wssz);
pwsname = (wchar_t*)malloc(wssz + 1);
memset(pwsname, 0, wssz + 1);
GetUserObjectInformationW(hws, UOI_NAME, pwsname, wssz, NULL);
wprintf(L"Current window station is %s\n", pwsname);
if (wcscmp(pwsname, L"WinSta0") != 0) {
wprintf(L"Attaching current process to WinSta0\n", pwsname);
free(pwsname);
CloseWindowStation(hws);
hws = OpenWindowStation(L"WinSta0", FALSE, READ_CONTROL | WRITE_DAC);
if (hws == NULL) {
wprintf(L"Failed to re-open WinSta0 for WRITE_DAC (%d)\n", GetLastError());
free(pselfsid);
return 1;
}
if (!GrantDaclPermissions(hws, pselfsid, WINSTA_ALL_ACCESS)) {
wprintf(L"Failed to adjust DACL on WinSta0 (%d)\n", GetLastError());
free(pselfsid);
CloseWindowStation(hws);
return 1;
}
CloseWindowStation(hws);
hws = OpenWindowStation(L"WinSta0", FALSE, WINSTA_ALL_ACCESS);
if (hws == NULL) {
wprintf(L"Failed to re-open WinSta0 for all access (%d)\n", GetLastError());
free(pselfsid);
return 1;
}
if (!SetProcessWindowStation(hws)) {
wprintf(L"Failed to attach current process to WinSta0 (%d)\n", GetLastError());
free(pselfsid);
CloseWindowStation(hws);
return 1;
}
CloseWindowStation(hws);
hws = GetProcessWindowStation();
hdesktop = OpenDesktop(L"Default", 0, FALSE, GENERIC_READ);
if (hdesktop == NULL) {
wprintf(L"Failed to open the default desktop for reading (%d)\n", GetLastError());
free(pselfsid);
CloseWindowStation(hws);
return 1;
}
if (!SetThreadDesktop(hdesktop)) {
wprintf(L"Failed to attached current thread to the default desktop (%d)\n", GetLastError());
free(pselfsid);
CloseDesktop(hdesktop);
CloseWindowStation(hws);
return 1;
}
CloseDesktop(hdesktop);
CloseWindowStation(hws);
wprintf(L"Current process is now attached to WinSta0\\Default\n");
} else {
free(pwsname);
CloseWindowStation(hws);
}
/* find the console user's explorer.exe process */
hpmwnd = FindWindow(L"Progman", L"Program Manager");
if (hpmwnd == NULL) {
wprintf(L"Failed to locate the desktop window (%d)\n", GetLastError());
free(pselfsid);
return 1;
}
GetWindowThreadProcessId(hpmwnd, &dwpmproc);
CloseHandle(hpmwnd);
if (!dwpmproc) {
wprintf(L"Failed to get EXPLORER.EXE process ID (%d)\n", GetLastError());
free(pselfsid);
return 1;
}
wprintf(L"EXPLORER.EXE process ID is %d\n", dwpmproc);
/* steal the console user's access token for impersonation */
if (!EnablePriv(SE_DEBUG_NAME)) {
wprintf(L"Failed to enable %s (%d)\n", SE_DEBUG_NAME, GetLastError());
free(pselfsid);
return 1;
}
hpmproc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwpmproc);
if (hpmproc == NULL) {
wprintf(L"Failed to open EXPLORER.EXE process for all access (%d)\n", GetLastError());
free(pselfsid);
return 1;
}
if (!EnablePriv(SE_TAKE_OWNERSHIP_NAME)) {
wprintf(L"Failed to enable %s (%d)\n", SE_TAKE_OWNERSHIP_NAME, GetLastError());
free(pselfsid);
CloseHandle(hpmproc);
return 1;
}
if (!OpenProcessToken(hpmproc, WRITE_OWNER, &hpmtok)) {
wprintf(L"Failed to open EXPLORER.EXE access token for WRITE_OWNER (%d)\n", GetLastError());
free(pselfsid);
CloseHandle(hpmproc);
return 1;
}
psd = (SECURITY_DESCRIPTOR*)malloc(sizeof(SECURITY_DESCRIPTOR));
InitializeSecurityDescriptor(psd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorOwner(psd, pselfsid, 0);
if (!SetKernelObjectSecurity(hpmtok, OWNER_SECURITY_INFORMATION, psd)) {
wprintf(L"Failed to take ownership of EXPLORER.EXE access token (%d)\n", GetLastError());
free(psd);
free(pselfsid);
CloseHandle(hpmproc);
return 1;
}
free(psd);
CloseHandle(hpmtok);
if (!OpenProcessToken(hpmproc, READ_CONTROL | WRITE_DAC, &hpmtok)) {
wprintf(L"Failed to re-open EXPLORER.EXE access token for WRITE_DAC (%d)\n", GetLastError());
free(pselfsid);
CloseHandle(hpmproc);
return 1;
}
if (!GrantDaclPermissions(hpmtok, pselfsid, TOKEN_ALL_ACCESS)) {
wprintf(L"Failed to adjust EXPLORER.EXE access token DACL (%d)\n", GetLastError());
free(pselfsid);
CloseHandle(hpmproc);
return 1;
}
CloseHandle(hpmtok);
if (!OpenProcessToken(hpmproc, TOKEN_DUPLICATE, &hpmtok)) {
wprintf(L"Failed to re-open EXPLORER.EXE access token for TOKEN_DUPLICATE (%d)\n", GetLastError());
free(pselfsid);
CloseHandle(hpmproc);
return 1;
}
if (!DuplicateTokenEx(hpmtok, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenPrimary, &hnewtok)) {
wprintf(L"Failed to duplicate EXPLORER.EXE access token (%d)\n", GetLastError());
free(pselfsid);
CloseHandle(hpmtok);
CloseHandle(hpmproc);
return 1;
}
if (!GetOwnerLogonName(hpmproc, &logonname, &domain)) {
wprintf(L"Failed to get EXPLORER.EXE owner name (%d)", GetLastError());
free(pselfsid);
CloseHandle(hpmtok);
CloseHandle(hpmproc);
return 1;
}
CloseHandle(hpmtok);
CloseHandle(hpmproc);
if (!ImpersonateLoggedOnUser(hnewtok)) {
wprintf(L"Failed to impersonate the console user (%d)\n", GetLastError());
free(pselfsid);
CloseHandle(hnewtok);
return 1;
}
wprintf(L"Now executing as %s\\%s\n", domain, logonname);
free(logonname);
free(domain);
free(pselfsid);
CloseHandle(hnewtok);
/* set the console user's wallpaper */
if (!SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (void*)argv[1], SPIF_SENDCHANGE)) {
wprintf(L"Failed to set desktop wallpaper (%d)\n", GetLastError());
return 1;
}
wprintf(L"Desktop wallpaper set successfully\n");
RevertToSelf();
return 0;
}
BOOL GetUserSid(HANDLE htok, PSID *psid)
{
BOOL result = FALSE;
DWORD dwlength = 0;
TOKEN_INFORMATION_CLASS tic = TokenUser;
PTOKEN_USER ptu = NULL;
if (!GetTokenInformation(htok, tic, (LPVOID)ptu, 0, &dwlength)) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
ptu = (PTOKEN_USER)malloc(dwlength);
memset(ptu, 0, dwlength);
} else {
wprintf(L"Failed to get the token user length (%d)\n", GetLastError());
return FALSE;
}
}
if (!GetTokenInformation(htok, tic, (LPVOID)ptu, dwlength, &dwlength)) {
wprintf(L"Failed to get the token user (%d)\n", GetLastError());
free(ptu);
return FALSE;
}
dwlength = GetLengthSid(ptu->User.Sid);
*psid = (PSID)malloc(dwlength);
memset(*psid, 0, dwlength);
if (!CopySid(dwlength, *psid, ptu->User.Sid)) {
wprintf(L"Failed to copy the user SID (%d)\n", GetLastError());
free(psid);
free(ptu);
return FALSE;
}
return TRUE;
}
BOOL EnablePriv(LPCWSTR privname)
{
HANDLE htok;
TOKEN_PRIVILEGES newtp;
TOKEN_PRIVILEGES oldtp;
DWORD dwtpsz = sizeof(TOKEN_PRIVILEGES);
LUID luid;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &htok)) {
wprintf(L"Failed to acquire process token (%d)\n", GetLastError());
return FALSE;
}
if (!LookupPrivilegeValue(NULL, privname, &luid)) {
wprintf(L"Failed to lookup privilege (%d)\n", GetLastError());
CloseHandle(htok);
return FALSE;
}
memset(&newtp, 0, sizeof(newtp));
newtp.PrivilegeCount = 1;
newtp.Privileges[0].Luid = luid;
newtp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (!AdjustTokenPrivileges(htok, FALSE, &newtp, sizeof(TOKEN_PRIVILEGES), &oldtp, &dwtpsz)) {
wprintf(L"Failed to adjust token privileges (%d)\n", GetLastError());
CloseHandle(htok);
return FALSE;
}
wprintf(L"%s enabled\n", privname);
return TRUE;
}
BOOL GrantDaclPermissions(HANDLE huserobj, PSID psid, ACCESS_MASK mask)
{
ACCESS_ALLOWED_ACE *pace;
ACL_SIZE_INFORMATION aclSizeInfo;
BOOL bDaclExist;
BOOL bDaclPresent;
DWORD dwNewAclSize;
DWORD dwSidSize = 0;
DWORD dwSdSizeNeeded;
PACL pacl;
PACL pNewAcl;
PSECURITY_DESCRIPTOR psd = NULL;
PSECURITY_DESCRIPTOR psdNew = NULL;
PVOID pTempAce;
SECURITY_INFORMATION si = DACL_SECURITY_INFORMATION;
unsigned int i;
BOOL ret = FALSE;
if (!GetUserObjectSecurity(huserobj, &si, psd, dwSidSize, &dwSdSizeNeeded)) {
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
psd = (SECURITY_DESCRIPTOR*)malloc(dwSdSizeNeeded);
memset(psd, 0, dwSdSizeNeeded);
psdNew = (SECURITY_DESCRIPTOR*)malloc(dwSdSizeNeeded);
memset(psdNew, 0, dwSdSizeNeeded);
dwSidSize = dwSdSizeNeeded;
if (!GetUserObjectSecurity(huserobj, &si, psd, dwSidSize, &dwSdSizeNeeded)) {
wprintf(L"Failed to obtain object security info (%d)\n", GetLastError());
free(psd);
free(psdNew);
return ret;
}
} else
return ret;
}
InitializeSecurityDescriptor(psdNew, SECURITY_DESCRIPTOR_REVISION);
if (!GetSecurityDescriptorDacl(psd, &bDaclPresent, &pacl, &bDaclExist)) {
wprintf(L"Failed to obtain object DACL (%d)\n", GetLastError());
free(psd);
free(psdNew);
return ret;
}
memset(&aclSizeInfo, 0, sizeof(ACL_SIZE_INFORMATION));
aclSizeInfo.AclBytesInUse = sizeof(ACL);
if (pacl != NULL) {
if (!GetAclInformation(pacl, (LPVOID)&aclSizeInfo, sizeof(ACL_SIZE_INFORMATION), AclSizeInformation)) {
wprintf(L"Failed to read ACL info (%d)\n", GetLastError());
free(psd);
free(psdNew);
return ret;
}
}
dwNewAclSize = aclSizeInfo.AclBytesInUse
+ (2 * sizeof(ACCESS_ALLOWED_ACE))
+ (2 * GetLengthSid(psid))
- (2 * sizeof(DWORD));
pNewAcl = (ACL*)malloc(dwNewAclSize);
InitializeAcl(pNewAcl, dwNewAclSize, ACL_REVISION);
/* copy any existing ace's to the new dacl */
if (bDaclPresent && aclSizeInfo.AceCount) {
for (i = 0; i < aclSizeInfo.AceCount; i++) {
GetAce(pacl, i, &pTempAce);
AddAce(pNewAcl, ACL_REVISION, MAXDWORD, pTempAce, ((PACE_HEADER)pTempAce)->AceSize);
}
}
pace = (ACCESS_ALLOWED_ACE*)malloc(sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psid) - sizeof(DWORD));
pace->Header.AceType = ACCESS_ALLOWED_ACE_TYPE;
pace->Header.AceFlags = CONTAINER_INHERIT_ACE | INHERIT_ONLY_ACE | OBJECT_INHERIT_ACE;
pace->Header.AceSize = (WORD)(sizeof(ACCESS_ALLOWED_ACE) + GetLengthSid(psid) - sizeof(DWORD));
pace->Mask = GENERIC_ALL;
CopySid(GetLengthSid(psid), &pace->SidStart, psid);
AddAce(pNewAcl, ACL_REVISION, MAXDWORD, (LPVOID)pace, pace->Header.AceSize);
pace->Header.AceFlags = NO_PROPAGATE_INHERIT_ACE;
pace->Mask = mask;
AddAce(pNewAcl, ACL_REVISION, MAXDWORD, (LPVOID)pace, pace->Header.AceSize);
SetSecurityDescriptorDacl(psdNew, TRUE, pNewAcl, FALSE);
ret = SetUserObjectSecurity(huserobj, &si, psdNew);
if (!ret)
wprintf(L"Failed to set object security info (%d)\n", GetLastError());
free(psd);
free(psdNew);
free(pNewAcl);
free(pace);
return ret;
}
BOOL GetOwnerLogonName(HANDLE huserobj, wchar_t **logonname, wchar_t **domain)
{
SECURITY_DESCRIPTOR *psd;
DWORD dwsdsz;
PSID powner;
BOOL isdefaulted = NULL;
DWORD namesz = 0;
DWORD domsz = 0;
SID_NAME_USE snu;
if (!GetKernelObjectSecurity(huserobj, DACL_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION, NULL, 0, &dwsdsz) && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
wprintf(L"Failed to get buffer size for object security info (%d)\n", GetLastError());
return FALSE;
}
psd = (SECURITY_DESCRIPTOR*)malloc(dwsdsz);
if (!GetKernelObjectSecurity(huserobj, DACL_SECURITY_INFORMATION | OWNER_SECURITY_INFORMATION, psd, dwsdsz, &dwsdsz)) {
wprintf(L"Failed to read object security info (%d)\n", GetLastError());
return FALSE;
}
if (!GetSecurityDescriptorOwner(psd, &powner, &isdefaulted)) {
wprintf(L"Failed to read security descriptor owner (%d)\n", GetLastError());
return FALSE;
}
if (!IsValidSid(powner)) {
wprintf(L"Owner SID is not valid\n");
return FALSE;
}
if (!LookupAccountSid(NULL, powner, NULL, &namesz, NULL, &domsz, &snu) && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
wprintf(L"Failed to get buffer size for account lookup (%d)\n", GetLastError());
return FALSE;
}
*logonname = (wchar_t*)malloc(sizeof(wchar_t) * namesz);
*domain = (wchar_t*)malloc(sizeof(wchar_t) * domsz);
memset(*logonname, 0, namesz);
memset(*domain, 0, domsz);
if (!LookupAccountSid(NULL, powner, *logonname, &namesz, *domain, &domsz, &snu)) {
wprintf(L"Failed to lookup account by SID (%d)\n", GetLastError());
return FALSE;
}
return TRUE;
}