Skip to content

Commit e78e12f

Browse files
committed
Land rapid7#6515, Autoadd for /post/windows/manage/autoroute
2 parents a1cfdd0 + ac051bd commit e78e12f

File tree

1 file changed

+48
-2
lines changed

1 file changed

+48
-2
lines changed

modules/post/windows/manage/autoroute.rb

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def initialize(info={})
1515
'Name' => 'Windows Manage Network Route via Meterpreter Session',
1616
'Description' => %q{This module manages session routing via an existing
1717
Meterpreter session. It enables other modules to 'pivot' through a
18-
compromised host when connecting to the named NETWORK and SUBMASK.},
18+
compromised host when connecting to the named NETWORK and SUBMASK.
19+
Autoadd will search session for valid subnets and route to them.},
1920
'License' => MSF_LICENSE,
2021
'Author' => [ 'todb'],
2122
'Platform' => [ 'win' ],
@@ -26,7 +27,7 @@ def initialize(info={})
2627
[
2728
OptString.new('SUBNET', [false, 'Subnet (IPv4, for example, 10.10.10.0)', nil]),
2829
OptString.new('NETMASK', [false, 'Netmask (IPv4 as "255.255.255.0" or CIDR as "/24"', '255.255.255.0']),
29-
OptEnum.new('CMD', [true, 'Specify the autoroute command', 'add', ['add','print','delete']])
30+
OptEnum.new('CMD', [true, 'Specify the autoroute command', 'autoadd', ['add','autoadd','print','delete']])
3031
], self.class)
3132
end
3233

@@ -58,6 +59,8 @@ def run
5859
print_status("Adding a route to %s/%s..." % [datastore['SUBNET'],netmask])
5960
add_route(:subnet => datastore['SUBNET'], :netmask => netmask)
6061
end
62+
when :autoadd
63+
autoadd_routes
6164
when :delete
6265
if datastore['SUBNET']
6366
print_status("Deleting route to %s/%s..." % [datastore['SUBNET'],netmask])
@@ -156,6 +159,49 @@ def delete_route(opts={})
156159
Rex::Socket::SwitchBoard.remove_route(subnet, netmask, session)
157160
end
158161

162+
def is_routable?(route)
163+
if route.subnet =~ /^224\.|127\./
164+
return false
165+
elsif route.subnet =~ /[\d\.]+\.0$/
166+
return false
167+
elsif route.subnet == '0.0.0.0'
168+
return false
169+
elsif route.subnet == '255.255.255.255'
170+
return false
171+
end
172+
173+
true
174+
end
175+
176+
# This function will search for valid subnets on the target and attempt
177+
# add a route to each. (Operation from auto_add_route plugin.)
178+
#
179+
# @return [void] A useful return value is not expected here
180+
def autoadd_routes
181+
switch_board = Rex::Socket::SwitchBoard.instance
182+
print_status("Searching for subnets to autoroute.")
183+
found = false
184+
185+
session.net.config.each_route do | route |
186+
next unless is_routable?(route)
187+
188+
if !switch_board.route_exists?(route.subnet, route.netmask)
189+
begin
190+
netmask = route.netmask == '255.255.255.255' ? '255.255.255.0' : route.netmask
191+
if Rex::Socket::SwitchBoard.add_route(route.subnet, netmask, session)
192+
print_good("Route added to subnet #{route.subnet}/#{netmask}")
193+
found = true
194+
else
195+
print_error("Could not add route to subnet #{route.subnet}/#{netmask}")
196+
end
197+
rescue ::Rex::Post::Meterpreter::RequestError => error
198+
print_error("Could not add route to subnet #{route.subnet}/(#{netmask})")
199+
print_error(error.to_s)
200+
end
201+
end
202+
end
203+
print_status("Did not find any new subnets to add.") if !found
204+
end
159205

160206
# Validates the command options
161207
def validate_cmd(subnet=nil,netmask=nil)

0 commit comments

Comments
 (0)