-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpac_type.go
380 lines (330 loc) · 13.4 KB
/
pac_type.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
package pac
import (
"bytes"
"fmt"
ndr "github.com/oiweiwei/go-msrpc/ndr"
claims "github.com/oiweiwei/go-msrpc/msrpc/adts/claims/claims/v1"
dtyp "github.com/oiweiwei/go-msrpc/msrpc/dtyp"
)
type PAC struct {
Version int `json:"version"`
Buffers []*PACInfoBuffer `json:"pac_info_buffer"`
LogonInformation *KerberosValidationInfo `json:"logon_information,omitempty"`
CredentialInformation *PACCredentialInfo `json:"credential_information,omitempty"`
ServerChecksum *PACSignatureData `json:"server_checksum,omitempty"`
KDCChecksum *PACSignatureData `json:"kdc_checksum,omitempty"`
ClientNameAndTicketInformation *PACClientInfo `json:"client_name_and_ticket_information,omitempty"`
ConstrainedDelegationInformation *S4UDelegationInfo `json:"constrained_delegation_information,omitempty"`
UPNAndDNSInformation *UPNDNSInfo `json:"upn_and_dns_information,omitempty"`
ClientClaimsInformation *claims.ClaimsSetMetadata `json:"client_claims_information,omitempty"`
DeviceInformation *PACDeviceInfo `json:"device_information,omitempty"`
DeviceClaimsInformation *claims.ClaimsSetMetadata `json:"device_claims_information,omitempty"`
TicketChecksum *PACSignatureData `json:"ticket_checksum,omitempty"`
Attributes *PACAttributesInfo `json:"attributes,omitempty"`
RequestorSID *dtyp.SID `json:"requestor_sid,omitempty"`
ExtendedKDCChecksum *PACSignatureData `json:"extended_kdc_checksum,omitempty"`
RequestorGUID *dtyp.GUID `json:"requestor_guid,omitempty"`
}
func (p *PAC) Unmarshal(b []byte) error {
var pac PACType
if err := ndr.Unmarshal(b, &pac, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: headers: %w", err)
}
p.Version = int(pac.Version)
p.Buffers = pac.Buffers
for _, buffer := range pac.Buffers {
b := b[buffer.Offset : buffer.Offset+uint64(buffer.BufferLength)]
if len(b) == 0 {
continue
}
switch buffer.Type {
case PACInfoBufferTypeLogonInfo:
var v KerberosValidationInfo
if err := ndr.UnmarshalWithTypeSerializationV1(b, ndr.UnmarshalerPointer(&v)); err != nil {
return fmt.Errorf("unmarshal_pac: kerberos_validation_info: %w", err)
}
p.LogonInformation = &v
case PACInfoBufferTypeCredentialsInfo:
var v PACCredentialInfo
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: credential_info: %w", err)
}
p.CredentialInformation = &v
case PACInfoBufferTypeServerChecksum:
var v PACSignatureData
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarhsal_pac: server_checksum: %w", err)
}
p.ServerChecksum = &v
case PACInfoBufferTypeKDCChecksum:
var v PACSignatureData
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: kdc_checksum: %w", err)
}
p.KDCChecksum = &v
case PACInfoBufferTypeClientNameAndTicketInfo:
var v PACClientInfo
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: client_name_and_ticket_information: %w", err)
}
p.ClientNameAndTicketInformation = &v
case PACInfoBufferTypeConstrainedDelegationInfo:
var v S4UDelegationInfo
if err := ndr.UnmarshalWithTypeSerializationV1(b, ndr.UnmarshalerPointer(&v)); err != nil {
return fmt.Errorf("unmarshal_pac: constrained_delegation_information: %w", err)
}
p.ConstrainedDelegationInformation = &v
case PACInfoBufferTypeUPNAndDNSInfo:
var v UPNDNSInfo
if err := v.Unmarshal(b); err != nil {
return fmt.Errorf("unmarshal_pac: upn_and_dns_information: %w", err)
}
p.UPNAndDNSInformation = &v
case PACInfoBufferTypeClientClaimsInfo:
var v claims.ClaimsSetMetadata
if err := ndr.UnmarshalWithTypeSerializationV1(b, ndr.UnmarshalerPointer(&v)); err != nil {
return fmt.Errorf("unmarshal_pac: client_claims_information: %w", err)
}
p.ClientClaimsInformation = &v
case PACInfoBufferTypeDeviceInfo:
var v PACDeviceInfo
if err := ndr.UnmarshalWithTypeSerializationV1(b, ndr.UnmarshalerPointer(&v)); err != nil {
return fmt.Errorf("unmarshal_pac: device_information: %w", err)
}
p.DeviceInformation = &v
case PACInfoBufferTypeDeviceClaimsInfo:
var v claims.ClaimsSetMetadata
if err := ndr.UnmarshalWithTypeSerializationV1(b, ndr.UnmarshalerPointer(&v)); err != nil {
return fmt.Errorf("unmarshal_pac: client_claims_information: %w", err)
}
p.DeviceClaimsInformation = &v
case PACInfoBufferTypeTicketChecksum:
var v PACSignatureData
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: ticket_checksum: %w", err)
}
p.TicketChecksum = &v
case PACInfoBufferTypeAttributes:
// XXX: the format of the PAC_ATTRIBUTE_INFO is defined as uint32 flag-size in bits
// and array of flags of rounded (flag-size / 32)
var v PACAttributesInfo
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: attributes: %w", err)
}
p.Attributes = &v
case PACInfoBufferTypeRequestorSID:
var v dtyp.SID
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: requestor_sid: %w", err)
}
p.RequestorSID = &v
case PACInfoBufferTypeExtendedKDCChecksum:
var v PACSignatureData
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: extended_kdc_checksum: %w", err)
}
p.ExtendedKDCChecksum = &v
case PACInfoBufferTypeRequestorGUID:
var v dtyp.GUID
if err := ndr.Unmarshal(b, &v, ndr.Opaque); err != nil {
return fmt.Errorf("unmarshal_pac: requestor_guid: %w", err)
}
p.RequestorGUID = &v
}
}
return nil
}
func IsSignatureBuffer(buffer *PACInfoBuffer) bool {
if buffer != nil {
return buffer.Type == 0x00000006 || buffer.Type == 0x00000007 || buffer.Type == 0x00000010 || buffer.Type == 0x00000013
}
return false
}
// FillInSignatureData function fills the signature data in the buffer.
func FillInSignatureData(b []byte, sign *PACInfoBuffer, data []byte) ([]byte, error) {
if !IsSignatureBuffer(sign) {
return b, nil
}
if len(b) < int(sign.Offset+uint64(sign.BufferLength)) {
return nil, fmt.Errorf("fill_signature_data: buffer too small")
}
if sign.BufferLength < 4 {
return nil, fmt.Errorf("fill_signature_data: buffer too small")
}
if n := copy(b[sign.Offset+4:sign.Offset+uint64(sign.BufferLength)], data); n != len(data) {
return nil, fmt.Errorf("fill_signature_data: short write")
}
return b, nil
}
// ZeroOutSignatureData function clears the signature data in the buffer.
func ZeroOutSignatureData(b []byte, sign *PACInfoBuffer) ([]byte, error) {
if !IsSignatureBuffer(sign) {
return b, nil
}
if len(b) < int(sign.Offset+uint64(sign.BufferLength)) {
return nil, fmt.Errorf("zero_out_signature_data: buffer too small")
}
if sign.BufferLength < 4 {
return nil, fmt.Errorf("zero_out_signature_data: buffer too small")
}
clear(b[sign.Offset+4 : sign.Offset+uint64(sign.BufferLength)])
return b, nil
}
// Marshal function marshals the PAC structure into a byte array also setting the
// PAC.Buffers field to the Buffers slice. This buffer slice will contain the
// information about the binary layout.
func (p *PAC) Marshal() ([]byte, error) {
buf := new(bytes.Buffer)
var pac PACType
pac.Version = uint32(p.Version)
// LogonInformation
if p.LogonInformation != nil {
b, err := ndr.MarshalWithTypeSerializationV1(ndr.MarshalerPointer(p.LogonInformation))
if err != nil {
return nil, fmt.Errorf("marshal_pac: logon_information: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000001, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// CredentialInformation
if p.CredentialInformation != nil {
b, err := ndr.Marshal(p.CredentialInformation, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: credential_info: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000002, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// ServerChecksum
if p.ServerChecksum != nil {
b, err := ndr.Marshal(p.ServerChecksum, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: server_checksum: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000006, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// KDCChecksum
if p.KDCChecksum != nil {
b, err := ndr.Marshal(p.KDCChecksum, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: kdc_checksum: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000007, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// ClientNameAndTicketInformation
if p.ClientNameAndTicketInformation != nil {
b, err := ndr.Marshal(p.ClientNameAndTicketInformation, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: client_name_and_ticket_information: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x0000000A, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// ConstrainedDelegationInformation
if p.ConstrainedDelegationInformation != nil {
b, err := ndr.MarshalWithTypeSerializationV1(ndr.MarshalerPointer(p.ConstrainedDelegationInformation))
if err != nil {
return nil, fmt.Errorf("marshal_pac: constrained_delegation_information: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x0000000B, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// UPNAndDNSInformation
if p.UPNAndDNSInformation != nil {
b, err := p.UPNAndDNSInformation.Marshal()
if err != nil {
return nil, fmt.Errorf("marshal_pac: upn_and_dns_information: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x0000000C, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// ClientClaimsInformation
if p.ClientClaimsInformation != nil {
b, err := ndr.MarshalWithTypeSerializationV1(ndr.MarshalerPointer(p.ClientClaimsInformation))
if err != nil {
return nil, fmt.Errorf("marshal_pac: client_claims_information: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x0000000D, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// DeviceInformation
if p.DeviceInformation != nil {
b, err := ndr.MarshalWithTypeSerializationV1(ndr.MarshalerPointer(p.DeviceInformation))
if err != nil {
return nil, fmt.Errorf("marshal_pac: device_information: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x0000000E, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// DeviceClaimsInformation
if p.DeviceClaimsInformation != nil {
b, err := ndr.MarshalWithTypeSerializationV1(ndr.MarshalerPointer(p.DeviceClaimsInformation))
if err != nil {
return nil, fmt.Errorf("marshal_pac: device_claims_information: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x0000000F, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// TicketChecksum
if p.TicketChecksum != nil {
b, err := ndr.Marshal(p.TicketChecksum, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: ticket_checksum: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000010, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// Attributes.
if p.Attributes != nil {
b, err := ndr.Marshal(p.Attributes, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: attributes: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000011, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// RequestorSID
if p.RequestorSID != nil {
b, err := ndr.Marshal(p.RequestorSID, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: requestor_sid: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000012, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// ExtendedKDCChecksum
if p.ExtendedKDCChecksum != nil {
b, err := ndr.Marshal(p.ExtendedKDCChecksum, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: extended_kdc_checksum: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000013, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
// RequestorGUID
if p.RequestorGUID != nil {
b, err := ndr.Marshal(p.RequestorGUID, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal_pac: requestor_guid: %w", err)
}
pac.Buffers = append(pac.Buffers, &PACInfoBuffer{Type: 0x00000014, Offset: uint64(buf.Len()), BufferLength: uint32(len(b))})
writeWithPad(buf, b)
}
for _, buffer := range pac.Buffers {
buffer.Offset += uint64(len(pac.Buffers)*16 + 8)
}
b, err := ndr.Marshal(&pac, ndr.Opaque)
if err != nil {
return nil, err
}
p.Buffers = pac.Buffers
return append(b, buf.Bytes()...), nil
}
func writeWithPad(buf *bytes.Buffer, b []byte) {
buf.Write(b)
if pad := len(b) % 8; pad != 0 {
buf.Write(make([]byte, 8-pad))
}
}