/
samr.go
183 lines (165 loc) · 5.7 KB
/
samr.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
package ditreader
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
)
type SAMRUserProperties struct { /*
# 2.2.10.1 USER_PROPERTIES
class USER_PROPERTIES(Structure):
structure = (
('Reserved1','<L=0'), L = 4 bytes (uint32)
('Length','<L=0'),
('Reserved2','<H=0'), H = 2 bytes (uint16)
('Reserved3','<H=0'),
('Reserved4','96s=""'), S = 1byte (String)
('PropertySignature','<H=0x50'),
('PropertyCount','<H=0'),
('UserProperties',':'),
)*/
Reserved1 uint32
Length uint32
Reserved2 uint16
Reserved3 uint16
Reserved4 [96]byte
PropertySignature uint16
PropertyCount uint16
Properties []SAMRUserProperty
}
type SAMRKerbStoredCredNew struct {
/*
structure = (
('Revision','<H=4'),
('Flags','<H=0'),
('CredentialCount','<H=0'),
('ServiceCredentialCount','<H=0'),
('OldCredentialCount','<H=0'),
('OlderCredentialCount','<H=0'),
('DefaultSaltLength','<H=0'),
('DefaultSaltMaximumLength','<H=0'),
('DefaultSaltOffset','<L=0'),
('DefaultIterationCount','<L=0'),
('Buffer',':'),
)
*/
Revision, Flags, CredentialCount,
ServiceCredentialCount, OldCredentialCount,
OlderCredentialCount, DefaultSaltLength,
DefaultSaltMaximumLength uint16
DefaultSaltOffset, DefaultIterationCount uint32
Buffer []byte
}
func NewSAMRKerbStoredCredNew(d []byte) SAMRKerbStoredCredNew {
r := SAMRKerbStoredCredNew{}
curs := 0
r.Revision = binary.LittleEndian.Uint16(getAndMoveCursor(d, &curs, 2))
r.Flags = binary.LittleEndian.Uint16(getAndMoveCursor(d, &curs, 2))
r.CredentialCount = binary.LittleEndian.Uint16(getAndMoveCursor(d, &curs, 2))
r.ServiceCredentialCount = binary.LittleEndian.Uint16(getAndMoveCursor(d, &curs, 2))
r.OldCredentialCount = binary.LittleEndian.Uint16(getAndMoveCursor(d, &curs, 2))
r.OlderCredentialCount = binary.LittleEndian.Uint16(getAndMoveCursor(d, &curs, 2))
r.DefaultSaltLength = binary.LittleEndian.Uint16(getAndMoveCursor(d, &curs, 2))
r.DefaultSaltMaximumLength = binary.LittleEndian.Uint16(getAndMoveCursor(d, &curs, 2))
r.DefaultSaltOffset = binary.LittleEndian.Uint32(getAndMoveCursor(d, &curs, 4))
r.DefaultIterationCount = binary.LittleEndian.Uint32(getAndMoveCursor(d, &curs, 4))
r.Buffer = d[curs:]
return r
}
type SAMRKerbKeyDataNew struct {
Reserved1, Reserved2 uint16
Reserved3, IterationCount,
KeyType, KeyLength, KeyOffset uint32
}
func NewSAMRKerbKeyDataNew(d []byte) SAMRKerbKeyDataNew {
kd := SAMRKerbKeyDataNew{}
binary.Read(bytes.NewReader(d), binary.LittleEndian, &kd)
return kd
}
func getAndMoveCursor(data []byte, curs *int, size int) []byte {
d := data[*curs : *curs+size]
*curs += size
return d
}
func NewSAMRUserProperties(data []byte) SAMRUserProperties {
r := SAMRUserProperties{}
// lData := make([]byte, len(data)) //avoid mutate
// copy(lData, data)
curs := 0
r.Reserved1 = binary.LittleEndian.Uint32(getAndMoveCursor(data, &curs, 4))
r.Length = binary.LittleEndian.Uint32(getAndMoveCursor(data, &curs, 4))
r.Reserved2 = binary.LittleEndian.Uint16(getAndMoveCursor(data, &curs, 2))
r.Reserved3 = binary.LittleEndian.Uint16(getAndMoveCursor(data, &curs, 2))
copy(r.Reserved4[:], data[curs:curs+96])
curs += 96
r.PropertySignature = binary.LittleEndian.Uint16(getAndMoveCursor(data, &curs, 2))
if len(data) > curs+2 {
r.PropertyCount = binary.LittleEndian.Uint16(getAndMoveCursor(data, &curs, 2))
//fill properties
for i := uint16(0); i < r.PropertyCount; i++ {
np := SAMRUserProperty{}
np.NameLength = binary.LittleEndian.Uint16(getAndMoveCursor(data, &curs, 2))
np.ValueLength = binary.LittleEndian.Uint16(getAndMoveCursor(data, &curs, 2))
np.Reserved = binary.LittleEndian.Uint16(getAndMoveCursor(data, &curs, 2))
np.PropertyName = data[curs : curs+int(np.NameLength)]
curs += int(np.NameLength)
np.PropertyValue = data[curs : curs+int(np.ValueLength)]
curs += int(np.ValueLength)
r.Properties = append(r.Properties, np)
}
}
return r
}
type SAMRUserProperty struct {
/*
class USER_PROPERTY(Structure):
structure = (
('NameLength','<H=0'),
('ValueLength','<H=0'),
('Reserved','<H=0'),
('_PropertyName','_-PropertyName', "self['NameLength']"),
('PropertyName',':'),
('_PropertyValue','_-PropertyValue', "self['ValueLength']"),
('PropertyValue',':'),
)
*/
NameLength uint16
ValueLength uint16
Reserved uint16
PropertyName []byte
PropertyValue []byte
}
type SAMRRPCSID struct {
Revision uint8 //'<B'
SubAuthorityCount uint8 //'<B'
IdentifierAuthority [6]byte //SAMR_RPC_SID_IDENTIFIER_AUTHORITY
SubLen int // ('SubLen','_-SubAuthority','self["SubAuthorityCount"]*4'),
SubAuthority []byte //':'
}
func (s SAMRRPCSID) FormatCanonical() string {
var ans strings.Builder
ans.WriteString(fmt.Sprintf("S-%d-%d", s.Revision, s.IdentifierAuthority[5]))
for i := 0; i < int(s.SubAuthorityCount); i++ {
ans.WriteString(fmt.Sprintf("-%d", binary.BigEndian.Uint32(s.SubAuthority[i*4:i*4+4])))
}
return ans.String()
}
func (s SAMRRPCSID) Rid() uint32 {
l := s.SubAuthorityCount
return binary.BigEndian.Uint32(s.SubAuthority[(l-1)*4 : (l-1)*4+4])
}
func NewSAMRRPCSID(data []byte) (SAMRRPCSID, error) {
r := SAMRRPCSID{}
if len(data) < 6 {
return r, fmt.Errorf("Bad SAMR data: %s", string(data))
}
curs := 0
r.Revision = data[0]
r.SubAuthorityCount = data[1]
getAndMoveCursor(data, &curs, 2)
copy(r.IdentifierAuthority[:], getAndMoveCursor(data, &curs, 6))
r.SubLen = int(r.SubAuthorityCount) * 4
r.SubAuthority = data[curs:] // make([]byte, len(data[curs:]))
//copy(r.SubAuthority, data[curs:])
return r, nil
}