psrp 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/DemoDLL_RemoteProcess.dll +0 -0
- data/Invoke-ReflectivePEInjection.ps1 +2952 -0
- data/LICENSE +202 -0
- data/README.md +16 -0
- data/lib/psrp.rb +175 -0
- data/lib/response_handler.rb +134 -0
- data/lib/transport.rb +208 -0
- data/lib/version.rb +7 -0
- data/lib/wsmv/command_output_processor.rb +153 -0
- data/lib/wsmv/commands/base.rb +293 -0
- data/lib/wsmv/commands/close_shell.rb +48 -0
- data/lib/wsmv/commands/create_pipeline.rb +64 -0
- data/lib/wsmv/commands/init_runspace_pool.rb +92 -0
- data/lib/wsmv/commands/receive.rb +81 -0
- data/lib/wsmv/commands/send_data.rb +64 -0
- data/lib/wsmv/psrp_message.rb +289 -0
- data/lib/wsmv/templates/create_pipeline.xml.erb +93 -0
- data/lib/wsmv/templates/init_runspacepool.xml.erb +221 -0
- data/lib/wsmv/templates/runspace_availability.xml.erb +5 -0
- data/lib/wsmv/templates/session_capability.xml.erb +7 -0
- data/psrp.gemspec +39 -0
- data/script.ps1 +2945 -0
- data/test_psrp.rb +32 -0
- metadata +181 -0
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
|
2
|
+
Apache License
|
3
|
+
Version 2.0, January 2004
|
4
|
+
http://www.apache.org/licenses/
|
5
|
+
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
7
|
+
|
8
|
+
1. Definitions.
|
9
|
+
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
12
|
+
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
14
|
+
the copyright owner that is granting the License.
|
15
|
+
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
17
|
+
other entities that control, are controlled by, or are under common
|
18
|
+
control with that entity. For the purposes of this definition,
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
20
|
+
direction or management of such entity, whether by contract or
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
23
|
+
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
25
|
+
exercising permissions granted by this License.
|
26
|
+
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
28
|
+
including but not limited to software source code, documentation
|
29
|
+
source, and configuration files.
|
30
|
+
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
32
|
+
transformation or translation of a Source form, including but
|
33
|
+
not limited to compiled object code, generated documentation,
|
34
|
+
and conversions to other media types.
|
35
|
+
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
37
|
+
Object form, made available under the License, as indicated by a
|
38
|
+
copyright notice that is included in or attached to the work
|
39
|
+
(an example is provided in the Appendix below).
|
40
|
+
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
47
|
+
the Work and Derivative Works thereof.
|
48
|
+
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
50
|
+
the original version of the Work and any modifications or additions
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
62
|
+
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
65
|
+
subsequently incorporated within the Work.
|
66
|
+
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
73
|
+
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
79
|
+
where such license applies only to those patent claims licensable
|
80
|
+
by such Contributor that are necessarily infringed by their
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
83
|
+
institute patent litigation against any entity (including a
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
86
|
+
or contributory patent infringement, then any patent licenses
|
87
|
+
granted to You under this License for that Work shall terminate
|
88
|
+
as of the date such litigation is filed.
|
89
|
+
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
92
|
+
modifications, and in Source or Object form, provided that You
|
93
|
+
meet the following conditions:
|
94
|
+
|
95
|
+
(a) You must give any other recipients of the Work or
|
96
|
+
Derivative Works a copy of this License; and
|
97
|
+
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
99
|
+
stating that You changed the files; and
|
100
|
+
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
103
|
+
attribution notices from the Source form of the Work,
|
104
|
+
excluding those notices that do not pertain to any part of
|
105
|
+
the Derivative Works; and
|
106
|
+
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
109
|
+
include a readable copy of the attribution notices contained
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
112
|
+
of the following places: within a NOTICE text file distributed
|
113
|
+
as part of the Derivative Works; within the Source form or
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
115
|
+
within a display generated by the Derivative Works, if and
|
116
|
+
wherever such third-party notices normally appear. The contents
|
117
|
+
of the NOTICE file are for informational purposes only and
|
118
|
+
do not modify the License. You may add Your own attribution
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
121
|
+
that such additional attribution notices cannot be construed
|
122
|
+
as modifying the License.
|
123
|
+
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
125
|
+
may provide additional or different license terms and conditions
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
129
|
+
the conditions stated in this License.
|
130
|
+
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
134
|
+
this License, without any additional terms or conditions.
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
136
|
+
the terms of any separate license agreement you may have executed
|
137
|
+
with Licensor regarding such Contributions.
|
138
|
+
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
141
|
+
except as required for reasonable and customary use in describing the
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
143
|
+
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
153
|
+
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
159
|
+
incidental, or consequential damages of any character arising as a
|
160
|
+
result of this License or out of the use or inability to use the
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
163
|
+
other commercial damages or losses), even if such Contributor
|
164
|
+
has been advised of the possibility of such damages.
|
165
|
+
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
169
|
+
or other liability obligations and/or rights consistent with this
|
170
|
+
License. However, in accepting such obligations, You may act only
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
175
|
+
of your accepting any such warranty or additional liability.
|
176
|
+
|
177
|
+
END OF TERMS AND CONDITIONS
|
178
|
+
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
180
|
+
|
181
|
+
To apply the Apache License to your work, attach the following
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
183
|
+
replaced with your own identifying information. (Don't include
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
185
|
+
comment syntax for the file format. We also recommend that a
|
186
|
+
file or class name and description of purpose be included on the
|
187
|
+
same "printed page" as the copyright notice for easier
|
188
|
+
identification within third-party archives.
|
189
|
+
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
191
|
+
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
193
|
+
you may not use this file except in compliance with the License.
|
194
|
+
You may obtain a copy of the License at
|
195
|
+
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
197
|
+
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
201
|
+
See the License for the specific language governing permissions and
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# PowerShell Remote Protocol (PSRP) for Ruby
|
2
|
+
|
3
|
+
This is a library the leverages the work done in WinRM for Ruby
|
4
|
+
|
5
|
+
Currently limited to Secure NTLM negotiation. Hopefully this work will be able to get merged into that library and we can just use that gem instead.
|
6
|
+
|
7
|
+
## Install
|
8
|
+
` gem install -r psrp`
|
9
|
+
|
10
|
+
## Example
|
11
|
+
```ruby
|
12
|
+
require 'psrp'
|
13
|
+
endpoint = 'http://192.168.142.231:5985/wsman'
|
14
|
+
psrp = PSRP::PSRPService.new(endpoint, :user => 'some-user', :pass => 'somepassword')
|
15
|
+
puts psrp.run_ps('systeminfo')
|
16
|
+
```
|
data/lib/psrp.rb
ADDED
@@ -0,0 +1,175 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2016 Sam Oluwalana <soluwalana@gmail.com>
|
4
|
+
#
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require 'date'
|
18
|
+
require 'logging'
|
19
|
+
require 'httpclient'
|
20
|
+
require 'builder'
|
21
|
+
require 'gyoku'
|
22
|
+
require 'base64'
|
23
|
+
require 'nori'
|
24
|
+
require 'rexml/document'
|
25
|
+
require 'securerandom'
|
26
|
+
require 'thread'
|
27
|
+
require 'cgi'
|
28
|
+
|
29
|
+
require_relative 'version'
|
30
|
+
require_relative 'transport'
|
31
|
+
|
32
|
+
require_relative 'wsmv/commands/init_runspace_pool'
|
33
|
+
require_relative 'wsmv/commands/create_pipeline'
|
34
|
+
require_relative 'wsmv/commands/close_shell'
|
35
|
+
require_relative 'wsmv/commands/send_data'
|
36
|
+
require_relative 'wsmv/command_output_processor'
|
37
|
+
|
38
|
+
|
39
|
+
module PSRP
|
40
|
+
|
41
|
+
class PSRPService
|
42
|
+
|
43
|
+
DEFAULT_TIMEOUT = 60
|
44
|
+
DEFAULT_MAX_ENV_SIZE = 153600
|
45
|
+
DEFAULT_LOCALE = 'en-US'
|
46
|
+
|
47
|
+
def initialize(endpoint, opts = {})
|
48
|
+
@session_opts = {
|
49
|
+
endpoint: endpoint,
|
50
|
+
max_envelope_size: DEFAULT_MAX_ENV_SIZE,
|
51
|
+
session_id: SecureRandom.uuid.to_s.upcase,
|
52
|
+
operation_timeout: DEFAULT_TIMEOUT,
|
53
|
+
locale: DEFAULT_LOCALE
|
54
|
+
}
|
55
|
+
|
56
|
+
@logger = Logging.logger[self]
|
57
|
+
@logger.level = opts[:log_level] || :debug
|
58
|
+
@logger.add_appenders(Logging.appenders.stdout)
|
59
|
+
@opts = opts
|
60
|
+
|
61
|
+
@xfer = HTTP::Negotiate.new(endpoint, @opts[:user], @opts[:pass], @opts)
|
62
|
+
end
|
63
|
+
|
64
|
+
def run_ps(command, shell_opts = {})
|
65
|
+
|
66
|
+
@logger.debug("[WinRM] opening remote runspacepool on #{@session_opts[:endpoint]}")
|
67
|
+
msg = PSRP::WSMV::InitRunspacePool.new(@session_opts)
|
68
|
+
|
69
|
+
resp_doc = @xfer.send_request(msg.build)
|
70
|
+
|
71
|
+
generated_shell_id = msg.shell_id
|
72
|
+
|
73
|
+
shell_id = REXML::XPath.first(resp_doc, "//*[@Name='ShellId']").text
|
74
|
+
@logger.debug("[WinRM] remote runspace #{shell_id} is open on #{@session_opts[:endpoint]}")
|
75
|
+
|
76
|
+
|
77
|
+
# spec says:
|
78
|
+
# The client sends a wxf:Receive message (section 3.1.5.3.7) to the server to start receiving data from
|
79
|
+
# the server. After each received wxf:ReceiveResponse message (section 3.2.5.3.8), the client sends another
|
80
|
+
# wxf:Receive message until the RunspacePool transitions to a Closed or Broken state.
|
81
|
+
|
82
|
+
out_processor = PSRP::WSMV::CommandOutputProcessor.new(@session_opts, @xfer)
|
83
|
+
|
84
|
+
out_processor.command_output(shell_id, nil)
|
85
|
+
return nil if out_processor.command_done?
|
86
|
+
|
87
|
+
while true
|
88
|
+
out_processor.command_output(shell_id, nil, true)
|
89
|
+
if out_processor.command_done?
|
90
|
+
break
|
91
|
+
end
|
92
|
+
|
93
|
+
runspace_open = false
|
94
|
+
|
95
|
+
# check to make sure the runspace is opened
|
96
|
+
out_processor.defragmented.each do |msg_id, msg|
|
97
|
+
if msg[:message_type] == PSRP::Message::MESSAGE_TYPES[:RUNSPACEPOOL_STATE]
|
98
|
+
xml = REXML::Document.new(msg[:data])
|
99
|
+
REXML::XPath.match(xml, "//*[@N='RunspaceState']").each do |n|
|
100
|
+
runspace_open = (n.text == '2')
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
if runspace_open
|
106
|
+
@logger.debug('Opened the runspace, sending command')
|
107
|
+
|
108
|
+
command_id = SecureRandom.uuid.to_s.upcase
|
109
|
+
pipeline = PSRP::MessageEncoder.new(generated_shell_id, command_id, :CREATE_PIPELINE, {command: CGI.escapeHTML(command)})
|
110
|
+
|
111
|
+
msg = PSRP::WSMV::CreatePipeline.new(@session_opts, shell_id, pipeline.fragments[0])
|
112
|
+
resp_doc = @xfer.send_request(msg.build)
|
113
|
+
|
114
|
+
command_id = REXML::XPath.first(resp_doc, "//#{PSRP::WSMV::NS_WIN_SHELL}:CommandId").text
|
115
|
+
|
116
|
+
# Send remaining fragments, if any
|
117
|
+
if pipeline.fragments.length > 1
|
118
|
+
for i in 1..(pipeline.fragments.length - 1)
|
119
|
+
msg = PSRP::WSMV::SendData.new(@session_opts, shell_id, command_id, pipeline.fragments[i])
|
120
|
+
resp_doc = @xfer.send_request(msg.build)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
@logger.debug("Command Sent, waiting on responses")
|
125
|
+
out_processor.command_output(shell_id, command_id, true)
|
126
|
+
if out_processor.input_required?
|
127
|
+
raise PSRPError.new('This Tool Does Not Support Accepting Runtime Input')
|
128
|
+
end
|
129
|
+
|
130
|
+
# retrieve all of the data from the command
|
131
|
+
while true
|
132
|
+
out_processor.command_output(shell_id, command_id)
|
133
|
+
if out_processor.input_required?
|
134
|
+
raise PSRPError.new('This Tool Does Not Support Accepting Runtime Input')
|
135
|
+
end
|
136
|
+
if out_processor.command_done?
|
137
|
+
break
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
@logger.debug("Closing shell")
|
142
|
+
msg = PSRP::WSMV::CloseShell.new(@session_opts, shell_id)
|
143
|
+
resp_doc = @xfer.send_request(msg.build)
|
144
|
+
|
145
|
+
# Deal with fragmented return values
|
146
|
+
datas = out_processor.defragmented
|
147
|
+
|
148
|
+
# Deal With Errors
|
149
|
+
# But not really doing it well
|
150
|
+
# PSRP - 2.2.3.15 ErrorRecord
|
151
|
+
if out_processor.has_error?
|
152
|
+
datas.each do |msg_id, msg|
|
153
|
+
if msg[:message_type] == PSRP::Message::MESSAGE_TYPES[:ERROR_RECORD]
|
154
|
+
raise PSRPError.new(msg[:data])
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
data = ''
|
160
|
+
out_processor.defragmented.each do |msg_id, msg|
|
161
|
+
xml = REXML::Document.new(msg[:data])
|
162
|
+
# TODO: do better deserialization based on MS-PSRP 2.2.5
|
163
|
+
# Right now this only returns String primitives and String Extended Primitives
|
164
|
+
REXML::XPath.match(xml, "/S|/Obj/S").each do |n|
|
165
|
+
next if n.text.nil? || n.text.empty?
|
166
|
+
data += n.text + "\n"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
return data
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
@@ -0,0 +1,134 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2014 Shawn Neal <sneal@sneal.net>
|
4
|
+
# Copyright 2016 Sam Oluwalana <soluwalana@gmail.com>
|
5
|
+
#
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
7
|
+
# you may not use this file except in compliance with the License.
|
8
|
+
# You may obtain a copy of the License at
|
9
|
+
#
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
#
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
# See the License for the specific language governing permissions and
|
16
|
+
# limitations under the License.
|
17
|
+
require 'rexml/document'
|
18
|
+
|
19
|
+
module PSRP
|
20
|
+
|
21
|
+
# WinRM base class for errors
|
22
|
+
class PSRPError < StandardError; end
|
23
|
+
|
24
|
+
# Authorization Error
|
25
|
+
class AuthorizationError < PSRPError; end
|
26
|
+
|
27
|
+
# A Fault returned in the SOAP response. The XML node is a WSManFault
|
28
|
+
class WSManFault < PSRPError
|
29
|
+
attr_reader :fault_code
|
30
|
+
attr_reader :fault_description
|
31
|
+
|
32
|
+
def initialize(fault_description, fault_code)
|
33
|
+
@fault_description = fault_description
|
34
|
+
@fault_code = fault_code
|
35
|
+
super("[WSMAN ERROR CODE: #{fault_code}]: #{fault_description}")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# A Fault returned in the SOAP response. The XML node is a MSFT_WmiError
|
40
|
+
class WMIError < PSRPError
|
41
|
+
attr_reader :error_code
|
42
|
+
attr_reader :error
|
43
|
+
|
44
|
+
def initialize(error, error_code)
|
45
|
+
@error = error
|
46
|
+
@error_code = error_code
|
47
|
+
super("[WMI ERROR CODE: #{error_code}]: #{error}")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# non-200 response without a SOAP fault
|
52
|
+
class HTTPTransportError < PSRPError
|
53
|
+
attr_reader :status_code
|
54
|
+
|
55
|
+
def initialize(msg, status_code)
|
56
|
+
@status_code = status_code
|
57
|
+
super(msg + " (#{status_code}).")
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Handles the raw WinRM HTTP response. Returns the body as an XML doc
|
62
|
+
# or raises the appropriate WinRM error if the response is an error.
|
63
|
+
class ResponseHandler
|
64
|
+
# @param [String] The raw unparsed response body, if any
|
65
|
+
# @param [Integer] The HTTP response status code
|
66
|
+
def initialize(response_body, status_code)
|
67
|
+
@response_body = response_body
|
68
|
+
@status_code = status_code
|
69
|
+
end
|
70
|
+
|
71
|
+
# Processes the response from the WinRM service and either returns an XML
|
72
|
+
# doc or raises an appropriate error.
|
73
|
+
#
|
74
|
+
# @returns [REXML::Document] The parsed response body
|
75
|
+
def parse_to_xml
|
76
|
+
raise_if_error
|
77
|
+
response_xml
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
def response_xml
|
83
|
+
@response_xml ||= REXML::Document.new(@response_body)
|
84
|
+
rescue REXML::ParseException => e
|
85
|
+
puts @response_body
|
86
|
+
raise PSRP::HTTPTransportError.new(
|
87
|
+
"Unable to parse WinRM response: #{e.message}", @status_code)
|
88
|
+
end
|
89
|
+
|
90
|
+
def raise_if_error
|
91
|
+
return if @status_code == 200
|
92
|
+
raise_if_auth_error
|
93
|
+
raise_if_wsman_fault
|
94
|
+
raise_if_wmi_error
|
95
|
+
raise_transport_error
|
96
|
+
end
|
97
|
+
|
98
|
+
def raise_if_auth_error
|
99
|
+
fail AuthorizationError if @status_code == 401
|
100
|
+
end
|
101
|
+
|
102
|
+
def raise_if_wsman_fault
|
103
|
+
soap_errors = REXML::XPath.match(
|
104
|
+
response_xml,
|
105
|
+
"//#{PSRP::WSMV::NS_SOAP_ENV}:Body/#{PSRP::WSMV::NS_SOAP_ENV}:Fault/*")
|
106
|
+
return if soap_errors.empty?
|
107
|
+
fault = REXML::XPath.first(
|
108
|
+
soap_errors,
|
109
|
+
"//#{PSRP::WSMV::NS_WSMAN_FAULT}:WSManFault")
|
110
|
+
fail PSRP::WSManFault.new(fault.to_s, fault.attributes['Code']) unless fault.nil?
|
111
|
+
end
|
112
|
+
|
113
|
+
def raise_if_wmi_error
|
114
|
+
soap_errors = REXML::XPath.match(
|
115
|
+
response_xml,
|
116
|
+
"//#{PSRP::WSMV::NS_SOAP_ENV}:Body/#{PSRP::WSMV::NS_SOAP_ENV}:Fault/*")
|
117
|
+
return if soap_errors.empty?
|
118
|
+
|
119
|
+
error = REXML::XPath.first(
|
120
|
+
soap_errors,
|
121
|
+
"//#{PSRP::WSMV::NS_WSMAN_MSFT}:MSFT_WmiError")
|
122
|
+
return if error.nil?
|
123
|
+
|
124
|
+
error_code = REXML::XPath.first(
|
125
|
+
error,
|
126
|
+
"//#{PSRP::WSMV::NS_WSMAN_MSFT}:error_Code").text
|
127
|
+
fail PSRP::WMIError.new(error.to_s, error_code)
|
128
|
+
end
|
129
|
+
|
130
|
+
def raise_transport_error
|
131
|
+
fail PSRP::HTTPTransportError.new('Bad HTTP response returned from server', @status_code)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|