From bdbdd2721f15c14b1757519d3d08dfc083acd076 Mon Sep 17 00:00:00 2001 From: RageLtMan Date: Sat, 24 Jun 2017 20:49:25 -0400 Subject: [PATCH] Upstream socket.rb matchers Add regex for matching MAC addresses, as well as convenience methods for checking if a string matches the regex, as well as a generic :is_ip? method to verify if something is either IPv4 or v6. --- lib/rex/socket.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/rex/socket.rb b/lib/rex/socket.rb index e0212cf..df05ae7 100644 --- a/lib/rex/socket.rb +++ b/lib/rex/socket.rb @@ -88,6 +88,8 @@ 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 @@ -132,6 +134,21 @@ def self.is_ipv6?(addr) ( addr =~ MATCH_IPV6 ) ? true : false end + # + # Determine whether this is a MAC address + # + def self.is_mac_addr?(addr) + ( addr =~ MATCH_MAC_ADDR) ? true : false + end + + # + # 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) ? true : self.is_ipv6?(addr) ? true : false + end + # # Checks to see if the supplied address is in "dotted" form #