SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sfp_isc: Cleanup (#1280)
bcolesdcb9c024f691
diff --git a/modules/sfp_isc.py b/modules/sfp_isc.pyindex 04a534e9..a5d7619c 100644--- a/modules/sfp_isc.py+++ b/modules/sfp_isc.py@@ -1,7 +1,7 @@# -*- coding: utf-8 -*-# -------------------------------------------------------------------------------# Name: sfp_isc-# Purpose: Checks if an ASN, IP or domain is malicious.+# Purpose: Check if an IP address is malicious according to SANS ISC.## Author: [email protected]#@@ -14,22 +14,12 @@ import refrom spiderfoot import SpiderFootEvent, SpiderFootPlugin-malchecks = {- 'Internet Storm Center': {- 'id': '_isc',- 'checks': ['ip'],- 'url': 'https://isc.sans.edu/api/ip/{0}',- 'badregex': [r'.*<attacks>\d+</attacks>.*'],- 'goodregex': []- }-}-class sfp_isc(SpiderFootPlugin):meta = {'name': "Internet Storm Center",- 'summary': "Check if an IP is malicious according to SANS ISC.",+ 'summary': "Check if an IP address is malicious according to SANS ISC.",'flags': [],'useCases': ["Investigate", "Passive"],'categories': ["Reputation Systems"],@@ -68,79 +58,46 @@ class sfp_isc(SpiderFootPlugin):'checkaffiliates': "Apply checks to affiliates?"}- # Be sure to completely clear any class variables in setup()- # or you run the risk of data persisting between scan runs.-results = None+ errorState = Falsedef setup(self, sfc, userOpts=dict()):self.sf = sfcself.results = self.tempStorage()-- # Clear / reset any other class member variables here- # or you risk them persisting between threads.+ self.errorState = Falsefor opt in list(userOpts.keys()):self.opts[opt] = userOpts[opt]# What events is this module interested in for input- # * = be notified about all events.def watchedEvents(self):return ["IP_ADDRESS", "AFFILIATE_IPADDR"]# What events this module produces- # This is to support the end user in selecting modules based on events- # produced.def producedEvents(self):return ["MALICIOUS_IPADDR", "MALICIOUS_AFFILIATE_IPADDR"]- # Check the regexps to see whether the content indicates maliciousness- def contentMalicious(self, content, goodregex, badregex):- # First, check for the bad indicators- if len(badregex) > 0:- for rx in badregex:- if re.match(rx, content, re.IGNORECASE | re.DOTALL):- self.sf.debug("Found to be bad against bad regex: " + rx)- return True-- # Finally, check for good indicators- if len(goodregex) > 0:- for rx in goodregex:- if re.match(rx, content, re.IGNORECASE | re.DOTALL):- self.sf.debug("Found to be good againt good regex: " + rx)- return False-- # If nothing was matched, reply None- self.sf.debug("Neither good nor bad, unknown.")- return None-- # Look up 'query' type sources- def resourceQuery(self, id, target, targetType):- self.sf.debug(f"Querying {id} for maliciousness of {target}")-- for check in list(malchecks.keys()):- cid = malchecks[check]['id']- if id == cid:- url = str(malchecks[check]['url'])- res = self.sf.fetchUrl(url.format(target), timeout=30, useragent=self.opts['_useragent'])-- if res['content'] is None:- self.sf.error("Unable to fetch " + url.format(target))- return None-- if self.contentMalicious(res['content'], malchecks[check]['goodregex'], malchecks[check]['badregex']):- return url.format(target)-- return None-- def lookupItem(self, resourceId, itemType, target):- for check in list(malchecks.keys()):- cid = malchecks[check]['id']- if cid == resourceId and itemType in malchecks[check]['checks']:- self.sf.debug(f"Checking maliciousness of {target} ({itemType}) with: {cid}")- return self.resourceQuery(cid, target, itemType)-- return None+ def query(self, ip):+ if not ip:+ return++ res = self.sf.fetchUrl(+ f"https://isc.sans.edu/api/ip/{ip}",+ timeout=self.opts['_fetchtimeout'],+ useragent=self.opts['_useragent'],+ )++ if res['code'] != "200":+ self.sf.error(f"Unexpected HTTP response code {res['code']} from ISC.")+ self.errorState = True+ return++ if res['content'] is None:+ self.sf.error("Received no content from ISC")+ self.errorState = True+ return++ return res['content']# Handle events sent to this moduledef handleEvent(self, event):@@ -154,47 +111,30 @@ class sfp_isc(SpiderFootPlugin):self.sf.debug(f"Skipping {eventData}, already checked.")return None- self.results[eventData] = True+ if self.errorState:+ return- if eventName == 'CO_HOSTED_SITE' and not self.opts.get('checkcohosts', False):- return None- if eventName == 'AFFILIATE_IPADDR' \- and not self.opts.get('checkaffiliates', False):- return None+ self.results[eventData] = True- for check in list(malchecks.keys()):- cid = malchecks[check]['id']-- if eventName in ['IP_ADDRESS', 'AFFILIATE_IPADDR']:- typeId = 'ip'- if eventName == 'IP_ADDRESS':- evtType = 'MALICIOUS_IPADDR'- else:- evtType = 'MALICIOUS_AFFILIATE_IPADDR'-- if eventName in ['BGP_AS_OWNER', 'BGP_AS_MEMBER']:- typeId = 'asn'- evtType = 'MALICIOUS_ASN'-- if eventName in ['INTERNET_NAME', 'CO_HOSTED_SITE',- 'AFFILIATE_INTERNET_NAME']:- typeId = 'domain'- if eventName == "INTERNET_NAME":- evtType = "MALICIOUS_INTERNET_NAME"- if eventName == 'AFFILIATE_INTERNET_NAME':- evtType = 'MALICIOUS_AFFILIATE_INTERNET_NAME'- if eventName == 'CO_HOSTED_SITE':- evtType = 'MALICIOUS_COHOST'-- url = self.lookupItem(cid, typeId, eventData)-- if self.checkForStop():- return None-- # Notify other modules of what you've found- if url is not None:- text = f"{check} [{eventData}]\n<SFURL>{url}</SFURL>"- evt = SpiderFootEvent(evtType, text, self.__name__, event)- self.notifyListeners(evt)+ if eventName == 'IP_ADDRESS':+ evtType = 'MALICIOUS_IPADDR'+ elif eventName == 'AFFILIATE_IPADDR':+ if not self.opts.get('checkaffiliates', False):+ return+ evtType = 'MALICIOUS_AFFILIATE_IPADDR'+ else:+ return++ data = self.query(eventData)++ if not data:+ return++ attacks = re.findall(r"<attacks>([0-9]+)</attacks>", data)+ if attacks:+ url = f"https://isc.sans.edu/api/ip/{eventData}"+ text = f"Internet Storm Center [{eventData}]\n<SFURL>{url}</SFURL>"+ evt = SpiderFootEvent(evtType, text, self.__name__, event)+ self.notifyListeners(evt)# End of sfp_isc class