SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
Nameapi module (#1090)
Krishnasis Mandald59eb59aab3c
diff --git a/modules/sfp_nameapi.py b/modules/sfp_nameapi.pynew file mode 100644index 00000000..b6aa8c83--- /dev/null+++ b/modules/sfp_nameapi.py@@ -0,0 +1,128 @@+# -*- coding: utf-8 -*-+# -------------------------------------------------------------------------------+# Name: sfp_nameapi+# Purpose: Spiderfoot plugin to check if an email is+# disposable using nameapi.org API.+#+# Author: Krishnasis Mandal <[email protected]>+#+# Created: 2020-10-02+# Copyright: (c) Steve Micallef+# Licence: GPL+# -------------------------------------------------------------------------------++import json+from spiderfoot import SpiderFootEvent, SpiderFootPlugin+++class sfp_nameapi(SpiderFootPlugin):++ meta = {+ 'name': "NameAPI",+ 'summary': "Check whether an email is disposable",+ 'flags': ["apikey"],+ 'useCases': ["Footprint", "Investigate", "Passive"],+ 'categories': ["Search Engines"],+ 'dataSource': {+ 'website': "https://www.nameapi.org/",+ 'model': "FREE_AUTH_LIMITED",+ 'references': [+ "https://www.nameapi.org/en/developer/manuals/rest-web-services/53/web-services/disposable-email-address-detector/"+ ],+ 'apiKeyInstructions': [+ "Visit https://nameapi.org",+ "Click on 'Get API Key'",+ "Register a free account",+ "The API key will be sent to your email"+ ],+ 'favIcon': "https://www.nameapi.org/fileadmin/favicon.ico",+ 'logo': "https://www.nameapi.org/fileadmin/templates/nameprofiler/images/name-api-logo.png",+ 'description': "The NameAPI DEA-Detector checks email addresses "+ "against a list of known trash domains such as mailinator.com.\n"+ "It classifies those as disposable which operate as a time-limited, "+ "web based way of receiving emails, for example, sign up confirmations.",+ }+ }++ opts = {+ 'api_key': ''+ }++ optdescs = {+ 'api_key': "API Key for NameAPI"+ }++ results = None+ errorState = False++ def setup(self, sfc, userOpts=dict()):+ self.sf = sfc+ self.results = self.tempStorage()++ for opt in list(userOpts.keys()):+ self.opts[opt] = userOpts[opt]++ def watchedEvents(self):+ return [+ "EMAILADDR"+ ]++ def producedEvents(self):+ return [+ "EMAILADDR_DISPOSABLE",+ "RAW_RIR_DATA"+ ]++ def queryEmailAddr(self, qry):+ res = self.sf.fetchUrl(+ f"http://api.nameapi.org/rest/v5.3/email/disposableemailaddressdetector?apiKey={self.opts['api_key']}&emailAddress={qry}",+ timeout=self.opts['_fetchtimeout'],+ useragent="SpiderFoot"+ )++ if res['content'] is None:+ self.sf.info(f"No NameAPI info found for {qry}")+ return None++ try:+ return json.loads(res['content'])+ except Exception as e:+ self.sf.error(f"Error processing JSON response from NameAPI: {e}")++ return None++ # Handle events sent to this module+ def handleEvent(self, event):+ eventName = event.eventType+ srcModuleName = event.module+ eventData = event.data++ if self.errorState:+ return++ self.sf.debug(f"Received event, {eventName}, from {srcModuleName}")++ if self.opts["api_key"] == "":+ self.sf.error(+ f"You enabled {self.__class__.__name__} but did not set an API key!"+ )+ self.errorState = True+ return None++ self.results[eventData] = True++ data = self.queryEmailAddr(eventData)++ if data is None:+ return++ isDisposable = data.get('disposable')++ if isDisposable == "YES":+ evt = SpiderFootEvent("RAW_RIR_DATA", str(data), self.__name__, event)+ self.notifyListeners(evt)++ evt = SpiderFootEvent("EMAILADDR_DISPOSABLE", eventData, self.__name__, event)+ self.notifyListeners(evt)++# End of sfp_nameapi classdiff --git a/test/unit/modules/test_sfp_nameapi.py b/test/unit/modules/test_sfp_nameapi.pynew file mode 100644index 00000000..c2f9d77a--- /dev/null+++ b/test/unit/modules/test_sfp_nameapi.py@@ -0,0 +1,80 @@+# test_sfp_nameapi.py+import unittest++from modules.sfp_nameapi import sfp_nameapi+from sflib import SpiderFoot+from spiderfoot import SpiderFootEvent, SpiderFootTarget+++class TestModulenameapi(unittest.TestCase):+ """+ Test modules.sfp_nameapi+ """++ 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_nameapi()+ 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_nameapi()+ module.setup(sf, dict())++ def test_watchedEvents_should_return_list(self):+ module = sfp_nameapi()+ self.assertIsInstance(module.watchedEvents(), list)++ def test_producedEvents_should_return_list(self):+ module = sfp_nameapi()+ self.assertIsInstance(module.producedEvents(), list)++ def test_handleEvent_no_api_key_should_set_errorState(self):+ """+ Test handleEvent(self, event)+ """+ sf = SpiderFoot(self.default_options)++ module = sfp_nameapi()+ module.setup(sf, dict())++ target_value = 'example target value'+ target_type = 'EMAILADDR'+ 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)+ self.assertTrue(module.errorState)