Class: Rex::Post::Meterpreter::Ui::Console::CommandDispatcher::Stdapi::Net

Inherits:
Object
  • Object
show all
Includes:
Rex::Post::Meterpreter::Ui::Console::CommandDispatcher
Defined in:
lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb

Overview

The networking portion of the standard API extension.

Defined Under Namespace

Modules: PortForwardTracker

Constant Summary collapse

Klass =
Console::CommandDispatcher::Stdapi::Net
@@route_opts =

Options for the route command.

Rex::Parser::Arguments.new(
"-h" => [ false, "Help banner." ])
@@portfwd_opts =

Options for the portfwd command.

Rex::Parser::Arguments.new(
"-h" => [ false, "Help banner." ],
"-l" => [ true,  "The local port to listen on." ],
"-r" => [ true,  "The remote host to connect to." ],
"-p" => [ true,  "The remote port to connect to." ],
"-L" => [ true,  "The local host to listen on (optional)." ])
@@netstat_opts =

Options for the netstat command.

Rex::Parser::Arguments.new(
"-h" => [ false, "Help banner." ],
"-S" => [ true, "Search string." ])
@@arp_opts =

Options for ARP command.

Rex::Parser::Arguments.new(
"-h" => [ false, "Help banner." ],
"-S" => [ true, "Search string." ])

Instance Attribute Summary

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

#shell, #tab_complete_items

Instance Method Summary collapse

Methods included from Rex::Post::Meterpreter::Ui::Console::CommandDispatcher

check_hash, #client, #initialize, #log_error, #msf_loaded?, set_hash

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

#cmd_help, #cmd_help_help, #cmd_help_tabs, #deprecated_cmd, #deprecated_commands, #deprecated_help, #help_to_s, #initialize, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #tab_complete_filenames, #update_prompt

Instance Method Details

#cmd_arp(*args) ⇒ Object

Displays ARP cache of the remote machine.



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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 171

def cmd_arp(*args)
  arp_table = client.net.config.arp_table
  search_term = nil
  @@arp_opts.parse(args) { |opt, idx, val|
    case opt
    when '-S'
      search_term = val
      if search_term.nil?
        print_error("Enter a search term")
        return true
      else
        search_term = /#{search_term}/nmi
      end
    when "-h"
      @@arp_opts.usage
      return 0

    end
  }
  tbl = Rex::Ui::Text::Table.new(
  'Header'  => "ARP cache",
  'Indent'  => 4,
  'Columns' =>
    [
      "IP address",
      "MAC address",
      "Interface"
    ],
  'SearchTerm' => search_term)

  arp_table.each { |arp|
    tbl << [ arp.ip_addr, arp.mac_addr, arp.interface ]
  }

  if tbl.rows.length > 0
    print("\n" + tbl.to_s + "\n")
  else
    print_line("ARP cache is empty.")
  end
end

#cmd_getproxyObject



469
470
471
472
473
474
475
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 469

def cmd_getproxy
  p = client.net.config.get_proxy_config()
  print_line( "Auto-detect     : #{p[:autodetect] ? "Yes" : "No"}" )
  print_line( "Auto config URL : #{p[:autoconfigurl]}" )
  print_line( "Proxy URL       : #{p[:proxy]}" )
  print_line( "Proxy Bypass    : #{p[:proxybypass]}" )
end

#cmd_ipconfig(*args) ⇒ Object Also known as: cmd_ifconfig

Displays interfaces on the remote machine.



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 216

def cmd_ipconfig(*args)
  ifaces = client.net.config.interfaces

  if (ifaces.length == 0)
    print_line("No interfaces were found.")
  else
    ifaces.sort{|a,b| a.index <=> b.index}.each do |iface|
      print("\n" + iface.pretty + "\n")
    end
  end
end

#cmd_netstat(*args) ⇒ Object

Displays network connections of the remote machine.



122
123
124
125
126
127
128
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
157
158
159
160
161
162
163
164
165
166
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 122

