Skip to content

Commit

Permalink
Land #5, add mac address matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Cook committed Jun 25, 2017
2 parents 4ebdb86 + 91e44eb commit d14de60
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions lib/rex/socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ def self.create_ip(opts = {})

MATCH_IPV4_PRIVATE = /^\s*(?:10\.|192\.168|172.(?:1[6-9]|2[0-9]|3[01])\.|169\.254)/

MATCH_MAC_ADDR = /^\s*(([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2})|([[0-9a-fA-F]{2}]{6})\s*$/i

##
#
# Serialization
#
##


# Cache our IPv6 support flag
@@support_ipv6 = nil

Expand Down Expand Up @@ -122,39 +123,44 @@ def self.support_ipv6?
# Determine whether this is an IPv4 address
#
def self.is_ipv4?(addr)
( addr =~ MATCH_IPV4 ) ? true : false
addr =~ MATCH_IPV4 ? true : false
end

#
# Determine whether this is an IPv6 address
#
def self.is_ipv6?(addr)
( addr =~ MATCH_IPV6 ) ? true : false
addr =~ MATCH_IPV6 ? true : false
end

#
# Checks to see if the supplied address is in "dotted" form
# Determine whether this is a MAC address
#
def self.dotted_ip?(addr)
# Match IPv6
return true if (support_ipv6? and addr =~ MATCH_IPV6)
def self.is_mac_addr?(addr)
addr =~ MATCH_MAC_ADDR ? true : false
end

# Match IPv4
return true if (addr =~ MATCH_IPV4)
#
# Determine whether this is an IP address at all
# Check for v4 (less expensive), v6, else false
#
def self.is_ip_addr?(addr)
self.is_ipv4?(addr) || self.is_ipv6?(addr)
end

false
#
# Checks to see if the supplied address is in "dotted" form
#
def self.dotted_ip?(addr)
(support_ipv6? && addr =~ MATCH_IPV6) || (addr =~ MATCH_IPV4)
end

#
# Return true if +addr+ is within the ranges specified in RFC1918, or
# RFC5735/RFC3927
#
def self.is_internal?(addr)
if self.dotted_ip?(addr)
addr =~ MATCH_IPV4_PRIVATE
else
false
end
self.dotted_ip?(addr) && addr =~ MATCH_IPV4_PRIVATE
end

# Get the first address returned by a DNS lookup for +hostname+.
Expand All @@ -176,7 +182,7 @@ def self.getaddress(hostname, accept_ipv6 = true)
# @param hostname [String] A hostname or ASCII IP address
# @return [Array<String>]
def self.getaddresses(hostname, accept_ipv6 = true)
if hostname =~ MATCH_IPV4 or (accept_ipv6 and hostname =~ MATCH_IPV6)
if hostname =~ MATCH_IPV4 || (accept_ipv6 && hostname =~ MATCH_IPV6)
return [hostname]
end

Expand Down Expand Up @@ -212,7 +218,7 @@ def self.getaddresses(hostname, accept_ipv6 = true)
# on Windows.
#
def self.gethostbyname(host)
if (is_ipv4?(host))
if is_ipv4?(host)
return [ host, [], 2, host.split('.').map{ |c| c.to_i }.pack("C4") ]
end

Expand All @@ -230,8 +236,7 @@ def self.gethostbyname(host)
# address family
#
def self.to_sockaddr(ip, port)

if (ip == '::ffff:0.0.0.0')
if ip == '::ffff:0.0.0.0'
ip = support_ipv6?() ? '::' : '0.0.0.0'
end

Expand All @@ -245,7 +250,7 @@ def self.to_sockaddr(ip, port)
def self.from_sockaddr(saddr)
port, host = ::Socket::unpack_sockaddr_in(saddr)
af = ::Socket::AF_INET
if (support_ipv6?() and is_ipv6?(host))
if support_ipv6?() && is_ipv6?(host)
af = ::Socket::AF_INET6
end
return [ af, host, port ]
Expand Down Expand Up @@ -471,7 +476,6 @@ def self.cidr_crack(cidr, v6=false)
# lame kid way of doing it.
#
def self.net2bitmask(netmask)

nmask = resolv_nbo(netmask)
imask = addr_ntoi(nmask)
bits = 32
Expand Down Expand Up @@ -751,17 +755,17 @@ def getpeername_as_array
def peerinfo
if (pi = getpeername_as_array)
return pi[1] + ':' + pi[2].to_s
end
end
end
end

#
# Returns local information (host + port) in host:port format.
#
def localinfo
if (pi = getlocalname)
return pi[1] + ':' + pi[2].to_s
end
end
end
end

#
# Returns a string that indicates the type of the socket, such as 'tcp'.
Expand Down

0 comments on commit d14de60

Please sign in to comment.