SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sflib: Ensure resolveIP/resolveHost functions return a list
Brendan Coles1522d47ef2e4
diff --git a/sflib.py b/sflib.pyindex b03e063d..cdecdd1d 100644--- a/sflib.py+++ b/sflib.py@@ -1330,66 +1330,94 @@ class SpiderFoot:#def resolveHost(self, host):- """Return a normalised resolution or None if not resolved."""+ """Return a normalised resolution of a hostname.++ Args:+ host (str): host to resolve++ Returns:+ list+ """++ addrs = list()if not host:- self.error("Invalid host", False)- return None+ self.error("Unable to resolve %s (Invalid host)" % host, False)+ return addrstry:addrs = self.normalizeDNS(socket.gethostbyname_ex(host))- if len(addrs) > 0:- return list(set(addrs))- return Noneexcept BaseException as e:- self.debug("Unable to resolve " + str(host) + ": " + str(e))- return None+ self.debug("Unable to resolve %s (%s)" % (host, e))++ if len(addrs):+ self.debug("Resolved %s to: %s" % (host, addrs))++ return list(set(addrs))def resolveIP(self, ipaddr):- """Return a normalised resolution of an IPv4 address or None if not resolved."""+ """Return a normalised resolution of an IPv4 address.- if not ipaddr:- self.error("Invalid IP address", False)- return None+ Args:+ ipaddr (str): IP address to reverse resolve++ Returns:+ list: list of domain names+ """++ addrs = list()++ if not self.validIP(ipaddr) and not self.validIP6(ipaddr):+ self.error("Unable to resolve %s (Invalid IP address)" % ipaddr, False)+ return addrsself.debug("Performing reverse-resolve of %s" % ipaddr)try:addrs = self.normalizeDNS(socket.gethostbyaddr(ipaddr))- if len(addrs) > 0:- return list(set(addrs))- return Noneexcept BaseException as e:- self.debug("Unable to resolve " + ipaddr + " (" + str(e) + ")")- return None+ self.debug("Unable to resolve %s (%s)" % (ipaddr, e))++ if len(addrs):+ self.debug("Resolved %s to: %s" % (ipaddr, addrs))++ return list(set(addrs))def resolveHost6(self, hostname):- """Return a normalised resolution of an IPv6 address or None if not resolved."""+ """Return a normalised resolution of an IPv6 address.++ Args:+ hostname (str): hostname to reverse resolve++ Returns:+ list+ """++ addrs = list()if not hostname:- self.error("Invalid hostname", False)- return None+ self.error("Unable to resolve %s (Invalid hostname)" % hostname, False)+ return addrstry:- addrs = list()res = socket.getaddrinfo(hostname, None, socket.AF_INET6)for addr in res:if addr[4][0] not in addrs:addrs.append(addr[4][0])- if len(addrs) < 1:- return None- self.debug("Resolved " + hostname + " to IPv6: " + str(addrs))- return list(set(addrs))except BaseException as e:- self.debug("Unable to IPv6 resolve " + hostname + " (" + str(e) + ")")- return None+ self.debug("Unable to IPv6 resolve %s (%s)" % (hostname, e))++ if len(addrs):+ self.debug("Resolved %s to IPv6: %s" % (hostname, addrs))++ return list(set(addrs))def validateIP(self, host, ip):"""Verify a host resolves to a given IP."""addrs = self.resolveHost(host)- if addrs is None:+ if not addrs:return Falsefor addr in addrs:@@ -2151,7 +2179,7 @@ class SpiderFoot:randpool = 'bcdfghjklmnpqrstvwxyz3456789'randhost = ''.join([random.SystemRandom().choice(randpool) for x in range(10)])- if self.resolveHost(randhost + "." + target) is None:+ if not self.resolveHost(randhost + "." + target):return Falsereturn Truediff --git a/test/unit/test_spiderfoot.py b/test/unit/test_spiderfoot.pyindex 0ccda5ce..39d70b32 100644--- a/test/unit/test_spiderfoot.py+++ b/test/unit/test_spiderfoot.py@@ -626,51 +626,64 @@ class TestSpiderFoot(unittest.TestCase):tree = sf.dataParentChildToTree(dict())self.assertIsInstance(tree, dict)- def test_resolve_host(self):+ def test_resolve_host_should_return_list(self):"""Test resolveHost(self, host)"""sf = SpiderFoot(self.default_options)- addrs = sf.resolveHost('spiderfoot.net')+ addrs = sf.resolveHost('one.one.one.one')self.assertIsInstance(addrs, list)self.assertTrue(addrs)-- def test_resolve_host_invalid_host_should_return_none(self):- """- Test resolveHost(self, host)- """- sf = SpiderFoot(self.default_options)+ self.assertIn('1.1.1.1', addrs)addrs = sf.resolveHost(None)- self.assertEqual(None, addrs)+ self.assertFalse(addrs)+ self.assertIsInstance(addrs, list)- def test_resolve_ip(self):+ def test_resolve_ip_should_return_list(self):"""Test resolveIP(self, ipaddr)"""sf = SpiderFoot(self.default_options)- addrs = sf.resolveIP('spiderfoot.net')+ addrs = sf.resolveIP('1.1.1.1')self.assertIsInstance(addrs, list)self.assertTrue(addrs)+ self.assertIn('one.one.one.one', addrs)- def test_resolve_ip_invalid_ip_should_return_none(self):- """- Test resolveIP(self, ipaddr)- """- sf = SpiderFoot(self.default_options)+ addrs = sf.resolveIP('2606:4700:4700::1001')+ self.assertIsInstance(addrs, list)+ self.assertTrue(addrs)+ self.assertIn('one.one.one.one', addrs)addrs = sf.resolveIP(None)- self.assertEqual(None, addrs)+ self.assertFalse(addrs)+ self.assertIsInstance(addrs, list)++ addrs = sf.resolveIP([])+ self.assertFalse(addrs)+ self.assertIsInstance(addrs, list)++ addrs = sf.resolveIP("")+ self.assertFalse(addrs)+ self.assertIsInstance(addrs, list)- def test_resolve_host6(self):+ def test_resolve_host6_should_return_a_list(self):"""Test resolveHost6(self, hostname)"""sf = SpiderFoot(self.default_options)- self.assertEqual('TBD', 'TBD')+ addrs = sf.resolveHost6('one.one.one.one')+ self.assertIsInstance(addrs, list)+ self.assertTrue(addrs)+ self.assertIn('2606:4700:4700::1001', addrs)+ self.assertIn('2606:4700:4700::1111', addrs)++ addrs = sf.resolveHost6(None)+ self.assertFalse(addrs)+ self.assertIsInstance(addrs, list)def test_validate_ip_should_return_bool(self):"""