Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent out of bounds error when traversing near 0.0.0.0 (fixes #29) #30

Merged
merged 3 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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