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, #log_error, set_hash

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

#cmd_help, #cmd_help_tabs, #initialize, #print, #print_error, #print_good, #print_line, #print_status, #tab_complete_filenames, #update_prompt

Instance Method Details

#cmd_ipconfig(*args) ⇒ Object

Displays interfaces on the remote machine.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 74

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

	if (ifaces.length == 0)
		print_line("No interfaces were found.")
	else
		client.net.config.each_interface { |iface|
			print("\n" + iface.pretty + "\n")
		}
	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.



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

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

		else
			cmd_portfwd_help
	end
end

#cmd_portfwd_helpObject



271
272
273
274
275
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 271

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

#cmd_route(*args) ⇒ Object

Displays or modifies the routing table on the remote machine.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
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
167
168
169
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 89

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

			if (routes.length == 0)
				print_line("No routes were found.")
			else
				tbl = Rex::Ui::Text::Table.new(
					'Header'  => "Network routes",
					'Indent'  => 4,
					'Columns' =>
						[
							"Subnet",
							"Netmask",
							"Gateway"
						])

				routes.each { |route|
					tbl << [ route.subnet, route.netmask, route.gateway ]
				}

				print("\n" + tbl.to_s + "\n")
			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.



56
57
58
59
60
61
62
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 56

def commands
	{
		"ipconfig" => "Display interfaces",
		"route"    => "View and modify the routing table",
		"portfwd"  => "Forward a local port to a remote service",
	}
end

#nameObject

Name for this dispatcher.



67
68
69
# File 'lib/rex/post/meterpreter/ui/console/command_dispatcher/stdapi/net.rb', line 67

def name
	"Stdapi: Networking"
end