-
Notifications
You must be signed in to change notification settings - Fork 1
/
sddlparse.go
819 lines (758 loc) · 18.9 KB
/
sddlparse.go
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
package sddlparse
import (
"encoding/base64"
"encoding/binary"
"errors"
)
// ACE is an Access Control Entry.
// This is a simplified version of the Windows ACE structure.
//
// This struct contains all fields for all the covered ACE types.
// Only the fields for the resolved ACE type will be populated.
type ACE struct {
// Common fields
Type AceType
Flags AceFlag
AccessMask AccessMask
SID string
// Object-specific fields
ObjectType GUID
InheritedObjectType GUID
ObjectFlags ObjectFlag
// Callback-specific fields
ApplicationData []byte
// System-resource-specific fields
AttributeData []byte
}
// String returns the ACE as a string.
// Shortcut for MustString, returning an empty string on error.
func (ace *ACE) String() string {
if str, err := ace.MustString(); err == nil {
return str
} else {
return ""
}
}
// MustString returns the ACE as a string.
// Returns an error if the ACE has unhandled elements.
func (ace *ACE) MustString() (string, error) {
str, err := ace.Type.MustString()
if err != nil {
return "", err
}
str += ";"
flags, err := ace.Flags.MustString()
if err != nil {
return "", err
}
str += flags
str += ";"
mask, err := ace.AccessMask.MustString()
if err != nil {
return "", err
}
str += mask
str += ";"
if !ace.ObjectType.IsNull() {
str += ace.ObjectType.String()
}
str += ";"
if !ace.InheritedObjectType.IsNull() {
str += ace.InheritedObjectType.String()
}
str += ";"
str += sidToSDDLAlias(ace.SID)
return "(" + str + ")", nil
}
// ACL represents a Windows ACL.
// Simplified to a slice of ACEs.
type ACL []*ACE
// SDDL represents a Windows SDDL string.
// This is a subset of the SDDL format, only containing the fields we care about.
//
// See https://docs.microsoft.com/en-us/windows/win32/secauthz/security-descriptor-string-format for more information.
type SDDL struct {
Version byte
ControlFlags uint16
// Owner and Group are SIDs as strings.
Owner string
Group string
SACL ACL
DACL ACL
}
// String returns the SDDL as a string.
// Shortcut for MustString, returning an empty string on error.
func (sddl *SDDL) String() string {
if str, err := sddl.MustString(); err == nil {
return str
} else {
return ""
}
}
// MustString returns the SDDL as a string.
// Returns an error if the SDDL has unhandled elements.
func (sddl *SDDL) MustString() (string, error) {
str := ""
if sddl.Owner != "" {
str += "O:" + sidToSDDLAlias(sddl.Owner)
}
if sddl.Group != "" {
str += "G:" + sidToSDDLAlias(sddl.Group)
}
/*
The security descriptor string format does not support NULL ACLs.
To denote an empty ACL, the security descriptor string includes the D: or S: token with no additional string information.
*/
if sddl.ControlFlags&SDDL_DACLPRESENT != 0 {
str += "D:"
}
if sddl.DACL != nil {
daclString := ""
if sddl.ControlFlags&SDDL_DACLPROTECTED != 0 {
daclString += "P"
}
if sddl.ControlFlags&SDDL_DACLINHERITANCEREQUIRED != 0 {
daclString += "AR"
}
if sddl.ControlFlags&SDDL_DACLAUTOINHERITED != 0 {
daclString += "AI"
}
for _, ace := range sddl.DACL {
aceString, err := ace.MustString()
if err != nil {
return "", err
}
daclString += aceString
}
str += daclString
}
if sddl.ControlFlags&SDDL_SACLPRESENT != 0 {
str += "S:"
}
if sddl.SACL != nil {
saclString := ""
if sddl.ControlFlags&SDDL_SACLPROTECTED != 0 {
saclString += "P"
}
if sddl.ControlFlags&SDDL_INHERITEANCEREQUIRED != 0 {
saclString += "AR"
}
if sddl.ControlFlags&SDDL_SACLAUTOINHERITED != 0 {
saclString += "AI"
}
for _, ace := range sddl.SACL {
aceString, err := ace.MustString()
if err != nil {
return "", err
}
saclString += aceString
}
str += saclString
}
return str, nil
}
func aclFromBytes(data []byte) (ACL, error) {
if len(data) < 8 {
return nil, errors.New(errInvalidACL)
}
revision := data[0]
if revision != 0x02 && revision != 0x04 {
return nil, errors.New(errInvalidACL)
}
if data[1] != 0x00 {
return nil, errors.New(errInvalidACL)
}
aclSize := binary.LittleEndian.Uint16(data[2:4])
aceCount := binary.LittleEndian.Uint16(data[4:6])
if binary.LittleEndian.Uint16(data[6:8]) != 0x00 {
return nil, errors.New(errInvalidACL)
}
if len(data) < int(aclSize) {
return nil, errors.New(errInvalidACL)
}
acl := make(ACL, aceCount)
tmp := data[8:]
for i := 0; i < int(aceCount); i++ {
ace, aceSize, err := aceFromBytes(tmp)
if err != nil {
return nil, err
}
acl[i] = ace
tmp = tmp[aceSize:]
}
return acl, nil
}
func aclToBytes(acl ACL) ([]byte, error) {
data := make([]byte, 8)
data[0] = 0x04
binary.LittleEndian.PutUint16(data[4:6], uint16(len(acl)))
totalSize := 0
for _, ace := range acl {
aceData, size, err := aceToBytes(ace)
if err != nil {
return nil, err
}
data = append(data, aceData...)
totalSize += int(size)
}
binary.LittleEndian.PutUint16(data[2:4], uint16(totalSize))
return data, nil
}
// SDDLFromBinary decodes a binary SDDL string.
// This expects the generic binary format Windows uses.
//
// Returns an error if the SDDL is invalid.
// The SDDL will be deemed invalid for any of the following reasons:
//
// - The SDDL is too short
// - The SDDL does not start with 0x01 0x00
// - The owner or group SID is invalid (if present)
// - The SACL or DACL is invalid (if present)
// - An unknown or unhandled ACE type is encountered
func SDDLFromBinary(data []byte) (*SDDL, error) {
if len(data) < 16 {
return nil, errors.New(errInvalidSDDL)
}
if data[0] != 0x01 {
return nil, errors.New(errInvalidSDDL)
}
if data[1] != 0x00 {
return nil, errors.New(errInvalidSDDL)
}
sddl := &SDDL{
Version: 0x1,
ControlFlags: binary.LittleEndian.Uint16(data[2:4]),
}
ownerOffset := binary.LittleEndian.Uint32(data[4:8])
groupOffset := binary.LittleEndian.Uint32(data[8:12])
saclOffset := binary.LittleEndian.Uint32(data[12:16])
daclOffset := binary.LittleEndian.Uint32(data[16:20])
if ownerOffset != 0 {
owner, _, err := sidFromLEBytes(data[ownerOffset:])
if err != nil {
return nil, err
}
sddl.Owner = owner
}
if groupOffset != 0 {
group, _, err := sidFromLEBytes(data[groupOffset:])
if err != nil {
return nil, err
}
sddl.Group = group
}
if saclOffset != 0 {
sacl, err := aclFromBytes(data[saclOffset:])
if err != nil {
return nil, err
}
sddl.SACL = sacl
}
if daclOffset != 0 {
dacl, err := aclFromBytes(data[daclOffset:])
if err != nil {
return nil, err
}
sddl.DACL = dacl
}
return sddl, nil
}
// SDDLFromBase64Encoded decodes a base64 encoded SDDL string.
// This is a shortcut for base64.StdEncoding.DecodeString and SDDLFromBinary.
//
// Returns an error if the base64 decoding fails or if the SDDL is invalid.
func SDDLFromBase64Encoded(data []byte) (*SDDL, error) {
b64, err := base64.StdEncoding.DecodeString(string(data))
if err != nil {
return nil, err
}
return SDDLFromBinary(b64)
}
// SDDLFromString decodes the standard SDDL string format.
//
// Domain and machine identity are used to resolve SID aliases to SIDs.
// If not provided, then SIDs will look like S-1-5--500.
//
// Machine identity is likely unnecessary for any active directory SDDLs,
// whereas domain identity is likely unnecessary for any local machine results.
func SDDLFromString(sddl, domainIdentity, machineIdentity string) (*SDDL, error) {
obj := &SDDL{}
datalen := len(sddl)
if datalen < 2 {
// Technically, I think this is valid, but it's not useful.
return nil, errors.New(errInvalidSDDL)
}
if sddl[0] == 'O' {
if datalen < 4 {
return nil, errors.New(errInvalidSDDL)
}
if sddl[1] != ':' {
return nil, errors.New(errInvalidSDDL)
}
if sddl[2:4] == "S-" {
sid := "S-"
i := 4
for i < datalen && ((sddl[i] >= '0' && sddl[i] <= '9') || sddl[i] == '-') {
sid += string(sddl[i])
i++
}
obj.Owner = sid
if i < datalen {
sddl = sddl[i:]
} else {
return obj, nil
}
} else {
sid, err := sddlAliasToSID(sddl[2:4], domainIdentity, machineIdentity)
if err != nil {
return nil, err
}
obj.Owner = sid
if datalen > 4 {
sddl = sddl[4:]
} else {
return obj, nil
}
}
}
datalen = len(sddl)
if datalen < 4 {
return nil, errors.New(errInvalidSDDL)
}
if sddl[0] == 'G' {
if sddl[1] != ':' {
return nil, errors.New(errInvalidSDDL)
}
if sddl[2:4] == "S-" {
sid := "S-"
i := 4
for i < datalen && ((sddl[i] >= '0' && sddl[i] <= '9') || sddl[i] == '-') {
sid += string(sddl[i])
i++
}
obj.Group = sid
if i < datalen {
sddl = sddl[i:]
} else {
return obj, nil
}
} else {
sid, err := sddlAliasToSID(sddl[2:4], domainIdentity, machineIdentity)
if err != nil {
return nil, err
}
obj.Group = sid
if datalen > 4 {
sddl = sddl[4:]
} else {
return obj, nil
}
}
}
datalen = len(sddl)
if datalen < 4 {
return nil, errors.New(errInvalidSDDL)
}
if sddl[0] == 'D' {
if sddl[1] != ':' {
return nil, errors.New(errInvalidSDDL)
}
obj.ControlFlags |= SDDL_DACLPRESENT
flags := 0
i := 2
for i < datalen && (sddl[i] == 'P' || sddl[i] == 'A' || sddl[i] == 'R' || sddl[i] == 'I') {
switch sddl[i] {
case 'P':
flags |= SDDL_DACLPROTECTED
case 'A':
if i+1 < datalen {
switch sddl[i+1] {
case 'R':
flags |= SDDL_DACLINHERITANCEREQUIRED
case 'I':
flags |= SDDL_DACLAUTOINHERITED
}
}
}
i++
}
obj.ControlFlags |= uint16(flags)
if i < datalen {
sddl = sddl[i:]
datalen = len(sddl)
} else {
return obj, nil
}
dacl, aclLen, err := aclFromString(sddl, domainIdentity, machineIdentity)
if err != nil {
return nil, err
}
obj.DACL = dacl
if datalen > aclLen {
sddl = sddl[aclLen:]
} else {
return obj, nil
}
}
datalen = len(sddl)
if datalen < 4 {
return nil, errors.New(errInvalidSDDL)
}
if sddl[0] == 'S' {
if sddl[1] != ':' {
return nil, errors.New(errInvalidSDDL)
}
obj.ControlFlags |= SDDL_SACLPRESENT
flags := 0
i := 2
for i < datalen && (sddl[i] == 'P' || sddl[i] == 'A' || sddl[i] == 'R' || sddl[i] == 'I') {
switch sddl[i] {
case 'P':
flags |= SDDL_SACLPROTECTED
case 'A':
if i+1 < datalen {
switch sddl[i+1] {
case 'R':
flags |= SDDL_INHERITEANCEREQUIRED
case 'I':
flags |= SDDL_SACLAUTOINHERITED
}
}
}
i++
}
obj.ControlFlags |= uint16(flags)
if i < datalen {
sddl = sddl[i:]
} else {
return obj, nil
}
sacl, aclLen, err := aclFromString(sddl, domainIdentity, machineIdentity)
if err != nil {
return nil, err
}
obj.SACL = sacl
if datalen > aclLen {
sddl = sddl[aclLen:]
} else {
return obj, nil
}
}
if len(sddl) > 0 {
return nil, errors.New(errInvalidSDDL)
}
return obj, nil
}
func aclFromString(sddl, domainIdentity, machineIdentity string) (ACL, int, error) {
var acl ACL
datalen := len(sddl)
if datalen < 8 {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[0] != '(' {
return nil, 0, errors.New(errInvalidACL)
}
inParen := true
i := 1
count := 0
for inParen {
count++
parsedLen := 0
if i+1 >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
curAce := &ACE{}
typeString := sddl[i : i+2]
extra := 1
switch typeString {
case "A;":
curAce.Type = ACETYPE_ACCESS_ALLOWED
extra = 0
case "D;":
curAce.Type = ACETYPE_ACCESS_DENIED
extra = 0
case "OA":
curAce.Type = ACETYPE_ACCESS_ALLOWED_OBJECT
case "OD":
curAce.Type = ACETYPE_ACCESS_DENIED_OBJECT
case "AU":
curAce.Type = ACETYPE_SYSTEM_AUDIT
case "AL":
curAce.Type = ACETYPE_SYSTEM_ALARM
case "AC":
curAce.Type = ACETYPE_ACCESS_ALLOWED_COMPOUND
case "OU":
curAce.Type = ACETYPE_SYSTEM_AUDIT_OBJECT
case "OL":
curAce.Type = ACETYPE_SYSTEM_ALARM_OBJECT
case "ML":
curAce.Type = ACETYPE_SYSTEM_MANDATORY_LABEL
case "XA":
curAce.Type = ACETYPE_ACCESS_ALLOWED_CALLBACK
case "XD":
curAce.Type = ACETYPE_ACCESS_DENIED_CALLBACK
case "RA":
curAce.Type = ACETYPE_SYSTEM_RESOURCE_ATTRIBUTE
case "SP":
curAce.Type = ACETYPE_SYSTEM_SCOPED_POLICY_ID
case "XU":
curAce.Type = ACETYPE_SYSTEM_AUDIT_CALLBACK
case "ZA":
curAce.Type = ACETYPE_ACCESS_ALLOWED_CALLBACK_OBJECT
default:
return nil, 0, errors.New(errInvalidACE)
}
parsedLen += 2 + extra
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen-1] != ';' {
return nil, 0, errors.New(errInvalidACL)
}
flags := AceFlag(0)
for i+parsedLen+1 < datalen && sddl[i+parsedLen] != ';' {
flagString := sddl[i+parsedLen : i+parsedLen+2]
switch flagString {
case "OI":
flags |= ACEFLAG_OBJECT_INHERIT
case "CI":
flags |= ACEFLAG_CONTAINER_INHERIT
case "NP":
flags |= ACEFLAG_NO_PROPAGATE_INHERIT
case "IO":
flags |= ACEFLAG_INHERIT_ONLY
case "ID":
flags |= ACEFLAG_INHERITED
case "SA":
flags |= ACEFLAG_SUCCESSFUL_ACCESS
case "FA":
flags |= ACEFLAG_FAILED_ACCESS
default:
return nil, 0, errors.New(errInvalidACE)
}
parsedLen += 2
}
curAce.Flags = flags
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen] != ';' {
return nil, 0, errors.New(errInvalidACL)
}
parsedLen++
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
accessMask := AccessMask(0)
for i+parsedLen+1 < datalen && sddl[i+parsedLen] != ';' {
maskString := sddl[i+parsedLen : i+parsedLen+2]
switch maskString {
case "GR":
accessMask |= ACCESS_MASK_GENERIC_READ
case "GW":
accessMask |= ACCESS_MASK_GENERIC_WRITE
case "GX":
accessMask |= ACCESS_MASK_GENERIC_EXECUTE
case "GA":
accessMask |= ACCESS_MASK_GENERIC_ALL
case "MA":
accessMask |= ACCESS_MASK_MAXIMUM_ALLOWED
case "AS":
accessMask |= ACCESS_MASK_ACCESS_SYSTEM_SECURITY
case "SY":
accessMask |= ACCESS_MASK_SYNCHRONIZE
case "WO":
accessMask |= ACCESS_MASK_WRITE_OWNER
case "WD":
accessMask |= ACCESS_MASK_WRITE_DACL
case "RC":
accessMask |= ACCESS_MASK_READ_CONTROL
case "DE":
accessMask |= ACCESS_MASK_DELETE
case "CC":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_CREATE_CHILD
case "DC":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_DELETE_CHILD
case "SW":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_SELF
case "RP":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_READ_PROP
case "WP":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_WRITE_PROP
case "CR":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_CONTROL_ACCESS
case "LC":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_LIST_CONTENTS
case "DT":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_DELETE_TREE
case "LO":
accessMask |= ACCESS_MASK_ADS_RIGHT_DS_LIST_OBJECT
default:
return nil, 0, errors.New(errInvalidACE)
}
parsedLen += 2
}
curAce.AccessMask = accessMask
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen] != ';' {
return nil, 0, errors.New(errInvalidACL)
}
parsedLen++
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen] != ';' {
if i+parsedLen+36 >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
objectGuid := sddl[i+parsedLen : i+parsedLen+36]
guid, err := GuidFromString(objectGuid)
if err != nil {
return nil, 0, err
}
curAce.ObjectType = *guid
parsedLen += 36
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen] != ';' {
return nil, 0, errors.New(errInvalidACL)
}
}
parsedLen++
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen] != ';' {
if i+parsedLen+36 >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
objectGuid := sddl[i+parsedLen : i+parsedLen+36]
guid, err := GuidFromString(objectGuid)
if err != nil {
return nil, 0, err
}
curAce.InheritedObjectType = *guid
parsedLen += 36
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen] != ';' {
return nil, 0, errors.New(errInvalidACL)
}
}
parsedLen++
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen] != ';' {
if i+parsedLen+2 >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
sidString := sddl[i+parsedLen : i+parsedLen+2]
if sidString == "S-" {
sid := "S-"
j := i + parsedLen + 2
for j < datalen && ((sddl[j] >= '0' && sddl[j] <= '9') || sddl[j] == '-') {
sid += string(sddl[j])
j++
}
curAce.SID = sid
parsedLen += j - (i + parsedLen)
} else {
sid, err := sddlAliasToSID(sidString, domainIdentity, machineIdentity)
if err != nil {
return nil, 0, err
}
curAce.SID = sid
parsedLen += 2
}
}
if i+parsedLen >= datalen {
return nil, 0, errors.New(errInvalidACL)
}
if sddl[i+parsedLen] != ')' {
return nil, 0, errors.New(errInvalidACL)
}
parsedLen++
if i+parsedLen >= datalen {
inParen = false
i += parsedLen
} else {
if sddl[i+parsedLen] != '(' {
inParen = false
} else {
parsedLen++
}
i += parsedLen
}
acl = append(acl, curAce)
}
return acl, i, nil
}
// ToBinary encodes the SDDL as a binary string.
// This is the generic binary format Windows uses.
//
// Returns an error if the SDDL is invalid.
func (sddl *SDDL) ToBinary() ([]byte, error) {
var data []byte
if sddl.Version != 0x1 {
return nil, errors.New(errInvalidSDDL)
}
data = append(data, 0x01)
data = append(data, 0x00)
data = append(data, byte(sddl.ControlFlags))
data = append(data, byte(sddl.ControlFlags>>8))
data = append(data, 0x00, 0x00, 0x00, 0x00) // Owner offset
data = append(data, 0x00, 0x00, 0x00, 0x00) // Group offset
data = append(data, 0x00, 0x00, 0x00, 0x00) // SACL offset
data = append(data, 0x00, 0x00, 0x00, 0x00) // DACL offset
if sddl.Owner != "" {
sid, err := sidToLEBytes(sddl.Owner)
if err != nil {
return nil, err
}
data = append(data, sid...)
binary.LittleEndian.PutUint32(data[4:8], uint32(len(data)-len(sid)))
}
if sddl.Group != "" {
sid, err := sidToLEBytes(sddl.Group)
if err != nil {
return nil, err
}
data = append(data, sid...)
binary.LittleEndian.PutUint32(data[8:12], uint32(len(data)-len(sid)))
}
if sddl.SACL != nil {
sacl, err := aclToBytes(sddl.SACL)
if err != nil {
return nil, err
}
data = append(data, sacl...)
binary.LittleEndian.PutUint32(data[12:16], uint32(len(data)-len(sacl)))
}
if sddl.DACL != nil {
dacl, err := aclToBytes(sddl.DACL)
if err != nil {
return nil, err
}
data = append(data, dacl...)
binary.LittleEndian.PutUint32(data[16:20], uint32(len(data)-len(dacl)))
}
return data, nil
}
// ToBase64Encoded encodes the SDDL as a base64 byte array.
// This is a shortcut for base64.StdEncoding.Encode and SDDLToBinary.
//
// Returns an error if the SDDL is invalid.
func (sddl *SDDL) ToBase64Encoded() ([]byte, error) {
bin, err := sddl.ToBinary()
if err != nil {
return nil, err
}
b64 := make([]byte, base64.StdEncoding.EncodedLen(len(bin)))
base64.StdEncoding.Encode(b64, bin)
return b64, nil
}