-
Notifications
You must be signed in to change notification settings - Fork 7
/
CWinStation.cpp
433 lines (389 loc) · 16.2 KB
/
CWinStation.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
//////////////////////////////////////////////////////////////////////////////
//
// CWinStation.cpp
// Win32::Daemon Perl extension windows station class source file
//
// Copyright (c) 1998-2008 Dave Roth
// Courtesy of Roth Consulting
// http://www.roth.net/
//
// This file may be copied or modified only under the terms of either
// the Artistic License or the GNU General Public License, which may
// be found in the Perl 5.0 source kit.
//
// 2008.03.24 :Date
// 20080324 :Version
//////////////////////////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "constant.h"
#include "CWinStation.hpp"
////////////////////////////////////////////////////////////////////////////
CWinStation::CWinStation()
{
m_hWinStation = NULL;
m_hDesktop = NULL;
m_pSid = NULL;
GetThisStation();
}
////////////////////////////////////////////////////////////////////////////
CWinStation::~CWinStation()
{
}
////////////////////////////////////////////////////////////////////////////
SID *CWinStation::SetSid( SID *pSid )
{
m_pSid = pSid;
return( m_pSid );
}
////////////////////////////////////////////////////////////////////////////
void CWinStation::GetThisStation()
{
// Call GetDesktopWindow() so that a window station and
// is associated with the service. THis only is a problem with pre-NT 4.0
HWND hDesktopWnd = GetDesktopWindow();
// _ASSERT( hDesktopWnd );
m_hWinStation = GetProcessWindowStation();
m_hDesktop = GetThreadDesktop( GetCurrentThreadId() );
}
////////////////////////////////////////////////////////////////////////////
BOOL CWinStation::Set( LPCTSTR pszWindowStation, LPCTSTR pszDesktop )
{
BOOL fResult = FALSE;
HWINSTA hWinStation = NULL;
HWINSTA hWinStationBackup = GetProcessWindowStation();
DWORD dwPerms = MAXIMUM_ALLOWED
| WINSTA_ACCESSCLIPBOARD
| WINSTA_EXITWINDOWS
| READ_CONTROL
| WRITE_DAC;
// | WINSTA_READATTRIBUTES
// | WINSTA_READSCREEN;
// | WINSTA_WRITEATTRIBUTES;
#ifdef _DEBUG
TCHAR ALERT_BUFFER[ 1024 ];
#endif
ALERT( "CWinStation::Set: Opening the Windows Station...\n" );
hWinStation = OpenWindowStation( (LPTSTR) pszWindowStation, TRUE, dwPerms );
if( NULL != hWinStation )
{
ALERT( "CWinStation::Set: Adding the SID to the WinStation...\n" );
if( AddSecurityPrivileges( (HANDLE) hWinStation, m_pSid, eWindowStation ) )
{
ALERT( "CWinStation::Set: Added the SID to the WinStation\n" );
}
if( SetProcessWindowStation( hWinStation ) )
{
HDESK hDesktopBackup = GetThreadDesktop( GetCurrentThreadId() );
// Get the new desktop: specify inheritance by new processes and max permissions
DWORD dwPerms = MAXIMUM_ALLOWED
| DESKTOP_SWITCHDESKTOP
| DESKTOP_READOBJECTS
| DESKTOP_WRITEOBJECTS
| DESKTOP_CREATEWINDOW
| READ_CONTROL
| WRITE_DAC;
HDESK hDesktop = OpenDesktop( (LPTSTR) pszDesktop, 0, TRUE, dwPerms );
if( NULL != hDesktop )
{
fResult = TRUE;
TCHAR szBuffer[ 2048 ];
TCHAR szBuffer2[ 4096];
TextFromSid( szBuffer, (SID*) m_pSid );
wsprintf( szBuffer2, TEXT( "CWinStation::Set: Got the process' SID: %s\n" ), szBuffer );
ALERT( szBuffer2 );
if( AddSecurityPrivileges( (HANDLE) hDesktop, m_pSid, eDesktop ) )
{
ALERT( "CWinStation::Set: Added the SID to the Desktop\n" );
fResult = SetThreadDesktop( hDesktop );
}
if( TRUE == fResult )
{
ALERT( "CWinStation::Set: Successfully set the desktop window & desktop" );
}
else
{
SetProcessWindowStation( hWinStationBackup );
SetThreadDesktop( hDesktopBackup );
}
}
#ifdef _DEBUG
else
{
wsprintf( ALERT_BUFFER, TEXT( "CWinStation::Set: ERROR: 0x%08x\n" ), GetLastError() );
ALERT( ALERT_BUFFER );
}
#endif
CloseDesktop( hDesktop );
}
#ifdef _DEBUG
else
{
wsprintf( ALERT_BUFFER, TEXT( "CWinStation::Set: ERROR: 0x%08x\n" ), GetLastError() );
ALERT( ALERT_BUFFER );
}
#endif
CloseWindowStation( hWinStation );
}
#ifdef _DEBUG
else
{
wsprintf( ALERT_BUFFER, TEXT( "CWinStation::Set: ERROR: 0x%08x\n" ), GetLastError() );
ALERT( ALERT_BUFFER );
}
#endif
return( fResult );
}
////////////////////////////////////////////////////////////////////////////
BOOL CWinStation::Restore()
{
BOOL fResult = FALSE;
if( FALSE != SetProcessWindowStation( m_hWinStation ) )
{
if( FALSE != SetThreadDesktop( m_hDesktop ) )
{
fResult = TRUE;
}
}
return( fResult );
}
////////////////////////////////////////////////////////////////////////////
BOOL CWinStation::AddSecurityPrivileges( HANDLE hHandle, SID *pSid, PermissionType PermType )
{
SECURITY_INFORMATION securityInfo;
PSECURITY_DESCRIPTOR pSd = NULL;
PSECURITY_DESCRIPTOR pNewSd = NULL;
DWORD dwLength = 0;
DWORD dwError;
BOOL bResult = FALSE;
BOOL bTempResult = FALSE;
#ifdef _DEBUG
TCHAR szDebugText[ 1024 ];
TextFromSid( szDebugText, (SID*) pSid );
#endif
securityInfo = DACL_SECURITY_INFORMATION;
// Fake out a call to fetch the security descriptor from the
// desktop object. Note that the size of the security descriptor
// object is 0 bytes long. this will cause the function to
// return false and populate dwLength with the # of bytes
// required to hold the security descriptor.
bTempResult = GetUserObjectSecurity(
hHandle,
&securityInfo,
pSd,
0,
&dwLength );
dwError = GetLastError();
if( ( FALSE == bTempResult ) && ( ERROR_INSUFFICIENT_BUFFER == dwError ) )
{
// alloc memory for the security desc.
pSd = (PSECURITY_DESCRIPTOR)new BYTE [ dwLength ];
pNewSd = (PSECURITY_DESCRIPTOR)new BYTE [ dwLength ];
if( ( NULL != pSd ) && ( NULL != pNewSd ) )
{
DWORD dwSdLength;
InitializeSecurityDescriptor(
pSd,
SECURITY_DESCRIPTOR_REVISION
);
InitializeSecurityDescriptor(
pNewSd,
SECURITY_DESCRIPTOR_REVISION
);
// Fetch the security descriptor from the
// desktop object
bTempResult = GetUserObjectSecurity(
hHandle,
&securityInfo,
pSd,
dwLength,
&dwSdLength );
if( ( FALSE != bTempResult ) && ( IsValidSecurityDescriptor( pSd ) ) )
{
BOOL bDaclPresent = FALSE;
BOOL bDaclDefaulted = FALSE;
PACL pDacl = NULL;
PACL pNewDacl = NULL;
// Fetch the DACL from the security descriptor
bTempResult = GetSecurityDescriptorDacl(
pSd,
&bDaclPresent,
&pDacl,
&bDaclDefaulted );
if( ( FALSE != bTempResult ) && ( FALSE != bDaclPresent ) )
{
ACL_SIZE_INFORMATION AclSizeInfo;
PACL pNewDacl = NULL;
ACCESS_ALLOWED_ACE *pAce = NULL;
// We should check to see of bDaclDefaulted is
// FALSE (meanign that the DACL was specified by
// some process; TRUE means that the Dacl was
// obtained by some default process--nothing specified
// a Dacl specifically).
ZeroMemory( &AclSizeInfo, sizeof( AclSizeInfo ) );
AclSizeInfo.AclBytesInUse = sizeof( ACL );
if( GetAclInformation(
pDacl,
(LPVOID) &AclSizeInfo,
sizeof( ACL_SIZE_INFORMATION ),
AclSizeInformation
) )
{
DWORD dwNewAclSize = AclSizeInfo.AclBytesInUse
+ ( 2 *
sizeof( ACCESS_ALLOWED_ACE ) )
+ ( 2 * GetLengthSid( pSid ) )
- ( 2 * sizeof( DWORD ) );
//
// allocate memory for the new acl
//
pNewDacl = (PACL) new BYTE [ dwNewAclSize ];
if( NULL != pNewDacl )
{
ZeroMemory( pNewDacl, dwNewAclSize );
InitializeAcl( pNewDacl, dwNewAclSize, ACL_REVISION );
}
}
// copy the ACEs to our new ACL
if( 0 != AclSizeInfo.AceCount )
{
ALERT( "[CWinStation::AddSecurityPrivileges] Populating new DACL.\n" );
for( DWORD dwIndex = 0; dwIndex < AclSizeInfo.AceCount; dwIndex++ )
{
// get an ACE
if( GetAce( pDacl, dwIndex, (PVOID*) &pAce ) )
{
#ifdef _DEBUG
TCHAR szSid[ 256 ];
SID *pSid = (SID*) pAce->SidStart;
_tcscpy( szSid, TEXT( "" ) );
TextFromSid( szSid, pSid );
wsprintf( szDebugText,
TEXT( "[CWinStation::AddSecurityPrivileges] Found ACE:\n\t\tType: 0x%04x Flags: 0x%04x Mask: 0x%08x\n\t\tSid: %s\n" ),
pAce->Header.AceType,
pAce->Header.AceFlags,
pAce->Mask,
szSid
);
ALERT( szDebugText );
#endif
if( AddAce(
pNewDacl,
ACL_REVISION,
MAXDWORD,
(LPVOID) pAce,
pAce->Header.AceSize ) )
{
ALERT( "[CWinStation::AddSecurityPrivileges] Added ACE." );
}
}
}
}
pAce = (ACCESS_ALLOWED_ACE *) new BYTE [ sizeof( ACCESS_ALLOWED_ACE )
+ GetLengthSid( pSid )
- sizeof( DWORD ) ];
if( NULL != pAce )
{
switch( PermType )
{
case eWindowStation:
// Add two ACE's to the WindowStation
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_ACCESS;
CopySid( GetLengthSid( pSid), &pAce->SidStart, pSid );
if( AddAce(
pNewDacl,
ACL_REVISION,
MAXDWORD,
(LPVOID) pAce,
pAce->Header.AceSize ) )
{
ALERT( "[CWinStation::AddSecurityPrivileges] Added new windowstation ACE 1.\n" );
}
#ifdef _DEBUG
else
{
ALERT( "[CWinStation::AddSecurityPrivileges] Failed to add new windowstation ACE 1.\n" );
}
#endif
//
// add the second ACE to the windowstation
//
pAce->Header.AceFlags = NO_PROPAGATE_INHERIT_ACE;
pAce->Mask = WINSTA_ALL;
if( AddAce(
pNewDacl,
ACL_REVISION,
MAXDWORD,
(LPVOID) pAce,
pAce->Header.AceSize ) )
{
ALERT( "[CWinStation::AddSecurityPrivileges] Added new windowstation ACE 2.\n" );
}
#ifdef _DEBUG
else
{
ALERT( "[CWinStation::AddSecurityPrivileges] Failed to add new windowstation ACE 2.\n" );
}
#endif
break;
case eDesktop:
// Add an access allowed ACE to the desktop
if( AddAccessAllowedAce(
pNewDacl,
ACL_REVISION,
DESKTOP_ALL,
pSid
) )
{
ALERT( "[CWinStation::AddSecurityPrivileges] Added new desktop ACE 2.\n" );
}
#ifdef _DEBUG
else
{
ALERT( "[CWinStation::AddSecurityPrivileges] Failed to add new desktop ACE 2.\n" );
}
#endif
break;
}
}
delete [] pAce;
// Update the Security Descriptor with the new
// DACL
if( TRUE == SetSecurityDescriptorDacl(
pNewSd,
TRUE,
pNewDacl,
FALSE ) )
{
// Go ahead and set the updated Security
// Descriptor on the desktop object
bResult = SetUserObjectSecurity(
hHandle,
&securityInfo,
pNewSd );
}
else
{
dwError = GetLastError();
}
if( NULL != pNewDacl )
{
delete [] pNewDacl;
}
}
}
}
}
delete [] (BYTE*)pSd;
delete [] (BYTE*)pNewSd;
return( bResult );
}