SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sfp_opennic: Add sfp_opennic module (#1574)
bcoles4f358d920172
diff --git a/modules/sfp_opennic.py b/modules/sfp_opennic.pynew file mode 100644index 00000000..fbebdf33--- /dev/null+++ b/modules/sfp_opennic.py@@ -0,0 +1,181 @@+# -*- coding: utf-8 -*-+# -------------------------------------------------------------------------------+# Name: sfp_opnenic+# Purpose: SpiderFoot plug-in for resolving host names on the OpenNIC+# alternative DNS system.+#+# Author: <[email protected]>+#+# Created: 2021-10-16+# Copyright: (c) bcoles 2021+# Licence: GPL+# -------------------------------------------------------------------------------++import dns.resolver++from spiderfoot import SpiderFootEvent, SpiderFootPlugin+++class sfp_opennic(SpiderFootPlugin):++ meta = {+ 'name': "OpenNIC DNS",+ 'summary': "Resolves host names in the OpenNIC alternative DNS system.",+ 'flags': [],+ 'useCases': ["Investigate", "Footprint", "Passive"],+ 'categories': ["DNS"],+ 'dataSource': {+ 'website': "https://www.opennic.org/",+ 'model': "FREE_NOAUTH_UNLIMITED",+ 'references': [+ "https://wiki.opennic.org/",+ "https://servers.opennic.org",+ ],+ 'description': "An organization of hobbyists who run an alternative DNS network, "+ "also provides access to domains not administered by ICANN."+ }+ }++ opts = {+ 'checkaffiliates': True,+ }++ optdescs = {+ 'checkaffiliates': "Apply checks to affiliates?",+ }++ 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]++ def watchedEvents(self):+ return [+ "INTERNET_NAME",+ "INTERNET_NAME_UNRESOLVED",+ "AFFILIATE_INTERNET_NAME",+ "AFFILIATE_INTERNET_NAME_UNRESOLVED",+ ]++ def producedEvents(self):+ return [+ "IP_ADDRESS",+ "IPV6_ADDRESS",+ "AFFILIATE_IPADDR",+ "AFFILIATE_IPV6_ADDRESS",+ ]++ def queryOpenNIC(self, qaddr):+ res = dns.resolver.Resolver()+ # https://servers.opennicproject.org/+ res.nameservers = [+ "185.121.177.177",+ "161.97.219.84",+ "163.172.168.171",+ "94.103.153.176",+ "207.192.71.13",+ "178.63.116.152",+ "209.141.36.19",+ "188.226.146.136",+ "198.98.51.33",+ "79.124.7.81",+ "144.76.103.143",+ ]++ try:+ return res.resolve(qaddr)+ except Exception:+ self.debug(f"Unable to resolve {qaddr}")++ return None++ def tlds(self):+ """Valid OpenNIC top-level domains.++ Returns:+ list: OpenNIC TLDs (and peer TLDs).+ """+ return [+ 'bbs',+ 'chan',+ 'cyb',+ 'dyn',+ 'epic',+ 'free',+ 'geek',+ 'glue',+ 'gopher',+ 'indy',+ 'libre',+ 'neo',+ 'null',+ 'o',+ 'oss',+ 'oz',+ 'parody',+ 'pirate',++ # Peers+ 'bazar',+ 'bit',+ 'coin',+ 'emc',+ 'fur',+ 'ku',+ 'lib',+ 'te',+ 'ti',+ 'uu',+ ]++ def handleEvent(self, event):+ eventName = event.eventType+ eventData = event.data++ self.debug(f"Received event, {eventName}, from {event.module}")++ if eventData in self.results:+ return++ self.results[eventData] = True++ if eventData.split('.')[-1] not in self.tlds():+ return++ affiliate = False++ if "AFFILIATE" in eventName:+ if not self.opts.get('checkaffiliates', False):+ return+ affiliate = True++ addrs = self.sf.normalizeDNS(self.queryOpenNIC(eventData))++ if not addrs:+ return++ self.debug(f"OpenNIC resolved {eventData} to addresses: {addrs}")++ for addr in set(addrs):+ if self.sf.validIP(addr):+ if affiliate and not self.getTarget().matches(addr, includeParents=True):+ evt = SpiderFootEvent("AFFILIATE_IPADDR", addr, self.__name__, event)+ else:+ evt = SpiderFootEvent("IP_ADDRESS", addr, self.__name__, event)++ self.notifyListeners(evt)+ elif self.sf.validIP6(addr):+ if affiliate and not self.getTarget().matches(addr, includeParents=True):+ evt = SpiderFootEvent("AFFILIATE_IPV6_ADDRESS", addr, self.__name__, event)+ else:+ evt = SpiderFootEvent("IPV6_ADDRESS", addr, self.__name__, event)++ self.notifyListeners(evt)+ else:+ continue++# End of sfp_opennic classdiff --git a/test/unit/modules/test_sfp_opennic.py b/test/unit/modules/test_sfp_opennic.pynew file mode 100644index 00000000..a3fc2e83--- /dev/null+++ b/test/unit/modules/test_sfp_opennic.py@@ -0,0 +1,72 @@+# test_sfp_opennic.py+import pytest+import unittest++from modules.sfp_opennic import sfp_opennic+from sflib import SpiderFoot+from spiderfoot import SpiderFootEvent, SpiderFootTarget+++class TestModuleOpenNic(unittest.TestCase):+ """+ Test modules.sfp_opennic+ """++ def test_opts(self):+ module = sfp_opennic()+ 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_opennic()+ module.setup(sf, dict())++ def test_watchedEvents_should_return_list(self):+ module = sfp_opennic()+ self.assertIsInstance(module.watchedEvents(), list)++ def test_producedEvents_should_return_list(self):+ module = sfp_opennic()+ self.assertIsInstance(module.producedEvents(), list)++ def test_handleEvent_event_data_internet_name_with_opennic_tld_should_return_ip_address_event(self):+ sf = SpiderFoot(self.default_options)++ module = sfp_opennic()+ module.setup(sf, dict())++ target_value = 'spiderfoot.net'+ target_type = 'INTERNET_NAME'+ target = SpiderFootTarget(target_value, target_type)+ module.setTarget(target)++ def new_notifyListeners(self, event):+ expected = 'IP_ADDRESS'+ if str(event.eventType) != expected:+ raise Exception(f"{event.eventType} != {expected}")++ raise Exception("OK")++ module.notifyListeners = new_notifyListeners.__get__(module, sfp_opennic)++ 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 = 'opennic.glue'+ event_module = 'example module'+ source_event = evt+ 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))