Skip to content

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

sflib: Add isPublicIpAddress function

Brendan Coles485be1d0cdc6
diff --git a/sflib.py b/sflib.py
index dbcdbfc0..1c113111 100644
--- a/sflib.py
+++ b/sflib.py
@@ -1254,6 +1254,33 @@ class SpiderFoot:
except BaseException:
return False
+ def isPublicIpAddress(self, ip):
+ """Check if an IP address is public.
+
+ Args:
+ ip (str): IP address
+
+ Returns:
+ bool: IP address is public
+ """
+ if not isinstance(ip, (str, netaddr.IPAddress)):
+ return False
+ if not self.validIP(ip) and not self.validIP6(ip):
+ return False
+
+ if not netaddr.IPAddress(ip).is_unicast():
+ return False
+
+ if netaddr.IPAddress(ip).is_loopback():
+ return False
+ if netaddr.IPAddress(ip).is_reserved():
+ return False
+ if netaddr.IPAddress(ip).is_multicast():
+ return False
+ if netaddr.IPAddress(ip).is_private():
+ return False
+ return True
+
def normalizeDNS(self, res):
"""Clean DNS results to be a simple list
diff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.py
index 2848f4e9..55b77c36 100644
--- a/test/unit/test_spiderfoot.py
+++ b/test/unit/test_spiderfoot.py
@@ -750,6 +750,34 @@ class TestSpiderFoot(unittest.TestCase):
self.assertIsInstance(valid_ip_network, bool)
self.assertTrue(valid_ip_network)
+ def test_isPublicIpAddress_should_return_a_boolean(self):
+ """
+ Test isPublicIpAddress(self, ip)
+ """
+ sf = SpiderFoot(dict())
+
+ self.assertTrue(sf.isPublicIpAddress('1.1.1.1'))
+
+ ips = [
+ 'invalid ip address',
+ '0.0.0.0',
+ '127.0.0.1',
+ '10.1.1.1',
+ '172.16.1.1',
+ '192.168.1.1',
+ '255.240.0.0',
+ '172.31.255.255',
+ '224.0.1.0',
+ '255.255.255.255',
+ '169.254.0.1',
+ '253.0.0.1',
+ '::1',
+ 'ff00::1',
+ ]
+ for ip in ips:
+ with self.subTest(ip=ip):
+ self.assertFalse(sf.isPublicIpAddress(ip))
+
def test_valid_email_should_return_a_boolean(self):
"""
Test validEmail(self, email)