Class: Msf::Plugin::Aggregator::AggregatorCommandDispatcher

Inherits:
Object
  • Object
show all
Includes:
Ui::Console::CommandDispatcher
Defined in:
plugins/aggregator.rb

Instance Attribute Summary

Attributes included from Ui::Console::CommandDispatcher

#driver

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

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Ui::Console::CommandDispatcher

#active_module, #active_module=, #active_session, #active_session=, #build_range_array, #docs_dir, #framework, #initialize, #log_error, #remove_lines

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, #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, #update_prompt

Instance Method Details

#cmd_aggregator_addresses(*_args) ⇒ Object


223
224
225
226
227
228
229
230
231
232
233
# File 'plugins/aggregator.rb', line 223

def cmd_aggregator_addresses(*_args)
  return if !aggregator_verify

  address_list = @aggregator.available_addresses
  return if address_list.nil?

  print_status("Remote addresses found:")
  address_list.each do |addr|
    print_status("    #{addr}")
  end
end

#cmd_aggregator_cable_add(*args) ⇒ Object


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 'plugins/aggregator.rb', line 235

def cmd_aggregator_cable_add(*args)
  host, port, certificate = nil
  case args.length
    when 1
      host, port = args[0].split(':', 2)
    when 2
      host, port = args[0].split(':', 2)
      if port.nil?
        port = args[1]
      else
        certificate = args[1]
      end
    when 3
      host, port, certificate = args
    else
      usage_cable_add
      return
  end

  if !aggregator_verify || args.length == 0 || args[0] == '-h' || \
      port.nil? || port.to_i <= 0
    usage_cable_add
    return
  end

  certificate = File.new(certificate).read if certificate && File.exists?(certificate)

  @aggregator.add_cable(Metasploit::Aggregator::Cable::HTTPS, host, port, certificate)
end

#cmd_aggregator_cable_remove(*args) ⇒ Object


275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'plugins/aggregator.rb', line 275

def cmd_aggregator_cable_remove(*args)
  case args.length
    when 1
      host, port = args[0].split(':', 2)
    when 2
      host, port = args
  end
  if !aggregator_verify || args.length == 0 || args[0] == '-h' || host.nil?
    usage_cable_remove
    return
  end
  @aggregator.remove_cable(host, port)
end

#cmd_aggregator_cables(*_args) ⇒ Object


265
266
267
268
269
270
271
272
273
# File 'plugins/aggregator.rb', line 265

def cmd_aggregator_cables(*_args)
  return if !aggregator_verify
  res = @aggregator.cables
  print_status("Remote Cables:")
  res.each do |k|
    print_status("    #{k}")
  end

end

#cmd_aggregator_connect(*args) ⇒ Object


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'plugins/aggregator.rb', line 136

def cmd_aggregator_connect(*args)
  if !args[0]
    if ::File.readable?("#{Aggregator_yaml}")
      lconfig = YAML.load_file("#{Aggregator_yaml}")
      @host = lconfig['default']['server']
      @port = lconfig['default']['port']
      
      return
    end
  end

  if args.length == 0 || args[0].empty? || args[0] == "-h"
    usage_connect
    return
  end

  @host = @port = @sslv = nil

  case args.length
  when 1
    @host, @port = args[0].split(':', 2)
    @port ||= '2447'
  when 2
    @host, @port = args
  else
    usage_connect
    return
  end
  
end

#cmd_aggregator_default_forward(*_args) ⇒ Object


314
315
316
317
318
# File 'plugins/aggregator.rb', line 314

def cmd_aggregator_default_forward(*_args)
  return if !aggregator_verify

  @aggregator.register_default(@aggregator.uuid, nil)
end

#cmd_aggregator_disconnect(*_args) ⇒ Object


341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'plugins/aggregator.rb', line 341

def cmd_aggregator_disconnect(*_args)
  if @aggregator && @aggregator.available?
    # check if this connection is the default forward
    @aggregator.register_default(nil, nil) if @aggregator.default == @aggregator.uuid

    # now check for any specifically forwarded sessions
    local_sessions_by_id = {}
    framework.sessions.each_pair do |_id, s|
      local_sessions_by_id[s.conn_id] = s
    end

    sessions = @aggregator.sessions
    unless sessions.nil?
      sessions.each_pair do |session, console|
        next unless local_sessions_by_id.keys.include?(session)
        if console == @aggregator.uuid
           # park each session locally addressed
          cmd_aggregator_session_park(framework.sessions.key(local_sessions_by_id[session]))
        else
          # simple disconnect session that were from the default forward
          framework.sessions.deregister(local_sessions_by_id[session])
        end
      end
    end
  end
  @aggregator.stop if @aggregator
  if @payload_job_ids
    @payload_job_ids.each do |id|
      framework.jobs.stop_job(id)
    end
    @payload_job_ids = nil
  end
  @aggregator = nil
end

#cmd_aggregator_save(*args) ⇒ Object


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'plugins/aggregator.rb', line 112

def cmd_aggregator_save(*args)
  # if we are logged in, save session details to aggregator.yaml
  if args.length > 0 || args[0] == "-h"
    usage_save
    return
  end

  if args[0]
    usage_save
    return
  end

  group = "default"

  if (@host && @host.length > 0) && (@port && @port.length > 0 && @port.to_i > 0)
    config = { "#{group}" => { 'server' => @host, 'port' => @port } }
    ::File.open("#{Aggregator_yaml}", "wb") { |f| f.puts YAML.dump(config) }
    print_good("#{Aggregator_yaml} created.")
  else
    print_error("Missing server/port - reconnect and then try again.")
    return
  end
