Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

SpiderFootTarget: Fix IPv6 parsing in 'matches' function

Brendan Coles2246a56355d7
diff --git a/spiderfoot/target.py b/spiderfoot/target.py
index d9f4e17d..fbe25095 100644
--- a/spiderfoot/target.py
+++ b/spiderfoot/target.py
@@ -157,13 +157,15 @@ class SpiderFootTarget():
to the original target.
Tightly in this case means:
- 1. If the value is an IP:
- 1.1 is it in the list of aliases or the target itself?
- 1.2 is it on the target's subnet?
- 2. If the value is a name (subdomain, domain, hostname):
- 2.1 is it in the list of aliases or the target itself?
- 2.2 is it a parent of the aliases of the target (domain/subdomain)
- 2.3 is it a child of the aliases of the target (hostname)
+
+ If the value is an IP:
+ * is it in the list of aliases or the target itself?
+ * is it on the target's subnet?
+
+ If the value is an internet name (subdomain, domain, hostname):
+ * is it in the list of aliases or the target itself?
+ * is it a parent of the aliases of the target (domain/subdomain)
+ * is it a child of the aliases of the target (hostname)
Args:
value (str): can be an Internet Name (hostname, subnet, domain) or an IP address.
@@ -191,28 +193,31 @@ class SpiderFootTarget():
if self.targetType in ["HUMAN_NAME", "PHONE_NUMBER", "USERNAME"]:
return True
- if netaddr.valid_ipv4(value):
- # 1.1
+ # TODO: review handling of other potential self.targetType target types:
+ # "INTERNET_NAME", "EMAILADDR", "BGP_AS_OWNER"
+
+ # For IP addreses, check if it is an alias of the target or within the target's subnet.
+ if netaddr.valid_ipv4(value) or netaddr.valid_ipv6(value):
if value in self.getAddresses():
return True
- # 1.2
- if self.targetType == "NETBLOCK_OWNER":
- if netaddr.IPAddress(value) in netaddr.IPNetwork(self.targetValue):
- return True
- if self.targetType in ["IP_ADDRESS", "IPV6_ADDRESS"]:
- if netaddr.IPAddress(value) in netaddr.IPNetwork(netaddr.IPAddress(self.targetValue)):
- return True
- else:
- for name in self.getNames():
- # 2.1
- if value == name:
- return True
- # 2.2
- if includeParents and name.endswith("." + value):
- return True
- # 2.3
- if includeChildren and value.endswith("." + name):
- return True
+
+ if self.targetType in ["IP_ADDRESS", "IPV6_ADDRESS", "NETBLOCK_OWNER"]:
+ try:
+ if netaddr.IPAddress(value) in netaddr.IPNetwork(self.targetValue):
+ return True
+ except netaddr.AddrFormatError as e:
+ return False
+
+ return False
+
+ # For everything else, check if the value is within or equal to target names
+ for name in self.getNames():
+ if value == name:
+ return True
+ if includeParents and name.endswith("." + value):
+ return True
+ if includeChildren and value.endswith("." + name):
+ return True
return False
diff --git a/test/unit/test_spiderfoottarget.py b/test/unit/test_spiderfoottarget.py
index d8d34582..6a3fa76e 100644
--- a/test/unit/test_spiderfoottarget.py
+++ b/test/unit/test_spiderfoottarget.py
@@ -170,7 +170,7 @@ class TestSpiderFootTarget(unittest.TestCase):
addresses = target.getAddresses()
self.assertEqual(list, type(addresses))
- def test_matches_should_return_a_boolean(self):
+ def test_matches_argument_value_invalid_type_should_return_False(self):
"""
Test matches(self, value, includeParents=False, includeChildren=True)
"""
@@ -178,8 +178,11 @@ class TestSpiderFootTarget(unittest.TestCase):
target_type = 'IP_ADDRESS'
target = SpiderFootTarget(target_value, target_type)
- matches = target.matches(target_value)
- self.assertIsInstance(matches, bool)
+ invalid_types = [None, list(), dict(), int()]
+ for invalid_type in invalid_types:
+ with self.subTest(invalid_type=invalid_type):
+ matches = target.matches(invalid_type)
+ self.assertFalse(matches)
def test_matches_argument_value_matching_ipv4_address_should_return_True(self):
"""
@@ -203,7 +206,17 @@ class TestSpiderFootTarget(unittest.TestCase):
matches = target.matches('1.1.1.2')
self.assertFalse(matches)
- @unittest.skip("fixme - this test fails due to a bug in the IPv6 matching logic")
+ def test_matches_argument_value_unmatching_ipv4_address_in_same_subnet_when_targetType_is_netblock_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = '127.0.0.0/24'
+ target_type = 'NETBLOCK_OWNER'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches('127.0.0.2')
+ self.assertTrue(matches)
+
def test_matches_argument_value_matching_ipv6_address_should_return_True(self):
"""
Test matches(self, value, includeParents=False, includeChildren=True)
@@ -226,11 +239,56 @@ class TestSpiderFootTarget(unittest.TestCase):
matches = target.matches('::2')
self.assertFalse(matches)
+ def test_matches_argument_value_matching_internet_name_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = 'spiderfoot.net'
+ target_type = 'INTERNET_NAME'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches(target_value)
+ self.assertTrue(matches)
+
+ def test_matches_argument_value_unmatching_internet_name_should_return_False(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = 'spiderfoot.net'
+ target_type = 'INTERNET_NAME'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches(f"{target_value}.test")
+ self.assertFalse(matches)
+
+ def test_matches_argument_includeChildren_true_with_matching_target_subdomain_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ target_value = 'spiderfoot.net'
+ target_type = 'INTERNET_NAME'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches(f"test.{target_value}", includeChildren=True)
+ self.assertTrue(matches)
+
+ def test_matches_argument_includeParents_true_with_matching_target_parent_domain_should_return_True(self):
+ """
+ Test matches(self, value, includeParents=False, includeChildren=True)
+ """
+ parent_domain = 'spiderfoot.net'
+ target_value = f"test.{parent_domain}"
+ target_type = 'INTERNET_NAME'
+ target = SpiderFootTarget(target_value, target_type)
+
+ matches = target.matches(parent_domain, includeParents=True)
+ self.assertTrue(matches)
+
def test_matches_argument_value_any_human_name_should_return_True(self):
"""
Test matches(self, value, includeParents=False, includeChildren=True)
"""
- target_value = 'Spider Foot'
+ target_value = 'SpiderFoot'
target_type = 'HUMAN_NAME'
target = SpiderFootTarget(target_value, target_type)
@@ -267,7 +325,7 @@ class TestSpiderFootTarget(unittest.TestCase):
target_type = 'IP_ADDRESS'
target = SpiderFootTarget(target_value, target_type)
- matches = target.matches(None)
+ matches = target.matches("")
self.assertFalse(matches)