-
Notifications
You must be signed in to change notification settings - Fork 2
/
payload.go
156 lines (144 loc) · 4.59 KB
/
payload.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
package gopsremote
import (
"bytes"
"fmt"
"text/template"
)
// Global template used to generate payloads
var gTemplate *template.Template
// Templates are initialized during startup
// TODO: Calculate memory consumed by holding templates in memory
// TODO: Find out better way to handle potential errors during intialization
func init() {
gTemplate, _ = template.New("payloadBuilder").Parse(body)
}
type RequestType int
const (
Create = 1 << iota
Execute
Send
Receive
Delete
)
// Data structure to build the xml payloads from templates
type PayloadBuilder struct {
// Format - https://{{ipaddress_or_fqdn}}:{{portno}}/wsman
Url string
OperationTimeout string
Locale string
MessageId string
ShellId string
CommandId string
Command string
Input string
MaxEnvelopeSize string
OpType RequestType
}
func (p *PayloadBuilder) GetAction() string {
switch p.OpType {
case Create:
return "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create"
case Execute:
return "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command"
case Send:
return "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send"
case Receive:
return "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive"
case Delete:
return "http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete"
default:
return ""
}
}
func (p *PayloadBuilder) GenerateOptionSet() string {
if p.OpType == Create {
return `
<wsman:OptionSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wsman:Option Name="WINRS_NOPROFILE">FALSE</wsman:Option>
<wsman:Option Name="WINRS_CODEPAGE">65001</wsman:Option>
</wsman:OptionSet>
`
} else if p.OpType == Execute {
return `
<wsman:OptionSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<wsman:Option Name="WINRS_CONSOLEMODE_STDIN">TRUE</wsman:Option>
<wsman:Option Name="WINRS_SKIP_CMD_SHELL">FALSE</wsman:Option>
</wsman:OptionSet>
`
}
return ""
}
func (p *PayloadBuilder) GenerateSelectorSet() string {
if p.OpType != Create {
return fmt.Sprintf(`
<wsman:SelectorSet xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
<wsman:Selector Name="ShellId">%s</wsman:Selector>
</wsman:SelectorSet>
`, p.ShellId)
}
return ""
}
func (p *PayloadBuilder) GenerateBody() string {
switch p.OpType {
case Create:
return `
<s:Body>
<rsp:Shell xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
<rsp:InputStreams>stdin</rsp:InputStreams>
<rsp:OutputStreams>stdout stderr</rsp:OutputStreams>
</rsp:Shell>
</s:Body>
`
case Execute:
return fmt.Sprintf(`
<s:Body>
<rsp:CommandLine xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
<rsp:Command><![CDATA[%s]]></rsp:Command>
</rsp:CommandLine>
</s:Body>`, p.Command)
case Send:
return fmt.Sprintf(`
<s:Body>
<rsp:Send xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
<rsp:Stream xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell" Name="stdin" CommandId="%s">%s</rsp:Stream>
</rsp:Send>
</s:Body>`, p.CommandId, p.Input)
case Receive:
return fmt.Sprintf(`
<s:Body>
<rsp:Receive xmlns:rsp="http://schemas.microsoft.com/wbem/wsman/1/windows/shell">
<rsp:DesiredStream CommandId="%s">stdout stderr</rsp:DesiredStream>
</rsp:Receive>
</s:Body>`, p.CommandId)
case Delete:
return "<s:Body/>"
default:
return ""
}
}
func (p *PayloadBuilder) Execute() (*bytes.Buffer, error) {
payload := &bytes.Buffer{}
err := gTemplate.Execute(payload, p)
if err != nil {
return nil, err
}
return payload, nil
}
var body = `
<?xml version="1.0" encoding="utf-8" ?>
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">
<s:Header>
<wsa:To>{{.Url}}</wsa:To>
<wsa:ReplyTo>
<wsa:Address s:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsman:ResourceURI xmlns:wsman="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd">http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd</wsman:ResourceURI>
<wsa:Action s:mustUnderstand="true">{{.GetAction}}</wsa:Action>
<wsa:MessageID>uuid:{{.MessageId}}</wsa:MessageID>
<wsman:Locale xml:lang="{{.Locale}}" s:mustUnderstand="false"/>
{{.GenerateOptionSet}}
{{.GenerateSelectorSet}}
</s:Header>
{{.GenerateBody}}
</s:Envelope>
`