Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -i option to Msf::Ui::Console::CommandDispatcher::Session mixin's sessions command #18885

Merged
merged 4 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 39 additions & 6 deletions lib/msf/ui/console/command_dispatcher/session.rb
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add in a Rex Parser here?:

@@session_opts = Rex::Parser::Arguments.new(
            ['-h', '--help] => [ false, 'Show this message' ],
            ['-i', '--interact'] => [ true, 'Interact with a provided session ID' ]
          )

@@session_opts.parse(args) do |opt, _idx, val|
              case opt
              when '-h'
                ...
              when '-i'
                ...
              else
                 ...
              end
end

Copy link
Contributor Author

@errorxyz errorxyz Mar 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there's only one option needed here, for simplicity's sake I feel we don't need this. If we use this, we'll also have to separately handle sessions <id> as done here. If there's plans of adding new options here, it'd make sense to use the rex parser. I'll happily add the rex parser if you still feel we should use it.

Yup will add that👍

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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why this test was changed, rather than a new one added for sessions -i <id>? It seems like now we only test the -i functionality, with the previous functionality of sessions <id> not being tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initially I thought since both use the same back-end, a single test would suffice. But now in hindsight, I feel we'd need two separate tests. Will add it👍

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