Skip to content

Commit

Permalink
Merge pull request #30 from timmc/bounded-traverser
Browse files Browse the repository at this point in the history
Prevent out of bounds error when traversing near 0.0.0.0 (fixes #29)
  • Loading branch information
mschwager committed Apr 29, 2019
2 parents f9eb045 + bdc77ae commit 926c555
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ This project adheres to [CHANGELOG](http://keepachangelog.com/).
- Filter out subdomains with an A record matching a wildcard A record
- Official Python 3.7 support

### Fixed
- Prevent out of bounds error when expanding IPs near 0.0.0.0 or
255.255.255.255

## [1.2.2] - 2018-04-24
### Changed
- Python 3 is now a requirement when installing via setup.py (including pip)
Expand Down
11 changes: 6 additions & 5 deletions fierce/fierce.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ def default_expander(ip):


def traverse_expander(ip, n=5):
class_c = get_class_c_network(ip)

result = [ipaddress.IPv4Address(ip + i) for i in range(-n, n + 1)]
result = [i for i in result if i in class_c]
ip = int(ip)
class_c_floor = ip - (ip % 256)
class_c_ceiling = class_c_floor + 255

return result
ip_min = max(ip - n, class_c_floor)
ip_max = min(ip + n, class_c_ceiling)
return [ipaddress.IPv4Address(i) for i in range(ip_min, ip_max + 1)]


def wide_expander(ip):
Expand Down
30 changes: 30 additions & 0 deletions tests/test_fierce.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,36 @@ def test_traverse_expander_no_cross_upper_boundary(self):

self.assertEqual(expected, result)

# Upper and lower bound tests are to avoid reintroducing out of
# bounds error from IPv4Address. (no_cross_*_boundary tests won't
# necessarily cover this; GitHub issue #29)

def test_traverse_expander_lower_bound_regression(self):
ip = ipaddress.IPv4Address('0.0.0.1')
expand = 2

result = fierce.traverse_expander(ip, expand)
expected = [
ipaddress.IPv4Address('0.0.0.0'),
ipaddress.IPv4Address('0.0.0.1'),
ipaddress.IPv4Address('0.0.0.2'),
ipaddress.IPv4Address('0.0.0.3')
]
self.assertEqual(expected, result)

def test_traverse_expander_upper_bound_regression(self):
ip = ipaddress.IPv4Address('255.255.255.254')
expand = 2

result = fierce.traverse_expander(ip, expand)
expected = [
ipaddress.IPv4Address('255.255.255.252'),
ipaddress.IPv4Address('255.255.255.253'),
ipaddress.IPv4Address('255.255.255.254'),
ipaddress.IPv4Address('255.255.255.255')
]
self.assertEqual(expected, result)

def test_wide_expander_basic(self):
ip = ipaddress.IPv4Address('192.168.1.50')

Expand Down

0 comments on commit 926c555

Please sign in to comment.