Skip to content

Commit

Permalink
Land #18885, update the sessions command to be consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
adfoster-r7 authored Mar 20, 2024
2 parents 1f1e6b2 + 287b072 commit 298e03b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
45 changes: 39 additions & 6 deletions lib/msf/ui/console/command_dispatcher/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<id>' ]
)

def commands
{
'?' => 'Help menu',
Expand Down Expand Up @@ -136,23 +142,50 @@ def cmd_pry(*args)
end

def cmd_sessions_help
print_line('Usage: sessions <id>')
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 <session id>')
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 298e03b

Please sign in to comment.