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, #load_config, #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, 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_aggregator_addresses(*_args) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
# File 'plugins/aggregator.rb', line 216

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



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'plugins/aggregator.rb', line 228

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.empty? || args[0] == '-h' || \
     port.nil? || port.to_i <= 0
    usage_cable_add
    return
  end

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

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

#cmd_aggregator_cable_remove(*args) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'plugins/aggregator.rb', line 268

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.empty? || args[0] == '-h' || host.nil?
    usage_cable_remove
    return
  end
  @aggregator.remove_cable(host, port)
end

#cmd_aggregator_cables(*_args) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'plugins/aggregator.rb', line 258

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'plugins/aggregator.rb', line 129

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

  if args.empty? || 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



305
306
307
308
309
# File 'plugins/aggregator.rb', line 305

def cmd_aggregator_default_forward(*_args)
  return if !aggregator_verify

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

#cmd_aggregator_disconnect(*_args) ⇒ Object



333
334
335
336
337
338
339
340
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
# File 'plugins/aggregator.rb', line 333

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'plugins/aggregator.rb', line 105

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

  if args[0]
    usage_save
    return
  end

  group = 'default'

  if (@host && !@host.empty?) && (@port && !@port.empty? && @port.to_i > 0)
    config = { group.to_s => { 'server' => @host, 'port' => @port } }
    ::File.open(Aggregator_yaml.to_s, '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



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'plugins/aggregator.rb', line 311

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



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'plugins/aggregator.rb', line 282

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)
    if s.nil?
      print_status("#{session_id} is not a valid session.")
    elsif @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
    usage('aggregator_session_park session_id')
    return
  end
end

#cmd_aggregator_sessions(*args) ⇒ Object



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

def cmd_aggregator_sessions(*args)
  case args.length
  when 0
    is_detailed = false
  when 1
    unless args[0] == '-v'
      usage_sessions
      return
    end
    is_detailed = 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.empty?
    print_status('No remote sessions.')
  else
    unless is_detailed
      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]
      if is_detailed
        show_session_detailed(details, target, local_id)
      else
        show_session(details, target, local_id)
      end
    end
  end
end

#commandsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'plugins/aggregator.rb', line 15

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



11
12
13
# File 'plugins/aggregator.rb', line 11

def name
  'Aggregator'
end

#usage(*lines) ⇒ Object



40
41
42
43
44
45
# File 'plugins/aggregator.rb', line 40

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

#usage_cable_addObject



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

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

#usage_cable_removeObject



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

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

#usage_connectObject



51
52
53
54
55
# File 'plugins/aggregator.rb', line 51

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

#usage_default_forwardObject



73
74
75
# File 'plugins/aggregator.rb', line 73

def usage_default_forward
  usage('aggregator_session_forward')
end

#usage_saveObject



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

def usage_save
  usage('aggregator_save')
end

#usage_session_forwardObject



69
70
71
# File 'plugins/aggregator.rb', line 69

def usage_session_forward
  usage('aggregator_session_forward remote_id')
end