-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
modPermissions.bas
1638 lines (1330 loc) · 66.7 KB
/
modPermissions.bas
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
Attribute VB_Name = "modPermissions"
'[modPermissions.bas]
'
' Reset Permissions Module by Alex Dragokas
'
' ver. 2.1
'
' This module is a part of HiJackThis project
'
Option Explicit
'Notes:
'How to get SDDL:
'Get-Acl C:\obj | Format-List -Property PSPath, Sddl
'Public Type LUID
' lowpart As Long
' highpart As Long
'End Type
'
'Public Type LUID_AND_ATTRIBUTES
' pLuid As LUID
' Attributes As Long
'End Type
'
'Public Type PRIVILEGE_SET
' PrivilegeCount As Long
' Control As Long
' Privilege(0) As LUID_AND_ATTRIBUTES 'ANY_SIZE
'End Type
'
'Public Type TOKEN_PRIVILEGES
' PrivilegeCount As Long
' LuidLowPart As Long
' LuidHighPart As Long
' Attributes As Long
'End Type
'
'Public Type SECURITY_DESCRIPTOR
' Revision As Byte
' Sbz1 As Byte
' Control As Integer 'SECURITY_DESCRIPTOR_CONTROL
' Owner As Long 'pSID
' Group As Long 'pSID
' SACL As Long 'pACL
' Dacl As Long 'pACL
'End Type
'
'Public Type GENERIC_MAPPING 'https://docs.microsoft.com/en-us/windows/desktop/SecAuthZ/access-mask
' GenericRead As Long 'ACCESS_MASK
' GenericWrite As Long 'ACCESS_MASK
' GenericExecute As Long 'ACCESS_MASK
' GenericAll As Long 'ACCESS_MASK
'End Type
'
'Public Enum SECURITY_IMPERSONATION_LEVEL
' SecurityAnonymous
' SecurityIdentification
' SecurityImpersonation
' SecurityDelegation
'End Enum
'
'Public Enum ACCESS_MODE
' NOT_USED_ACCESS = 0
' GRANT_ACCESS
' SET_ACCESS
' DENY_ACCESS
' REVOKE_ACCESS
' SET_AUDIT_SUCCESS
' SET_AUDIT_FAILURE
'End Enum
'
'Public Enum TRUSTEE_FORM
' TRUSTEE_IS_SID = 0
' TRUSTEE_IS_NAME
' TRUSTEE_BAD_FORM
' TRUSTEE_IS_OBJECTS_AND_SID
' TRUSTEE_IS_OBJECTS_AND_NAME
'End Enum
'
'Public Enum TRUSTEE_TYPE
' TRUSTEE_IS_UNKNOWN = 0
' TRUSTEE_IS_USER
' TRUSTEE_IS_GROUP
' TRUSTEE_IS_DOMAIN
' TRUSTEE_IS_ALIAS
' TRUSTEE_IS_WELL_KNOWN_GROUP
' TRUSTEE_IS_DELETED
' TRUSTEE_IS_INVALID
' TRUSTEE_IS_COMPUTER
'End Enum
'
'Public Type TRUSTEE
' pMultipleTrustee As Long
' MultipleTrusteeOperation As Long
' TrusteeForm As TRUSTEE_FORM
' TrusteeType As TRUSTEE_TYPE
' ptstrName As Long
'End Type
'
'Public Type EXPLICIT_ACCESS
' grfAccessPermissions As Long
' grfAccessMode As ACCESS_MODE
' grfInheritance As Long
' tTrustee As TRUSTEE
'End Type
'
'Public Type ACE_HEADER
' AceType As Byte
' AceFlags As Byte
' AceSize As Integer
'End Type
'
'Public Type ACCESS_DENIED_ACE
' Header As ACE_HEADER
' Mask As Long 'ACCESS_MASK
' SidStart As Long
'End Type
'
'Public Type ACL_SIZE_INFORMATION
' AceCount As Long
' AclBytesInUse As Long
' AclBytesFree As Long
'End Type
'
'Public Type SID
' Revision As Byte
' SubAuthorityCount As Byte
' IdentifierAuthority(5) As Byte
' SubAuthority As Long
'End Type
'
'Public Enum ACL_INFORMATION_CLASS
' AclRevisionInformation = 1
' AclSizeInformation
'End Enum
'Public Enum SE_OBJECT_TYPE
' SE_UNKNOWN_OBJECT_TYPE = 0
' SE_FILE_OBJECT
' SE_SERVICE
' SE_PRINTER
' SE_REGISTRY_KEY
' SE_LMSHARE
' SE_KERNEL_OBJECT
' SE_WINDOW_OBJECT
' SE_DS_OBJECT
' SE_DS_OBJECT_ALL
' SE_PROVIDER_DEFINED_OBJECT
' SE_WMIGUID_OBJECT
' SE_REGISTRY_WOW64_32KEY
'End Enum
'Private Enum SECURITY_INFORMATION 'required access - to query / to set info:
' ATTRIBUTE_SECURITY_INFORMATION = &H20& 'query: READ_CONTROL; set: WRITE_DAC
' BACKUP_SECURITY_INFORMATION = &H10000 'query: READ_CONTROL and ACCESS_SYSTEM_SECURITY; set: WRITE_DAC and WRITE_OWNER and ACCESS_SYSTEM_SECURITY
' DACL_SECURITY_INFORMATION = 4 'query: READ_CONTROL; set: WRITE_DAC
' GROUP_SECURITY_INFORMATION = 2 'query: READ_CONTROL; set: WRITE_OWNER
' LABEL_SECURITY_INFORMATION = 16 'query: READ_CONTROL; set: WRITE_OWNER
' OWNER_SECURITY_INFORMATION = 1 'query: READ_CONTROL; set: WRITE_OWNER
' PROTECTED_DACL_SECURITY_INFORMATION = &H80000000 'query: -; set: WRITE_DAC
' PROTECTED_SACL_SECURITY_INFORMATION = &H40000000 'query: -; set: ACCESS_SYSTEM_SECURITY
' SACL_SECURITY_INFORMATION = 8 'query: ACCESS_SYSTEM_SECURITY; set: ACCESS_SYSTEM_SECURITY
' SCOPE_SECURITY_INFORMATION = &H40& 'query: READ_CONTROL; set: ACCESS_SYSTEM_SECURITY
' UNPROTECTED_DACL_SECURITY_INFORMATION = &H20000000 'query: -; set: WRITE_DAC
' UNPROTECTED_SACL_SECURITY_INFORMATION = &H10000000 'query: -; set: ACCESS_SYSTEM_SECURITY
'End Enum
'Private Declare Sub GetNativeSystemInfo Lib "kernel32.dll" (ByVal lpSystemInfo As Long)
Private Declare Function GetVersionEx Lib "kernel32.dll" Alias "GetVersionExW" (lpVersionInformation As Any) As Long
Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As Long
Private Declare Function GetCurrentThread Lib "kernel32.dll" () As Long
Private Declare Function LookupPrivilegeValue Lib "Advapi32.dll" Alias "LookupPrivilegeValueW" (ByVal lpSystemName As Long, ByVal lpName As Long, lpLuid As Long) As Long
Private Declare Function OpenProcessToken Lib "Advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function OpenThreadToken Lib "Advapi32.dll" (ByVal ThreadHandle As Long, ByVal DesiredAccess As Long, ByVal OpenAsSelf As Long, TokenHandle As Long) As Long
Private Declare Function AdjustTokenPrivileges Lib "Advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, ByVal PreviousState As Long, ByVal ReturnLength As Long) As Long
Private Declare Function ConvertStringSidToSid Lib "Advapi32.dll" Alias "ConvertStringSidToSidW" (ByVal StringSid As Long, pSid As Long) As Long
Private Declare Function RegOpenKeyEx Lib "Advapi32.dll" Alias "RegOpenKeyExW" (ByVal hKey As Long, ByVal lpSubKey As Long, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegCloseKey Lib "Advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "Advapi32.dll" Alias "RegCreateKeyExW" (ByVal hKey As Long, ByVal lpSubKey As Long, ByVal Reserved As Long, ByVal lpClass As Long, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As Any, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function LocalFree Lib "kernel32.dll" (ByVal hMem As Long) As Long
Private Declare Function CopySid Lib "Advapi32.dll" (ByVal nDestinationSidLength As Long, ByVal pDestinationSid As Long, ByVal pSourceSid As Long) As Long
Private Declare Function GetLengthSid Lib "Advapi32.dll" (ByVal pSid As Long) As Long
Private Declare Function IsValidSid Lib "Advapi32.dll" (ByVal pSid As Long) As Long
'Private Declare Function RegDeleteKeyEx Lib "advapi32.dll" Alias "RegDeleteKeyExW" (ByVal hKey As Long, ByVal lpSubKey As Long, ByVal samDesired As Long, ByVal Reserved As Long) As Long
Private Declare Function GetKernelObjectSecurity Lib "Advapi32.dll" (ByVal Handle As Long, ByVal RequestedInformation As SECURITY_INFORMATION, ByVal pSecurityDescriptor As Long, ByVal nLength As Long, ByVal lpnLengthNeeded As Long) As Long
Private Declare Function MakeAbsoluteSD Lib "Advapi32.dll" (ByVal pSelfRelativeSD As Long, ByVal pAbsoluteSD As Long, ByVal lpdwAbsoluteSDSize As Long, ByVal pDACL As Long, ByVal lpdwDaclSize As Long, ByVal pSACL As Long, ByVal lpdwSaclSize As Long, ByVal pOwner As Long, ByVal lpdwOwnerSize As Long, ByVal pPrimaryGroup As Long, ByVal lpdwPrimaryGroupSize As Long) As Long
Private Declare Function IsValidSecurityDescriptor Lib "Advapi32.dll" (ByVal pSecurityDescriptor As Long) As Long
Private Declare Function SetEntriesInAcl Lib "Advapi32.dll" Alias "SetEntriesInAclW" (ByVal cCountOfExplicitEntries As Long, ByVal pListOfExplicitEntries As Long, ByVal pOldAcl As Long, NewAcl As Long) As Long
Private Declare Function SetSecurityInfo Lib "Advapi32.dll" (ByVal Handle As Long, ByVal ObjectType As SE_OBJECT_TYPE, ByVal SecurityInfo As SECURITY_INFORMATION, ByVal psidOwner As Long, ByVal psidGroup As Long, ByVal pDACL As Long, ByVal pSACL As Long) As Long
'Private Declare Function SetNamedSecurityInfo Lib "advapi32.dll" Alias "SetNamedSecurityInfoW" (ByVal pObjectName As Long, ByVal ObjectType As SE_OBJECT_TYPE, ByVal SecurityInfo As Long, ByVal psidOwner As Long, ByVal psidGroup As Long, ByVal pDacl As Long, ByVal pSacl As Long) As Long
Private Declare Function GetAclInformation Lib "Advapi32.dll" (ByVal pAcl As Long, ByVal pAclInformation As Long, ByVal nAclInformationLength As Long, ByVal dwAclInformationClass As ACL_INFORMATION_CLASS) As Long
Private Declare Function GetAce Lib "Advapi32.dll" (ByVal pAcl As Long, ByVal dwAceIndex As Long, pAce As Long) As Long
Private Declare Function memcpy Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) As Long
'Private Declare Function GetExplicitEntriesFromAcl Lib "advapi32.dll" Alias "GetExplicitEntriesFromAclW" (ByVal pAcl As Long, pcCountOfExplicitEntries As Long, pListOfExplicitEntries As Long) As Long
Private Declare Function DeleteAce Lib "Advapi32.dll" (ByVal pAcl As Long, ByVal dwAceIndex As Long) As Long
Private Declare Function InitializeAcl Lib "Advapi32.dll" (ByVal pAcl As Long, ByVal nAclLength As Long, ByVal dwAclRevision As Long) As Long
Private Declare Function LocalAlloc Lib "kernel32.dll" (ByVal uFlags As Long, ByVal uBytes As Long) As Long
Private Declare Function IsValidAcl Lib "Advapi32.dll" (ByVal pAcl As Long) As Long
'Private Declare Function TreeResetNamedSecurityInfo Lib "advapi32.dll" Alias "TreeResetNamedSecurityInfoW" (ByVal pObjectName As Long, ByVal ObjectType As SE_OBJECT_TYPE, ByVal SecurityInfo As SECURITY_INFORMATION, ByVal pOwner As Long, ByVal pGroup As Long, ByVal pDacl As Long, ByVal pSacl As Long, ByVal KeepExplicit As Long, ByVal fnProgress As Long, ByVal ProgressInvokeSetting As Long, ByVal Args As Long) As Long
Private Declare Function RegEnumKeyEx Lib "Advapi32.dll" Alias "RegEnumKeyExW" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As Long, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As Long, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenW" (ByVal lpString As Long) As Long
Private Declare Function CreateFile Lib "kernel32.dll" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function AccessCheck Lib "Advapi32.dll" (SecurityDescriptor As Any, ByVal ClientToken As Long, ByVal DesiredAccess As Long, GenericMapping As GENERIC_MAPPING, PrivilegeSet As Any, PrivilegeSetLength As Long, GrantedAccess As Long, AccessStatus As Long) As Long
Private Declare Sub MapGenericMask Lib "Advapi32.dll" (AccessMask As Long, GenericMapping As GENERIC_MAPPING)
Private Declare Function DuplicateToken Lib "Advapi32.dll" (ByVal hExistingToken As Long, ByVal ImpersonationLevel As Long, DuplicateTokenHandle As Long) As Long
'Private Declare Function ImpersonateSelf Lib "advapi32.dll" (ByVal ImpersonationLevel As Long) As Long
'Private Declare Function RevertToSelf Lib "advapi32.dll" () As Long
Private Declare Function ConvertSecurityDescriptorToStringSecurityDescriptor Lib "Advapi32.dll" Alias "ConvertSecurityDescriptorToStringSecurityDescriptorW" (SecurityDescriptor As Any, ByVal RequestedStringSDRevision As Long, ByVal SecurityInformation As SECURITY_INFORMATION, lpStringSecurityDescriptor As Long, StringSecurityDescriptorLen As Long) As Long
Private Declare Function ConvertStringSecurityDescriptorToSecurityDescriptor Lib "Advapi32.dll" Alias "ConvertStringSecurityDescriptorToSecurityDescriptorW" (ByVal StringSecurityDescriptor As Long, ByVal StringSDRevision As Long, pSecurityDescriptor As Long, SecurityDescriptorSize As Long) As Long
Private Const MAX_KEYNAME As Long = 255&
Private Const REG_OPTION_BACKUP_RESTORE As Long = 4&
Private Const GENERIC_ALL As Long = &H10000000
Private Const GENERIC_READ As Long = &H80000000
Private Const WRITE_DAC As Long = &H40000
Private Const WRITE_OWNER As Long = &H80000
Private Const READ_CONTROL As Long = &H20000
Private Const KEY_WOW64_64KEY As Long = &H100&
Private Const KEY_ENUMERATE_SUB_KEYS As Long = &H8
Private Const TOKEN_ADJUST_PRIVILEGES As Long = &H20
Private Const TOKEN_QUERY As Long = 8&
Private Const SE_PRIVILEGE_ENABLED As Long = 2&
Private Const ACCESS_SYSTEM_SECURITY As Long = &H1000000
Private Const OBJECT_INHERIT_ACE As Long = 1&
Private Const CONTAINER_INHERIT_ACE As Long = 2&
Private Const NO_MULTIPLE_TRUSTEE As Long = 0&
Private Const ACCESS_DENIED_ACE_TYPE As Long = 1&
Private Const SDDL_REVISION_1 As Long = 1&
Private Const ACL_REVISION As Long = 2&
Private Const REG_CREATED_NEW_KEY As Long = 1&
'Private Const ERROR_MORE_DATA As Long = 234&
Private Const ERROR_SUCCESS As Long = 0&
Private Const ERROR_NO_TOKEN As Long = 1008&
Private Const LMEM_FIXED As Long = 0&
Private Const LMEM_ZEROINIT As Long = &H40&
Private Const ProgressInvokeNever As Long = 1&
Private Const HKEY_CLASSES_ROOT As Long = &H80000000
Private Const HKEY_CURRENT_USER As Long = &H80000001
Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
Private Const HKEY_USERS As Long = &H80000003
Private Const HKEY_PERFORMANCE_DATA As Long = &H80000004
Private Const HKEY_CURRENT_CONFIG As Long = &H80000005
Private Const HKEY_DYN_DATA As Long = &H80000006
Private Const OPEN_EXISTING As Long = 3&
Private Const FILE_SHARE_READ As Long = &H1&
Private Const FILE_SHARE_WRITE As Long = &H2&
Private Const FILE_SHARE_DELETE As Long = 4&
Private Const FILE_FLAG_BACKUP_SEMANTICS As Long = &H2000000
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Private Const INVALID_HANDLE_VALUE As Long = &HFFFFFFFF
Private Const TOKEN_IMPERSONATE As Long = 4&
Private Const TOKEN_DUPLICATE As Long = 2&
Private Const FILE_ATTRIBUTE_DIRECTORY As Long = &H10&
Private Const SYNCHRONIZE = &H100000
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_READ = &H20000
Private Const STANDARD_RIGHTS_WRITE = &H20000
Private Const STANDARD_RIGHTS_EXECUTE = &H20000
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const SPECIFIC_RIGHTS_ALL = &HFFFF&
Private Const FILE_READ_DATA = &H1& ' file & pipe
Private Const FILE_LIST_DIRECTORY = &H1& ' directory
Private Const FILE_WRITE_DATA = &H2& ' file & pipe
Private Const FILE_ADD_FILE = &H2& ' directory
Private Const FILE_APPEND_DATA = &H4& ' file
Private Const FILE_ADD_SUBDIRECTORY = &H4& ' directory
Private Const FILE_CREATE_PIPE_INSTANCE = &H4& ' named pipe
Private Const FILE_READ_EA = &H8& ' file & directory
Private Const FILE_WRITE_EA = &H10& ' file & directory
Private Const FILE_EXECUTE = &H20& ' file
Private Const FILE_TRAVERSE = &H20& ' directory
Private Const FILE_DELETE_CHILD = &H40& ' directory
Private Const FILE_READ_ATTRIBUTES = &H80& ' all
Private Const FILE_WRITE_ATTRIBUTES = &H100& ' all
Private Const FILE_GENERIC_READ = STANDARD_RIGHTS_READ Or FILE_READ_DATA Or FILE_READ_ATTRIBUTES Or FILE_READ_EA Or SYNCHRONIZE
Private Const FILE_GENERIC_WRITE = STANDARD_RIGHTS_WRITE Or FILE_WRITE_DATA Or FILE_WRITE_ATTRIBUTES Or FILE_WRITE_EA Or FILE_APPEND_DATA Or SYNCHRONIZE
Private Const FILE_GENERIC_EXECUTE = STANDARD_RIGHTS_EXECUTE Or FILE_READ_ATTRIBUTES Or FILE_EXECUTE Or SYNCHRONIZE
Private Const FILE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &H1FF&
Private Const KEY_ALL_ACCESS = STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or KEY_CREATE_LINK And Not SYNCHRONIZE
' Creates array of EXPLICIT_ACCESS structures with access rights needed
Function Make_Default_Ace_Explicit(lHive As Long, KeyName As String) As EXPLICIT_ACCESS()
Dim idx As Long
Dim pSid As Long
Dim sKey As String, pos As Long
Dim inf(68) As Long: inf(0) = 276: GetVersionEx inf(0)
Dim MajorMinor As Single: MajorMinor = inf(1) + inf(2) / 10
Static isInit As Boolean
Static bufSidAdmin() As Byte
Static bufSidSystem() As Byte
Static bufSidUsers() As Byte
Static bufSidPowerUsers() As Byte
Static bufSidRestricted() As Byte
Static bufSidCreator() As Byte
Static bufSidTI() As Byte
Static bufSidAppX() As Byte
If Not isInit Then
isInit = True
bufSidSystem = CreateBufferedSID("S-1-5-18")
bufSidAdmin = CreateBufferedSID("S-1-5-32-544")
bufSidUsers = CreateBufferedSID("S-1-5-32-545")
bufSidPowerUsers = CreateBufferedSID("S-1-5-32-547") '( < Vista)
bufSidRestricted = CreateBufferedSID("S-1-5-12")
bufSidCreator = CreateBufferedSID("S-1-3-0")
bufSidTI = CreateBufferedSID("S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464") '(Win Vista+)
bufSidAppX = CreateBufferedSID("S-1-15-2-1")
'ÖÅÍÒÐ ÏÀÊÅÒÎÂ ÏÐÈËÎÆÅÍÈÉ\ÂÑÅ ÏÀÊÅÒÛ ÏÐÈËÎÆÅÍÈÉ (AppX) - S-1-15-2-1 (Win 8.0+)
'TrustedInstaller - details on:
'(EN) https://technet.microsoft.com/en-us/magazine/2007.06.acl.aspx
'(RU) http://www.oszone.net/5003
' Well-known SIDs: https://support.microsoft.com/en-us/kb/243330
'
' Other useful SIDs:
'
'Creator - S-1-3-0
'Everyone = S-1-1-0
'All Services = S-1-5-80-0
'Local System - S-1-5-18
'Local Administrator - S-1-5-21-500
'Administrators - S-1-5-32-544
'Users - S-1-5-32-545
'Power Users = S-1-5-32-547
'Guests = S-1-5-32-546
'Restricted Code - S-1-5-12
'Low Mandatory Level - S-1-16-4096
'Medium Mandatory Level - S-1-16-8192
'Medium Plus Mandatory Level - S-1-16-8448
'High Mandatory Level - S-1-16-12288
'System Mandatory Level - S-1-16-16384
'Protected Process Mandatory Level - S-1-16-20480
'Secure Process Mandatory Level - S-1-16-28672
'Authenticated Users (Ïðîøåäøèå ïðîâåðêó) - S-1-5-11
End If
'array should be consistent
Dim Ace_Explicit() As EXPLICIT_ACCESS
ReDim Ace_Explicit(10) As EXPLICIT_ACCESS '// now used 5-8/10
'1. Local System:F (OI)(CI)
idx = 0
pSid = VarPtr(bufSidSystem(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_ALL
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
'2. Administrators:F (OI)(CI)
pSid = VarPtr(bufSidAdmin(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_ALL
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
'3. Service:F (OI)(CI) (optional), depends on Key name
If lHive = HKEY_LOCAL_MACHINE And InStr(1, KeyName, "SYSTEM\CurrentControlSet\services\", 1) = 1 Then
pos = InStr(1 + Len("SYSTEM\CurrentControlSet\services\"), KeyName, "\")
If pos = 0 Then
sKey = KeyName
Else
sKey = Left$(KeyName, pos - 1)
End If
If Reg.GetDword(HKLM, sKey, "ServiceSidType") = 3 Then 'Restricted SID
Dim Tok() As String, SrvName As String
Tok = Split(KeyName, "\")
If UBound(Tok) >= 3 Then SrvName = Tok(3)
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_ALL
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_NAME
.TrusteeType = TRUSTEE_IS_UNKNOWN
.ptstrName = StrPtr("NT SERVICE\" & SrvName)
End With
End With
idx = idx + 1
End If
End If
'4. Trusted Installer:F (OI)(CI) (optional) (Vista+)
If MajorMinor >= 6 Then
pSid = VarPtr(bufSidTI(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_ALL
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_UNKNOWN
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
End If
'5. AppX:R (OI)(CI) (optional) (Win 8.0+)
If MajorMinor >= 6.2 Then
pSid = VarPtr(bufSidAppX(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_READ
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_UNKNOWN
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
End If
'+ 2-3 "ACE" descriptions. Rights based on hive name - LM / CU.
If lHive = HKEY_CURRENT_USER Then
'HKCU
'Users:F (OI)(CI)
'Restricted:R (OI)(CI)
pSid = VarPtr(bufSidUsers(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_ALL
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
pSid = VarPtr(bufSidRestricted(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_READ
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
Else
'HKLM
'Creator:F (CI)
'Users:R (OI)(CI)
'PowerUsers:R (OI)(CI) (XP only)
pSid = VarPtr(bufSidCreator(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_ALL
.grfAccessMode = SET_ACCESS
.grfInheritance = CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
pSid = VarPtr(bufSidUsers(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_READ
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
If MajorMinor < 6 Then
pSid = VarPtr(bufSidPowerUsers(0))
If IsValidSid(pSid) Then
With Ace_Explicit(idx)
.grfAccessPermissions = GENERIC_READ
.grfAccessMode = SET_ACCESS
.grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
With .tTrustee
.TrusteeForm = TRUSTEE_IS_SID
.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP
.ptstrName = pSid
End With
End With
idx = idx + 1
End If
End If
End If
If idx > 0 Then
ReDim Preserve Ace_Explicit(idx - 1)
End If
Make_Default_Ace_Explicit = Ace_Explicit
End Function
'Creates SID buffer array from SID string
Public Function CreateBufferedSID(SidString As String) As Byte()
Dim pSid As Long
Dim cbSID As Long
Dim bufSid() As Byte
ReDim bufSid(0) As Byte
If 0 = ConvertStringSidToSid(StrPtr(SidString), pSid) Then ' * -> *
If Not StrBeginWith(SidString, "Sandbox_") Then
Debug.Print "ErrorHandler: ConvertStringSidToSidW failed with code: " & Err.LastDllError & ". Input buffer: " & SidString
End If
Else
If IsValidSid(pSid) Then
cbSID = GetLengthSid(pSid)
If cbSID <> 0 Then
ReDim bufSid(cbSID - 1) As Byte
CopySid cbSID, VarPtr(bufSid(0)), pSid
End If
LocalFree pSid
End If
CreateBufferedSID = bufSid
End If
End Function
'' if main hive handle wasn't defined, assigns handle according to hive's name defined by Full key name directed
'Sub NormalizeKeyNameAndHiveHandle(ByRef lHive As Long, ByRef KeyName As String)
' Dim iPos As Long
' If lHive = 0 Then
' lHive = GetHKey(KeyName)
' iPos = InStr(KeyName, "\")
' If (iPos <> 0&) Then KeyName = Mid$(KeyName, iPos + 1&) Else KeyName = vbNullString
' End If
'End Sub
''concat structure 'EXPLICIT_ACCESS' to array in consistent order
'Function Add_Ace_Explicit(ByRef Ace_Explicit() As EXPLICIT_ACCESS, New_Ace_Explicit As EXPLICIT_ACCESS)
' Dim i As Long
' ReDim Concat_Ace_Explicit(UBound(Ace_Explicit) + 1) As EXPLICIT_ACCESS
'
' For i = 0 To UBound(Ace_Explicit) 'duplicate old array
' Concat_Ace_Explicit(i) = Ace_Explicit(i)
' Next
' Concat_Ace_Explicit(UBound(Ace_Explicit) + 1) = New_Ace_Explicit
' 'Replacing array
' Ace_Explicit = Concat_Ace_Explicit
'End Function
' obtains ownership on registry key
Public Function RegKeySetOwnerShip(lHive&, ByVal KeyName$, SidString As String, Optional bUseWow64 As Boolean) As Boolean
'
'Parameters:
'
'lHive - pseudohandle to root key (hive). This value can be 0.
'KeyName - Path to registry key. Is lHive is 0, this path must be Full, otherwise it must be relative to hive.
'SidString - string representation of SID
'bUseWow64 - (optional) if true, this function use registry redirector, so all calls will be directed to 32-bit keys on 64-bit machine
On Error GoTo ErrorHandler:
Dim flagDisposition As Long
Dim bufSid() As Byte
Dim hKey As Long
Dim lret As Long
Call Reg.NormalizeKeyNameAndHiveHandle(lHive, KeyName)
' -->>> moved to main form
'
' SetCurrentProcessPrivileges "SeBackupPrivilege"
' SetCurrentProcessPrivileges "SeRestorePrivilege"
' SetCurrentProcessPrivileges "SeTakeOwnershipPrivilege"
' SeTakeOwnershipPrivilege + WRITE_OWNER
If ERROR_SUCCESS <> RegOpenKeyEx(lHive, StrPtr(KeyName), 0&, WRITE_OWNER Or (bIsWOW64 And KEY_WOW64_64KEY And Not bUseWow64), hKey) Then
'Key doesn't exist
Exit Function
Else
RegCloseKey hKey
End If
bufSid = CreateBufferedSID(SidString)
'Note: ACCESS_SYSTEM_SECURITY for SACL
If ERROR_SUCCESS = RegCreateKeyEx(lHive, StrPtr(KeyName), 0&, 0&, REG_OPTION_BACKUP_RESTORE, WRITE_DAC Or (bIsWOW64 And KEY_WOW64_64KEY And Not bUseWow64), ByVal 0&, hKey, flagDisposition) Then
' If flagDisposition = REG_CREATED_NEW_KEY Then
' RegCloseKey hKey
' RegDeleteKeyEx lHive, StrPtr(KeyName), KEY_WOW64_64KEY And Not bUseWow64, 0&
' Debug.Print "Key doesn't exist"
' Exit Function
' End If
'IIf(bUseWow64 And isWin64(), SE_REGISTRY_WOW64_32KEY, SE_REGISTRY_KEY)
lret = SetSecurityInfo(hKey, SE_REGISTRY_KEY, OWNER_SECURITY_INFORMATION, VarPtr(bufSid(0)), 0&, 0&, 0&)
If lret = ERROR_SUCCESS Then
RegKeySetOwnerShip = True
Debug.Print KeyName & " - OwnerShip granted successfully."
Else
Debug.Print KeyName & " - Error in SetSecurityInfo: " & lret
End If
RegCloseKey hKey
End If
Exit Function
ErrorHandler:
Debug.Print "Error in RegSetOwnerShip", Err, Err.Description
End Function
'resets access on registry key into some standart (look below for details)
Public Function RegKeyResetDACL(lHive&, ByVal KeyName$, Optional bUseWow64 As Boolean, Optional Recursive As Boolean = False) As Boolean
'
'Parameters:
'
'lHive - pseudohandle to root key (hive). This value can be 0.
'KeyName - Path to registry key. If lHive is 0, this path must be Full, otherwise it must be relative to hive.
'bUseWow64 - (optional) if true, this function use registry redirector, so all calls will be directed to 32-bit keys on 64-bit machine
'Recursive - (optional) apply action to all subkeys.
On Error GoTo ErrorHandler:
'Description:
'
'This function also made call to RegKeySetOwnerShip function.
'So, you don't need to call it twice.
'
'Note:
'There are 6 types of DACL ACEs: 3 of them - General, other 3 - Object specific (has more fields on its struct, incl. GUIDs)
'This function performs check of ACCESS_DENIED_ACE presence.
'
'If DACL contains such struct, this ACE will be removed from it.
'So, all denied access rights will be revoked.
'
'Default access rights will be written using EXPLICIT_ACCESS array supplemented by default access masks for standart trustees
'like 'Local System', 'Administrators' and so on, see details on top: Make_Default_Ace_Explicit function.
'
'EXPLICIT_ACCESS will be applied by merging to ACL in consistent order using SetEntriesInAcl function.
'
Dim flagDisposition As Long
Dim hKey As Long
Dim RelSD() As Byte
Dim AbsSD() As Byte
Dim cbRelSD As Long
Dim cbAbsSD As Long
Dim oldDACL() As Byte
'Dim newDACL() As Byte
Dim cbDACL As Long
Dim cbSACL As Long
Dim cbSID_Owner As Long
Dim cbPrimGrp As Long
Dim pNewDacl As Long
Dim AclInfo As ACL_SIZE_INFORMATION
'Dim AceDenied As ACCESS_DENIED_ACE
Dim AceHead As ACE_HEADER
Dim i As Long
Dim pAce As Long
Dim lret As Long
'Dim Revoke_Ace_Explicit As EXPLICIT_ACCESS
Dim pAcl As Long
Dim hKeyEnum As Long
Dim sSubKeyName As String
Call Reg.NormalizeKeyNameAndHiveHandle(lHive, KeyName)
RegKeySetOwnerShip lHive, KeyName, "S-1-5-32-544", bUseWow64
' -->>> moved to main form
'
' SetCurrentProcessPrivileges "SeBackupPrivilege"
' SetCurrentProcessPrivileges "SeRestorePrivilege"
' SetCurrentProcessPrivileges "SeTakeOwnershipPrivilege"
' SetCurrentProcessPrivileges "SeSecurityPrivilege" 'SACL
If ERROR_SUCCESS <> RegOpenKeyEx(lHive, StrPtr(KeyName), 0&, WRITE_OWNER Or (bIsWOW64 And KEY_WOW64_64KEY And Not bUseWow64), hKey) Then
'Key doesn't exist
Exit Function
Else
RegCloseKey hKey
End If
If ERROR_SUCCESS = RegCreateKeyEx(lHive, StrPtr(KeyName), 0&, 0&, _
REG_OPTION_BACKUP_RESTORE, _
READ_CONTROL Or WRITE_DAC Or (bIsWOW64 And KEY_WOW64_64KEY And Not bUseWow64), _
ByVal 0&, hKey, flagDisposition) Then
ReDim RelSD(0)
'extracting relative SD
GetKernelObjectSecurity hKey, DACL_SECURITY_INFORMATION Or PROTECTED_DACL_SECURITY_INFORMATION Or UNPROTECTED_DACL_SECURITY_INFORMATION, VarPtr(RelSD(0)), 0&, VarPtr(cbRelSD)
If cbRelSD <> 0 Then
ReDim RelSD(cbRelSD - 1)
If GetKernelObjectSecurity(hKey, DACL_SECURITY_INFORMATION Or PROTECTED_DACL_SECURITY_INFORMATION Or UNPROTECTED_DACL_SECURITY_INFORMATION, VarPtr(RelSD(0)), cbRelSD, VarPtr(cbRelSD)) Then
'relative SD -> Absolute
MakeAbsoluteSD VarPtr(RelSD(0)), 0&, VarPtr(cbAbsSD), 0&, VarPtr(cbDACL), 0&, VarPtr(cbSACL), 0&, VarPtr(cbSID_Owner), 0&, VarPtr(cbPrimGrp)
ReDim AbsSD(cbAbsSD - 1)
If cbDACL <> 0 Then
ReDim oldDACL(cbDACL - 1)
Else 'if not contains DACL information
ReDim oldDACL(0)
End If
If MakeAbsoluteSD(VarPtr(RelSD(0)), VarPtr(AbsSD(0)), VarPtr(cbAbsSD), VarPtr(oldDACL(0)), VarPtr(cbDACL), 0&, VarPtr(cbSACL), 0&, VarPtr(cbSID_Owner), 0&, VarPtr(cbPrimGrp)) Then
If IsValidSecurityDescriptor(VarPtr(AbsSD(0))) Then
'making default ACE descriptions
Dim Ace_Explicit() As EXPLICIT_ACCESS
Ace_Explicit = Make_Default_Ace_Explicit(lHive, KeyName)
' 'appending it with revoking ACE descriptions of those SIDs who currently has denied access rights on SD
'
' 'LookupSecurityDescriptorParts (if need SACL / DACL)
'
' If ERROR_SUCCESS = GetExplicitEntriesFromAcl(VarPtr(oldDACL(0)), cExplicit, pListExplicit) Then
'
' For i = 0 To cExplicit - 1
'
' memcpy Revoke_Ace_Explicit, ByVal (pListExplicit + LenB(Revoke_Ace_Explicit) * i), LenB(Revoke_Ace_Explicit)
'
' If Revoke_Ace_Explicit.grfAccessMode = DENY_ACCESS Then
'
' Revoke_Ace_Explicit.grfAccessMode = GRANT_ACCESS ' REVOKE_ACCESS
'
' ' rebuild array into consistent order
' Call Add_Ace_Explicit(Ace_Explicit(), Revoke_Ace_Explicit)
'
' End If
' Next
'
' LocalFree pListExplicit
' End If
'appending it with revoking ACE descriptions of those SIDs who currently has denied access rights on SD
If cbDACL > 0 Then
'has DACL
If GetAclInformation(VarPtr(oldDACL(0)), VarPtr(AclInfo), LenB(AclInfo), AclSizeInformation) Then
For i = AclInfo.AceCount - 1 To 0 Step -1
If GetAce(VarPtr(oldDACL(0)), i, pAce) Then
memcpy AceHead, ByVal pAce, LenB(AceHead) ' ((ACE_HEADER) pAce) -> AceType
If AceHead.AceType = ACCESS_DENIED_ACE_TYPE Then
lret = DeleteAce(VarPtr(oldDACL(0)), i)
'old routine - revoking access (but SetEntriesInAcl doesn't support it for ACCESS_DENIED_ACE type)
' 'memcpy AceDenied, ByVal pAce, LenB(AceDenied)
' 'Debug.Print AceDenied.SidStart
'
' 'SidStart contains first DWORD of SID buffer.
' 'Rest part is stored directly behind the structure.
' 'So, ptr to SidStart can be used (its offset = 0x8)
'
' With Revoke_Ace_Explicit
'
' .grfAccessPermissions = GENERIC_ALL
' .grfAccessMode = REVOKE_ACCESS ' SET_ACCESS
' .grfInheritance = OBJECT_INHERIT_ACE Or CONTAINER_INHERIT_ACE
' With .tTrustee
' .TrusteeForm = TRUSTEE_IS_SID
' .TrusteeType = TRUSTEE_IS_UNKNOWN
' .ptstrName = pAce + 8&
' End With
' End With
'
' ' rebuild array into consistent order
' Call Add_Ace_Explicit(Ace_Explicit(), Revoke_Ace_Explicit)
End If
End If
Next
End If
End If
lret = -1
If cbDACL = 0 Then
pAcl = CreateEmptyACL(Ace_Explicit)
Else
pAcl = VarPtr(oldDACL(0))
End If
'merging ACE descriptions with existed DACL
If IsValidAcl(pAcl) Then
lret = SetEntriesInAcl(UBound(Ace_Explicit) + 1, VarPtr(Ace_Explicit(0)), pAcl, pNewDacl)
End If
If cbDACL = 0 Then LocalFree pAcl
If cbDACL > 0 And ERROR_SUCCESS <> lret Then
'for instance, not enough quota -> making DACL from default ACE_EXPLICIT
pAcl = CreateEmptyACL(Ace_Explicit)
If IsValidAcl(pAcl) Then
lret = SetEntriesInAcl(UBound(Ace_Explicit) + 1, VarPtr(Ace_Explicit(0)), pAcl, pNewDacl)
End If
LocalFree pAcl
End If
If ERROR_SUCCESS = lret Then
'apply it
' If ERROR_SUCCESS = SetNamedSecurityInfo( _
' StrPtr(ConvertHiveHandleToSeObjectName(lHive) & "\" & KeyName), _
' SE_REGISTRY_KEY, DACL_SECURITY_INFORMATION, 0&, 0&, pNewDacl, 0&) Then
'
' Debug.Print "Permissions granted successfully."
' End If
'x64 support
'+ protected DACL (prevent DACL to inherite ACEs from parent)
'IIf(bUseWow64 And isWin64(), SE_REGISTRY_WOW64_32KEY, SE_REGISTRY_KEY)
If ERROR_SUCCESS = SetSecurityInfo(hKey, SE_REGISTRY_KEY, _
DACL_SECURITY_INFORMATION Or PROTECTED_DACL_SECURITY_INFORMATION Or UNPROTECTED_DACL_SECURITY_INFORMATION, _
0&, 0&, pNewDacl, 0&) Then
RegKeyResetDACL = True
Debug.Print KeyName & " - Permissions granted successfully."
If Recursive Then
'This 'tree' function produces some strange ACEs with duplicate records of inheritance from parent objects.
'Besides, inherintance affects by grand parent objects too that may be not a part of fix.
'For instance, it may cause apllying denied access from much higher group like well-known SID 'All'.
'As a result, access to any objects still be denied.
'
'So, TreeResetNamedSecurityInfo is not an option here, I guess.
'And explicit ACE with manual enumaration of all subkeys must be much better decision.
'It will be compatible with 64-keys too.
' 'WOW64_64 not supported
' lret = TreeResetNamedSecurityInfo( _
' StrPtr(ConvertHiveHandleToSeObjectName(lHive) & "\" & KeyName), _
' SE_REGISTRY_KEY, DACL_SECURITY_INFORMATION, 0&, 0&, pNewDacl, 0&, CLng(False), 0&, ProgressInvokeNever, 0&)
'
' If lret = ERROR_SUCCESS Then
' Debug.Print "Permissions on tree granted successfully."
' RegKeyResetDACL = True
' Else
' RegKeyResetDACL = False
' End If
'Ùå íå âìåðëà Óêðà¿íà :)
'Let all USA people involved in our genocide, will die, slowly and horribly. Good bye, Obama democracy.
If RegOpenKeyEx(lHive, StrPtr(KeyName), 0&, KEY_ENUMERATE_SUB_KEYS Or (bIsWOW64 And KEY_WOW64_64KEY And Not bUseWow64), hKeyEnum) = ERROR_SUCCESS Then
sSubKeyName = String$(MAX_KEYNAME, vbNullChar)
i = 0
Do While RegEnumKeyEx(hKeyEnum, i, StrPtr(sSubKeyName), MAX_KEYNAME, 0&, 0&, 0&, ByVal 0&) = ERROR_SUCCESS
sSubKeyName = Left$(sSubKeyName, lstrlen(StrPtr(sSubKeyName)))
RegKeyResetDACL lHive, KeyName & IIf(0 <> Len(KeyName), "\", vbNullString) & sSubKeyName, bUseWow64, True
sSubKeyName = String$(MAX_KEYNAME, vbNullChar)
i = i + 1
Loop
RegCloseKey hKeyEnum
End If
End If
End If
LocalFree pNewDacl
End If
End If
End If
End If
End If
RegCloseKey hKey
End If
If Not RegKeyResetDACL Then Debug.Print KeyName & " - Failed to grant permissions!"
Exit Function
ErrorHandler:
Debug.Print "Error in SetDACL", Err, Err.Description
End Function
'returns ptr to new ACL
'size of ACL calculated from array of EXPLICIT_ACCESS
Function CreateEmptyACL(Ace_Explicit() As EXPLICIT_ACCESS) As Long
Dim pAcl As Long
Dim cbACL As Long
Dim Num_of_ACEs As Long
Dim i As Long
Num_of_ACEs = UBound(Ace_Explicit) + 1
cbACL = 8& + (12& * Num_of_ACEs) 'sizeof(ACL) + (NUM_OF_ACES * sizeof(ACCESS_ALLOWED_ACE))
For i = 0 To Num_of_ACEs - 1
If IsValidSid(Ace_Explicit(i).tTrustee.ptstrName) Then
cbACL = cbACL + GetLengthSid(Ace_Explicit(i).tTrustee.ptstrName) - 4& ' - sizeof(DWORD)
End If
Next
'Align cbAcl to a DWORD
cbACL = (cbACL + 3) And &HFFFFFFFC ' 3 = sizeof(DWORD) - 1)