-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdualstringarray.go
159 lines (125 loc) · 3.64 KB
/
dualstringarray.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
package dcom
import (
"encoding/json"
"strconv"
"unicode/utf16"
dcerpc "github.com/oiweiwei/go-msrpc/dcerpc"
"github.com/oiweiwei/go-msrpc/msrpc/dcetypes"
)
type StringBinding struct {
TowerID uint16 `json:"tower_id"`
NetworkAddr string `json:"network_addr"`
}
func (o *StringBinding) String() string {
switch int(o.TowerID) {
case dcetypes.ProtocolTCP:
return "ncacn_ip_tcp:" + o.NetworkAddr
case dcetypes.ProtocolNamedPipe:
return "ncacn_np:" + o.NetworkAddr
case dcetypes.ProtocolHTTP:
return "ncacn_http:" + o.NetworkAddr
case dcetypes.ProtocolLRPC:
return "ncacnlrpc:" + o.NetworkAddr
}
return "nca_unknown_" + strconv.Itoa(int(o.TowerID)) + ":" + o.NetworkAddr
}
type SecurityBinding struct {
AuthnType uint16 `json:"authn_type"`
AuthzService uint16 `json:"authz_service"`
PrincipalName string `json:"principal_name"`
}
func (o *DualStringArray) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
StringBindings []*StringBinding `json:"string_bindings"`
SecurityBindings []*SecurityBinding `json:"security_bindings"`
}{
o.GetStringBindings(),
o.GetSecurityBindings(),
})
}
func (o *DualStringArray) GetSecurityBindings() []*SecurityBinding {
if o == nil {
return nil
}
ret := []*SecurityBinding{}
for buf := o.StringArray[o.SecurityOffset:]; len(buf) > 2; {
i := 2
for buf[i] != 0x0000 && i < len(buf) {
i++
}
ret, buf = append(ret, &SecurityBinding{
AuthnType: buf[0],
AuthzService: buf[1],
PrincipalName: string(utf16.Decode(buf[2:i])),
}), buf[i+1:]
}
return ret
}
// Endpoints function returns the string bindings as DCE/RPC client options.
func (o *DualStringArray) Endpoints() []dcerpc.Option {
eps := []dcerpc.Option{}
for _, binding := range o.GetStringBindings() {
eps = append(eps, dcerpc.WithEndpoint(binding.String()))
}
return eps
}
// GetProtocolSequences function returns the set of protocol sequences allowed
// for the binding.
func (o *DualStringArray) GetProtocolSequences() []uint16 {
seen, ret := make(map[uint16]struct{}), []uint16{}
for _, binding := range o.GetStringBindings() {
if _, ok := seen[binding.TowerID]; ok {
continue
}
ret, seen[binding.TowerID] = append(ret, binding.TowerID), struct{}{}
}
return ret
}
// GetStringBindings function returns the string bindings encoded in the
// dual string array structure.
func (o *DualStringArray) GetStringBindings() []*StringBinding {
if o == nil {
return nil
}
ret := []*StringBinding{}
for buf := o.StringArray[:o.SecurityOffset]; len(buf) > 1; {
i := 1
for buf[i] != 0x0000 && i < len(buf) {
i++
}
ret, buf = append(ret, &StringBinding{
TowerID: buf[0],
NetworkAddr: string(utf16.Decode(buf[1:i])),
}), buf[i+1:]
}
return ret
}
// EndpointsByTowerID function returns the string bindings as DCE/RPC client options
// filtered by the tower identifier.
func (o *DualStringArray) EndpointsByTowerID(tower int) []dcerpc.Option {
eps := []dcerpc.Option{}
for _, binding := range o.GetStringBindings() {
if tower == int(binding.TowerID) {
eps = append(eps, dcerpc.WithEndpoint(binding.String()))
}
}
return eps
}
// EndpointsByProtocol function returns the string bindings as DCE/RPC client options
// filtered by human-readable protocol sequence name.
func (o *DualStringArray) EndpointsByProtocol(protocol string) []dcerpc.Option {
var tower int
switch protocol {
case "ncacn_ip_tcp":
tower = dcetypes.ProtocolTCP
case "ncacn_np":
tower = dcetypes.ProtocolNamedPipe
case "ncacn_http":
tower = dcetypes.ProtocolHTTP
case "ncacnlrpc":
tower = dcetypes.ProtocolLRPC
default:
return []dcerpc.Option{}
}
return o.EndpointsByTowerID(tower)
}