-
Notifications
You must be signed in to change notification settings - Fork 2
/
upn_dns_info.go
161 lines (122 loc) · 2.95 KB
/
upn_dns_info.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
package pac
import (
"bytes"
"context"
dtyp "github.com/oiweiwei/go-msrpc/msrpc/dtyp"
ndr "github.com/oiweiwei/go-msrpc/ndr"
"github.com/oiweiwei/go-msrpc/text/encoding/utf16le"
)
func (o *UPNDNSInfo) Unmarshal(b []byte) error {
r := ndr.NDR20(b, ndr.Opaque)
if err := r.Unmarshal(context.Background(), o); err != nil {
return err
}
if o.Flags&0x00000002 != 0 {
r = ndr.NDR20(o.Raw, ndr.Opaque)
if err := r.ReadData(&o.SAMNameLength); err != nil {
return err
}
if err := r.ReadData(&o.SAMNameOffset); err != nil {
return err
}
if err := r.ReadData(&o.SIDLength); err != nil {
return err
}
if err := r.ReadData(&o.SIDOffset); err != nil {
return err
}
o.Raw = r.Bytes()
}
var err error
if o.UPNLength != 0 {
if o.UPN, err = utf16le.Decode(b[o.UPNOffset : o.UPNOffset+o.UPNLength]); err != nil {
return err
}
}
if o.DNSDomainNameLength != 0 {
if o.DNSDomainName, err = utf16le.Decode(b[o.DNSDomainNameOffset : o.DNSDomainNameOffset+o.DNSDomainNameLength]); err != nil {
return err
}
}
if o.SAMNameLength != 0 {
if o.SAMName, err = utf16le.Decode(b[o.SAMNameOffset : o.SAMNameOffset+o.SAMNameLength]); err != nil {
return err
}
}
if o.SIDLength != 0 {
o.SID = &dtyp.SID{}
if err := o.SID.DecodeBinary(b[o.SIDOffset : o.SIDOffset+o.SIDLength]); err != nil {
return err
}
}
return nil
}
func (o *UPNDNSInfo) Marshal() ([]byte, error) {
if o == nil {
o = &UPNDNSInfo{}
}
buf, oft := new(bytes.Buffer), uint16(12) /* upn, dnsname, flags */
if o.SAMName != "" || o.SID != nil {
o.Flags |= 0x00000002
oft += 8 /* sid and sam offset/length */
buf.Write(make([]byte, 8))
}
if o.UPN != "" {
b, err := utf16le.Encode(o.UPN)
if err != nil {
return nil, err
}
o.UPNLength, o.UPNOffset = uint16(len(b)), oft
buf.Write(b)
}
oft += o.UPNLength
if o.DNSDomainName != "" {
b, err := utf16le.Encode(o.DNSDomainName)
if err != nil {
return nil, err
}
o.DNSDomainNameLength, o.DNSDomainNameOffset = uint16(len(b)), oft
buf.Write(b)
}
oft += o.DNSDomainNameLength
if o.SAMName != "" {
b, err := utf16le.Encode(o.SAMName)
if err != nil {
return nil, err
}
o.SAMNameLength, o.SAMNameOffset = uint16(len(b)), oft
buf.Write(b)
}
oft += o.SAMNameLength
if o.SID != nil {
b, err := o.SID.Bytes()
if err != nil {
return nil, err
}
o.SIDLength, o.SIDOffset = uint16(len(b)), oft
buf.Write(b)
}
o.Raw = buf.Bytes()
if o.Flags&0x00000002 != 0 {
r := ndr.NDR20(nil, ndr.Opaque)
if err := r.WriteData(o.SAMNameLength); err != nil {
return nil, err
}
if err := r.WriteData(o.SAMNameOffset); err != nil {
return nil, err
}
if err := r.WriteData(o.SIDLength); err != nil {
return nil, err
}
if err := r.WriteData(o.SIDOffset); err != nil {
return nil, err
}
// copy sam/sid data at the beginning of the raw.
copy(o.Raw, r.Bytes())
}
b, err := ndr.Marshal(o, ndr.Opaque)
if err != nil {
return nil, err
}
return b, nil
}