Module: Msf::Ui::Console::CommandDispatcher::Session

Constant Summary collapse

@@irb_opts =
Rex::Parser::Arguments.new(
  %w[-h --help] => [false, 'Help menu.' ],
  '-e' => [true, 'Expression to evaluate.']
)

Instance Attribute Summary

Attributes included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Rex::Ui::Text::DispatcherShell::CommandDispatcher

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #docs_dir, #help_to_s, included, #initialize, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_directory, #tab_complete_filenames, #tab_complete_generic, #tab_complete_source_address, #unknown_command, #update_prompt

Instance Method Details

#cmd_background(*args) ⇒ Object Also known as: cmd_bg



37
38
39
40
41
42
43
44
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 37

def cmd_background(*args)
  if args.include?('-h') || args.include?('--help')
    cmd_background_help
    return
  end
  print_status("Backgrounding session #{session.name}...")
  session.interacting = false
end

#cmd_background_helpObject Also known as: cmd_bg_help



30
31
32
33
34
35
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 30

def cmd_background_help
  print_line('Usage: background')
  print_line
  print_line('Stop interacting with this session and return to the parent prompt')
  print_line
end

#cmd_exit(*args) ⇒ Object Also known as: cmd_quit

Terminates the session.



52
53
54
55
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 52

def cmd_exit(*args)
  print_status("Shutting down session: #{session.sid}")
  session.exit
end

#cmd_irb(*args) ⇒ Object

Open an interactive Ruby shell on the current session



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 75

def cmd_irb(*args)
  expressions = []

  # Parse the command options
  @@irb_opts.parse(args) do |opt, _idx, val|
    case opt
    when '-e'
      expressions << val
    when '-h', '--help'
      return cmd_irb_help
    end
  end

  framework = session.framework

  if expressions.empty?
    print_status('Starting IRB shell...')
    print_status("You are in the session object\n")
    framework.history_manager.with_context(name: :irb) do
      Rex::Ui::Text::IrbShell.new(session).run
    end
  else
    # XXX: No vprint_status here
    if framework.datastore['VERBOSE'].to_s == 'true'
      print_status("You are executing expressions in #{binding.receiver}")
    end

    expressions.each { |expression| eval(expression, binding) }
  end
end

#cmd_irb_helpObject



59
60
61
62
63
64
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 59

def cmd_irb_help
  print_line('Usage: irb')
  print_line
  print_line('Open an interactive Ruby shell on the current session.')
  print @@irb_opts.usage
end

#cmd_irb_tabs(str, words) ⇒ Object



66
67
68
69
70
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 66

def cmd_irb_tabs(str, words)
  return [] if words.length > 1

  @@irb_opts.option_keys
end

#cmd_pry(*args) ⇒ Object

Open the Pry debugger on the current session



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 116

def cmd_pry(*args)
  if args.include?('-h') || args.include?('--help')
    cmd_pry_help
    return
  end

  begin
    require 'pry'
  rescue LoadError
    print_error('Failed to load Pry, try "gem install pry"')
    return
  end

  print_status('Starting Pry shell...')
  print_status("You are in the session object\n")

  Pry.config.history_load = false
  session.framework.history_manager.with_context(history_file: Msf::Config.pry_history, name: :pry) do
    session.pry
  end
end

#cmd_pry_helpObject



106
107
108
109
110
111
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 106

def cmd_pry_help
  print_line 'Usage: pry'
  print_line
  print_line 'Open the Pry debugger on the current session.'
  print_line
end

#cmd_resource(*args) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 168

def cmd_resource(*args)
  if args.empty? || args.include?('-h') || args.include?('--help')
    cmd_resource_help
    return false
  end

  args.each do |res|
    good_res = nil
    if res == '-'
      good_res = res
    elsif ::File.exist?(res)
      good_res = res
    elsif [
      ::Msf::Config.script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter',
      ::Msf::Config.user_script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter'
    ].each do |dir|
            res_path = dir + ::File::SEPARATOR + res
            if ::File.exist?(res_path)
              good_res = res_path
              break
            end
          end
      # let's check to see if it's in the scripts/resource dir (like when tab completed)
    end
    unless good_res
      print_error("#{res} is not a valid resource file")
      next
    end

    session.console.load_resource(good_res)
  end
end

#cmd_resource_helpObject



160
161
162
163
164
165
166
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 160

def cmd_resource_help
  print_line 'Usage: resource path1 [path2 ...]'
  print_line
  print_line 'Run the commands stored in the supplied files. (- for stdin, press CTRL+D to end input from stdin)'
  print_line 'Resource files may also contain ERB or Ruby code between <ruby></ruby> tags.'
  print_line
end

#cmd_resource_tabs(str, words) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 201

def cmd_resource_tabs(str, words)
  tabs = []
  # return tabs if words.length > 1
  if (str && str =~ (/^#{Regexp.escape(::File::SEPARATOR)}/))
    # then you are probably specifying a full path so let's just use normal file completion
    return tab_complete_filenames(str, words)
  elsif (!(words[1]) || !words[1].match(%r{^/}))
    # then let's start tab completion in the scripts/resource directories
    begin
      [
        ::Msf::Config.script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter',
        ::Msf::Config.user_script_directory + ::File::SEPARATOR + 'resource' + ::File::SEPARATOR + 'meterpreter',
        '.'
      ].each do |dir|
        next if !::File.exist? dir

        tabs += ::Dir.new(dir).find_all do |e|
          path = dir + ::File::SEPARATOR + e
          ::File.file?(path) and ::File.readable?(path)
        end
      end
    rescue StandardError => e
      elog('Problem tab completing resource file names in the scripts/resource directories', error: e)
    end
  else
    tabs += tab_complete_filenames(str, words)
  end

  return tabs
end

#cmd_sessions(*args) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 146

def cmd_sessions(*args)
  if args.empty? || args[0].to_i == 0
    cmd_sessions_help
  elsif args[0].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.interacting = false
  end
end

#cmd_sessions_helpObject



138
139
140
141
142
143
144
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 138

def cmd_sessions_help
  print_line('Usage: 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
end

#commandsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 14

def commands
  {
    '?' => 'Help menu',
    'background' => 'Backgrounds the current session',
    'bg' => 'Alias for background',
    'exit' => 'Terminate the session',
    'help' => 'Help menu',
    'irb' => 'Open an interactive Ruby shell on the current session',
    'pry' => 'Open the Pry debugger on the current session',
    'quit' => 'Terminate the session',
    'resource' => 'Run the commands stored in a file',
    'uuid' => 'Get the UUID for the current session',
    'sessions' => 'Quickly switch to another session'
  }
end