SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
Update sfp_ipinfo module
Brendan Coles04f12889cf94
diff --git a/modules/sfp_ipinfo.py b/modules/sfp_ipinfo.pyindex 23d18077..31bde5d2 100644--- a/modules/sfp_ipinfo.py+++ b/modules/sfp_ipinfo.py@@ -1,8 +1,8 @@# -*- coding: utf-8 -*-# --------------------------------------------------------------------------------# Name: sfp_ipinfo-# Purpose: SpiderFoot plug-in to identify the Geo-location of IP addresses-# identified by other modules using ipinfo.io.+# Name: sfp_ipinfo+# Purpose: SpiderFoot plug-in to identify the Geo-location of IP addresses+# identified by other modules using ipinfo.io.## Author: Steve Micallef <[email protected]>#@@ -18,7 +18,6 @@ from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEventclass sfp_ipinfo(SpiderFootPlugin):"""IPInfo.io:Footprint,Investigate,Passive:Real World:apikey:Identifies the physical location of IP addresses identified using ipinfo.io."""-# Default optionsopts = {"api_key": ""@@ -26,12 +25,13 @@ class sfp_ipinfo(SpiderFootPlugin):optdescs = {"api_key": "Ipinfo.io access token."}- results = dict()++ results = NoneerrorState = Falsedef setup(self, sfc, userOpts=dict()):self.sf = sfc- self.results = dict()+ self.results = self.tempStorage()self.errorState = Falsefor opt in userOpts.keys():@@ -39,7 +39,7 @@ class sfp_ipinfo(SpiderFootPlugin):# What events is this module interested in for inputdef watchedEvents(self):- return ['IP_ADDRESS']+ return ['IP_ADDRESS', 'IPV6_ADDRESS']# What events this module produces# This is to support the end user in selecting modules based on events@@ -47,6 +47,33 @@ class sfp_ipinfo(SpiderFootPlugin):def producedEvents(self):return ["GEOINFO"]+ # https://ipinfo.io/developers+ def queryIP(self, ip):+ headers = {+ 'Authorization': "Bearer " + self.opts['api_key']+ }+ res = self.sf.fetchUrl("https://ipinfo.io/" + ip + "/json",+ timeout=self.opts['_fetchtimeout'],+ useragent=self.opts['_useragent'],+ headers=headers)++ if res['code'] == "429":+ self.sf.error("You are being rate-limited by ipinfo.io.", False)+ self.errorState = True+ return None++ if res['content'] is None:+ self.sf.info("No GeoIP info found for " + ip)+ return None++ try:+ result = json.loads(res['content'])+ except Exception as e:+ self.sf.debug("Error processing JSON response.")+ return None++ return result+# Handle events sent to this moduledef handleEvent(self, event):eventName = event.eventType@@ -67,31 +94,21 @@ class sfp_ipinfo(SpiderFootPlugin):if eventData in self.results:self.sf.debug("Skipping " + eventData + " as already mapped.")return None- else:- self.results[eventData] = True- res = self.sf.fetchUrl("https://ipinfo.io/" + eventData + "/json?token=" + self.opts['api_key'],- timeout=self.opts['_fetchtimeout'], useragent=self.opts['_useragent'])+ self.results[eventData] = True- if res['code'] == "429":- self.sf.error("You are being rate-limited by ipinfo.io.", False)- return+ data = self.queryIP(eventData)- if res['content'] is None:- self.sf.info("No GeoIP info found for " + eventData)- try:- hostip = json.loads(res['content'])- except Exception as e:- self.sf.debug("Error processing JSON response.")+ if data is None:return None- if 'country' not in hostip:+ if 'country' not in data:return None- self.sf.info("Found GeoIP for " + eventData + ": " + hostip['country'])- countrycity = hostip['country'] + ", " + hostip.get('region', "Unknown region") + \- ", " + hostip.get('city', "Unknown city")- evt = SpiderFootEvent("GEOINFO", countrycity, self.__name__, event)+ location = ', '.join(filter(None, [data.get('city'), data.get('region'), data.get('country')]))+ self.sf.info("Found GeoIP for " + eventData + ": " + location)++ evt = SpiderFootEvent("GEOINFO", location, self.__name__, event)self.notifyListeners(evt)return None