def cmd_netstat(*args)
  connection_table = client.net.config.netstat
  search_term = nil
  @@netstat_opts.parse(args) { |opt, idx, val|
    case opt
    when '-S'
      search_term = val
      if search_term.nil?
        print_error("Enter a search term")
        return true
      else
        search_term = /#{search_term}/nmi
      end
    when "-h"
      @@netstat_opts.usage
      return 0

    end
  }
  tbl = Rex::Ui::Text::Table.new(
  'Header'  => "Connection list",
  'Indent'  => 4,
  'Columns' =>
    [
      "Proto",
      "Local address",
      "Remote address",
      "State",
      "User",
      "Inode",
      "PID/Program name"
    ],
   'SearchTerm' => search_term)

  connection_table.each { |connection|
    tbl << [ connection.protocol, connection.local_addr_str, connection.remote_addr_str,
      connection.state, connection.uid, connection.inode, connection.pid_name]
  }

  if tbl.rows.length > 0
    print("\n" + tbl.to_s + "\n")
  else
    print_line("Connection list is empty.")
  end
end

#cmd_portfwd(*args) ⇒ Object

Starts and stops local port forwards to remote hosts on the target network. This provides an elementary pivoting interface.



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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 350

def cmd_portfwd(*args)
  args.unshift("list") if args.empty?

  # For clarity's sake.
  lport = nil
  lhost = nil
  rport = nil
  rhost = nil

  # Parse the options
  @@portfwd_opts.parse(args) { |opt, idx, val|
    case opt
      when "-h"
        cmd_portfwd_help
        return true
      when "-l"
        lport = val.to_i
      when "-L"
        lhost = val
      when "-p"
        rport = val.to_i
      when "-r"
        rhost = val
    end
  }

  # If we haven't extended the session, then do it now since we'll
  # need to track port forwards
  if client.kind_of?(PortForwardTracker) == false
    client.extend(PortForwardTracker)
    client.pfservice = Rex::ServiceManager.start(Rex::Services::LocalRelay)
  end

  # Build a local port forward in association with the channel
  service = client.pfservice

  # Process the command
  case args.shift
    when "list"

      cnt = 0

      # Enumerate each TCP relay
      service.each_tcp_relay { |lhost, lport, rhost, rport, opts|
        next if (opts['MeterpreterRelay'] == nil)

        print_line("#{cnt}: #{lhost}:#{lport} -> #{rhost}:#{rport}")

        cnt += 1
      }

      print_line
      print_line("#{cnt} total local port forwards.")


    when "add"

      # Validate parameters
      if (!lport or !rhost or !rport)
        print_error("You must supply a local port, remote host, and remote port.")
        return
      end

      # Start the local TCP relay in association with this stream
      service.start_tcp_relay(lport,
        'LocalHost'         => lhost,
        'PeerHost'          => rhost,
        'PeerPort'          => rport,
        'MeterpreterRelay'  => true,
        'OnLocalConnection' => Proc.new { |relay, lfd|
          create_tcp_channel(relay)
          })

      print_status("Local TCP relay created: #{lhost || '0.0.0.0'}:#{lport} <-> #{rhost}:#{rport}")

    # Delete local port forwards
    when "delete"

      # No local port, no love.
      if (!lport)
        print_error("You must supply a local port.")
        return
      end

      # Stop the service
      if (service.stop_tcp_relay(lport, lhost))
        print_status("Successfully stopped TCP relay on #{lhost || '0.0.0.0'}:#{lport}")
      else
        print_error("Failed to stop TCP relay on #{lhost || '0.0.0.0'}:#{lport}")
      end

    when "flush"

      counter = 0
      service.each_tcp_relay do |lhost, lport, rhost, rport, opts|
        next if (opts['MeterpreterRelay'] == nil)

        if (service.stop_tcp_relay(lport, lhost))
          print_status("Successfully stopped TCP relay on #{lhost || '0.0.0.0'}:#{lport}")
        else
          print_error("Failed to stop TCP relay on #{lhost || '0.0.0.0'}:#{lport}")
          next
        end

        counter += 1
      end
      print_status("Successfully flushed #{counter} rules")

    else
      cmd_portfwd_help
  end
end

#cmd_portfwd_helpObject



463
464
465
466
467
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 463

def cmd_portfwd_help
  print_line "Usage: portfwd [-h] [add | delete | list | flush] [args]"
  print_line
  print @@portfwd_opts.usage
end

#cmd_route(*args) ⇒ Object

Displays or modifies the routing table on the remote machine.



233
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 233

