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