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.']
)
@@sessions_opts =
Rex::Parser::Arguments.new(
  ['-h', '--help'] => [ false, 'Show this message' ],
  ['-i', '--interact'] => [ true, 'Interact with a provided session ID', '<id>' ]
)

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



43
44
45
46
47
48
49
50
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 43

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



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

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.



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

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 81

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



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

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



72
73
74
75
76
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 72

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



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 122

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



112
113
114
115
116
117
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 112

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



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
231
232
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 201

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



193
194
195
196
197
198
199
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 193

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 234

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 152

def cmd_sessions(*args)
  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
    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 = sid
    session.interacting = false
  end
end

#cmd_sessions_helpObject



144
145
146
147
148
149
150
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 144

def cmd_sessions_help
  print_line('Usage: sessions [options] or sessions [id]')
  print_line
  print_line('Interact with a different session ID.')
  print(@@sessions_opts.usage)
  print_line
end

#commandsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/msf/ui/console/command_dispatcher/session.rb', line 20

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