CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/spec/lib/msf/base/sessions/command_shell_spec.rb
Views: 11766
1
# frozen_string_literal: true
2
3
require 'spec_helper'
4
5
RSpec.describe Msf::Sessions::CommandShell do
6
let(:type) { 'shell' }
7
8
describe '.type' do
9
it 'should have the correct type' do
10
expect(described_class.type).to eq(type)
11
end
12
end
13
14
describe '.can_cleanup_files' do
15
it 'should be able to cleanup files' do
16
expect(described_class.can_cleanup_files).to eq(true)
17
end
18
end
19
20
context 'when we have a command shell session' do
21
subject(:command_shell) { described_class.new(nil) }
22
let(:command_functions) do
23
%i[help background sessions resource shell download upload source irb pry].map { |command| "cmd_#{command}" }
24
end
25
let(:command_help_functions) do
26
command_functions.map { |command| "#{command}_help" }
27
end
28
let(:description) { 'Command shell' }
29
30
describe '#type' do
31
it 'should have the correct type' do
32
expect(subject.type).to eq(type)
33
end
34
end
35
36
describe '#desc' do
37
it 'should have the correct description' do
38
expect(subject.desc).to eq(description)
39
end
40
end
41
42
describe '#abort_foreground_supported' do
43
it 'should not support aborting the process running in the session' do
44
expect(subject.abort_foreground_supported).to be(true)
45
end
46
end
47
48
describe '#shell_init' do
49
it 'should initialise the shell by default' do
50
expect(subject.shell_init).to be(true)
51
end
52
end
53
54
describe 'Builtin commands' do
55
%i[background sessions resource shell download upload source irb pry].each do |command|
56
before(:each) do
57
allow(subject).to receive("cmd_#{command}_help")
58
end
59
60
describe "#cmd_#{command}" do
61
context 'when called with the `-h` argument' do
62
it 'should call the corresponding help function' do
63
subject.send("cmd_#{command}", '-h')
64
expect(subject).to have_received("cmd_#{command}_help")
65
end
66
end
67
68
context 'when called with the `--help` argument' do
69
it 'should call the corresponding help function' do
70
subject.send("cmd_#{command}", '--help')
71
expect(subject).to have_received("cmd_#{command}_help")
72
end
73
end
74
end
75
end
76
end
77
78
describe '#run_builtin_cmd' do
79
%i[help background sessions resource shell download upload source irb pry].each do |command|
80
before(:each) do
81
allow(subject).to receive("cmd_#{command}")
82
end
83
context "when called with `#{command}`" do
84
it "should call cmd_#{command}" do
85
subject.run_builtin_cmd(command.to_s, nil)
86
expect(subject).to have_received("cmd_#{command}")
87
end
88
end
89
end
90
end
91
92
describe '#run_single' do
93
before(:each) do
94
allow(subject).to receive(:run_builtin_cmd)
95
allow(subject).to receive(:shell_write)
96
end
97
%i[help background sessions resource shell download upload source irb pry].each do |command|
98
context "when called with builtin command `#{command}`" do
99
it 'should call the builtin function' do
100
subject.run_single(command.to_s)
101
expect(subject).to have_received(:run_builtin_cmd)
102
end
103
end
104
end
105
106
context 'when called with a non-builtin command' do
107
let(:cmd) { 'some_command' }
108
it 'should write the command to the shell' do
109
subject.run_single(cmd)
110
expect(subject).to have_received(:shell_write).with("#{cmd}\n")
111
end
112
end
113
end
114
115
describe '#process_autoruns' do
116
let(:initial_auto_run_script) { 'initial_auto_run_script' }
117
let(:auto_run_script) { 'auto_run_script' }
118
119
before(:each) do
120
allow(subject).to receive(:execute_script)
121
end
122
123
context 'The datastore is empty' do
124
let(:datastore) do
125
Msf::DataStore.new
126
end
127
it 'should not execute any script' do
128
subject.process_autoruns(datastore)
129
is_expected.not_to have_received(:execute_script)
130
end
131
end
132
133
context 'The datastore contains an `InitialAutoRunScript`' do
134
let(:datastore) do
135
datastore = Msf::DataStore.new
136
datastore['InitialAutoRunScript'] = initial_auto_run_script
137
datastore
138
end
139
140
it 'should execute the script' do
141
subject.process_autoruns(datastore)
142
is_expected.to have_received(:execute_script).with(initial_auto_run_script)
143
end
144
end
145
146
context 'The datastore contains an `AutoRunScript`' do
147
let(:datastore) do
148
datastore = Msf::DataStore.new
149
datastore['AutoRunScript'] = auto_run_script
150
datastore
151
end
152
it 'should execute the script' do
153
subject.process_autoruns(datastore)
154
is_expected.to have_received(:execute_script).with(auto_run_script)
155
end
156
end
157
158
context 'The datastore contains both `InitialAutoRunScript` and `AutoRunScript`' do
159
let(:datastore) do
160
datastore = Msf::DataStore.new
161
datastore['InitialAutoRunScript'] = initial_auto_run_script
162
datastore['AutoRunScript'] = auto_run_script
163
datastore
164
end
165
it 'should execute initial script before the auto run script' do
166
subject.process_autoruns(datastore)
167
is_expected.to have_received(:execute_script).ordered.with(initial_auto_run_script)
168
is_expected.to have_received(:execute_script).ordered.with(auto_run_script)
169
end
170
end
171
end
172
173
context 'when the platform is windows' do
174
let(:platform) { 'windows' }
175
before(:each) do
176
subject.platform = platform
177
end
178
179
it 'should not support aborting the process running in the session' do
180
expect(subject.abort_foreground_supported).to be(false)
181
end
182
end
183
end
184
end
185
186