SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sfp_cloudflaredns: Use both Family and Malware DNS servers (#1502)
bcoles06611ea6d943
diff --git a/modules/sfp_cloudflaredns.py b/modules/sfp_cloudflaredns.pyindex a046730e..597a49c4 100644--- a/modules/sfp_cloudflaredns.py+++ b/modules/sfp_cloudflaredns.py@@ -2,7 +2,7 @@# -------------------------------------------------------------------------------# Name: sfp_cloudflaredns# Purpose: SpiderFoot plug-in for looking up whether hosts are blocked by-# CloudFlare malware DNS (1.1.1.2).+# CloudFlare family and malware filtering DNS servers.## Author: Steve Micallef <[email protected]>#@@ -19,8 +19,8 @@ from spiderfoot import SpiderFootEvent, SpiderFootPluginclass sfp_cloudflaredns(SpiderFootPlugin):meta = {- 'name': "CloudFlare Malware DNS",- 'summary': "Check if a host would be blocked by CloudFlare Malware-blocking DNS",+ 'name': "CloudFlare DNS",+ 'summary': "Check if a host would be blocked by CloudFlare DNS.",'flags': [],'useCases': ["Investigate", "Passive"],'categories': ["Reputation Systems"],@@ -39,11 +39,9 @@ class sfp_cloudflaredns(SpiderFootPlugin):}}- # Default optionsopts = {}- # Option descriptionsoptdescs = {}@@ -56,66 +54,96 @@ class sfp_cloudflaredns(SpiderFootPlugin):for opt in list(userOpts.keys()):self.opts[opt] = userOpts[opt]- # What events is this module interested in for inputdef watchedEvents(self):- return ["INTERNET_NAME", "AFFILIATE_INTERNET_NAME", "CO_HOSTED_SITE"]+ return [+ "INTERNET_NAME",+ "AFFILIATE_INTERNET_NAME",+ "CO_HOSTED_SITE"+ ]- # What events this module produces- # This is to support the end user in selecting modules based on events- # produced.def producedEvents(self):- return ["MALICIOUS_INTERNET_NAME", "MALICIOUS_AFFILIATE_INTERNET_NAME",- "MALICIOUS_COHOST"]-- def queryAddr(self, qaddr):+ return [+ "BLACKLISTED_INTERNET_NAME",+ "BLACKLISTED_AFFILIATE_INTERNET_NAME",+ "BLACKLISTED_COHOST",+ "MALICIOUS_INTERNET_NAME",+ "MALICIOUS_AFFILIATE_INTERNET_NAME",+ "MALICIOUS_COHOST",+ ]++ def queryFamilyDNS(self, qaddr):res = dns.resolver.Resolver()- res.nameservers = ["1.1.1.2", "1.0.0.2"]+ res.nameservers = ["1.1.1.3", "1.0.0.3"]try:- addrs = res.resolve(qaddr)- self.debug(f"Addresses returned: {addrs}")+ return res.resolve(qaddr)except Exception:self.debug(f"Unable to resolve {qaddr}")- return False- if not addrs:- return False+ return None- if "0.0.0.0" in self.sf.normalizeDNS(addrs):- return False+ def queryMalwareDNS(self, qaddr):+ res = dns.resolver.Resolver()+ res.nameservers = ["1.1.1.2", "1.0.0.2"]- return True+ try:+ return res.resolve(qaddr)+ except Exception:+ self.debug(f"Unable to resolve {qaddr}")++ return None- # Handle events sent to this moduledef handleEvent(self, event):eventName = event.eventType- srcModuleName = event.moduleeventData = event.data- parentEvent = event- resolved = False- self.debug(f"Received event, {eventName}, from {srcModuleName}")+ self.debug(f"Received event, {eventName}, from {event.module}")if eventData in self.results:return+self.results[eventData] = True- # Check that it resolves first, as it becomes a valid- # malicious host only if NOT resolved by CloudFlare DNS.- try:- if self.sf.resolveHost(eventData) or self.sf.resolveHost6(eventData):- resolved = True- except Exception:+ if eventName == "INTERNET_NAME":+ e = "BLACKLISTED_INTERNET_NAME"+ elif eventName == "AFFILIATE_INTERNET_NAME":+ e = "BLACKLISTED_AFFILIATE_INTERNET_NAME"+ elif eventName == "CO_HOSTED_SITE":+ e = "BACKLISTED_COHOST"+ else:+ self.debug(f"Unexpected event type {eventName}, skipping")++ family = self.sf.normalizeDNS(self.queryFamilyDNS(eventData))+ malware = self.sf.normalizeDNS(self.queryMalwareDNS(eventData))++ if not family or not malware:+ return++ if '0.0.0.0' not in family and '0.0.0.0' not in malware:+ return++ # Host is blocked only by family filters+ if '0.0.0.0' not in malware:+ self.debug(f"{eventData} blocked by CloudFlare Family DNS")+ evt = SpiderFootEvent(e, f"CloudFlare - Family [{eventData}]", self.__name__, event)+ self.notifyListeners(evt)return- if resolved:- found = self.queryAddr(eventData)- typ = "MALICIOUS_" + eventName- if eventName == "CO_HOSTED_SITE":- typ = "MALICIOUS_COHOST"- if not found:- evt = SpiderFootEvent(typ, "Blocked by CloudFlare DNS [" + eventData + "]",- self.__name__, parentEvent)- self.notifyListeners(evt)+ # Host is blocked only by malware filters+ self.debug(f"{eventData} blocked by CloudFlare Malware DNS")+ evt = SpiderFootEvent(e, f"CloudFlare - Malware [{eventData}]", self.__name__, event)+ self.notifyListeners(evt)++ if eventName == "INTERNET_NAME":+ e = "MALICIOUS_INTERNET_NAME"+ elif eventName == "AFFILIATE_INTERNET_NAME":+ e = "MALICIOUS_AFFILIATE_INTERNET_NAME"+ elif eventName == "CO_HOSTED_SITE":+ e = "MALICIOUS_COHOST"+ else:+ self.debug(f"Unexpected event type {eventName}, skipping")++ evt = SpiderFootEvent(e, f"CloudFlare - Malware [{eventData}]", self.__name__, event)+ self.notifyListeners(evt)# End of sfp_cloudflaredns classdiff --git a/test/unit/modules/test_sfp_cloudflaredns.py b/test/unit/modules/test_sfp_cloudflaredns.pyindex faf3e45e..4456a3c0 100644--- a/test/unit/modules/test_sfp_cloudflaredns.py+++ b/test/unit/modules/test_sfp_cloudflaredns.py@@ -34,10 +34,7 @@ class TestModulecloudflaredns(unittest.TestCase):module = sfp_cloudflaredns()self.assertIsInstance(module.producedEvents(), list)- def test_queryAddr_should_resolve_unblocked_host(self):- """- Test queryAddr(self, qaddr)- """+ def test_handleEvent_event_data_safe_internet_name_not_blocked_should_not_return_event(self):sf = SpiderFoot(self.default_options)module = sfp_cloudflaredns()@@ -48,29 +45,65 @@ class TestModulecloudflaredns(unittest.TestCase):target = SpiderFootTarget(target_value, target_type)module.setTarget(target)- resolved = module.queryAddr('cloudflare.com')- self.assertTrue(resolved)+ def new_notifyListeners(self, event):+ raise Exception(f"Raised event {event.eventType}: {event.data}")- def test_handleEvent(self):- """- Test handleEvent(self, event)- """+ module.notifyListeners = new_notifyListeners.__get__(module, sfp_cloudflaredns)++ event_type = 'ROOT'+ event_data = 'example data'+ event_module = ''+ source_event = ''+ evt = SpiderFootEvent(event_type, event_data, event_module, source_event)++ event_type = 'INTERNET_NAME'+ event_data = 'cloudflare.com'+ event_module = 'example module'+ source_event = evt++ evt = SpiderFootEvent(event_type, event_data, event_module, source_event)+ result = module.handleEvent(evt)++ self.assertIsNone(result)++ def test_handleEvent_event_data_adult_internet_name_blocked_should_return_event(self):sf = SpiderFoot(self.default_options)module = sfp_cloudflaredns()module.setup(sf, dict())- target_value = 'example target value'- target_type = 'IP_ADDRESS'+ target_value = 'spiderfoot.net'+ target_type = 'INTERNET_NAME'target = SpiderFootTarget(target_value, target_type)module.setTarget(target)+ def new_notifyListeners(self, event):+ expected = 'BLACKLISTED_INTERNET_NAME'+ if str(event.eventType) != expected:+ raise Exception(f"{event.eventType} != {expected}")++ expected = 'CloudFlare - Family [pornhub.com]'+ if str(event.data) != expected:+ raise Exception(f"{event.data} != {expected}")++ raise Exception("OK")++ module.notifyListeners = new_notifyListeners.__get__(module, sfp_cloudflaredns)+event_type = 'ROOT'event_data = 'example data'event_module = ''source_event = ''evt = SpiderFootEvent(event_type, event_data, event_module, source_event)- result = module.handleEvent(evt)+ event_type = 'INTERNET_NAME'+ event_data = 'pornhub.com'+ event_module = 'example module'+ source_event = evt- self.assertIsNone(result)+ evt = SpiderFootEvent(event_type, event_data, event_module, source_event)++ with self.assertRaises(Exception) as cm:+ module.handleEvent(evt)++ self.assertEqual("OK", str(cm.exception))