SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
ipapi.com module (#1138)
Krishnasis Mandalc91c9e44d1f9
diff --git a/modules/sfp_ipapicom.py b/modules/sfp_ipapicom.pynew file mode 100644index 00000000..2cbc219b--- /dev/null+++ b/modules/sfp_ipapicom.py@@ -0,0 +1,143 @@+# -*- coding: utf-8 -*-+# -------------------------------------------------------------------------------+# Name: sfp_ipapicom+# Purpose: SpiderFoot plug-in to identify the Geo-location of IP addresses+# identified by other modules using ipapi.com+#+# Author: Krishnasis Mandal <[email protected]>+#+# Created: 29/01/2021+# Copyright: (c) Steve Micallef+# Licence: GPL+# -------------------------------------------------------------------------------++import json+import time++from spiderfoot import SpiderFootEvent, SpiderFootPlugin+++class sfp_ipapicom(SpiderFootPlugin):++ meta = {+ 'name': "ipapi.com",+ 'summary': "Queries ipapi.com to identify geolocation of IP Addresses using ipapi.com API",+ 'flags': ["apikey"],+ 'useCases': ["Footprint", "Investigate", "Passive"],+ 'categories': ["Real World"],+ 'dataSource': {+ 'website': "https://ipapi.com/",+ 'model': "FREE_AUTH_LIMITED",+ 'references': [+ "https://ipapi.com/documentation"+ ],+ 'apiKeyInstructions': [+ "Visit https://ipapi.com/",+ "Register a free account",+ "Browse to https://ipapi.com/dashboard",+ "Your API Key will be listed under Your API Access Key",+ ],+ 'favIcon': "https://ipapi.com/site_images/ipapi_shortcut_icon.ico",+ 'logo': "https://ipapi.com/site_images/ipapi_icon.png",+ 'description': "ipapi provides an easy-to-use API interface allowing customers "+ "to look various pieces of information IPv4 and IPv6 addresses are associated with. "+ "For each IP address processed, the API returns more than 45 unique data points, "+ "such as location data, connection data, ISP information, time zone, currency and security assessment data.",+ }+ }++ # Default options+ opts = {+ 'api_key': '',+ }++ # Option descriptions+ optdescs = {+ 'api_key': "API Key for ipapi.com",+ }++ results = None++ def setup(self, sfc, userOpts=dict()):+ self.sf = sfc+ self.results = self.tempStorage()++ for opt in list(userOpts.keys()):+ self.opts[opt] = userOpts[opt]++ # What events is this module interested in for input+ def watchedEvents(self):+ return [+ "IP_ADDRESS",+ "IPV6_ADDRESS"+ ]++ # What events this module produces+ # This is to support the end user in selecting modules based on events+ # produced.+ def producedEvents(self):+ return [+ "GEOINFO",+ "RAW_RIR_DATA"+ ]++ def query(self, qry):+ queryString = f"http://api.ipapi.com/api/{qry}?access_key={self.opts['api_key']}"++ res = self.sf.fetchUrl(queryString,+ timeout=self.opts['_fetchtimeout'],+ useragent=self.opts['_useragent'])+ time.sleep(1.5)++ if res['code'] == "429":+ self.sf.error("You are being rate-limited by IP-API.com.")+ self.errorState = True+ return None+ if res['content'] is None:+ self.sf.info(f"No ipapi.com data found for {qry}")+ return None+ try:+ return json.loads(res['content'])+ except Exception as e:+ self.sf.debug(f"Error processing JSON response: {e}")+ return None++ # Handle events sent to this module+ def handleEvent(self, event):+ eventName = event.eventType+ srcModuleName = event.module+ eventData = event.data++ self.sf.debug(f"Received event, {eventName}, from {srcModuleName}")++ if self.errorState:+ return None++ if self.opts['api_key'] == "":+ self.sf.error("You enabled sfp_ipapicom but did not set an API key!")+ self.errorState = True+ return None++ # Don't look up stuff twice+ if eventData in self.results:+ self.sf.debug(f"Skipping {eventData}, already checked.")+ return None+ else:+ self.results[eventData] = True++ data = self.query(eventData)++ if data.get('country_name'):+ location = ', '.join(filter(None, [data.get('city'), data.get('region_name'), data.get('region_code'), data.get('country_name'), data.get('country_code')]))+ evt = SpiderFootEvent('GEOINFO', location, self.__name__, event)+ self.notifyListeners(evt)++ if data.get('latitude') and data.get('longitude'):+ evt = SpiderFootEvent("PHYSICAL_COORDINATES", f"{data.get('latitude')}, {data.get('longitude')}", self.__name__, event)+ self.notifyListeners(evt)++ evt = SpiderFootEvent('RAW_RIR_DATA', str(data), self.__name__, event)+ self.notifyListeners(evt)+++# End of sfp_ipapicom classdiff --git a/test/unit/modules/test_sfp_ipapicom.py b/test/unit/modules/test_sfp_ipapicom.pynew file mode 100644index 00000000..41a0062c--- /dev/null+++ b/test/unit/modules/test_sfp_ipapicom.py@@ -0,0 +1,80 @@+# test_sfp_ipapicom.py+import unittest++from modules.sfp_ipapicom import sfp_ipapicom+from sflib import SpiderFoot+from spiderfoot import SpiderFootEvent, SpiderFootTarget+++class TestModuleipapicom(unittest.TestCase):+ """+ Test modules.sfp_ipapicom+ """++ default_options = {+ '_debug': False, # Debug+ '__logging': True, # Logging in general+ '__outputfilter': None, # Event types to filter from modules' output+ '_useragent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0', # User-Agent to use for HTTP requests+ '_dnsserver': '', # Override the default resolver+ '_fetchtimeout': 5, # number of seconds before giving up on a fetch+ '_internettlds': 'https://publicsuffix.org/list/effective_tld_names.dat',+ '_internettlds_cache': 72,+ '_genericusers': "abuse,admin,billing,compliance,devnull,dns,ftp,hostmaster,inoc,ispfeedback,ispsupport,list-request,list,maildaemon,marketing,noc,no-reply,noreply,null,peering,peering-notify,peering-request,phish,phishing,postmaster,privacy,registrar,registry,root,routing-registry,rr,sales,security,spam,support,sysadmin,tech,undisclosed-recipients,unsubscribe,usenet,uucp,webmaster,www",+ '__version__': '3.3-DEV',+ '__database': 'spiderfoot.test.db', # note: test database file+ '__modules__': None, # List of modules. Will be set after start-up.+ '_socks1type': '',+ '_socks2addr': '',+ '_socks3port': '',+ '_socks4user': '',+ '_socks5pwd': '',+ '_torctlport': 9051,+ '__logstdout': False+ }++ def test_opts(self):+ module = sfp_ipapicom()+ self.assertEqual(len(module.opts), len(module.optdescs))++ def test_setup(self):+ """+ Test setup(self, sfc, userOpts=dict())+ """+ sf = SpiderFoot(self.default_options)++ module = sfp_ipapicom()+ module.setup(sf, dict())++ def test_watchedEvents_should_return_list(self):+ module = sfp_ipapicom()+ self.assertIsInstance(module.watchedEvents(), list)++ def test_producedEvents_should_return_list(self):+ module = sfp_ipapicom()+ self.assertIsInstance(module.producedEvents(), list)++ @unittest.skip("todo")+ def test_handleEvent(self):+ """+ Test handleEvent(self, event)+ """+ sf = SpiderFoot(self.default_options)++ module = sfp_ipapicom()+ module.setup(sf, dict())++ target_value = 'example target value'+ target_type = 'IP_ADDRESS'+ target = SpiderFootTarget(target_value, target_type)+ module.setTarget(target)++ 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)++ self.assertIsNone(result)