-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.go
312 lines (277 loc) · 8.64 KB
/
protocol.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
package winrm
import (
"encoding/xml"
// "io/ioutil"
"fmt"
"errors"
)
type Envelope struct {
XMLName xml.Name `xml:"env:Envelope"`
EnvelopeAttrs
Headers *Headers `xml:"env:Header,omitempty"`
Body *BodyStruct `xml:"env:Body,omitempty"`
}
type ShellParams struct {
IStream string
OStream string
WorkingDir string
EnvVars *Environment
NoProfile bool
Codepage string
}
type HeaderParams struct {
ResourceURI string
Action string
ShellID string
MessageID string
}
type CmdParams struct {
ShellID string
Cmd string
Args string
Timeout string
}
func (envelope *Envelope) RunCommand(shellParams ShellParams, params CmdParams, soap SoapRequest) (string, string, int, error){
shell, err_shell := envelope.GetShell(shellParams, soap)
if err_shell != nil {
return "", "", 0, err_shell
}
params.ShellID = shell
commID, commErr := envelope.SendCommand(params, soap)
if commErr != nil {
return "", "", 0, commErr
}
strdout, stderr, ret_code, err := envelope.GetCommandOutput(params.ShellID, commID, soap)
if err != nil{
return "", "", 0, err
}
err_clean := envelope.CleanupShell(params.ShellID, commID, soap)
if err_clean != nil {
return "", "", 0, err
}
err_close := envelope.CloseShell(params.ShellID, soap)
if err_close != nil {
return "", "", 0, err
}
return strdout, stderr, ret_code, err
}
// Generate SOAP envelope headers
func (envelope *Envelope) GetSoapHeaders(params HeaderParams) (error){
envelope.Headers = &Headers{
OperationTimeout:"PT60S",
To:"http://windows-host:5985/wsman",
ReplyTo:&ReplyAddress{
ValueMustUnderstand{
Value:"http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous",
Attr:"true",
},
},
DataLocale:&LocaleAttr{
MustUnderstand:"true",
Lang:"en-US",
},
Locale:&LocaleAttr{
MustUnderstand:"true",
Lang:"en-US",
},
MaxEnvelopeSize:&ValueMustUnderstand{
Value:"153600",
Attr:"true",
},
}
if params.ResourceURI != "" {
envelope.Headers.ResourceURI = &ValueMustUnderstand{
Value:params.ResourceURI,
Attr:"true",
}
}
if params.Action != "" {
envelope.Headers.Action = &ValueMustUnderstand{
Value:params.Action,
Attr:"true",
}
}
if params.ShellID != "" {
envelope.Headers.SelectorSet = &Selector{
ValueName{
Value:params.ShellID,
Attr:"ShellId",
},
}
}
if params.MessageID == "" {
uuid, err := Uuid()
if err != nil{
return err
}
params.MessageID = fmt.Sprintf("uuid:%s", uuid)
}
envelope.Headers.MessageID = params.MessageID
return nil
}
// TODO: Do a soap request and return ShellID
func (envelope *Envelope) GetShell(params ShellParams, soap SoapRequest) (string, error){
HeadParams := HeaderParams {
ResourceURI: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd",
Action: "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create",
}
// envelope := Envelope{}
envelope.GetSoapHeaders(HeadParams)
if params.Codepage == "" {
params.Codepage = "437"
}
envelope.Headers.OptionSet = &OptionSet{
[]ValueName{
ValueName{Attr:"WINRS_NOPROFILE", Value:"FALSE"},
ValueName{Attr:"WINRS_CODEPAGE", Value:params.Codepage},
},
}
var Body BodyStruct = BodyStruct{}
var ShellVars Shell = Shell {}
if params.IStream == "" {
ShellVars.InputStreams = "stdin"
}else{
ShellVars.InputStreams = params.IStream
}
if params.OStream == "" {
ShellVars.OutputStreams = "stdout stderr"
}else{
ShellVars.OutputStreams = params.OStream
}
if params.EnvVars != nil {
ShellVars.Environment = params.EnvVars
}
// send request to WinRm
Body.Shell = &ShellVars
envelope.Body = &Body
envelope.EnvelopeAttrs = Namespaces
// response from WinRM
resp, err := soap.SendMessage(envelope)
if err != nil{
return "", err
}
defer resp.Body.Close()
respObj, err := GetObjectFromXML(resp.Body)
//fmt.Printf("%v\n", respObj)
if err != nil {
return "", err
}
shellID := respObj.Body.Shell.ShellId
return shellID, err
}
func (envelope *Envelope) SendCommand(params CmdParams, soap SoapRequest) (string, error){
HeadParams := HeaderParams {
ResourceURI: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd",
Action: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command",
}
if params.ShellID == "" {
return "", errors.New("Invalid ShellId")
}
HeadParams.ShellID = params.ShellID
envelope.GetSoapHeaders(HeadParams)
envelope.Headers.OptionSet = &OptionSet{
[]ValueName{
ValueName{Attr:"WINRS_CONSOLEMODE_STDIN", Value:"TRUE"},
ValueName{Attr:"WINRS_SKIP_CMD_SHELL", Value:"FALSE"},
},
}
if params.Timeout == "" {
envelope.Headers.OperationTimeout = "PT3600S"
}else{
envelope.Headers.OperationTimeout = params.Timeout
}
// var Body BodyStruct = BodyStruct{}
envelope.EnvelopeAttrs = Namespaces
if params.Cmd == "" {
return "", errors.New("Invalid command")
}
envelope.Body = &BodyStruct{
CommandLine: &Command{
Command: params.Cmd,
},
}
if params.Args != "" {
envelope.Body.CommandLine.Arguments = params.Args
}
// fmt.Printf("%s\n", output)
resp, err := soap.SendMessage(envelope)
if err != nil{
return "", err
}
defer resp.Body.Close()
respObj, err := GetObjectFromXML(resp.Body)
if err != nil{
return "", err
}
// contents, _ := ioutil.ReadAll(resp.Body)
// fmt.Printf("REQ:%s\n\nRESP:%s\n\nSHELL:%s\n\n", output, contents, shellID)
return respObj.Body.CommandResponse.CommandId, nil
}
func (envelope *Envelope) GetCommandOutput(shellID, commandID string, soap SoapRequest) (string, string, int, error){
HeadParams := HeaderParams {
ResourceURI: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd",
Action: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive",
ShellID: shellID,
}
envelope.GetSoapHeaders(HeadParams)
envelope.EnvelopeAttrs = Namespaces
envelope.Body = &BodyStruct{
Receive: &Receive{
DesiredStream: DesiredStreamProps{
Value: "stdout stderr",
Attr: commandID,
},
},
}
resp, err := soap.SendMessage(envelope)
if err != nil{
return "", "", 0, err
}
defer resp.Body.Close()
stdout, stderr, retCode := ParseCommandOutput(resp.Body)
// fmt.Printf("%s\n", output)
return stdout, stderr, retCode, nil
}
func (envelope *Envelope) CleanupShell(shellID, commandID string, soap SoapRequest) (error){
HeadParams := HeaderParams {
ResourceURI: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd",
Action: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal",
ShellID: shellID,
}
envelope.GetSoapHeaders(HeadParams)
envelope.EnvelopeAttrs = Namespaces
sig := Signal{
Attr: commandID,
Code: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate",
}
envelope.Body = &BodyStruct{
Signal: &sig,
}
resp, err := soap.SendMessage(envelope)
if err != nil{
return err
}
defer resp.Body.Close()
// contents, err2 := ioutil.ReadAll(resp.Body)
// fmt.Printf("%s --> %s", contents, err2)
return nil
}
func (envelope *Envelope) CloseShell(shellID string, soap SoapRequest) (error){
HeadParams := HeaderParams {
ResourceURI: "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd",
Action: "http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete",
ShellID: shellID,
}
var Body BodyStruct = BodyStruct{}
envelope.EnvelopeAttrs = Namespaces
envelope.GetSoapHeaders(HeadParams)
envelope.Body = &Body
resp, err := soap.SendMessage(envelope)
// contents, err := ioutil.ReadAll(resp.Body)
// fmt.Printf("REQ:%s\n\nRESP:%s\n\nSHELL:%s\n\n", output, contents, shellID)
if err != nil{
return err
}
defer resp.Body.Close()
return nil
}