end

#cmd_aggregator_session_forward(*args) ⇒ Object


320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'plugins/aggregator.rb', line 320

def cmd_aggregator_session_forward(*args)
  return if !aggregator_verify

  remote_id = nil
  case args.length
    when 1
      remote_id = args[0]
    else
      usage_session_forward
      return
  end
  # find session with ID matching request
  @aggregator.sessions.each do |session|
    session_uri, _target = session
    details = @aggregator.session_details(session_uri)
    next unless details['ID'] == remote_id
      return @aggregator.obtain_session(session_uri, @aggregator.uuid)
  end
  print_error("#{remote_id} was not found.")
end

#cmd_aggregator_session_park(*args) ⇒ Object


289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'plugins/aggregator.rb', line 289

def cmd_aggregator_session_park(*args)
  return if !aggregator_verify

  case args.length
    when 1
      session_id = args[0]
      s = framework.sessions.get(session_id)
      unless s.nil?
        if @aggregator.sessions.keys.include? s.conn_id
          @aggregator.release_session(s.conn_id)
          framework.sessions.deregister(s)
        else
          # TODO: determine if we can add a transport and route with the
          # aggregator. For now, just report action not taken.
          print_status("#{session_id} does not originate from the aggregator connection.")
        end
      else
        print_status("#{session_id} is not a valid session.")
      end
    else
      usage('aggregator_session_park session_id')
      return
  end
end

#cmd_aggregator_sessions(*args) ⇒ Object


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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'plugins/aggregator.rb', line 167

def cmd_aggregator_sessions(*args)
  case args.length
    when 0
      isDetailed = false
    when 1
      unless args[0] == "-v"
        usage_sessions
        return
      end
      isDetailed = true
    else
      usage_sessions
      return
  end
  return unless aggregator_verify

  sessions_list = @aggregator.sessions
  return if sessions_list.nil?

  session_map = {}

  # get details for each session and print in format of sessions -v
  sessions_list.each do |session|
    session_id, target = session
    details = @aggregator.session_details(session_id)
    local_id = nil
    framework.sessions.each_pair do |key, value|
      next unless value.conn_id == session_id
      local_id = key
    end
    # filter session that do not have details as forwarding options (this may change later)
    next unless details && details['ID']
    session_map[details['ID']] = [details, target, local_id]
  end

  print_status("Remote sessions")
  print_status("===============")
  print_status("")
  if session_map.length == 0
    print_status("No remote sessions.")
  else
    unless isDetailed
      print_status("  Id  Remote Id  Type                      Information          Connection")
      print_status("  --  ---------  ----                      -----------          ----------")
    end
    session_map.keys.sort.each do |key|
      details, target, local_id = session_map[key]
      unless isDetailed
        show_session(details, target, local_id)
      else
        show_session_detailed(details, target, local_id)
      end
    end
  end
end

#commandsObject


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'plugins/aggregator.rb', line 22

def commands
  {
    'aggregator_connect'         => "Connect to a running Aggregator instance ( host[:port] )",
    'aggregator_save'            => "Save connection details to an Aggregator instance",
    'aggregator_disconnect'      => "Disconnect from an active Aggregator instance",
    'aggregator_addresses'       => "List all remote ip addresses available for ingress",
    'aggregator_cables'          => "List all remote listeners for sessions",
    'aggregator_cable_add'       => "Setup remote https listener for sessions",
    'aggregator_cable_remove'    => "Stop remote listener for sessions",
    'aggregator_default_forward' => "forward a unlisted/unhandled sessions to a specified listener",
    'aggregator_sessions'        => "List all remote sessions currently available from the Aggregator instance",
    'aggregator_session_forward' => "forward a session to a specified listener",
    'aggregator_session_park'    => "Park an existing session on the Aggregator instance"
  }
end

#nameObject


18
19
20
# File 'plugins/aggregator.rb', line 18

def name
  "Aggregator"
end

#usage(*lines) ⇒ Object


47
48
49
50
51
52
# File 'plugins/aggregator.rb', line 47

def usage(*lines)
  print_status("Usage: ")
  lines.each do |line|
    print_status("       #{line}")
  end
end

#usage_cable_addObject


64
65
66
67
68
# File 'plugins/aggregator.rb', line 64

def usage_cable_add
  usage('aggregator_cable_add host:port [certificate]',
        ' -OR- ',
        'aggregator_cable_add host port [certificate]')
end

#usage_cable_removeObject


70
71
72
73
74
# File 'plugins/aggregator.rb', line 70

def usage_cable_remove
  usage('aggregator_cable_remove host:port',
        ' -OR- ',
        'aggregator_cable_remove host port')
end

#usage_connectObject


58
59
60
61
62
# File 'plugins/aggregator.rb', line 58

def usage_connect
  usage("aggregator_connect host[:port]",
        " -OR- ",
        "aggregator_connect host port")
end

#usage_default_forwardObject


80
81
82
# File 'plugins/aggregator.rb', line 80

def usage_default_forward
  usage("aggregator_session_forward")
end

#usage_saveObject


54
55
56
# File 'plugins/aggregator.rb', line 54

def usage_save
  usage("aggregator_save")
end

#usage_session_forwardObject


76
77
78
# File 'plugins/aggregator.rb', line 76

def usage_session_forward
  usage("aggregator_session_forward remote_id")
end