Class: Rex::Post::Meterpreter::Extensions::Stdapi::Net::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/post/meterpreter/extensions/stdapi/net/config.rb

Overview

This class provides an interface to the network configuration that exists on the remote machine, such as interfaces, and routes.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Config

Initializes a Config instance that is used to get information about the network configuration of the remote machine.



34
35
36
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/config.rb', line 34

def initialize(client)
	self.client = client
end

Instance Method Details

#add_route(subnet, netmask, gateway) ⇒ Object

Adds a route to the target machine.



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/config.rb', line 145

def add_route(subnet, netmask, gateway)
	request = Packet.create_request('stdapi_net_config_add_route')

	request.add_tlv(TLV_TYPE_SUBNET_STRING, subnet)
	request.add_tlv(TLV_TYPE_NETMASK_STRING, netmask)
	request.add_tlv(TLV_TYPE_GATEWAY_STRING, gateway)

	response = client.send_request(request)

	return true
end

#each_interface(&block) ⇒ Object

Enumerates each interface.



47
48
49
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/config.rb', line 47

def each_interface(&block)
	get_interfaces().each(&block)
end

#each_route(&block) ⇒ Object

Enumerates each route.



113
114
115
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/config.rb', line 113

def each_route(&block)
	get_routes().each(&block)
end

#get_interfacesObject Also known as: interfaces

Returns an array of network interfaces with each element.

being an Interface



55
56
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
98
99
100
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/config.rb', line 55

def get_interfaces
	request = Packet.create_request('stdapi_net_config_get_interfaces')
	ifaces  = []

	response = client.send_request(request)

	response.each(TLV_TYPE_NETWORK_INTERFACE) { |iface|
		addrs = []
		netmasks = []
		scopes = []
		while (a = iface.get_tlv_value(TLV_TYPE_IP, addrs.length))
			# Netmasks aren't tightly associated with addresses, they're
			# just thrown all together in the interface TLV ordered to
			# match up. This could be done better by creating another
			# GroupTlv type for addresses containing an address, a netmask,
			# and possibly a scope.
			n = iface.get_tlv_value(TLV_TYPE_NETMASK, addrs.length)
			if (n.nil?)
				# Some systems can't report a netmask, only a network
				# prefix, so figure out the netmask from that.
				n = iface.get_tlv_value(TLV_TYPE_IP_PREFIX, addrs.length)
				if n
					n = Rex::Socket.bit2netmask(n, !!(a.length == 16))
				end
			else
				n = Rex::Socket.addr_ntoa(n)
			end
			s = iface.get_tlv_value(TLV_TYPE_IP6_SCOPE, addrs.length)
			scopes[addrs.length] = s if s
			netmasks[addrs.length] = n if n
			addrs << Rex::Socket.addr_ntoa(a)
		end
		ifaces << Interface.new(
				:index    => iface.get_tlv_value(TLV_TYPE_INTERFACE_INDEX),
				:mac_addr => iface.get_tlv_value(TLV_TYPE_MAC_ADDRESS),
				:mac_name => iface.get_tlv_value(TLV_TYPE_MAC_NAME),
				:mtu      => iface.get_tlv_value(TLV_TYPE_INTERFACE_MTU),
				:flags    => iface.get_tlv_value(TLV_TYPE_INTERFACE_FLAGS),
				:addrs    => addrs,
				:netmasks => netmasks,
				:scopes   => scopes
			)
	}

	return ifaces
end

#get_routesObject Also known as: routes

Returns an array of routes with each element being a Route.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/config.rb', line 120

def get_routes
	request = Packet.create_request('stdapi_net_config_get_routes')
	routes  = []

	response = client.send_request(request)

	# Build out the array of routes
	# Note: This will include both IPv4 and IPv6 routes
	response.each(TLV_TYPE_NETWORK_ROUTE) { |route|
		routes << Route.new(
				route.get_tlv_value(TLV_TYPE_SUBNET),
				route.get_tlv_value(TLV_TYPE_NETMASK),
				route.get_tlv_value(TLV_TYPE_GATEWAY),
				route.get_tlv_value(TLV_TYPE_STRING),
				route.get_tlv_value(TLV_TYPE_ROUTE_METRIC))
	}

	return routes
end

#remove_route(subnet, netmask, gateway) ⇒ Object

Removes a route from the target machine.



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/rex/post/meterpreter/extensions/stdapi/net/config.rb', line 160

def remove_route(subnet, netmask, gateway)
	request = Packet.create_request('stdapi_net_config_remove_route')

	request.add_tlv(TLV_TYPE_SUBNET_STRING, subnet)
	request.add_tlv(TLV_TYPE_NETMASK_STRING, netmask)
	request.add_tlv(TLV_TYPE_GATEWAY_STRING, gateway)

	response = client.send_request(request)

	return true
end