Class: BetterCap::Network::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/bettercap/network/validator.rb

Overview

Simple class to perform validation of various addresses, ranges, etc.

Constant Summary collapse

IP_ADDRESS_REGEX =

Basic expression to validate an IP address.

'(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})'
IPV6_REGEX =

Basic expression to validate an IPv6 address.

/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|
(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|
[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]
{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]
?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:)
{4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]
\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))
|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]
{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|
1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4})
{1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)
(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:)
{1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|
2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))
|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|
2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))
(%.+)?\s*$/x
IP_OCTECT_MAX =

Quite self explainatory :)

255
IPV6_GATEWAY_REGEX =

Basic expression for default IPv6 gateway.

/fe80[^\s]*/

Class Method Summary collapse

Class Method Details

.each_in_netmask(m) ⇒ Object

Parse m as a netmask and yields each address in it.



108
109
110
111
112
# File 'lib/bettercap/network/validator.rb', line 108

def self.each_in_netmask(m)
  IPAddr.new(m).to_range.each do |o|
     yield o.to_s
  end
end

.each_in_range(r) ⇒ Object

Parse r as an IP range and yields each address in it.



98
99
100
101
102
103
104
105
# File 'lib/bettercap/network/validator.rb', line 98

def self.each_in_range(r)
  first, last = self.parse_range(r)
  loop do
    yield first.to_s
    break if first == last
    first = first.succ
  end
end

.each_ip(data) ⇒ Object

Extract valid IP addresses from data and yields each one of them.



66
67
68
69
70
# File 'lib/bettercap/network/validator.rb', line 66

def self.each_ip(data)
  data.scan(/(#{IP_ADDRESS_REGEX})/).each do |m|
    yield( m[0] ) if m[0] != '0.0.0.0'
  end
end

.each_ipv6_gateway(data) ⇒ Object

Extract default IPv6 gateway address from data.



73
74
75
76
77
# File 'lib/bettercap/network/validator.rb', line 73

def self.each_ipv6_gateway(data)
  data.scan(/(#{IPV6_GATEWAY_REGEX})/).each do |m|
    yield ( m[0] )
  end
end

.is_ip?(ip) ⇒ Boolean

Return true if ip is a valid IP address, otherwise false.

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'lib/bettercap/network/validator.rb', line 43

def self.is_ip?(ip)
  if /\A#{IP_ADDRESS_REGEX}\Z/ =~ ip.to_s
    return $~.captures.all? { |i| i.to_i <= IP_OCTECT_MAX }
  end
  false
end

.is_ipv6?(ip) ⇒ Boolean

Return true if ip is a valid IPv6 address, otherwise false.

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/bettercap/network/validator.rb', line 51

def self.is_ipv6?(ip)
  result = ip =~ IPV6_REGEX
  return result ? true : false
end

.is_mac?(mac) ⇒ Boolean

Return true if mac is a valid MAC address, otherwise false.

Returns:

  • (Boolean)


123
124
125
# File 'lib/bettercap/network/validator.rb', line 123

def self.is_mac?(mac)
  ( /^[a-f0-9]{1,2}\:[a-f0-9]{1,2}\:[a-f0-9]{1,2}\:[a-f0-9]{1,2}\:[a-f0-9]{1,2}\:[a-f0-9]{1,2}$/i =~ mac.to_s )
end

.is_netmask?(n) ⇒ Boolean

Return true if n is a valid IP netmask range ( 192.168.1.1/24 ), otherwise false.

Returns:

  • (Boolean)


115
116
117
118
119
120
# File 'lib/bettercap/network/validator.rb', line 115

def self.is_netmask?(n)
  if /\A#{IP_ADDRESS_REGEX}\/(\d+)\Z/ =~ n.to_s
    return $~.captures.all? { |i| i.to_i <= IP_OCTECT_MAX }
  end
  false
end

.is_range?(r) ⇒ Boolean

Return true if r is a valid IP address range ( 192.168.1.1-93 ), otherwise false.

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/bettercap/network/validator.rb', line 80

def self.is_range?(r)
  if /\A#{IP_ADDRESS_REGEX}\-(\d{1,3})\Z/ =~ r.to_s
    return $~.captures.all? { |i| i.to_i <= IP_OCTECT_MAX }
  end
  false
end

.is_valid_port?(port) ⇒ Boolean

Return true if port is a valid port, otherwise false.

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/bettercap/network/validator.rb', line 57

def self.is_valid_port?(port)
  port ||= ""
  return false if port.strip.empty?
  return false unless port =~ /^[0-9]+$/
  port = port.to_i
  return ( port > 0 and port <= 65535 )
end

.parse_range(r) ⇒ Object

Parse r as an IP range and return the first and last IP.



88
89
90
91
92
93
94
95
# File 'lib/bettercap/network/validator.rb', line 88

def self.parse_range(r)
  first, last_part = r.split('-')
  last = first.split('.')[0..2].join('.') + ".#{last_part}"
  first = IPAddr.new(first)
  last  = IPAddr.new(last)

  [ first, last ]
end