diff --git a/lib/rex/socket.rb b/lib/rex/socket.rb index 11a48ffc82..de846518b8 100644 --- a/lib/rex/socket.rb +++ b/lib/rex/socket.rb @@ -201,7 +201,11 @@ module Socket addr_ntoi(resolv_nbo(host)) end - def self.resolv_to_cidr(mask) + # + # Converts an ASCII IP address to a CIDR mask. Returns + # nil if it's not convertable. + # + def self.addr_atoc(mask) mask_i = resolv_nbo_i(mask) cidr = nil 0.upto(32) do |i| @@ -213,6 +217,15 @@ module Socket return cidr end + # + # Resolves a CIDR bitmask into a dotted-quad. Returns + # nil if it's not convertable. + # + def self.addr_ctoa(cidr) + return nil unless (0..32) === cidr.to_i + addr_itoa(((1 << cidr)-1) << 32-cidr) + end + # # Resolves a host to a dotted address. # diff --git a/scripts/meterpreter/autoroute.rb b/scripts/meterpreter/autoroute.rb index b50eefcaf0..03ae13a328 100644 --- a/scripts/meterpreter/autoroute.rb +++ b/scripts/meterpreter/autoroute.rb @@ -28,9 +28,18 @@ remove_route = false usage return false when "-s" - subnet = v + if v =~ /[0-9\x2e]+\x2f[0-9]{1,2}/ + subnet,cidr = v.split("\x2f") + netmask = Rex::Socket.addr_ctoa(cidr.to_i) + else + subnet = v + end when "-n" - netmask = v + if (0..32) === v.to_i + netmask = Rex::Socket.addr_ctoa(v.to_i) + else + netmask = v + end when "-p" print_only = true when "-d" @@ -111,6 +120,7 @@ def usage() print_status "Examples:" print_status " run autoroute -s 10.1.1.0 -n 255.255.255.0 # Add a route to 10.10.10.1/255.255.255.0" print_status " run autoroute -s 10.10.10.1 # Netmask defaults to 255.255.255.0" + print_status " run autoroute -s 10.10.10.1/24 # CIDR notation is also okay" print_status " run autoroute -p # Print active routing table" print_status " run autoroute -d -s 10.10.10.1 # Deletes the 10.10.10.1/255.255.255.0 route" print_status "Use the \"route\" and \"ipconfig\" Meterpreter commands to learn about available routes" @@ -129,7 +139,7 @@ def validate_cmd(subnet=nil,netmask=nil) return false end - if(netmask and !(Rex::Socket.resolv_to_cidr(netmask))) + if(netmask and !(Rex::Socket.addr_atoc(netmask))) print_error "Netmask invalid (must define contiguous IP addressing)" usage return false