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.

Defined Under Namespace

Classes: UnitTest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subnet, netmask) ⇒ SubnetWalker

Initializes a subnet walker instance using the supplied subnet information.



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

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.



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

def netmask
  @netmask
end

#num_ipsObject

The total number of IPs within the subnet.



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

def num_ips
  @num_ips
end

#subnetObject

The subnet that is being enumerated.



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

def subnet
  @subnet
end

Instance Method Details

#next_ipObject

Returns the next IP address.



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

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.



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

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