forked from ladislav-zezula/FileTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDlgPrivileges.cpp
419 lines (356 loc) · 13.8 KB
/
DlgPrivileges.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
/*****************************************************************************/
/* DlgPrivileges.cpp Copyright (c) Ladislav Zezula 2009 */
/*---------------------------------------------------------------------------*/
/* Description : */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 19.05.09 1.00 Lad The first version of DlgPrivileges.cpp */
/*****************************************************************************/
#include "FileTest.h"
#include "resource.h"
//-----------------------------------------------------------------------------
// Local functions
static HANDLE OpenCurrentToken(DWORD dwDesiredAccess)
{
HANDLE hToken = NULL;
// Open process or thread token
if(!OpenThreadToken(GetCurrentThread(), dwDesiredAccess, TRUE, &hToken))
{
if(GetLastError() == ERROR_NO_TOKEN)
OpenProcessToken(GetCurrentProcess(), dwDesiredAccess, &hToken);
}
return hToken;
}
static void SetIntegrityLevelAsTest(HWND hDlg)
{
LPCTSTR szIntLevel = _T("");
DWORD dwIntLevel;
DlgText2Hex32(hDlg, IDC_INTLEVEL_VALUE, &dwIntLevel);
switch(dwIntLevel)
{
case SECURITY_MANDATORY_UNTRUSTED_RID:
szIntLevel = _T("(Untrusted)");
break;
case SECURITY_MANDATORY_LOW_RID:
szIntLevel = _T("(Low)");
break;
case SECURITY_MANDATORY_MEDIUM_RID:
szIntLevel = _T("(Medium)");
break;
case SECURITY_MANDATORY_HIGH_RID:
szIntLevel = _T("(High)");
break;
case SECURITY_MANDATORY_SYSTEM_RID:
szIntLevel = _T("(System)");
break;
case SECURITY_MANDATORY_PROTECTED_PROCESS_RID:
szIntLevel = _T("(Protected Process)");
break;
}
SetDlgItemText(hDlg, IDC_INTLEVEL_TEXT, szIntLevel);
}
static void SetPrivilegeCheckBox(
HWND hDlg,
UINT nIDCheckBox,
LPCTSTR szCheckedPrivName,
LPCTSTR szPrivName,
DWORD dwPrivilegeState)
{
HWND hCheck;
// Is it the searched privilege name ?
if(!_tcsicmp(szPrivName, szCheckedPrivName))
{
// Is the checkbox there ?
hCheck = GetDlgItem(hDlg, nIDCheckBox);
if(hCheck != NULL)
{
// Enable the check box
EnableWindow(hCheck, TRUE);
// If the privilege is granted, check the checkbox
if(dwPrivilegeState & SE_PRIVILEGE_ENABLED)
Button_SetCheck(hCheck, BST_CHECKED);
}
}
}
static int UpdateProcessPrivilege(HWND hDlg, HANDLE hToken, UINT nIDCtrl, LPCTSTR szPrivName)
{
TOKEN_PRIVILEGES tkp;
HWND hCheck;
BOOL bEnabled = (IsDlgButtonChecked(hDlg, nIDCtrl) == BST_CHECKED);
// Token handle is expected to be valid
assert(hToken != NULL);
// Get the handle to the checkbox window
hCheck = GetDlgItem(hDlg, nIDCtrl);
if(IsWindowEnabled(hCheck))
{
// Determine if the privilege should be enabled or not
bEnabled = (Button_GetCheck(hCheck) == BST_CHECKED);
// Get the LUID for the privilege.
if(LookupPrivilegeValue(NULL, szPrivName, &tkp.Privileges[0].Luid))
{
tkp.PrivilegeCount = 1; // one privilege to set
tkp.Privileges[0].Attributes = bEnabled ? SE_PRIVILEGE_ENABLED : 0;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, 0);
}
}
return TRUE;
}
static void UpdateDialogFromToken(HWND hDlg)
{
PTOKEN_MANDATORY_LABEL pMandatoryLabel = NULL;
PTOKEN_PRIVILEGES pTokenPrivs = NULL;
LPTSTR szErrorText;
HANDLE hToken;
DWORD cbMandatoryLabel = 0;
DWORD cbTokenPrivs = 0;
int nError = ERROR_SUCCESS;
// Open current thread/process token
hToken = OpenCurrentToken(TOKEN_QUERY);
if(hToken == NULL)
{
szErrorText = GetErrorText(GetLastError());
MessageBoxRc(hDlg, IDS_ERROR, IDS_E_OPEN_TOKEN, szErrorText);
delete [] szErrorText;
return;
}
// Query the token privileges
if(!GetTokenInformation(hToken, TokenPrivileges, pTokenPrivs, cbTokenPrivs, &cbTokenPrivs))
nError = GetLastError();
if(nError == ERROR_INSUFFICIENT_BUFFER && cbTokenPrivs != 0)
{
pTokenPrivs = (PTOKEN_PRIVILEGES)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, cbTokenPrivs);
GetTokenInformation(hToken, TokenPrivileges, pTokenPrivs, cbTokenPrivs, &cbTokenPrivs);
}
// Query the token mandatory label. Supported on Vista or newer.
if(!GetTokenInformation(hToken, TokenIntegrityLevel, pMandatoryLabel, cbMandatoryLabel, &cbMandatoryLabel))
nError = GetLastError();
if(nError == ERROR_INSUFFICIENT_BUFFER && cbMandatoryLabel != 0)
{
pMandatoryLabel = (PTOKEN_MANDATORY_LABEL)HeapAlloc(GetProcessHeap(), 0, cbMandatoryLabel);
GetTokenInformation(hToken, TokenIntegrityLevel, pMandatoryLabel, cbMandatoryLabel, &cbMandatoryLabel);
}
// If query token succeeded, parse them
if(pTokenPrivs != NULL)
{
for(DWORD i = 0; i < pTokenPrivs->PrivilegeCount; i++)
{
PLUID_AND_ATTRIBUTES pLuid = pTokenPrivs->Privileges + i;
TCHAR szPrivName[128];
DWORD cchName = _maxchars(szPrivName);
LookupPrivilegeName(NULL, &pLuid->Luid, szPrivName, &cchName);
SetPrivilegeCheckBox(hDlg, IDC_TAKE_OWNERSHIP_NAME, SE_TAKE_OWNERSHIP_NAME, szPrivName, pLuid->Attributes);
SetPrivilegeCheckBox(hDlg, IDC_CHANGE_NOTIFY_NAME, SE_CHANGE_NOTIFY_NAME, szPrivName, pLuid->Attributes);
SetPrivilegeCheckBox(hDlg, IDC_MANAGE_VOLUME_NAME, SE_MANAGE_VOLUME_NAME, szPrivName, pLuid->Attributes);
SetPrivilegeCheckBox(hDlg, IDC_SECURITY_NAME, SE_SECURITY_NAME, szPrivName, pLuid->Attributes);
SetPrivilegeCheckBox(hDlg, IDC_RESTORE_NAME, SE_RESTORE_NAME, szPrivName, pLuid->Attributes);
SetPrivilegeCheckBox(hDlg, IDC_BACKUP_NAME, SE_BACKUP_NAME, szPrivName, pLuid->Attributes);
SetPrivilegeCheckBox(hDlg, IDC_TCB_NAME, SE_TCB_NAME, szPrivName, pLuid->Attributes);
}
HeapFree(g_hHeap, 0, pTokenPrivs);
}
// If any token privileges found, pass them to dialog.
// Otherwise, disable all parts for integrity levels
if(pMandatoryLabel != NULL)
{
SID_IDENTIFIER_AUTHORITY SiaMandatoryLabel = SECURITY_MANDATORY_LABEL_AUTHORITY;
PSID pSid = pMandatoryLabel->Label.Sid;
DWORD dwIntegrityLevel = SECURITY_MANDATORY_MEDIUM_RID;
TCHAR szText[0x20];
UCHAR dwSubAuthCount = *GetSidSubAuthorityCount(pSid);
// Is that a mandatory label SID ?
if(!memcmp(GetSidIdentifierAuthority(pSid), &SiaMandatoryLabel, sizeof(SID_IDENTIFIER_AUTHORITY)))
dwIntegrityLevel = *GetSidSubAuthority(pSid, dwSubAuthCount - 1);
StringCchPrintf(szText, _countof(szText), _T("%08lX"), dwIntegrityLevel);
SetDlgItemText(hDlg, IDC_INTLEVEL_VALUE, szText);
HeapFree(g_hHeap, 0, pMandatoryLabel);
}
else
{
EnableDlgItems(hDlg, FALSE, IDC_INTLEVEL_FRAME,
IDC_INTLEVEL_TITLE,
IDC_INTLEVEL_VALUE,
IDC_INTLEVEL_UPDOWN,
IDC_INTLEVEL_EXPLAIN,
IDC_INTLEVEL_EXPLAIN2,
0);
}
// Close the token handle
CloseHandle(hToken);
}
static int UpdateTokenFromDialog(HWND hDlg)
{
SID_IDENTIFIER_AUTHORITY Sia = SECURITY_MANDATORY_LABEL_AUTHORITY;
TOKEN_MANDATORY_LABEL tml;
LPTSTR szErrorText;
HANDLE hToken = NULL;
DWORD dwDesiredAccess = TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY;
DWORD dwIntLevel;
HWND hEdit;
PSID pMandatorySid = NULL;
BOOL bLevelChanged;
BOOL bResult = TRUE;
// Determine if we changed integrity level
bLevelChanged = (BOOL)GetWindowLongPtr(hDlg, DWLP_USER);
if(bLevelChanged)
dwDesiredAccess |= TOKEN_ADJUST_DEFAULT;
// Open current thread/process token
hToken = OpenCurrentToken(dwDesiredAccess);
if(hToken == NULL)
{
szErrorText = GetErrorText(GetLastError());
MessageBoxRc(hDlg, IDS_ERROR, IDS_E_OPEN_TOKEN, szErrorText);
delete [] szErrorText;
return FALSE;
}
// Update the privileges
UpdateProcessPrivilege(hDlg, hToken, IDC_TAKE_OWNERSHIP_NAME, SE_TAKE_OWNERSHIP_NAME);
UpdateProcessPrivilege(hDlg, hToken, IDC_CHANGE_NOTIFY_NAME, SE_CHANGE_NOTIFY_NAME);
UpdateProcessPrivilege(hDlg, hToken, IDC_MANAGE_VOLUME_NAME, SE_MANAGE_VOLUME_NAME);
UpdateProcessPrivilege(hDlg, hToken, IDC_SECURITY_NAME, SE_SECURITY_NAME);
UpdateProcessPrivilege(hDlg, hToken, IDC_RESTORE_NAME, SE_RESTORE_NAME);
UpdateProcessPrivilege(hDlg, hToken, IDC_BACKUP_NAME, SE_BACKUP_NAME);
UpdateProcessPrivilege(hDlg, hToken, IDC_TCB_NAME, SE_TCB_NAME);
// Update process integrity level
hEdit = GetDlgItem(hDlg, IDC_INTLEVEL_VALUE);
if(bLevelChanged && hEdit != NULL && IsWindowEnabled(hEdit))
{
// Create the SID containing integrity level
DlgText2Hex32(hDlg, IDC_INTLEVEL_VALUE, &dwIntLevel);
if(AllocateAndInitializeSid(&Sia, 1, dwIntLevel, 0, 0, 0, 0, 0, 0, 0, &pMandatorySid))
{
tml.Label.Attributes = SE_GROUP_INTEGRITY;
tml.Label.Sid = pMandatorySid;
// Apply the integrity level to the token
bResult = SetTokenInformation(hToken,
TokenIntegrityLevel,
&tml,
sizeof(TOKEN_MANDATORY_LABEL));
if(bResult == FALSE)
{
szErrorText = GetErrorText(GetLastError());
MessageBoxRc(hDlg, IDS_ERROR, IDS_FAILED_TO_SET_ILEVEL, szErrorText);
delete [] szErrorText;
}
FreeSid(pMandatorySid);
}
}
// Close the token
CloseHandle(hToken);
return bResult;
}
//-----------------------------------------------------------------------------
// Dialog handlers
static int OnInitDialog(HWND hDlg)
{
HWND hUpDown;
HWND hEdit;
// Configure the dialog
SetDialogIcon(hDlg, IDI_FILE_TEST);
UpdateDialogFromToken(hDlg);
// Set parameters of the Integrity value edit box
hEdit = GetDlgItem(hDlg, IDC_INTLEVEL_VALUE);
if(hEdit != NULL)
Edit_LimitText(hEdit, 12);
// Set the parameters of the UpDown box
hUpDown = GetDlgItem(hDlg, IDC_INTLEVEL_UPDOWN);
if(hUpDown != NULL)
{
SendMessage(hUpDown, UDM_SETRANGE32, 0, 0x7FFFFFFF);
SendMessage(hUpDown, UDM_SETPOS32, 0, 0x40000000);
}
SetWindowLongPtr(hDlg, DWLP_USER, 0);
CenterWindowToParent(hDlg);
return TRUE;
}
static int SelectAllCheckBoxes(HWND hDlg, int nChecked)
{
HWND hCheck;
UINT nIDs[] =
{
IDC_TAKE_OWNERSHIP_NAME,
IDC_CHANGE_NOTIFY_NAME,
IDC_MANAGE_VOLUME_NAME,
IDC_SECURITY_NAME,
IDC_RESTORE_NAME,
IDC_BACKUP_NAME,
IDC_TCB_NAME,
0
};
// Go through all checkboxes
for(int i = 0; nIDs[i] != 0; i++)
{
hCheck = GetDlgItem(hDlg, nIDs[i]);
if(IsWindowEnabled(hCheck))
Button_SetCheck(hCheck, nChecked);
}
return TRUE;
}
static int OnDeltaPos(HWND hDlg, NMUPDOWN * pNMUpDown)
{
DWORD dwIntLevel = 0;
DlgText2Hex32(hDlg, IDC_INTLEVEL_VALUE, &dwIntLevel);
if(pNMUpDown->iDelta > 0)
{
if(dwIntLevel <= 0x0000E000)
dwIntLevel += 0x00001000;
else
dwIntLevel = 0x0000F000;
}
else
{
if(dwIntLevel >= 0x1000)
dwIntLevel -= 0x00001000;
else
dwIntLevel = 0;
}
Hex2DlgText32(hDlg, IDC_INTLEVEL_VALUE, dwIntLevel);
return TRUE;
}
static INT_PTR CALLBACK DialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// Dialog initialization
if(uMsg == WM_INITDIALOG)
return OnInitDialog(hDlg);
if(uMsg == WM_COMMAND)
{
if(HIWORD(wParam) == BN_CLICKED)
{
switch(LOWORD(wParam))
{
case IDC_SELECT_ALL:
SelectAllCheckBoxes(hDlg, BST_CHECKED);
break;
case IDC_CLEAR_ALL:
SelectAllCheckBoxes(hDlg, BST_UNCHECKED);
break;
case IDOK:
if(UpdateTokenFromDialog(hDlg) == FALSE)
break;
// No break here !!
case IDCANCEL:
EndDialog(hDlg, LOWORD(wParam));
break;
}
}
if(HIWORD(wParam) == EN_CHANGE)
{
// Remember that we modified the integrity level
SetIntegrityLevelAsTest(hDlg);
SetWindowLongPtr(hDlg, DWLP_USER, TRUE);
}
}
if(uMsg == WM_NOTIFY)
{
NMHDR * pNMHDR = (NMHDR *)lParam;
if(pNMHDR->code == UDN_DELTAPOS)
{
OnDeltaPos(hDlg, (NMUPDOWN *)pNMHDR);
}
}
return FALSE;
}
INT_PTR PrivilegesDialog(HWND hParent)
{
return DialogBox(g_hInst, MAKEINTRESOURCE(IDD_PRIVILEGES), hParent, DialogProc);
}