winrm-elevated 0.4.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -8
- data/.rspec +3 -3
- data/.rubocop.yml +17 -17
- data/.travis.yml +10 -10
- data/Gemfile +9 -9
- data/LICENSE +202 -202
- data/README.md +86 -82
- data/Rakefile +28 -28
- data/VERSION +1 -1
- data/appveyor.yml +39 -39
- data/changelog.md +18 -18
- data/lib/winrm/shells/elevated.rb +99 -0
- data/lib/winrm-elevated/scripts/elevated_shell.ps1 +108 -105
- data/lib/winrm-elevated.rb +18 -25
- data/spec/config-example.yml +3 -5
- data/spec/matchers.rb +50 -64
- data/spec/powershell_elevated_spec.rb +87 -93
- data/spec/spec_helper.rb +58 -46
- data/winrm-elevated.gemspec +33 -33
- metadata +10 -10
- data/lib/winrm-elevated/runner.rb +0 -83
@@ -1,105 +1,108 @@
|
|
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
|
-
$task_xml = $task_xml.Replace("{
|
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
|
-
$host.ui.
|
85
|
-
}
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
}
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
1
|
+
$username = "<%= username %>"
|
2
|
+
$password = "<%= password %>"
|
3
|
+
$script_file = "<%= script_path %>"
|
4
|
+
|
5
|
+
$pass_to_use = $password
|
6
|
+
$logon_type = 1
|
7
|
+
$logon_type_xml = "<LogonType>Password</LogonType>"
|
8
|
+
if($pass_to_use.length -eq 0) {
|
9
|
+
$pass_to_use = $null
|
10
|
+
$logon_type = 5
|
11
|
+
$logon_type_xml = ""
|
12
|
+
}
|
13
|
+
|
14
|
+
$task_name = "WinRM_Elevated_Shell"
|
15
|
+
$out_file = [System.IO.Path]::GetTempFileName()
|
16
|
+
$err_file = [System.IO.Path]::GetTempFileName()
|
17
|
+
|
18
|
+
$task_xml = @'
|
19
|
+
<?xml version="1.0" encoding="UTF-16"?>
|
20
|
+
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
|
21
|
+
<Principals>
|
22
|
+
<Principal id="Author">
|
23
|
+
<UserId>{username}</UserId>
|
24
|
+
{logon_type}
|
25
|
+
<RunLevel>HighestAvailable</RunLevel>
|
26
|
+
</Principal>
|
27
|
+
</Principals>
|
28
|
+
<Settings>
|
29
|
+
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
|
30
|
+
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
|
31
|
+
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
|
32
|
+
<AllowHardTerminate>true</AllowHardTerminate>
|
33
|
+
<StartWhenAvailable>false</StartWhenAvailable>
|
34
|
+
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
|
35
|
+
<IdleSettings>
|
36
|
+
<StopOnIdleEnd>false</StopOnIdleEnd>
|
37
|
+
<RestartOnIdle>false</RestartOnIdle>
|
38
|
+
</IdleSettings>
|
39
|
+
<AllowStartOnDemand>true</AllowStartOnDemand>
|
40
|
+
<Enabled>true</Enabled>
|
41
|
+
<Hidden>false</Hidden>
|
42
|
+
<RunOnlyIfIdle>false</RunOnlyIfIdle>
|
43
|
+
<WakeToRun>false</WakeToRun>
|
44
|
+
<ExecutionTimeLimit>PT24H</ExecutionTimeLimit>
|
45
|
+
<Priority>4</Priority>
|
46
|
+
</Settings>
|
47
|
+
<Actions Context="Author">
|
48
|
+
<Exec>
|
49
|
+
<Command>cmd</Command>
|
50
|
+
<Arguments>{arguments}</Arguments>
|
51
|
+
</Exec>
|
52
|
+
</Actions>
|
53
|
+
</Task>
|
54
|
+
'@
|
55
|
+
|
56
|
+
$arguments = "/c powershell.exe -File $script_file > $out_file 2>$err_file"
|
57
|
+
|
58
|
+
$task_xml = $task_xml.Replace("{arguments}", $arguments)
|
59
|
+
$task_xml = $task_xml.Replace("{username}", $username)
|
60
|
+
$task_xml = $task_xml.Replace("{logon_type}", $logon_type_xml)
|
61
|
+
|
62
|
+
$schedule = New-Object -ComObject "Schedule.Service"
|
63
|
+
$schedule.Connect()
|
64
|
+
$task = $schedule.NewTask($null)
|
65
|
+
$task.XmlText = $task_xml
|
66
|
+
$folder = $schedule.GetFolder("\")
|
67
|
+
$folder.RegisterTaskDefinition($task_name, $task, 6, $username, $pass_to_use, $logon_type, $null) | Out-Null
|
68
|
+
|
69
|
+
$registered_task = $folder.GetTask("\$task_name")
|
70
|
+
$registered_task.Run($null) | Out-Null
|
71
|
+
|
72
|
+
$timeout = 10
|
73
|
+
$sec = 0
|
74
|
+
while ( (!($registered_task.state -eq 4)) -and ($sec -lt $timeout) ) {
|
75
|
+
Start-Sleep -s 1
|
76
|
+
$sec++
|
77
|
+
}
|
78
|
+
|
79
|
+
function SlurpOutput($file, $cur_line, $out_type) {
|
80
|
+
if (Test-Path $file) {
|
81
|
+
get-content $file | select -skip $cur_line | ForEach {
|
82
|
+
$cur_line += 1
|
83
|
+
if ($out_type -eq 'err') {
|
84
|
+
$host.ui.WriteErrorLine("$_")
|
85
|
+
} else {
|
86
|
+
$host.ui.WriteLine("$_")
|
87
|
+
}
|
88
|
+
}
|
89
|
+
}
|
90
|
+
return $cur_line
|
91
|
+
}
|
92
|
+
|
93
|
+
$err_cur_line = 0
|
94
|
+
$out_cur_line = 0
|
95
|
+
do {
|
96
|
+
Start-Sleep -m 100
|
97
|
+
$out_cur_line = SlurpOutput $out_file $out_cur_line 'out'
|
98
|
+
$err_cur_line = SlurpOutput $err_file $err_cur_line 'err'
|
99
|
+
} while (!($registered_task.state -eq 3))
|
100
|
+
|
101
|
+
del $out_file
|
102
|
+
del $err_file
|
103
|
+
del $script_file
|
104
|
+
|
105
|
+
$exit_code = $registered_task.LastTaskResult
|
106
|
+
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($schedule) | Out-Null
|
107
|
+
|
108
|
+
exit $exit_code
|
data/lib/winrm-elevated.rb
CHANGED
@@ -1,25 +1,18 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
#
|
3
|
-
# Copyright 2015 Shawn Neal <sneal@sneal.net>
|
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 'winrm'
|
18
|
-
require_relative 'winrm
|
19
|
-
|
20
|
-
module WinRM
|
21
|
-
# WinRM File System
|
22
|
-
module Elevated
|
23
|
-
# Top level module code
|
24
|
-
end
|
25
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
#
|
3
|
+
# Copyright 2015 Shawn Neal <sneal@sneal.net>
|
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 'winrm'
|
18
|
+
require_relative 'winrm/shells/elevated'
|
data/spec/config-example.yml
CHANGED
data/spec/matchers.rb
CHANGED
@@ -1,64 +1,50 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
require 'rspec/expectations'
|
3
|
-
|
4
|
-
# rspec matchers
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
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
|
-
end
|
52
|
-
failure_message do |actual_output|
|
53
|
-
"expected that '#{WinRMSpecs.stderr(actual_output)}' would have no stderr"
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
RSpec::Matchers.define :have_exit_code do |expected_exit_code|
|
58
|
-
match do |actual_output|
|
59
|
-
expected_exit_code == actual_output[:exitcode]
|
60
|
-
end
|
61
|
-
failure_message do |actual_output|
|
62
|
-
"expected exit code #{expected_exit_code}, but got #{actual_output[:exitcode]}"
|
63
|
-
end
|
64
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rspec/expectations'
|
3
|
+
|
4
|
+
# rspec matchers
|
5
|
+
RSpec::Matchers.define :have_stdout_match do |expected_stdout|
|
6
|
+
match do |actual_output|
|
7
|
+
!expected_stdout.match(actual_output.stdout).nil?
|
8
|
+
end
|
9
|
+
failure_message do |actual_output|
|
10
|
+
"expected that '#{actual_output.stdout}' would match #{expected_stdout}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
RSpec::Matchers.define :have_stderr_match do |expected_stderr|
|
15
|
+
match do |actual_output|
|
16
|
+
!expected_stderr.match(actual_output.stderr).nil?
|
17
|
+
end
|
18
|
+
failure_message do |actual_output|
|
19
|
+
"expected that '#{actual_output.stderr}' would match #{expected_stderr}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
RSpec::Matchers.define :have_no_stdout do
|
24
|
+
match do |actual_output|
|
25
|
+
stdout = actual_output.stdout
|
26
|
+
stdout == '\r\n' || stdout == ''
|
27
|
+
end
|
28
|
+
failure_message do |actual_output|
|
29
|
+
"expected that '#{actual_output.stdout}' would have no stdout"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
RSpec::Matchers.define :have_no_stderr do
|
34
|
+
match do |actual_output|
|
35
|
+
stderr = actual_output.stderr
|
36
|
+
stderr == '\r\n' || stderr == ''
|
37
|
+
end
|
38
|
+
failure_message do |actual_output|
|
39
|
+
"expected that '#{actual_output.stderr}' would have no stderr"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
RSpec::Matchers.define :have_exit_code do |expected_exit_code|
|
44
|
+
match do |actual_output|
|
45
|
+
expected_exit_code == actual_output.exitcode
|
46
|
+
end
|
47
|
+
failure_message do |actual_output|
|
48
|
+
"expected exit code #{expected_exit_code}, but got #{actual_output.exitcode}"
|
49
|
+
end
|
50
|
+
end
|
@@ -1,93 +1,87 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
describe 'powershell elevated runner', integration: true do
|
3
|
-
describe 'ipconfig' do
|
4
|
-
subject(:output) {
|
5
|
-
it { should have_exit_code 0 }
|
6
|
-
it { should have_stdout_match(/Windows IP Configuration/) }
|
7
|
-
it { should have_no_stderr }
|
8
|
-
end
|
9
|
-
|
10
|
-
describe 'ipconfig as Service' do
|
11
|
-
subject(:output)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
EOH
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
outvar
|
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
|
-
expect(output.output).to eq("Hello\r\n#< CLIXML\r\n<Objs Version=\"1.1.0.1\" " \
|
89
|
-
"xmlns=\"http://schemas.microsoft.com/powershell/2004/04\">" \
|
90
|
-
"<S S=\"Error\">, world!_x000D__x000A_</S></Objs>\r\n")
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
describe 'powershell elevated runner', integration: true do
|
3
|
+
describe 'ipconfig' do
|
4
|
+
subject(:output) { elevated_shell.run('ipconfig') }
|
5
|
+
it { should have_exit_code 0 }
|
6
|
+
it { should have_stdout_match(/Windows IP Configuration/) }
|
7
|
+
it { should have_no_stderr }
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'ipconfig as Service' do
|
11
|
+
subject(:output) do
|
12
|
+
elevated_shell.username = 'System'
|
13
|
+
elevated_shell.password = nil
|
14
|
+
elevated_shell.run('ipconfig')
|
15
|
+
end
|
16
|
+
it { should have_exit_code 0 }
|
17
|
+
it { should have_stdout_match(/Windows IP Configuration/) }
|
18
|
+
it { should have_no_stderr }
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 'echo \'hello world\' using apostrophes' do
|
22
|
+
subject(:output) { elevated_shell.run("echo 'hello world'") }
|
23
|
+
it { should have_exit_code 0 }
|
24
|
+
it { should have_stdout_match(/hello world/) }
|
25
|
+
it { should have_no_stderr }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'ipconfig with incorrect argument -z' do
|
29
|
+
subject(:output) { elevated_shell.run('ipconfig 127.0.0.1 -z') }
|
30
|
+
it { should have_exit_code 1 }
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'Math area calculation' do
|
34
|
+
subject(:output) do
|
35
|
+
cmd = <<-EOH
|
36
|
+
$diameter = 4.5
|
37
|
+
$area = [Math]::pow([Math]::PI * ($diameter/2), 2)
|
38
|
+
Write-Host $area
|
39
|
+
EOH
|
40
|
+
elevated_shell.run(cmd)
|
41
|
+
end
|
42
|
+
it { should have_exit_code 0 }
|
43
|
+
it { should have_stdout_match(/49.9648722805149/) }
|
44
|
+
it { should have_no_stderr }
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'ipconfig with a block' do
|
48
|
+
subject(:stdout) do
|
49
|
+
outvar = ''
|
50
|
+
elevated_shell.run('ipconfig') do |stdout, _stderr|
|
51
|
+
outvar << stdout
|
52
|
+
end
|
53
|
+
outvar
|
54
|
+
end
|
55
|
+
it { should match(/Windows IP Configuration/) }
|
56
|
+
end
|
57
|
+
|
58
|
+
describe 'capturing output from Write-Host and Write-Error' do
|
59
|
+
subject(:output) do
|
60
|
+
script = <<-eos
|
61
|
+
Write-Host 'Hello'
|
62
|
+
$host.ui.WriteErrorLine(', world!')
|
63
|
+
eos
|
64
|
+
|
65
|
+
@captured_stdout = ''
|
66
|
+
@captured_stderr = ''
|
67
|
+
elevated_shell.run(script) do |stdout, stderr|
|
68
|
+
@captured_stdout << stdout if stdout
|
69
|
+
@captured_stderr << stderr if stderr
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'should have stdout' do
|
74
|
+
expect(output.stdout).to eq("Hello\r\n")
|
75
|
+
expect(output.stdout).to eq(@captured_stdout)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should have stderr' do
|
79
|
+
expect(output.stderr).to eq(", world!\r\n")
|
80
|
+
expect(output.stderr).to eq(@captured_stderr)
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should have output' do
|
84
|
+
expect(output.output).to eq("Hello\r\n, world!\r\n")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|