Class: Rex::Socket::SubnetWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/socket/subnet_walker.rb

Overview

This class provides an interface to enumerating a subnet with a supplied netmask.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subnet, netmask) ⇒ SubnetWalker

Initializes a subnet walker instance using the supplied subnet information.



19
20
21
22
23
24
# File 'lib/rex/socket/subnet_walker.rb', line 19

def initialize(subnet, netmask)
  self.subnet  = Socket.resolv_to_dotted(subnet)
  self.netmask = Socket.resolv_to_dotted(netmask)

  reset
end

Instance Attribute Details

#netmaskObject

The netmask of the subnet.



62
63
64
# File 'lib/rex/socket/subnet_walker.rb', line 62

def netmask
  @netmask
end

#num_ipsObject

The total number of IPs within the subnet.



66
67
68
# File 'lib/rex/socket/subnet_walker.rb', line 66

def num_ips
  @num_ips
end

#subnetObject

The subnet that is being enumerated.



58
59
60
# File 'lib/rex/socket/subnet_walker.rb', line 58

def subnet
  @subnet
end

Instance Method Details

#next_ipObject

Returns the next IP address.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rex/socket/subnet_walker.rb', line 38

def next_ip
  if (curr_ip_idx >= num_ips)
    return nil
  end

  if (curr_ip_idx > 0)
    self.curr_ip[3] = (curr_ip[3].to_i + 1) % 256
    self.curr_ip[2] = (curr_ip[2].to_i + 1) % 256 if (curr_ip[3] == 0)
    self.curr_ip[1] = (curr_ip[1].to_i + 1) % 256 if (curr_ip[2] == 0)
    self.curr_ip[0] = (curr_ip[0].to_i + 1) % 256 if (curr_ip[1] == 0)
  end

  self.curr_ip_idx += 1

  self.curr_ip.join('.')
end

#resetObject

Resets the subnet walker back to its original state.



29
30
31
32
33
# File 'lib/rex/socket/subnet_walker.rb', line 29

def reset
  self.curr_ip     = self.subnet.split('.')
  self.num_ips     = (1 << (32 - Socket.net2bitmask(self.netmask).to_i))
  self.curr_ip_idx = 0
end