-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateObjectTree.cpp
919 lines (781 loc) · 27.9 KB
/
createObjectTree.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
#include <Windows.h>
#include <dsgetdc.h>
#include <winldap.h>
#include <winber.h>
#include <LM.h>
#include <authz.h>
#include <string>
#include <memory>
#include <vector>
#include <map>
#include <algorithm>
#include <functional>
#include <iostream>
#include <format>
#include <sstream>
#include <regex>
#include <iomanip>
#include <cwctype>
#pragma comment(lib, "Activeds.lib")
#pragma comment(lib, "adsiid.lib")
#pragma comment(lib, "Authz.lib")
#pragma comment(lib, "NetApi32.lib")
#pragma comment(lib, "Wldap32.lib")
//****************************************************************************************************
using _ldap_msgfree = decltype([](LDAPMessage* value){ ldap_msgfree(value); });
using _ldap_memfree = decltype([](PWCHAR value){ ldap_memfreeW(value); });
using _ldap_value_free = decltype([](PWCHAR* value){ ldap_value_freeW(value); });
using _ldap_value_free_len = decltype([](berval** value){ ldap_value_free_len(value); });
struct _ldap_search_abandon_page
{
PLDAP _ldap_handle;
void operator()(PLDAPSearch value) const noexcept { ldap_search_abandon_page(_ldap_handle, value); };
};
using _NetApiBufferFree = decltype([](PDOMAIN_CONTROLLER_INFOW value){ NetApiBufferFree(value); });
using _ber_free = decltype([](BerElement* value){ ber_free(value, 0); });
using _ber_free_1 = decltype([](BerElement* value){ ber_free(value, 1); });
using _ber_bvfree = decltype([](berval* value){ ber_bvfree(value); });
//****************************************************************************************************
std::shared_ptr<LDAP> ldap_init_connection()
{
#pragma region Initialize connection with LDAP server
#pragma region Get information about LDAP server name
std::unique_ptr<DOMAIN_CONTROLLER_INFOW, _NetApiBufferFree> inflow;
auto result = DsGetDcNameW(nullptr, nullptr, nullptr, nullptr, DS_ONLY_LDAP_NEEDED | DS_RETURN_DNS_NAME, std::out_ptr(inflow));
if(nullptr == inflow)
throw std::exception("Error in DsGetDcNameW");
#pragma endregion
#pragma region Initialize TLS connection with LDAP server
std::shared_ptr<LDAP> ldap_handle{ ldap_initW(&inflow->DomainControllerName[2], LDAP_PORT), [](LDAP* value){ ldap_unbind(value); } };
if(nullptr == ldap_handle)
throw std::exception("Error during ldap_sslinit");
#pragma region Set additional options for LDAP connection
if(ldap_set_optionW(ldap_handle.get(), LDAP_OPT_AREC_EXCLUSIVE, LDAP_OPT_ON))
throw std::exception("Cannot set LDAP_OPT_AREC_EXCLUSIVE option");
ULONG ldap_version = LDAP_VERSION3;
if(ldap_set_optionW(ldap_handle.get(), LDAP_OPT_PROTOCOL_VERSION, &ldap_version))
throw std::exception("Cannot set LDAP_OPT_PROTOCOL_VERSION option");
#pragma endregion
if(ldap_bind_sW(ldap_handle.get(), nullptr, nullptr, LDAP_AUTH_NEGOTIATE))
throw std::exception("Cannot bind to LDAP");
#pragma endregion
#pragma endregion
return ldap_handle;
}
//****************************************************************************************************
std::map<std::wstring, std::wstring> ldap_search_contexts(std::shared_ptr<LDAP> handle)
{
#pragma region Initial variables
std::map<std::wstring, std::wstring> result;
#pragma endregion
#pragma region Search for all namingContexts
const wchar_t* namingContexts[] = {
L"namingContexts",
nullptr
};
std::unique_ptr<LDAPMessage, _ldap_msgfree> namingContextsMessage;
if(ldap_search_sW(handle.get(), (wchar_t*)L"", LDAP_SCOPE_BASE, (wchar_t*)L"(objectCategory=*)", (wchar_t**)namingContexts, FALSE, std::out_ptr(namingContextsMessage)))
throw std::exception("Cannot make ldap_search_s");
#pragma endregion
#pragma region Get values from the search
auto namingContextsEntry = ldap_first_entry(handle.get(), namingContextsMessage.get());
if(nullptr == namingContextsEntry)
throw std::exception("No entries in result search");
std::unique_ptr<BerElement, _ber_free> namingContextsBER;
std::unique_ptr<wchar_t, _ldap_memfree> namingContextsAttribute{ ldap_first_attributeW(handle.get(), namingContextsEntry, std::out_ptr(namingContextsBER)) };
if(nullptr == namingContextsAttribute)
throw std::exception("Cannot get first attributes for the search");
std::unique_ptr<wchar_t* [], _ldap_value_free> namingContextsValues{ ldap_get_valuesW(handle.get(), namingContextsEntry, namingContextsAttribute.get()) };
if(nullptr == namingContextsValues)
throw std::exception("Cannot get values from attribute");
auto namingContextsValuesCount = ldap_count_valuesW(namingContextsValues.get());
#pragma endregion
#pragma region Find correct DN names for necessary namespaces
std::wstring config_ns;
std::wstring schema_ns;
for(ULONG i = 0; i < namingContextsValuesCount; i++)
{
std::wstring value{ namingContextsValues[i] };
std::transform(value.begin(), value.end(), value.begin(), std::towlower);
if(value.find(L"cn=configuration") == 0)
config_ns = std::move(value);
else
{
if(value.find(L"cn=schema") == 0)
schema_ns = std::move(value);
else
result[L""] = value;
}
if(config_ns.size() && schema_ns.size())
break;
}
if(!(config_ns.size() && schema_ns.size()))
throw std::exception("Cannot find Configuration or Schema contexts");
#pragma endregion
#pragma region Set necessary values to result map
result[L"config"] = config_ns;
result[L"schema"] = schema_ns;
#pragma endregion
return result;
}
//****************************************************************************************************
std::wstring string_to_wstring(std::string value)
{
size_t size = value.size() + 1;
std::wstring result(size, L'\0');
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, result.data(), size, value.data(), _TRUNCATE);
return result;
}
//****************************************************************************************************
void ldap_search_element_ext(
std::shared_ptr<LDAP> handle,
std::wstring dn,
std::vector<std::wstring> attributes,
std::function<void(std::shared_ptr<LDAP>, LDAPMessage*)> func,
std::wstring_view filter = L"objectClass=*"
)
{
#pragma region Initial variables
unsigned long errorCode = LDAP_SUCCESS;
SECURITY_INFORMATION info =
OWNER_SECURITY_INFORMATION
| GROUP_SECURITY_INFORMATION
| DACL_SECURITY_INFORMATION
//| SACL_SECURITY_INFORMATION
//| LABEL_SECURITY_INFORMATION
//| ATTRIBUTE_SECURITY_INFORMATION
//| SCOPE_SECURITY_INFORMATION
//| PROCESS_TRUST_LABEL_SECURITY_INFORMATION
//| ACCESS_FILTER_SECURITY_INFORMATION
//| BACKUP_SECURITY_INFORMATION
//| PROTECTED_DACL_SECURITY_INFORMATION
//| PROTECTED_SACL_SECURITY_INFORMATION
//| UNPROTECTED_DACL_SECURITY_INFORMATION
//| UNPROTECTED_SACL_SECURITY_INFORMATION
;
std::unique_ptr <berval, _ber_bvfree> pBerVal;
std::unique_ptr<BerElement, _ber_free_1> pBerElmt{ ber_alloc_t(LBER_USE_DER) };
ber_printf(pBerElmt.get(), (PSTR)"{i}", info);
ber_flatten(pBerElmt.get(), std::out_ptr(pBerVal));
LDAPControlW infoControl =
{
(PWCHAR)L"1.2.840.113556.1.4.801", // LDAP_SERVER_SD_FLAGS_OID_W,
*pBerVal,
TRUE
};
PLDAPControlW ServerControls[2] =
{
&infoControl,
nullptr
};
#pragma endregion
#pragma region Create correct values for LDAP search
std::vector<wchar_t*> schemaAttrs;
std::for_each(attributes.begin(), attributes.end(), [&schemaAttrs](std::wstring& value){ schemaAttrs.push_back(value.data()); });
schemaAttrs.push_back(nullptr);
#pragma endregion
#pragma region Perform LDAP search
std::unique_ptr<LDAPMessage, _ldap_msgfree> search_message;
errorCode = ldap_search_ext_sW(
handle.get(),
(PWSTR)dn.data(),
LDAP_SCOPE_BASE,
(PWSTR)filter.data(),
(PZPWSTR)schemaAttrs.data(),
FALSE,
ServerControls,
nullptr,
nullptr,
1000000,
std::out_ptr(search_message)
);
if(search_message)
{
auto search_entry = ldap_first_entry(handle.get(), search_message.get());
func(handle, search_entry);
}
#pragma endregion
}
//****************************************************************************************************
void ldap_search_tree(
std::shared_ptr<LDAP> handle,
std::wstring dn,
std::vector<std::wstring> attributes,
std::function<void(std::shared_ptr<LDAP>, LDAPMessage*)> func,
std::wstring_view filter = L"objectClass=*"
)
{
#pragma region Initial variables
LDAP_TIMEVAL tm{ .tv_sec = 1000, .tv_usec = 1000 };
unsigned long pageSize = 100;
unsigned long pageTimeLimit = 100000;
unsigned long entryCount;
unsigned long errorCode = LDAP_SUCCESS;
#pragma endregion
#pragma region Create correct values for LDAP search
std::vector<wchar_t*> schemaAttrs;
std::for_each(attributes.begin(), attributes.end(), [&schemaAttrs](std::wstring& value){ schemaAttrs.push_back(value.data()); });
schemaAttrs.push_back(nullptr);
#pragma endregion
#pragma region Perform main search
std::unique_ptr<LDAPSearch, decltype(_ldap_search_abandon_page(handle.get()))> page_handle{
ldap_search_init_pageW(
handle.get(),
dn.data(),
LDAP_SCOPE_SUBTREE,
(PWSTR)filter.data(),
(PZPWSTR)schemaAttrs.data(),
0,
nullptr,
nullptr,
pageTimeLimit,
pageSize,
nullptr
)
};
do
{
std::unique_ptr<LDAPMessage, _ldap_msgfree> search_message;
errorCode = ldap_get_next_page_s(
handle.get(),
page_handle.get(),
&tm,
pageSize,
&entryCount,
std::out_ptr(search_message)
);
if(search_message)
{
auto search_entry = ldap_first_entry(handle.get(), search_message.get());
while(nullptr != search_entry)
{
func(handle, search_entry);
search_entry = ldap_next_entry(handle.get(), search_entry);
}
}
} while(errorCode == LDAP_SUCCESS);
#pragma endregion
}
//****************************************************************************************************
void ldap_search_element(
std::shared_ptr<LDAP> handle,
std::wstring dn,
std::vector<std::wstring> attributes,
std::function<void(std::shared_ptr<LDAP>, LDAPMessage*)> func,
std::wstring_view filter = L"objectClass=*"
)
{
#pragma region Initial variables
unsigned long errorCode = LDAP_SUCCESS;
#pragma endregion
#pragma region Create correct values for LDAP search
std::vector<wchar_t*> schemaAttrs;
std::for_each(attributes.begin(), attributes.end(), [&schemaAttrs](std::wstring& value){ schemaAttrs.push_back(value.data()); });
schemaAttrs.push_back(nullptr);
#pragma endregion
#pragma region Perform LDAP search
std::unique_ptr<LDAPMessage, _ldap_msgfree> search_message;
errorCode = ldap_search_sW(
handle.get(),
(PWSTR)dn.data(),
LDAP_SCOPE_BASE,
(PWSTR)filter.data(),
(PZPWSTR)schemaAttrs.data(),
FALSE,
std::out_ptr(search_message)
);
if(search_message)
{
auto search_entry = ldap_first_entry(handle.get(), search_message.get());
func(handle, search_entry);
}
#pragma endregion
}
//****************************************************************************************************
std::vector<unsigned char> from_hex_codes(std::string value)
{
std::vector<unsigned char> result;
std::stringstream stream(value);
stream >> std::hex;
std::copy(std::istream_iterator<int>(stream), std::istream_iterator<int>(), std::back_inserter(result));
return result;
}
//****************************************************************************************************
std::vector<byte> string_to_guid(std::string value)
{
#pragma region Initial variables
std::stringstream stream;
std::regex regex("([0-9a-fA-F]{8})-{0,1}([0-9a-fA-F]{4})-{0,1}([0-9a-fA-F]{4})-{0,1}([0-9a-fA-F]{4})-{0,1}([0-9a-fA-F]{12})");
std::match_results<std::string::const_iterator> match;
#pragma endregion
#pragma region Check input string format
if(false == std::regex_match(value, match, regex))
throw std::exception("Invalid format of the input string");
#pragma endregion
#pragma region Parse input string
for(size_t i = 1; i < 6; i++)
{
size_t index = 0;
std::string value = match[i];
std::vector<std::string> chunks(value.size() >> 1, std::string{ 2, ' ' });
for(auto j = value.begin(); j != value.end(); j += 2)
std::copy_n(j, 2, chunks[index++].begin());
if(i < 4)
std::reverse(chunks.begin(), chunks.end());
std::copy(chunks.begin(), chunks.end(), std::ostream_iterator<std::string, char>(stream, " "));
}
#pragma endregion
#pragma region Convert string to binary format
return from_hex_codes(stream.str());
#pragma endregion
}
//****************************************************************************************************
std::wstring hex_codes(std::vector<byte> value)
{
std::wstringstream stream;
stream << std::setfill(L'0') << std::hex;
std::for_each(value.begin(), value.end(), [&stream](byte element){ stream << std::setw(2) << (int)element; });
return stream.str();
}
//****************************************************************************************************
std::wstring guid_to_string(std::vector<byte> value)
{
#pragma region Initial variables
std::wstring hex = hex_codes(value);
std::wstringstream stream;
std::wregex regex(L"([0-9a-fA-F]{8})-{0,1}([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{12})");
std::match_results<std::wstring::const_iterator> match;
#pragma endregion
#pragma region Check input string format
if(false == std::regex_match(hex, match, regex))
throw std::exception("GUID: invalid Value");
#pragma endregion
#pragma region Parse input string
for(size_t i = 1; i < 6; i++)
{
size_t index = 0;
std::wstring value = match[i];
std::vector<std::wstring> chunks(value.size() >> 1, std::wstring{ 2, L' ' });
for(auto j = value.begin(); j != value.end(); j += 2)
std::copy_n(j, 2, chunks[index++].begin());
if(i < 4)
std::reverse(chunks.begin(), chunks.end());
if((size_t)stream.tellp())
stream << L"-";
std::copy(chunks.begin(), chunks.end(), std::ostream_iterator<std::wstring, wchar_t>(stream));
}
#pragma endregion
return stream.str();
}
//****************************************************************************************************
struct class_info
{
std::wstring className;
std::wstring classDName;
std::vector<byte> classGUID;
std::vector<std::wstring> classAttributes;
};
//****************************************************************************************************
void check()
{
std::wstring target_dn = L"CN=Administrator,CN=Users,DC=WDOMAIN,DC=LAN";
#pragma region Aux "using" declarations
using valuesPtr = std::unique_ptr<berval* [], _ldap_value_free_len>;
#pragma endregion
#pragma region Initial variables
std::vector<byte> majorClassGUID;
std::vector<byte> majorClassSecurityDescriptor;
std::vector<std::wstring> classes;
std::wstring majorClassName;
std::wstring majorClassDName;
std::vector<class_info> classInfos;
std::map<std::wstring, std::vector<byte>> attributes;
std::map<std::vector<byte>, std::vector<std::vector<byte>>> propertySets;
std::vector<std::vector<byte>> freeProperties;
std::map<std::vector<byte>, int> extendedRights;
std::vector<std::wstring> childClassesNames;
std::vector<std::vector<byte>> childClassesGUIDs;
#pragma endregion
#pragma region Initialize connection to LDAP server
auto handle = ldap_init_connection();
#pragma endregion
#pragma region Find all namespace contexts
auto contexts = ldap_search_contexts(handle);
#pragma endregion
#pragma region Find major class for particular object instance
auto classFind = [&classes, contexts, &majorClassSecurityDescriptor](std::shared_ptr<LDAP> handle, LDAPMessage* entry)
{
#pragma region Initial variables
ULONG i = 0;
#pragma endregion
valuesPtr objectClassValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"objectClass") };
if(!objectClassValues)
throw std::exception("Cannot get objectClass values from DN");
#pragma region Major class name would be very least in the array
while(objectClassValues[i] != nullptr)
classes.emplace_back(string_to_wstring(objectClassValues[i++]->bv_val));
if(!classes.size())
throw std::exception("Cannot find major class name");
#pragma endregion
#pragma region Find object class name
valuesPtr nTSecurityDescriptorValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"nTSecurityDescriptor") };
if(!nTSecurityDescriptorValues)
throw std::exception("Cannot get nTSecurityDescriptor for target DN");
majorClassSecurityDescriptor.insert(
majorClassSecurityDescriptor.end(),
nTSecurityDescriptorValues[0]->bv_val,
nTSecurityDescriptorValues[0]->bv_val + nTSecurityDescriptorValues[0]->bv_len
);
#pragma endregion
};
ldap_search_element_ext(
handle,
target_dn,
{ L"objectClass", L"nTSecurityDescriptor" },
classFind
);
majorClassName = classes[classes.size() - 1];
#pragma endregion
#pragma region Find other necessary information for all classes
auto classInfosFind = [&majorClassGUID, &classes, &classInfos, majorClassName, &majorClassDName](std::shared_ptr<LDAP> handle, LDAPMessage* entry)
{
#pragma region Find object class name
valuesPtr nameValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"lDAPDisplayName") };
if(!nameValues)
throw std::exception("Cannot get values for lDAPDisplayName");
#pragma endregion
#pragma region Find object class distinguished name
valuesPtr dNameValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"distinguishedName") };
if(!dNameValues)
throw std::exception("Cannot get values for distinguishedName");
#pragma endregion
#pragma region Find object GUID
valuesPtr guidValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"schemaIDGUID") };
if(!guidValues)
throw std::exception("Cannot get values for schemaIDGUID");
std::vector<byte> guid{ guidValues[0]->bv_val, guidValues[0]->bv_val + guidValues[0]->bv_len };
#pragma endregion
#pragma region Append all possible attributes of the class
std::vector<std::wstring> attributes;
valuesPtr mayContainValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"mayContain") };
if(mayContainValues)
{
ULONG i = 0;
while(mayContainValues[i] != nullptr)
attributes.emplace_back(string_to_wstring(mayContainValues[i++]->bv_val));
}
valuesPtr mustContainValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"mustContain") };
if(mustContainValues)
{
ULONG i = 0;
while(mustContainValues[i] != nullptr)
attributes.emplace_back(string_to_wstring(mustContainValues[i++]->bv_val));
}
valuesPtr systemMayContainValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"systemMayContain") };
if(systemMayContainValues)
{
ULONG i = 0;
while(systemMayContainValues[i] != nullptr)
attributes.emplace_back(string_to_wstring(systemMayContainValues[i++]->bv_val));
}
valuesPtr systemMustContainValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"systemMustContain") };
if(systemMustContainValues)
{
ULONG i = 0;
while(systemMustContainValues[i] != nullptr)
attributes.emplace_back(string_to_wstring(systemMustContainValues[i++]->bv_val));
}
#pragma endregion
#pragma region Append all aux classes to global class array
valuesPtr auxClassValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"auxiliaryClass") };
if(auxClassValues)
{
ULONG i = 0;
while(auxClassValues[i] != nullptr)
classes.emplace_back(string_to_wstring(auxClassValues[i++]->bv_val));
}
valuesPtr sysAuxClassValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"systemAuxiliaryClass") };
if(sysAuxClassValues)
{
ULONG i = 0;
while(sysAuxClassValues[i] != nullptr)
classes.emplace_back(string_to_wstring(sysAuxClassValues[i++]->bv_val));
}
#pragma endregion
auto className = string_to_wstring(nameValues[0]->bv_val);
auto classDName = string_to_wstring(dNameValues[0]->bv_val);
if(className == majorClassName)
{
majorClassDName = classDName;
majorClassGUID = guid;
}
classInfos.push_back({
.className = className,
.classDName = classDName,
.classGUID = guid,
.classAttributes = attributes
});
};
// Because inside lambda we updating primary array
// it is neccessary to use position indexes only
size_t pos = 0;
while(pos != classes.size())
{
ldap_search_tree(
handle,
contexts[L"schema"],
{
L"lDAPDisplayName",
L"distinguishedName",
L"schemaIDGUID",
L"mayContain",
L"mustContain",
L"systemMayContain",
L"systemMustContain",
L"auxiliaryClass",
L"systemAuxiliaryClass"
},
classInfosFind,
std::format(L"lDAPDisplayName={}", classes[pos])
);
pos++;
}
#pragma endregion
#pragma region Find child object classes
auto childFind = [&childClassesNames](std::shared_ptr<LDAP> handle, LDAPMessage* entry)
{
valuesPtr possibleInferiorsValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"possibleInferiors") };
if(possibleInferiorsValues)
{
ULONG i = 0;
while(possibleInferiorsValues[i] != nullptr)
childClassesNames.emplace_back(string_to_wstring(possibleInferiorsValues[i++]->bv_val));
}
};
ldap_search_element(
handle,
majorClassDName,
{ L"possibleInferiors" },
childFind
);
auto childGUIDFind = [&childClassesGUIDs](std::shared_ptr<LDAP> handle, LDAPMessage* entry)
{
valuesPtr schemaIDGUIDValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"schemaIDGUID") };
if(!schemaIDGUIDValues)
throw std::exception("Cannot find schemaIDGUIDValues");
childClassesGUIDs.push_back({ schemaIDGUIDValues[0]->bv_val, schemaIDGUIDValues[0]->bv_val + schemaIDGUIDValues[0]->bv_len });
};
for(auto&& element : childClassesNames)
{
ldap_search_tree(
handle,
contexts[L"schema"],
{
L"lDAPDisplayName",
L"schemaIDGUID"
},
childGUIDFind,
std::format(L"lDAPDisplayName={}", element)
);
}
#pragma endregion
#pragma region Find information about all attributes
for(auto&& element : classInfos)
{
for(auto&& attribute : element.classAttributes)
{
std::transform(attribute.begin(), attribute.end(), attribute.begin(), tolower);
attributes[attribute] = {};
}
}
auto attributesFind = [&attributes, &propertySets, &freeProperties](std::shared_ptr<LDAP> handle, LDAPMessage* entry)
{
#pragma region Find name of the attribute
valuesPtr nameValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"lDAPDisplayName") };
if(!nameValues)
throw std::exception("Cannot get values for lDAPDisplayName");
std::wstring name = string_to_wstring(nameValues[0]->bv_val);
std::transform(name.begin(), name.end(), name.begin(), tolower);
#pragma endregion
#pragma region Find object GUID
valuesPtr guidValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"schemaIDGUID") };
if(!guidValues)
throw std::exception("Cannot get values for schemaIDGUID");
std::vector<byte> guid{ guidValues[0]->bv_val, guidValues[0]->bv_val + guidValues[0]->bv_len };
#pragma endregion
#pragma region Find information about property set which includes the attribute
valuesPtr attributeSecurityGUID{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"attributeSecurityGUID") };
if(attributeSecurityGUID)
{
std::vector<byte> secguid{ attributeSecurityGUID[0]->bv_val, attributeSecurityGUID[0]->bv_val + attributeSecurityGUID[0]->bv_len };
if(propertySets.contains(secguid))
propertySets[secguid].push_back(guid);
else
propertySets[secguid] = { guid };
}
else
freeProperties.push_back(guid);
#pragma endregion
attributes[name] = guid;
};
for(auto&& [key, value] : attributes)
{
ldap_search_tree(
handle,
contexts[L"schema"],
{
L"lDAPDisplayName",
L"attributeSecurityGUID",
L"schemaIDGUID"
},
attributesFind,
std::format(L"lDAPDisplayName={}", key)
);
}
#pragma endregion
#pragma region Find information about all applicable extended rights
auto rightsFind = [&propertySets, &extendedRights](std::shared_ptr<LDAP> handle, LDAPMessage* entry)
{
#pragma region Find object GUID
valuesPtr guidValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"rightsGuid") };
if(!guidValues)
throw std::exception("Cannot get values for rightsGuid");
std::string guid_str = guidValues[0]->bv_val;
std::transform(guid_str.begin(), guid_str.end(), guid_str.begin(), tolower);
std::vector<byte> guid = string_to_guid(guid_str);
#pragma endregion
#pragma region Get validAccesses value
valuesPtr validAccessesValues{ ldap_get_values_lenW(handle.get(), entry, (PWSTR)L"validAccesses") };
if(!validAccessesValues)
throw std::exception("Cannot get values for validAccesses");
auto validAccesses = std::stoi(validAccessesValues[0]->bv_val);
#pragma endregion
#pragma region Check what type of entry do we have
if(validAccesses & (0x00000010 | 0x00000020)) // Should be property set
{
// Could be that property set has no entries
if(!propertySets.contains(guid))
propertySets[guid] = {};
}
else // Other types: control rights or validated writes
extendedRights[guid] = 1;
#pragma endregion
};
for(auto&& element : classInfos)
{
ldap_search_tree(
handle,
contexts[L"config"],
{
L"rightsGuid",
L"validAccesses"
},
rightsFind,
std::format(L"(&(objectClass=controlAccessRight)(appliesTo={}))", guid_to_string(element.classGUID))
);
}
#pragma endregion
#pragma region Build final object type list
std::vector<OBJECT_TYPE_LIST> object_list;
#pragma region Major class GUID is on top of the list
object_list.push_back({ ACCESS_OBJECT_GUID, 0, (GUID*)majorClassGUID.data() });
#pragma endregion
#pragma region Add all extended rights first
for(auto&& element : extendedRights)
object_list.push_back({ ACCESS_PROPERTY_SET_GUID, 0, (GUID*)element.first.data() });
#pragma endregion
#pragma region Add all property sets
for(auto&& element : propertySets)
{
object_list.push_back({ ACCESS_PROPERTY_SET_GUID, 0, (GUID*)element.first.data() });
for(auto&& prop : element.second)
object_list.push_back({ ACCESS_PROPERTY_GUID, 0, (GUID*)prop.data() });
}
#pragma endregion
#pragma region Add all properties which are not in any property set
for(auto&& element : freeProperties)
object_list.push_back({ ACCESS_PROPERTY_SET_GUID, 0, (GUID*)element.data() });
#pragma endregion
#pragma region Add all child classes
for(auto&& element : childClassesGUIDs)
object_list.push_back({ ACCESS_PROPERTY_SET_GUID, 0, (GUID*)element.data() });
#pragma endregion
#pragma endregion
#pragma region Perform access checking
HANDLE token;
if(!OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &token))
throw std::exception("Cannot get process token");
HANDLE dup_token;
if(FALSE == DuplicateTokenEx(token, MAXIMUM_ALLOWED, nullptr, SecurityDelegation, TokenImpersonation, &dup_token))
throw std::exception("Cannot duplicate token");
size_t size = object_list.size();
std::vector<DWORD> granted_access(size, 0);
std::vector<DWORD> access_status(size, 0);
std::vector<DWORD> sacl_eval_results(size, 0);
std::vector<DWORD> errors(size, 0);
PSID sid_self = NULL;
AUTHZ_RESOURCE_MANAGER_HANDLE AuthzResMgrHandle = NULL;
AUTHZ_CLIENT_CONTEXT_HANDLE AuthzClientHandle = NULL;
LUID ZeroLuid = { 0, 0 };
AUTHZ_ACCESS_REQUEST AccessRequest = {
MAXIMUM_ALLOWED,
sid_self,
object_list.data(),
(DWORD)object_list.size(),
NULL
};
AUTHZ_ACCESS_REPLY AccessReply = { 0 };
AccessReply.ResultListLength = (DWORD)object_list.size();
AccessReply.GrantedAccessMask = granted_access.data();
AccessReply.SaclEvaluationResults = sacl_eval_results.data();
AccessReply.Error = errors.data();
if(!AuthzInitializeResourceManager(
AUTHZ_RM_FLAG_NO_AUDIT,
NULL,
NULL,
NULL,
NULL,
&AuthzResMgrHandle
))
{
throw std::exception("AuthzInitializeResourceManager: error");
}
if(!AuthzInitializeContextFromToken(
0,
dup_token,
AuthzResMgrHandle,
NULL,
ZeroLuid,
NULL,
&AuthzClientHandle
))
{
throw std::exception("AuthzInitializeContextFromToken: error");
}
auto check_result = AuthzAccessCheck(
0,
AuthzClientHandle,
&AccessRequest,
NULL,
(PSECURITY_DESCRIPTOR)majorClassSecurityDescriptor.data(),
NULL,
0,
&AccessReply,
NULL
);
if(FALSE == check_result)
{
std::stringstream stream;
stream << "AccessCheck: error during a check, #" << GetLastError() << "\n";
throw std::exception(stream.str().c_str());
}
#pragma endregion
}
//****************************************************************************************************
int main()
{
check();
return 0;
}
//****************************************************************************************************