2016-10-01 16:34:42 -04:00
|
|
|
package winrm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
|
|
|
|
"github.com/masterzen/winrm/soap"
|
|
|
|
"github.com/nu7hatch/gouuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
func genUUID() string {
|
2017-08-20 14:21:42 -04:00
|
|
|
id, _ := uuid.NewV4()
|
|
|
|
return "uuid:" + id.String()
|
2016-10-01 16:34:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func defaultHeaders(message *soap.SoapMessage, url string, params *Parameters) *soap.SoapHeader {
|
|
|
|
return message.
|
|
|
|
Header().
|
|
|
|
To(url).
|
|
|
|
ReplyTo("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous").
|
|
|
|
MaxEnvelopeSize(params.EnvelopeSize).
|
|
|
|
Id(genUUID()).
|
|
|
|
Locale(params.Locale).
|
|
|
|
Timeout(params.Timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
//NewOpenShellRequest makes a new soap request
|
|
|
|
func NewOpenShellRequest(uri string, params *Parameters) *soap.SoapMessage {
|
|
|
|
if params == nil {
|
|
|
|
params = DefaultParameters
|
|
|
|
}
|
|
|
|
|
|
|
|
message := soap.NewMessage()
|
|
|
|
defaultHeaders(message, uri, params).
|
|
|
|
Action("http://schemas.xmlsoap.org/ws/2004/09/transfer/Create").
|
|
|
|
ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
|
|
|
|
AddOption(soap.NewHeaderOption("WINRS_NOPROFILE", "FALSE")).
|
|
|
|
AddOption(soap.NewHeaderOption("WINRS_CODEPAGE", "65001")).
|
|
|
|
Build()
|
|
|
|
|
2017-08-20 14:21:42 -04:00
|
|
|
body := message.CreateBodyElement("Shell", soap.DOM_NS_WIN_SHELL)
|
|
|
|
input := message.CreateElement(body, "InputStreams", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
input.SetContent("stdin")
|
2017-08-20 14:21:42 -04:00
|
|
|
output := message.CreateElement(body, "OutputStreams", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
output.SetContent("stdout stderr")
|
|
|
|
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDeleteShellRequest ...
|
|
|
|
func NewDeleteShellRequest(uri, shellId string, params *Parameters) *soap.SoapMessage {
|
|
|
|
if params == nil {
|
|
|
|
params = DefaultParameters
|
|
|
|
}
|
|
|
|
message := soap.NewMessage()
|
|
|
|
defaultHeaders(message, uri, params).
|
|
|
|
Action("http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete").
|
|
|
|
ShellId(shellId).
|
|
|
|
ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
|
|
|
|
Build()
|
|
|
|
|
|
|
|
message.NewBody()
|
|
|
|
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewExecuteCommandRequest exec command on specific shellID
|
|
|
|
func NewExecuteCommandRequest(uri, shellId, command string, arguments []string, params *Parameters) *soap.SoapMessage {
|
|
|
|
if params == nil {
|
|
|
|
params = DefaultParameters
|
|
|
|
}
|
|
|
|
message := soap.NewMessage()
|
|
|
|
defaultHeaders(message, uri, params).
|
|
|
|
Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command").
|
|
|
|
ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
|
|
|
|
ShellId(shellId).
|
|
|
|
AddOption(soap.NewHeaderOption("WINRS_CONSOLEMODE_STDIN", "TRUE")).
|
|
|
|
AddOption(soap.NewHeaderOption("WINRS_SKIP_CMD_SHELL", "FALSE")).
|
|
|
|
Build()
|
|
|
|
|
2017-08-20 14:21:42 -04:00
|
|
|
body := message.CreateBodyElement("CommandLine", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
|
|
|
|
// ensure special characters like & don't mangle the request XML
|
|
|
|
command = "<![CDATA[" + command + "]]>"
|
2017-08-20 14:21:42 -04:00
|
|
|
commandElement := message.CreateElement(body, "Command", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
commandElement.SetContent(command)
|
|
|
|
|
|
|
|
for _, arg := range arguments {
|
|
|
|
arg = "<![CDATA[" + arg + "]]>"
|
2017-08-20 14:21:42 -04:00
|
|
|
argumentsElement := message.CreateElement(body, "Arguments", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
argumentsElement.SetContent(arg)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGetOutputRequest(uri, shellId, commandId, streams string, params *Parameters) *soap.SoapMessage {
|
|
|
|
if params == nil {
|
|
|
|
params = DefaultParameters
|
|
|
|
}
|
|
|
|
message := soap.NewMessage()
|
|
|
|
defaultHeaders(message, uri, params).
|
|
|
|
Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive").
|
|
|
|
ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
|
|
|
|
ShellId(shellId).
|
|
|
|
Build()
|
|
|
|
|
2017-08-20 14:21:42 -04:00
|
|
|
receive := message.CreateBodyElement("Receive", soap.DOM_NS_WIN_SHELL)
|
|
|
|
desiredStreams := message.CreateElement(receive, "DesiredStream", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
desiredStreams.SetAttr("CommandId", commandId)
|
|
|
|
desiredStreams.SetContent(streams)
|
|
|
|
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSendInputRequest(uri, shellId, commandId string, input []byte, params *Parameters) *soap.SoapMessage {
|
|
|
|
if params == nil {
|
|
|
|
params = DefaultParameters
|
|
|
|
}
|
|
|
|
message := soap.NewMessage()
|
|
|
|
|
|
|
|
defaultHeaders(message, uri, params).
|
|
|
|
Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send").
|
|
|
|
ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
|
|
|
|
ShellId(shellId).
|
|
|
|
Build()
|
|
|
|
|
|
|
|
content := base64.StdEncoding.EncodeToString(input)
|
|
|
|
|
2017-08-20 14:21:42 -04:00
|
|
|
send := message.CreateBodyElement("Send", soap.DOM_NS_WIN_SHELL)
|
|
|
|
streams := message.CreateElement(send, "Stream", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
streams.SetAttr("Name", "stdin")
|
|
|
|
streams.SetAttr("CommandId", commandId)
|
|
|
|
streams.SetContent(content)
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSignalRequest(uri string, shellId string, commandId string, params *Parameters) *soap.SoapMessage {
|
|
|
|
if params == nil {
|
|
|
|
params = DefaultParameters
|
|
|
|
}
|
|
|
|
message := soap.NewMessage()
|
|
|
|
|
|
|
|
defaultHeaders(message, uri, params).
|
|
|
|
Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal").
|
|
|
|
ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
|
|
|
|
ShellId(shellId).
|
|
|
|
Build()
|
|
|
|
|
2017-08-20 14:21:42 -04:00
|
|
|
signal := message.CreateBodyElement("Signal", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
signal.SetAttr("CommandId", commandId)
|
2017-08-20 14:21:42 -04:00
|
|
|
code := message.CreateElement(signal, "Code", soap.DOM_NS_WIN_SHELL)
|
2016-10-01 16:34:42 -04:00
|
|
|
code.SetContent("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate")
|
|
|
|
|
|
|
|
return message
|
|
|
|
}
|