def cmd_route(*args)
  # Default to list
  if (args.length == 0)
    args.unshift("list")
  end

  # Check to see if they specified -h
  @@route_opts.parse(args) { |opt, idx, val|
    case opt
      when "-h"
        print(
          "Usage: route [-h] command [args]\n\n" +
          "Display or modify the routing table on the remote machine.\n\n" +
          "Supported commands:\n\n" +
          "   add    [subnet] [netmask] [gateway]\n" +
          "   delete [subnet] [netmask] [gateway]\n" +
          "   list\n\n")
        return true
    end
  }

  cmd = args.shift

  # Process the commands
  case cmd
    when "list"
      routes = client.net.config.routes

      # IPv4
      tbl = Rex::Ui::Text::Table.new(
        'Header'  => "IPv4 network routes",
        'Indent'  => 4,
        'Columns' =>
          [
            "Subnet",
            "Netmask",
            "Gateway",
            "Metric",
            "Interface"
          ])

      routes.select {|route|
        Rex::Socket.is_ipv4?(route.netmask)
      }.each { |route|
        tbl << [ route.subnet, route.netmask, route.gateway, route.metric, route.interface ]
      }

      if tbl.rows.length > 0
        print("\n" + tbl.to_s + "\n")
      else
        print_line("No IPv4 routes were found.")
      end

      # IPv6
      tbl = Rex::Ui::Text::Table.new(
        'Header'  => "IPv6 network routes",
        'Indent'  => 4,
        'Columns' =>
          [
            "Subnet",
            "Netmask",
            "Gateway",
            "Metric",
            "Interface"
          ])

      routes.select {|route|
        Rex::Socket.is_ipv6?(route.netmask)
      }.each { |route|
        tbl << [ route.subnet, route.netmask, route.gateway, route.metric, route.interface ]
      }

      if tbl.rows.length > 0
        print("\n" + tbl.to_s + "\n")
      else
        print_line("No IPv6 routes were found.")
      end

    when "add"
                      	# Satisfy check to see that formatting is correct
                              unless Rex::Socket::RangeWalker.new(args[0]).length == 1
                                      print_error "Invalid IP Address"
                                      return false
                              end

                              unless Rex::Socket::RangeWalker.new(args[1]).length == 1
                                      print_error "Invalid Subnet mask"
                                      return false
                              end

      print_line("Creating route #{args[0]}/#{args[1]} -> #{args[2]}")

      client.net.config.add_route(*args)
    when "delete"
            # Satisfy check to see that formatting is correct
                              unless Rex::Socket::RangeWalker.new(args[0]).length == 1
                                      print_error "Invalid IP Address"
                                      return false
                              end

                              unless Rex::Socket::RangeWalker.new(args[1]).length == 1
                                      print_error "Invalid Subnet mask"
                                      return false
                              end

      print_line("Deleting route #{args[0]}/#{args[1]} -> #{args[2]}")

      client.net.config.remove_route(*args)
    else
      print_error("Unsupported command: #{cmd}")
  end
end

#commandsObject

List of supported commands.



71
72
73
74
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
105
106
107
108
109
110
111
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 71

def commands
  all = {
    "ipconfig" => "Display interfaces",
    "ifconfig" => "Display interfaces",
    "route"    => "View and modify the routing table",
    "portfwd"  => "Forward a local port to a remote service",
    "arp"      => "Display the host ARP cache",
    "netstat"  => "Display the network connections",
    "getproxy" => "Display the current proxy configuration",
  }
  reqs = {
    "ipconfig" => [ "stdapi_net_config_get_interfaces" ],
    "ifconfig" => [ "stdapi_net_config_get_interfaces" ],
    "route"    => [
      # Also uses these, but we don't want to be unable to list them
      # just because we can't alter them.
      #"stdapi_net_config_add_route",
      #"stdapi_net_config_remove_route",
      "stdapi_net_config_get_routes"
    ],
    # Only creates tcp channels, which is something whose availability
    # we can't check directly at the moment.
    "portfwd"  => [ ],
    "arp"      => [ "stdapi_net_config_get_arp_table" ],
    "netstat"  => [ "stdapi_net_config_get_netstat" ],
    "getproxy" => [ "stdapi_net_config_get_proxy" ],
  }

  all.delete_if do |cmd, desc|
    del = false
    reqs[cmd].each do |req|
      next if client.commands.include? req
      del = true
      break
    end

    del
  end

  all
end

#nameObject

Name for this dispatcher.



116
117
118
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 116

def name
  "Stdapi: Networking"
end