forked from ladislav-zezula/FileTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page09Security.cpp
2336 lines (1970 loc) · 76.1 KB
/
Page09Security.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************/
/* Page09Security.cpp Copyright (c) Ladislav Zezula 2005 */
/*---------------------------------------------------------------------------*/
/* Description : */
/*---------------------------------------------------------------------------*/
/* Date Ver Who Comment */
/* -------- ---- --- ------- */
/* 15.08.05 1.00 Lad The first version of Page09Security.cpp */
/*****************************************************************************/
#include "FileTest.h"
#include "resource.h"
//-----------------------------------------------------------------------------
// Local variables
#define TREE_ITEM_OWNER OWNER_SECURITY_INFORMATION
#define TREE_ITEM_GROUP GROUP_SECURITY_INFORMATION
#define TREE_ITEM_DACL DACL_SECURITY_INFORMATION
#define TREE_ITEM_SACL SACL_SECURITY_INFORMATION
#define TREE_ITEM_LABEL LABEL_SECURITY_INFORMATION
#define TREE_ITEM_NO_SID 0x10000000 // Under OWNER/GROUP, No SID present
#define TREE_ITEM_SID 0x10000001 // Under OWNER/GROUP: Present SID
#define TREE_ITEM_NULL_ACL 0x10000003 // NULL Acl ("click to create new")
#define TREE_ITEM_EMPTY_ACL 0x10000004 // Empty Acl ("click to create new")
#define TREE_ITEM_ACE 0x20000000 // An ACE. Lower 8 bits indicate the ACE type
#define TREE_ITEM_ACE_HEADER_TYPE 0x30000005 // ACE_HEADER::AceType
#define TREE_ITEM_ACE_HEADER_FLAGS 0x30000006 // ACE_HEADER::AceFlags
#define TREE_ITEM_ACE_HEADER_SIZE 0x30000007 // ACE_HEADER::AceFlags
#define TREE_ITEM_ACE_MASK 0x30000008 // ACE::Mask
#define TREE_ITEM_ADS_ACE_MASK 0x30000009 // ACE::Mask for ADS ACEs
#define TREE_ITEM_MANDATORY_MASK 0x3000000A // ACE::Mask for SYSTEM_MANDATORY_LABEL_ACE
#define TREE_ITEM_MANDATORY_LABEL 0x3000000B // Integrity level as 32-bit value (SECURITY_MANDATORY_LABEL_AUTHORITY)
#define TREE_ITEM_ACE_FLAGS 0x3000000C // ACE:Flags
#define TREE_ITEM_ACE_OBJ_GUID 0x3000000D // ACE:ObjectType
#define TREE_ITEM_ACE_OBJ_GUID2 0x3000000E // ACE:InheritedObjectType
#define TREE_ITEM_TYPE_MASK 0xF0000000
#define TREE_ITEM_VALUE_MASK 0x0FFFFFFF
#define MAXIMUM_ACL_SIZE 0xFFF8 // The biggest ACL that can possibly exist
#define FLAG_EDIT_VALUES 0x80000000 // Means that the item is value, not a bit mask
typedef bool (*ACE_FILTER_PROC)(PACE_HEADER pAceHeader);
// Masks for each tre item
static LPCTSTR szUnknownSid = _T("<UNKNOWN-SID>");
static LPCTSTR szNullAcl = _T("<NULL ACL. Double-click to create new...>");
static LPCTSTR szEmptyAcl = _T("<Empty ACL. Double-click to create new...>");
static LPCTSTR szAceHdrTypeFmt = _T("AceType: 0x%02lX");
static LPCTSTR szAceHdrFlagsFmt = _T("AceFlags: 0x%02lX ");
static LPCTSTR szAceHdrSizeFmt = _T("AceSize: 0x%04lX");
static LPCTSTR szAceMaskFmt = _T("Mask: 0x%08lX ");
static LPCTSTR szIntLevelFmt = _T("IntLevel: 0x%08lX");
static LPCTSTR szAceFlagsFmt = _T("Flags: 0x%08lX ");
static LPCTSTR szAceObjTypeFmt = _T("ObjectType: %s");
static LPCTSTR szAceObjTypeFmt2 = _T("InheritedObjectType: %s");
static SID_IDENTIFIER_AUTHORITY SiaNull = SECURITY_NULL_SID_AUTHORITY;
static SID_IDENTIFIER_AUTHORITY SiaWorld = SECURITY_WORLD_SID_AUTHORITY;
static SID_IDENTIFIER_AUTHORITY SiaLabel = SECURITY_MANDATORY_LABEL_AUTHORITY;
static TFlagInfo AceHdrTypes[] =
{
FLAG_INFO_ENTRY(ACCESS_ALLOWED_ACE_TYPE),
FLAG_INFO_ENTRY(ACCESS_DENIED_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_AUDIT_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_ALARM_ACE_TYPE),
FLAG_INFO_ENTRY(ACCESS_ALLOWED_COMPOUND_ACE_TYPE),
FLAG_INFO_ENTRY(ACCESS_ALLOWED_OBJECT_ACE_TYPE),
FLAG_INFO_ENTRY(ACCESS_DENIED_OBJECT_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_AUDIT_OBJECT_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_ALARM_OBJECT_ACE_TYPE),
FLAG_INFO_ENTRY(ACCESS_ALLOWED_CALLBACK_ACE_TYPE),
FLAG_INFO_ENTRY(ACCESS_DENIED_CALLBACK_ACE_TYPE),
FLAG_INFO_ENTRY(ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE),
FLAG_INFO_ENTRY(ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_AUDIT_CALLBACK_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_ALARM_CALLBACK_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE),
FLAG_INFO_ENTRY(SYSTEM_MANDATORY_LABEL_ACE_TYPE),
FLAG_INFO_END
};
static TFlagInfo AceHdrFlags[] =
{
FLAG_INFO_ENTRY(OBJECT_INHERIT_ACE),
FLAG_INFO_ENTRY(CONTAINER_INHERIT_ACE),
FLAG_INFO_ENTRY(NO_PROPAGATE_INHERIT_ACE),
FLAG_INFO_ENTRY(INHERIT_ONLY_ACE),
FLAG_INFO_ENTRY(INHERITED_ACE),
FLAG_INFO_ENTRY(SUCCESSFUL_ACCESS_ACE_FLAG),
FLAG_INFO_ENTRY(FAILED_ACCESS_ACE_FLAG),
FLAG_INFO_END
};
static TFlagInfo AceMasks[] =
{
FLAG_INFO_ENTRY(FILE_READ_DATA),
FLAG_INFO_ENTRY(FILE_WRITE_DATA),
FLAG_INFO_ENTRY(FILE_APPEND_DATA),
FLAG_INFO_ENTRY(FILE_READ_EA),
FLAG_INFO_ENTRY(FILE_WRITE_EA),
FLAG_INFO_ENTRY(FILE_EXECUTE),
FLAG_INFO_ENTRY(FILE_DELETE_CHILD),
FLAG_INFO_ENTRY(FILE_READ_ATTRIBUTES),
FLAG_INFO_ENTRY(FILE_WRITE_ATTRIBUTES),
FLAG_INFO_ENTRY(DELETE),
FLAG_INFO_ENTRY(READ_CONTROL),
FLAG_INFO_ENTRY(WRITE_DAC),
FLAG_INFO_ENTRY(WRITE_OWNER),
FLAG_INFO_ENTRY(SYNCHRONIZE),
FLAG_INFO_ENTRY(GENERIC_WRITE),
FLAG_INFO_ENTRY(GENERIC_READ),
FLAG_INFO_ENTRY(GENERIC_WRITE),
FLAG_INFO_ENTRY(GENERIC_EXECUTE),
FLAG_INFO_ENTRY(GENERIC_ALL),
FLAG_INFO_END
};
static TFlagInfo AdsAceMasks[] =
{
FLAG_INFO_ENTRY(ADS_RIGHT_DS_CREATE_CHILD),
FLAG_INFO_ENTRY(ADS_RIGHT_DS_DELETE_CHILD),
FLAG_INFO_ENTRY(ADS_RIGHT_ACTRL_DS_LIST),
FLAG_INFO_ENTRY(ADS_RIGHT_DS_SELF),
FLAG_INFO_ENTRY(ADS_RIGHT_DS_READ_PROP),
FLAG_INFO_ENTRY(ADS_RIGHT_DS_WRITE_PROP),
FLAG_INFO_ENTRY(ADS_RIGHT_DS_DELETE_TREE),
FLAG_INFO_ENTRY(ADS_RIGHT_DS_LIST_OBJECT),
FLAG_INFO_ENTRY(ADS_RIGHT_DS_CONTROL_ACCESS),
FLAG_INFO_ENTRY(ADS_RIGHT_DELETE),
FLAG_INFO_ENTRY(ADS_RIGHT_READ_CONTROL),
FLAG_INFO_ENTRY(ADS_RIGHT_WRITE_DAC),
FLAG_INFO_ENTRY(ADS_RIGHT_WRITE_OWNER),
FLAG_INFO_ENTRY(ADS_RIGHT_SYNCHRONIZE),
FLAG_INFO_ENTRY(ADS_RIGHT_ACCESS_SYSTEM_SECURITY),
FLAG_INFO_ENTRY(ADS_RIGHT_GENERIC_READ),
FLAG_INFO_ENTRY(ADS_RIGHT_GENERIC_WRITE),
FLAG_INFO_ENTRY(ADS_RIGHT_GENERIC_EXECUTE),
FLAG_INFO_ENTRY(ADS_RIGHT_GENERIC_ALL),
FLAG_INFO_END
};
static TFlagInfo MandatoryMasks[] =
{
FLAG_INFO_ENTRY(SYSTEM_MANDATORY_LABEL_NO_WRITE_UP),
FLAG_INFO_ENTRY(SYSTEM_MANDATORY_LABEL_NO_READ_UP),
FLAG_INFO_ENTRY(SYSTEM_MANDATORY_LABEL_NO_EXECUTE_UP),
FLAG_INFO_END
};
static TFlagInfo AceFlags[] =
{
FLAG_INFO_ENTRY(ACE_OBJECT_TYPE_PRESENT),
FLAG_INFO_ENTRY(ACE_INHERITED_OBJECT_TYPE_PRESENT),
FLAG_INFO_END
};
static TFlagInfo IntegrityLevels[] =
{
FLAG_INFO_ENTRY(SECURITY_MANDATORY_UNTRUSTED_RID),
FLAG_INFO_ENTRY(SECURITY_MANDATORY_LOW_RID),
FLAG_INFO_ENTRY(SECURITY_MANDATORY_MEDIUM_RID),
FLAG_INFO_ENTRY(SECURITY_MANDATORY_HIGH_RID),
FLAG_INFO_ENTRY(SECURITY_MANDATORY_SYSTEM_RID),
FLAG_INFO_ENTRY(SECURITY_MANDATORY_PROTECTED_PROCESS_RID),
FLAG_INFO_END
};
static DWORD AceSizes[] =
{
sizeof(ACCESS_ALLOWED_ACE), // ACCESS_ALLOWED_ACE_TYPE
sizeof(ACCESS_DENIED_ACE), // ACCESS_DENIED_ACE_TYPE
sizeof(SYSTEM_AUDIT_ACE), // SYSTEM_AUDIT_ACE_TYPE
sizeof(SYSTEM_ALARM_ACE), // SYSTEM_ALARM_ACE_TYPE
0, // ACCESS_ALLOWED_COMPOUND_ACE_TYPE (?)
sizeof(ACCESS_ALLOWED_OBJECT_ACE), // ACCESS_ALLOWED_OBJECT_ACE_TYPE
sizeof(ACCESS_DENIED_OBJECT_ACE), // ACCESS_DENIED_OBJECT_ACE_TYPE
sizeof(SYSTEM_AUDIT_OBJECT_ACE), // SYSTEM_AUDIT_OBJECT_ACE_TYPE
sizeof(SYSTEM_ALARM_OBJECT_ACE), // SYSTEM_ALARM_OBJECT_ACE_TYPE
sizeof(ACCESS_ALLOWED_CALLBACK_ACE), // ACCESS_ALLOWED_CALLBACK_ACE_TYPE
sizeof(ACCESS_DENIED_CALLBACK_ACE), // ACCESS_DENIED_CALLBACK_ACE_TYPE
sizeof(ACCESS_ALLOWED_CALLBACK_OBJECT_ACE), // ACCESS_ALLOWED_CALLBACK_OBJECT_ACE_TYPE
sizeof(ACCESS_DENIED_CALLBACK_OBJECT_ACE), // ACCESS_DENIED_CALLBACK_OBJECT_ACE_TYPE
sizeof(SYSTEM_AUDIT_CALLBACK_ACE), // SYSTEM_AUDIT_CALLBACK_ACE_TYPE
sizeof(SYSTEM_ALARM_CALLBACK_ACE), // SYSTEM_ALARM_CALLBACK_ACE_TYPE
sizeof(SYSTEM_AUDIT_CALLBACK_OBJECT_ACE), // SYSTEM_AUDIT_CALLBACK_OBJECT_ACE_TYPE
sizeof(SYSTEM_ALARM_CALLBACK_OBJECT_ACE), // SYSTEM_ALARM_CALLBACK_OBJECT_ACE_TYPE
sizeof(SYSTEM_MANDATORY_LABEL_ACE), // SYSTEM_MANDATORY_LABEL_ACE_TYPE
};
static DWORD AceSidOffsets[] =
{
FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart),
FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart),
FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart),
FIELD_OFFSET(SYSTEM_ALARM_ACE, SidStart),
0, // ACCESS_ALLOWED_COMPOUND_ACE(?)
FIELD_OFFSET(ACCESS_ALLOWED_OBJECT_ACE, SidStart),
FIELD_OFFSET(ACCESS_DENIED_OBJECT_ACE, SidStart),
FIELD_OFFSET(SYSTEM_AUDIT_OBJECT_ACE, SidStart),
FIELD_OFFSET(SYSTEM_ALARM_OBJECT_ACE, SidStart),
FIELD_OFFSET(ACCESS_ALLOWED_CALLBACK_ACE, SidStart),
FIELD_OFFSET(ACCESS_DENIED_CALLBACK_ACE, SidStart),
FIELD_OFFSET(ACCESS_ALLOWED_CALLBACK_OBJECT_ACE, SidStart),
FIELD_OFFSET(ACCESS_DENIED_CALLBACK_OBJECT_ACE, SidStart),
FIELD_OFFSET(SYSTEM_AUDIT_CALLBACK_ACE, SidStart),
FIELD_OFFSET(SYSTEM_ALARM_CALLBACK_ACE, SidStart),
FIELD_OFFSET(SYSTEM_AUDIT_CALLBACK_OBJECT_ACE, SidStart),
FIELD_OFFSET(SYSTEM_ALARM_CALLBACK_OBJECT_ACE, SidStart),
FIELD_OFFSET(SYSTEM_MANDATORY_LABEL_ACE, SidStart)
};
//-----------------------------------------------------------------------------
// Local functions - SID
static PSID Sid_Allocate(DWORD dwLength)
{
#ifdef _DEBUG
return (PSID)malloc(dwLength); // Allocate using malloc, so we can track the leaks
#else
return (PSID)HeapAlloc(g_hHeap, 0, dwLength);
#endif
}
// A public function, also used in ACE_HELPER
void Sid_Free(PSID pSid)
{
#ifdef _DEBUG
free(pSid);
#else
HeapFree(g_hHeap, 0, pSid);
#endif
}
static PSID Sid_AllocateAndInitialize(
IN PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority,
IN BYTE nSubAuthorityCount,
IN DWORD nSubAuthority0,
IN DWORD nSubAuthority1 = 0,
IN DWORD nSubAuthority2 = 0,
IN DWORD nSubAuthority3 = 0,
IN DWORD nSubAuthority4 = 0,
IN DWORD nSubAuthority5 = 0,
IN DWORD nSubAuthority6 = 0,
IN DWORD nSubAuthority7 = 0)
{
PSID pSidToFree = NULL;
PSID pSid = NULL;
DWORD dwLength;
// Call the native SID initializing function
if(AllocateAndInitializeSid(pIdentifierAuthority,
nSubAuthorityCount,
nSubAuthority0,
nSubAuthority1,
nSubAuthority2,
nSubAuthority3,
nSubAuthority4,
nSubAuthority5,
nSubAuthority6,
nSubAuthority7,
&pSidToFree))
{
// Retrieve the length of the SID
dwLength = GetLengthSid(pSidToFree);
// Now re-allocate the SID to have it in heap memory
pSid = Sid_Allocate(dwLength);
if(pSid != NULL)
CopySid(dwLength, pSid, pSidToFree);
// Free the original SID
FreeSid(pSidToFree);
}
// Return our new SID
return pSid;
}
// Creates a new SID of "Everyone" depending on the ACE type
// Caller must free the returned SID using Sid_Free
static PSID Sid_CreateNew(BYTE AceType)
{
// We only create two types of SID - "Everyone" and "Mandatory Medium"
if(AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE)
{
return Sid_AllocateAndInitialize(&SiaLabel, 1, SECURITY_MANDATORY_MEDIUM_RID);
}
else
{
return Sid_AllocateAndInitialize(&SiaWorld, 1, SECURITY_WORLD_RID);
}
}
// Changes an user name service ("fltmgr") into a properly prefixed name ("NT_SERVICE\\fltmgr")
static bool CheckForServiceAccount(LPTSTR szUserName)
{
TCHAR szSaveUserName[MAX_PATH];
TCHAR szKeyName[MAX_PATH];
HKEY hSubKey;
// Save the user name
StringCchCopy(szSaveUserName, _countof(szSaveUserName), szUserName);
// If the name resembles a service, give it the NT_SERVICE prefix. This only applies in Vista or newer
if(g_dwWinVer >= 0x0600 && _tcschr(szUserName, _T('\\')) == NULL)
{
StringCchPrintf(szKeyName, _countof(szKeyName), _T("SYSTEM\\CurrentControlSet\\Services\\%s"), szUserName);
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szKeyName, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS)
{
StringCchPrintf(szUserName, MAX_PATH, _T("NT SERVICE\\%s"), szSaveUserName);
RegCloseKey(hSubKey);
return true;
}
}
return false;
}
static void SidToString(PSID pvSid, LPTSTR szString, size_t cchString, bool bAddUserName)
{
PSID_IDENTIFIER_AUTHORITY pSia;
SID * pSid = (SID *)pvSid;
LPTSTR szStringEnd = szString + cchString;
UCHAR SubAuthCount;
// Add the "S-%u-" begin with revision
pSia = GetSidIdentifierAuthority(pSid);
StringCchPrintfEx(szString, (szStringEnd - szString), &szString, NULL, 0, _T("S-%u-%u"),
pSid->Revision,
pSia->Value[5]);
// Add the subauthorities
SubAuthCount = *GetSidSubAuthorityCount(pSid);
for(DWORD i = 0; i < SubAuthCount; i++)
{
DWORD dwSubAuth = *GetSidSubAuthority(pSid, i);
StringCchPrintfEx(szString, (szStringEnd - szString), &szString, NULL, 0, L"-%u", dwSubAuth);
}
// If we are required to add user name, do it.
if(bAddUserName)
{
SID_NAME_USE SidNameUse;
TCHAR szDomainName[128] = _T("");
TCHAR szUserName[128] = _T("");
DWORD cchDomainName = _countof(szDomainName);
DWORD cchUserName = _countof(szUserName);
if(LookupAccountSid(NULL, pSid, szUserName, &cchUserName, szDomainName, &cchDomainName, &SidNameUse))
{
if(szDomainName[0] != 0)
StringCchPrintf(szString, (szStringEnd - szString), _T(" (%s\\%s)"), szDomainName, szUserName);
else
StringCchPrintf(szString, (szStringEnd - szString), _T(" (%s)"), szUserName);
}
else
{
StringCchPrintf(szString, (szStringEnd - szString), _T(" (Unknown SID)"));
}
}
}
static bool StringToSid(LPTSTR szSid, PSID * ppSid)
{
SID_IDENTIFIER_AUTHORITY Sia = SiaNull;
SID_NAME_USE SidNameUse;
PSID pNewSid = NULL;
TCHAR szDomainName[128] = _T("");
DWORD dwSubAuthCount = 0;
DWORD dwSubAuth[8];
DWORD dwDomainName = _countof(szDomainName);
DWORD dwRevision = SID_REVISION;
DWORD dwLength = 0;
BOOL bResult;
// Verify the string sid value
if(szSid == NULL || szSid[0] == 0)
return false;
// Case 1: Sid is entered as text form ("S-1-....")
if(szSid[0] == _T('S') && szSid[1] == _T('-') && szSid[2] == _T('1') && szSid[3] == _T('-'))
{
// Skip revision
dwRevision = SID_REVISION;
szSid += 4;
// Get the identifier authority
Sia.Value[5] = (BYTE)StrToInt(szSid, &szSid, 10);
if(szSid[0] != _T('-'))
return false;
// Get the subauthorities
memset(dwSubAuth, 0, sizeof(dwSubAuth));
while(szSid[0] == _T('-') && dwSubAuthCount < 8)
{
dwSubAuth[dwSubAuthCount++] = StrToInt(szSid + 1, &szSid, 10);
}
// If an unknown character found, do nothing
if(szSid[0] == 0 || szSid[0] == _T(' '))
{
// Create the SID
*ppSid = Sid_AllocateAndInitialize(&Sia, (BYTE)dwSubAuthCount,
dwSubAuth[0],
dwSubAuth[1],
dwSubAuth[2],
dwSubAuth[3],
dwSubAuth[4],
dwSubAuth[5],
dwSubAuth[6],
dwSubAuth[7]);
return (*ppSid != NULL);
}
return false;
}
// Case 2: Domain\Username
__LookupAccountName:
bResult = LookupAccountName(NULL, szSid, pNewSid, &dwLength, szDomainName, &dwDomainName, &SidNameUse);
// If we haven't received the length, try some well-known user names
if(bResult == FALSE && dwLength == 0)
{
if(GetLastError() == ERROR_NONE_MAPPED && CheckForServiceAccount(szSid))
goto __LookupAccountName;
}
// If we received a non-zero length, allocate SID and try again
if(bResult == FALSE && dwLength != 0)
{
pNewSid = Sid_Allocate(dwLength);
bResult = LookupAccountName(NULL, szSid, pNewSid, &dwLength, szDomainName, &dwDomainName, &SidNameUse);
}
// If succeeded, give the SID
if(bResult)
*ppSid = pNewSid;
return (bool)(bResult != FALSE);
}
//
// The SID in the SYSTEM_MANDATORY_LABEL_ACE has the following format:
//
// - IdentifierAuthority is set to SECURITY_MANDATORY_LABEL_AUTHORITY
// - The last subauthority is set to one of the SECURITY_MANDATORY_XXXX values
//
static DWORD SidToIntegrityLevel(PSID pSid)
{
DWORD dwSubAuthCount;
if(pSid != NULL)
{
// Retrieve integrity level from SID
if(!memcmp(GetSidIdentifierAuthority(pSid), &SiaLabel, sizeof(SID_IDENTIFIER_AUTHORITY)))
{
dwSubAuthCount = *GetSidSubAuthorityCount(pSid);
if(dwSubAuthCount > 0)
{
return *GetSidSubAuthority(pSid, dwSubAuthCount - 1);
}
}
}
// Set default integrity level
return SECURITY_MANDATORY_MEDIUM_RID;
}
//-----------------------------------------------------------------------------
// Local functions ACEs, ACLs
static PACL Acl_CreateEmpty(DWORD dwAclSize = MAXIMUM_ACL_SIZE)
{
PACL pAcl;
// Allocate new ACL of the maximum size
pAcl = (PACL)HeapAlloc(g_hHeap, HEAP_ZERO_MEMORY, MAXIMUM_ACL_SIZE);
if(pAcl != NULL)
InitializeAcl(pAcl, dwAclSize, ACL_REVISION);
// Return the ACL
return pAcl;
}
// Creates a new ACL with one ACE, granting full access to Everyone
// Caller must free the returned buffer using HeapFree
static PACL Acl_CreateOneItem(BYTE AceType)
{
DWORD dwSidLength = 0;
DWORD dwAclLength = 0;
PSID pSid = NULL;
PACL pAcl = NULL;
BOOL bResult = FALSE;
// Create SID for "Everyone"
pSid = Sid_CreateNew(AceType);
if(pSid != NULL)
{
// Calculate the size of the new ACE and initialize it
dwSidLength = GetLengthSid(pSid);
dwAclLength = sizeof(ACL) + AceSizes[AceType] - sizeof(DWORD) + dwSidLength;
// Create new empty ACL
pAcl = Acl_CreateEmpty(dwAclLength);
if(pAcl != NULL)
{
// Perform the ACE-specific addition
switch(AceType)
{
case ACCESS_ALLOWED_ACE_TYPE:
bResult = AddAccessAllowedAce(pAcl, ACL_REVISION, GENERIC_ALL, pSid);
break;
case ACCESS_DENIED_ACE_TYPE:
bResult = AddAccessDeniedAce(pAcl, ACL_REVISION, GENERIC_ALL, pSid);
break;
case SYSTEM_AUDIT_ACE_TYPE:
bResult = AddAuditAccessAce(pAcl, ACL_REVISION, GENERIC_ALL, pSid, TRUE, TRUE);
break;
case SYSTEM_MANDATORY_LABEL_ACE_TYPE:
bResult = MyAddMandatoryAce(pAcl, ACL_REVISION, 0, SYSTEM_MANDATORY_LABEL_NO_WRITE_UP, pSid);
break;
default: // Not supported
bResult = FALSE;
break;
}
// If something went wrong, free the ACL
if(bResult == FALSE)
{
HeapFree(g_hHeap, 0, pAcl);
pAcl = NULL;
}
}
// Free the allocated SID
Sid_Free(pSid);
}
return pAcl;
}
static PACL Acl_FinishBuild(PACL pAcl)
{
PACE_HEADER pAceHeader;
DWORD dwAceCount = pAcl->AceCount;
DWORD dwAceSize = sizeof(ACL);
// Acount the size of all ACEs
for(DWORD i = 0; i < dwAceCount; i++)
{
// Retrieve the n-th ACE
if(!GetAce(pAcl, i, (LPVOID *)&pAceHeader))
break;
// Add the size to the ACL
dwAceSize = dwAceSize + pAceHeader->AceSize;
}
// Adjust the size of the ACL
pAcl->AclSize = (WORD)dwAceSize;
return pAcl;
}
static bool AceFilterIncludeAll(PACE_HEADER)
{
return true;
}
static bool AceFilterExcludeMandatoryAces(PACE_HEADER pAceHeader)
{
return (pAceHeader->AceType != SYSTEM_MANDATORY_LABEL_ACE_TYPE);
}
static bool AceFilterMandatoryAcesOnly(PACE_HEADER pAceHeader)
{
return (pAceHeader->AceType == SYSTEM_MANDATORY_LABEL_ACE_TYPE);
}
//-----------------------------------------------------------------------------
// Local functions - tree items
static BOOL IsTreeItemAce(LPARAM lParam)
{
return ((lParam & TREE_ITEM_TYPE_MASK) == TREE_ITEM_ACE) ? TRUE : FALSE;
}
static LPCTSTR GetAceTypeString(DWORD AceType)
{
BYTE MaxAceType = (BYTE)(_countof(AceHdrTypes) - 1);
// Insert the "root" item with ACE type
return (AceType < MaxAceType) ? AceHdrTypes[AceType].szFlagText : _T("UNKNOWN_ACE");
}
// The item text is expected to be in format "Name: 0x12345678"
static LPTSTR GetItemTextValue(LPTSTR szItemText)
{
LPTSTR szSpacePtr;
// Retrieve the first occurence of ":"
szItemText = _tcschr(szItemText, _T(':'));
if(szItemText != NULL)
{
// Skip the colon
szItemText++;
// Skip spaces
while(szItemText[0] == ' ')
szItemText++;
// If the number is followed by a space, cut it
szSpacePtr = _tcschr(szItemText, _T(' '));
if(szSpacePtr != NULL)
szSpacePtr[0] = 0;
}
// Return the text
return szItemText;
}
static BYTE GetDefaultAceType(HWND hTreeView, HTREEITEM hParent)
{
BYTE AceType = ACCESS_ALLOWED_ACE_TYPE;
// Retrieve the parent item
switch(TreeView_GetItemParam(hTreeView, hParent))
{
case TREE_ITEM_DACL:
AceType = ACCESS_ALLOWED_ACE_TYPE;
break;
case TREE_ITEM_SACL:
AceType = SYSTEM_AUDIT_ACE_TYPE;
break;
case TREE_ITEM_LABEL:
AceType = SYSTEM_MANDATORY_LABEL_ACE_TYPE;
break;
default:
assert(FALSE);
break;
}
return AceType;
}
static bool TreeView_ItemToValue32(HWND hTreeView, HTREEITEM hItem, LPDWORD PtrValue)
{
TVITEM tvi;
LPTSTR szValue;
TCHAR szItemText[256];
int nError;
// Get the text from the
tvi.mask = TVIF_TEXT;
tvi.hItem = hItem;
tvi.pszText = szItemText;
tvi.cchTextMax = _countof(szItemText);
if(TreeView_GetItem(hTreeView, &tvi))
{
// Retrieve the value
szValue = GetItemTextValue(szItemText);
if(szValue != NULL)
{
// Convert the 32-bit value to an integer
nError = Text2Hex32(szValue, PtrValue);
return (nError == ERROR_SUCCESS);
}
}
return false;
}
static bool TreeView_ItemToGuid(HWND hTreeView, HTREEITEM hItem, LPGUID PtrGuid)
{
TVITEM tvi;
LPTSTR szValue;
TCHAR szItemText[256];
// Get the text from the
tvi.mask = TVIF_TEXT;
tvi.hItem = hItem;
tvi.pszText = szItemText;
tvi.cchTextMax = _countof(szItemText);
if(TreeView_GetItem(hTreeView, &tvi))
{
// Retrieve the value
szValue = GetItemTextValue(szItemText);
if(szValue != NULL)
{
return StringToGuid(szValue, PtrGuid);
}
}
return false;
}
static HTREEITEM TreeView_SidToItem(HWND hTreeView, HTREEITEM hItem, PSID pSid, LPCTSTR szDefaultText)
{
LPARAM lParam = TREE_ITEM_NO_SID;
TCHAR szTextBuff[256];
// If the SID is present, convert the SID to the tree item
if(pSid != NULL)
{
// Convert the SID to text
SidToString(pSid, szTextBuff, _countof(szTextBuff), true);
szDefaultText = szTextBuff;
lParam = TREE_ITEM_SID;
}
return TreeView_SetTreeItem(hTreeView, hItem, szDefaultText, lParam);
}
static HTREEITEM TreeView_InsertSidItem(HWND hTreeView, HTREEITEM hParent, PSID pSid, LPCTSTR szDefaultText)
{
HTREEITEM hItem;
// Insert a new item
hItem = InsertTreeItem(hTreeView, hParent, szDefaultText, TREE_ITEM_NO_SID);
if(hItem != NULL)
{
// Apply the SID to the item and expand the parent
TreeView_SidToItem(hTreeView, hItem, pSid, szDefaultText);
TreeView_Expand(hTreeView, hParent, TVE_EXPAND);
}
return hItem;
}
static bool TreeView_ItemToSid(HWND hTreeView, HTREEITEM hItem, PSID * ppSid, bool bCanCreateNewSid)
{
TVITEM tvi;
TCHAR szItemText[256];
// Get the text from the treeview item
tvi.mask = TVIF_TEXT | TVIF_PARAM;
tvi.hItem = hItem;
tvi.pszText = szItemText;
tvi.cchTextMax = _countof(szItemText);
TreeView_GetItem(hTreeView, &tvi);
// Convert the tree item to SID
switch(tvi.lParam)
{
case TREE_ITEM_NO_SID:
if(bCanCreateNewSid)
*ppSid = Sid_CreateNew(ACCESS_ALLOWED_ACE_TYPE);
break;
case TREE_ITEM_SID:
StringToSid(szItemText, ppSid);
break;
}
return (*ppSid != NULL);
}
static bool TreeView_ItemToSid(HWND hTreeView, HTREEITEM hItem, ACE_HELPER & AceHelper)
{
PSID pSid = NULL;
bool bResult = false;
if(TreeView_ItemToSid(hTreeView, hItem, &pSid, false))
{
AceHelper.SetAllocatedSid(pSid);
bResult = true;
}
return bResult;
}
static HTREEITEM TreeView_MandatorySidToItem(HWND hTreeView, HTREEITEM hParent, PSID pSid)
{
TCHAR szItemText[256];
// Format the integrity level
StringCchPrintf(szItemText, _countof(szItemText), szIntLevelFmt, SidToIntegrityLevel(pSid));
return TreeView_SetTreeItem(hTreeView, hParent, szItemText, TREE_ITEM_MANDATORY_LABEL);
}
static HTREEITEM TreeView_InsertMandatorySidItem(HWND hTreeView, HTREEITEM hParent, PSID pSid, LPCTSTR szDefaultText)
{
HTREEITEM hItem;
// Insert a new item
hItem = InsertTreeItem(hTreeView, hParent, szDefaultText, TREE_ITEM_MANDATORY_LABEL);
if(hItem != NULL)
{
// Apply the SID to the item and expand the parent
TreeView_MandatorySidToItem(hTreeView, hItem, pSid);
TreeView_Expand(hTreeView, hParent, TVE_EXPAND);
}
return hItem;
}
static bool TreeView_ItemToMandatorySid(HWND hTreeView, HTREEITEM hItem, ACE_HELPER & AceHelper)
{
DWORD dwIntLevel = SECURITY_MANDATORY_MEDIUM_RID;
PSID pSid = NULL;
bool bResult = false;
// Convert the item to SID
if(TreeView_ItemToValue32(hTreeView, hItem, &dwIntLevel))
{
// Create new mandatory label SID
pSid = Sid_AllocateAndInitialize(&SiaLabel, 1, dwIntLevel);
if(pSid != NULL)
{
// Store the SID to the ACE_HELPER structure
AceHelper.SetAllocatedSid(pSid);
bResult = true;
}
}
return bResult;
}
static HTREEITEM TreeView_AceToItem(
HWND hTreeView,
HTREEITEM hItem,
ACE_HELPER & AceHelper)
{
TCHAR szItemText[256];
// Check if the layout is supported
if(AceHelper.AceLayout == ACE_LAYOUT_UNKNOWN)
return NULL;
// If the parent is valis
if(hItem != NULL)
{
// Delete all children
TreeView_DeleteChildren(hTreeView, hItem);
// Insert the subitem with ACE type
StringCchPrintf(szItemText, _countof(szItemText), szAceHdrTypeFmt, AceHelper.AceType);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_ACE_HEADER_TYPE);
// Insert the subitem with ACE flags
NamedValueToString(AceHdrFlags, szItemText, _countof(szItemText), szAceHdrFlagsFmt, AceHelper.AceFlags);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_ACE_HEADER_FLAGS);
// Insert the subitem with ACE size
StringCchPrintf(szItemText, _countof(szItemText), szAceHdrSizeFmt, AceHelper.AceSize);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_ACE_HEADER_SIZE);
// Insert the ACE:Mask (ACCESS_MASK), if present
if(AceHelper.AceLayout & ACE_FIELD_ACCESS_MASK)
{
NamedValueToString(AceMasks, szItemText, _countof(szItemText), szAceMaskFmt, AceHelper.Mask);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_ACE_MASK);
}
// Insert the ACE:Mask (Object ACEs), if present
if(AceHelper.AceLayout & ACE_FIELD_ADS_ACCESS_MASK)
{
NamedValueToString(AdsAceMasks, szItemText, _countof(szItemText), szAceMaskFmt, AceHelper.Mask);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_ADS_ACE_MASK);
}
// Insert the ACE:Mask (MANDATORY_LABEL_MASK), if present
if(AceHelper.AceLayout & ACE_FIELD_MANDATORY_MASK)
{
NamedValueToString(MandatoryMasks, szItemText, _countof(szItemText), szAceMaskFmt, AceHelper.Mask);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_MANDATORY_MASK);
}
// Insert the ACE::Flags, if present
if(AceHelper.AceLayout & ACE_FIELD_FLAGS)
{
NamedValueToString(AceFlags, szItemText, _countof(szItemText), szAceFlagsFmt, AceHelper.Flags);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_ACE_FLAGS);
}
// Insert the ACE:ObjectType, if present
// Info about object GUIDs, see https://msdn.microsoft.com/en-us/library/cc223512.aspx
if(AceHelper.AceLayout & ACE_FIELD_OBJECT_TYPE)
{
GuidValueToString(szItemText, _countof(szItemText), szAceObjTypeFmt, &AceHelper.ObjectType);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_ACE_OBJ_GUID);
}
// Insert the ACE:InheritedObjectType, if present
if(AceHelper.AceLayout & ACE_FIELD_OBJECT_TYPE2)
{
GuidValueToString(szItemText, _countof(szItemText), szAceObjTypeFmt2, &AceHelper.InheritedObjectType);
InsertTreeItem(hTreeView, hItem, szItemText, TREE_ITEM_ACE_OBJ_GUID2);
}
// Insert the integrity level, if present
if(AceHelper.AceLayout & ACE_FIELD_MANDATORY_SID)
TreeView_InsertMandatorySidItem(hTreeView, hItem, AceHelper.Sid, szUnknownSid);
// Insert the access SID, if present
if(AceHelper.AceLayout & ACE_FIELD_ACCESS_SID)
TreeView_InsertSidItem(hTreeView, hItem, AceHelper.Sid, szUnknownSid);
// Expand the item
TreeView_Expand(hTreeView, hItem, TVE_EXPAND);
}
// Return the item handle
return hItem;
}
static HTREEITEM TreeView_AceToItem(
HWND hTreeView,
HTREEITEM hParent,
PACE_HEADER pAceHeader)
{
ACE_HELPER AceHelper;
if(!AceHelper.SetAce(pAceHeader))
return NULL;
return TreeView_AceToItem(hTreeView, hParent, AceHelper);
}
static HTREEITEM TreeView_InsertAceItem(
HWND hTreeView,
HTREEITEM hParent,
HTREEITEM hInsertAfter,
PACE_HEADER pAceHeader)
{
HTREEITEM hItem;
// Insert the "root" item with ACE type
hItem = InsertTreeItem(hTreeView, hParent, hInsertAfter, GetAceTypeString(pAceHeader->AceType), (PVOID)(ULONG_PTR)(TREE_ITEM_ACE | pAceHeader->AceType));
if(hItem != NULL)
{
// Fill the ACE structure
TreeView_AceToItem(hTreeView, hItem, pAceHeader);
TreeView_Expand(hTreeView, hParent, TVE_EXPAND);
}
// Return the newly created tree item
return hItem;
}
static bool TreeView_ItemToAce(
HWND hTreeView,
HTREEITEM hItem,
ACE_HELPER & AceHelper)
{
DWORD dwAceType = 0;
// Fill the entire structure with zeros
AceHelper.Reset();
// Retrieve the (first/next) child
hItem = TreeView_GetChild(hTreeView, hItem);
while(hItem != NULL)
{
bool bResult = false;
// Get the values of the ACE according the item type
switch(TreeView_GetItemParam(hTreeView, hItem))
{
case TREE_ITEM_ACE_HEADER_TYPE: // Save the ACE_Header::AceType
if(TreeView_ItemToValue32(hTreeView, hItem, &dwAceType))
bResult = AceHelper.SetAceType(dwAceType);
break;
case TREE_ITEM_ACE_HEADER_FLAGS: // Save the ACE_Header::AceFlags
bResult = TreeView_ItemToValue32(hTreeView, hItem, &AceHelper.AceFlags);
break;
case TREE_ITEM_ACE_HEADER_SIZE: // Save the ACE_HEADER::AceSize (even if it will not be used)
bResult = TreeView_ItemToValue32(hTreeView, hItem, &AceHelper.AceSize);
break;
case TREE_ITEM_ACE_MASK:
case TREE_ITEM_ADS_ACE_MASK:
case TREE_ITEM_MANDATORY_MASK:
bResult = TreeView_ItemToValue32(hTreeView, hItem, &AceHelper.Mask);
break;
case TREE_ITEM_ACE_FLAGS:
bResult = TreeView_ItemToValue32(hTreeView, hItem, &AceHelper.Flags);
break;
case TREE_ITEM_ACE_OBJ_GUID:
bResult = TreeView_ItemToGuid(hTreeView, hItem, &AceHelper.ObjectType);
break;
case TREE_ITEM_ACE_OBJ_GUID2:
bResult = TreeView_ItemToGuid(hTreeView, hItem, &AceHelper.InheritedObjectType);
break;
case TREE_ITEM_MANDATORY_LABEL:
bResult = TreeView_ItemToMandatorySid(hTreeView, hItem, AceHelper);