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)." ])

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.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 139

def cmd_arp(*args)
  arp_table = client.net.config.arp_table
  tbl = Rex::Ui::Text::Table.new(
  'Header'  => "ARP cache",
  'Indent'  => 4,
  'Columns' =>
    [
      "IP address",
      "MAC address",
      "Interface"
    ])

  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



419
420
421
422
423
424
425
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 419

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.



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 166

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.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 108

def cmd_netstat(*args)
  connection_table = client.net.config.netstat
  tbl = Rex::Ui::Text::Table.new(
  'Header'  => "Connection list",
  'Indent'  => 4,
  'Columns' =>
    [
      "Proto",
      "Local address",
      "Remote address",
      "State",
      "User",
      "Inode",
      "PID/Program name"
    ])

  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.



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

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



413
414
415
416
417
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 413

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.



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
222
223
224
225
226
227
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
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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 183

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.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 57

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.



102
103
104
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 102

def name
  "Stdapi: Networking"
end