diff --git a/lib/msf/ui/console/command_dispatcher/session.rb b/lib/msf/ui/console/command_dispatcher/session.rb index 9ba26213fef2..de791bdea29d 100644 --- a/lib/msf/ui/console/command_dispatcher/session.rb +++ b/lib/msf/ui/console/command_dispatcher/session.rb @@ -11,6 +11,12 @@ module Session %w[-h --help] => [false, 'Help menu.' ], '-e' => [true, 'Expression to evaluate.'] ) + + @@sessions_opts = Rex::Parser::Arguments.new( + ['-h', '--help'] => [ false, 'Show this message' ], + ['-i', '--interact'] => [ true, 'Interact with a provided session ID', '' ] + ) + def commands { '?' => 'Help menu', @@ -136,23 +142,50 @@ def cmd_pry(*args) end def cmd_sessions_help - print_line('Usage: sessions ') + print_line('Usage: sessions [options] or sessions [id]') print_line - print_line('Interact with a different session Id.') - print_line('This works the same as calling this from the MSF shell: sessions -i ') + print_line('Interact with a different session ID.') + print(@@sessions_opts.usage) print_line end def cmd_sessions(*args) - if args.empty? || args[0].to_i == 0 + if args.empty? + cmd_sessions_help + return false + end + + sid = nil + + if args.length == 1 && args[0] =~ /-?\d+/ + sid = args[0].to_i + else + @@sessions_opts.parse(args) do |opt, _idx, val| + case opt + when '-h', '--help' + cmd_sessions_help + return false + when '-i', '--interact' + sid = val.to_i + else + cmd_sessions_help + return false + end + end + end + + if sid == 0 || sid.nil? cmd_sessions_help - elsif args[0].to_s == session.name.to_s + return false + end + + if sid.to_s == session.name.to_s print_status("Session #{session.name} is already interactive.") else print_status("Backgrounding session #{session.name}...") # store the next session id so that it can be referenced as soon # as this session is no longer interacting - session.next_session = args[0] + session.next_session = sid session.interacting = false end end diff --git a/spec/support/shared/examples/msf/ui/console/command_dispatcher/session.rb b/spec/support/shared/examples/msf/ui/console/command_dispatcher/session.rb index 962b221d35bc..4679811c6686 100644 --- a/spec/support/shared/examples/msf/ui/console/command_dispatcher/session.rb +++ b/spec/support/shared/examples/msf/ui/console/command_dispatcher/session.rb @@ -66,12 +66,20 @@ allow(session).to receive(:next_session=) end - let(:new_session_id) { 2 } + let(:new_session_id) { '2' } + + it 'backgrounds the session and switches to the new session' do + subject.cmd_sessions('-i', new_session_id) + expect(session).to have_received(:interacting=).with(false) + expect(session).to have_received(:next_session=).with(new_session_id.to_i) + end + + let(:new_session_id) { '1' } it 'backgrounds the session and switches to the new session' do subject.cmd_sessions(new_session_id) expect(session).to have_received(:interacting=).with(false) - expect(session).to have_received(:next_session=).with(new_session_id) + expect(session).to have_received(:next_session=).with(new_session_id.to_i) end end end