-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathextension.go
329 lines (254 loc) · 7.46 KB
/
extension.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
package dcom
import (
"bytes"
"fmt"
ndr "github.com/oiweiwei/go-msrpc/ndr"
dtyp "github.com/oiweiwei/go-msrpc/msrpc/dtyp"
)
const ErrorStringSignature uint32 = 0xFFFFFFFF
const ContextORPCExtensionSignature string = "ANUK"
const EntryHeaderSignature string = "INAN"
// Extension represents the ORPC Extension object.
type Extension interface {
// ExtensionID returns the ClassID of the extension.
ExtensionID() *ClassID
// Data returns the binary data of the extension.
Data(...any) ([]byte, error)
// Parse parses the binary data to the extension.
Parse([]byte, ...any) error
}
// Extensions represents the ORPC Extensions object used
// to parse and generate the ORPC Extensions.
type Extensions struct {
// DataRepresentation is needed for ContextExtension.
DataRepresentation ndr.DataRepresentation
// Entries is the list of extensions.
Entries []Extension
}
// ParseWithDataRepresentation parses the ORPCExtentArray with the given data representation.
func (o *Extensions) ParseWithDataRepresentation(array *ORPCExtentArray, drep ndr.DataRepresentation) error {
o.DataRepresentation = drep
return o.Parse(array)
}
// Parse parses the ORPCExtentArray.
func (o *Extensions) Parse(array *ORPCExtentArray) error {
for i := range array.Extent {
if array.Extent[i] == nil {
continue
}
var ext Extension
switch id := (*ClassID)(array.Extent[i].ID); {
case id.Equal(ErrorExtensionClassID):
ext = &ErrorExtension{}
case id.Equal(ContextExtensionClassID):
ext = &ContextExtension{}
default:
continue
}
if err := ext.Parse(array.Extent[i].Data, o.DataRepresentation); err != nil {
return fmt.Errorf("parse extension error: %w", err)
}
o.Entries = append(o.Entries, ext)
}
return nil
}
// ORPCExtentArray function marshals the extensions list to ORPCExtentArray.
func (o *Extensions) ORPCExtentArray() (*ORPCExtentArray, error) {
ret := make([]*ORPCExtent, 0)
for i := range o.Entries {
if o.Entries[i] == nil {
continue
}
data, err := o.Entries[i].Data(o.DataRepresentation)
if err != nil {
return nil, fmt.Errorf("data error: %w", err)
}
ret = append(ret, &ORPCExtent{
ID: o.Entries[i].ExtensionID().GUID(),
Data: data,
})
}
return &ORPCExtentArray{
Extent: ret,
}, nil
}
// ErrorExtension represents the Error Extension object.
// (see ErrorObjectData).
type ErrorExtension struct {
HelpContext uint32
IID *IID
Source string
Description string
HelpFile string
}
func (ErrorExtension) ExtensionID() *ClassID {
return ErrorExtensionClassID
}
func (o *ErrorExtension) Data(_ ...any) ([]byte, error) {
ref, err := o.ObjectReference()
if err != nil {
return nil, fmt.Errorf("object reference error: %w", err)
}
blob, err := ndr.Marshal(ref, ndr.Opaque)
if err != nil {
return nil, fmt.Errorf("marshal object reference error: %w", err)
}
return blob, nil
}
func (o *ErrorExtension) Parse(b []byte, _ ...any) error {
var obj ObjectReference
if err := ndr.Unmarshal(b, &obj); err != nil {
return fmt.Errorf("unmarshal object reference error: %w", err)
}
cref, ok := obj.ObjectReference.GetValue().(*ObjectReferenceCustom)
if !ok {
return fmt.Errorf("object reference is invalid")
}
if !cref.ClassID.Equal(ErrorObjectClassID) {
return fmt.Errorf("object reference is not error")
}
if err := o.Unmarshal(cref.ObjectData); err != nil {
return fmt.Errorf("unmarshal error object: %w", err)
}
return nil
}
func (o *ErrorExtension) ObjectReference() (*ObjectReference, error) {
blob, err := o.Marshal()
if err != nil {
return nil, fmt.Errorf("marshal error object: %w", err)
}
return &ObjectReference{
Signature: ([]byte)(ObjectReferenceCustomSignature),
Flags: ObjectReferenceTypeCustom,
IID: o.IID,
ObjectReference: &ObjectReference_ObjectReference{
Value: &ObjectReference_Custom{
Custom: &ObjectReferenceCustom{
ClassID: ErrorObjectClassID,
Size: uint32(len(blob)),
ObjectData: blob,
},
},
},
}, nil
}
func (o *ErrorExtension) Marshal() ([]byte, error) {
obj := &ErrorObjectData{
HelpContext: o.HelpContext,
IID: o.IID,
}
if o.Source != "" {
obj.SourceSignature, obj.Source = ErrorStringSignature, &ErrorObjectDataString{
Value: &ErrorObjectDataString_String{
String: &ErrorInfoString{Name: o.Source},
},
}
}
if o.Description != "" {
obj.DescriptionSignature, obj.Description = ErrorStringSignature, &ErrorObjectDataString{
Value: &ErrorObjectDataString_String{
String: &ErrorInfoString{Name: o.Description},
},
}
}
if o.HelpFile != "" {
obj.HelpFileSignature, obj.HelpFile = ErrorStringSignature, &ErrorObjectDataString{
Value: &ErrorObjectDataString_String{
String: &ErrorInfoString{Name: o.HelpFile},
},
}
}
b, err := ndr.Marshal(obj, ndr.Opaque)
if err != nil {
return nil, err
}
return b, nil
}
func (o *ErrorExtension) Unmarshal(b []byte) error {
var obj ErrorObjectData
if err := ndr.Unmarshal(b, &obj, ndr.Opaque); err != nil {
return err
}
if val, ok := obj.Source.GetValue().(*ErrorInfoString); ok {
o.Source = val.Name
}
if val, ok := obj.Description.GetValue().(*ErrorInfoString); ok {
o.Description = val.Name
}
if val, ok := obj.HelpFile.GetValue().(*ErrorInfoString); ok {
o.HelpFile = val.Name
}
o.HelpContext = obj.HelpContext
o.IID = obj.IID
return nil
}
// ContextEntry represents the ORPC Context Extension entry.
type ContextEntry struct {
// ID is the PolicyID.
ID *dtyp.GUID
// Data is the binary data.
Data []byte
}
// ContextExtension represents the ORPC Context Extension object.
type ContextExtension struct {
ServerHResult uint32
Entries []*ContextEntry
}
func (ContextExtension) ExtensionID() *ClassID {
return ContextExtensionClassID
}
func (o *ContextExtension) Data(opts ...any) ([]byte, error) {
ctx := &ContextORPCExtension{
Signature: ([]byte)(ContextORPCExtensionSignature),
Version: 0x00010000,
ServerHResult: o.ServerHResult,
Length: uint32(32 + 32*len(o.Entries)), /* header size + entry header sizes */
}
buf := bytes.NewBuffer(nil)
for i1 := range o.Entries {
ctx.EntryHeader = append(ctx.EntryHeader, &EntryHeader{
Signature: ([]byte)(EntryHeaderSignature),
BufferLength: uint32(len(o.Entries[i1].Data)),
Length: ctx.Length,
PolicyID: o.Entries[i1].ID,
})
// padded to 16 bytes.
ctx.Length += uint32(len(o.Entries[i1].Data)) & (^uint32(15))
// write entry data to buffer.
if _, err := buf.Write(o.Entries[i1].Data); err != nil {
return nil, err
}
// write pad.
for i2 := uint32(len(o.Entries[i1].Data)); i2 < ctx.Length; i2++ {
if err := buf.WriteByte(0); err != nil {
return nil, err
}
}
}
ctx.PolicyData = buf.Bytes()
ctx.BufferLength = uint32(len(ctx.PolicyData))
b, err := ndr.Marshal(ctx, opts...)
if err != nil {
return nil, err
}
return b, nil
}
func (o *ContextExtension) Parse(b []byte, opts ...any) error {
var ctx ContextORPCExtension
if err := ndr.Unmarshal(b, &ctx, opts...); err != nil {
return fmt.Errorf("unmarshal context extension error: %w", err)
}
o.ServerHResult = ctx.ServerHResult
hdrSize := 32 + 32*len(ctx.EntryHeader)
for i1 := range ctx.EntryHeader {
eOft, eLen := int(ctx.EntryHeader[i1].Length)-(hdrSize), int(ctx.EntryHeader[i1].BufferLength)
if eOft <= 0 || eOft+eLen > len(ctx.PolicyData) {
return fmt.Errorf("entry data is invalid")
}
o.Entries = append(o.Entries, &ContextEntry{
ID: ctx.EntryHeader[i1].PolicyID,
Data: ctx.PolicyData[eOft : eOft+eLen],
})
}
return nil
}