SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sfp_tool_retirejs: New module for the retire.js tool.
smicallef012fbdff791d
diff --git a/modules/sfp_tool_retirejs.py b/modules/sfp_tool_retirejs.pynew file mode 100644index 00000000..2bf2abc2--- /dev/null+++ b/modules/sfp_tool_retirejs.py@@ -0,0 +1,176 @@+# -*- coding: utf-8 -*-+# -------------------------------------------------------------------------------+# Name: sfp_tool_retirejs+# Purpose: SpiderFoot plug-in for using the 'Retire.js' tool.+#+# Author: Steve Micallef <[email protected]>+#+# Created: 2022-04-02+# Copyright: (c) Steve Micallef 2022+# Licence: GPL+# -------------------------------------------------------------------------------++import os+import sys+import json+import shutil+import tempfile+from subprocess import Popen, PIPE, TimeoutExpired++from spiderfoot import SpiderFootPlugin, SpiderFootEvent+++class sfp_tool_retirejs(SpiderFootPlugin):++ meta = {+ "name": "Tool - Retire.js",+ "summary": "Scanner detecting the use of JavaScript libraries with known vulnerabilities",+ "flags": ["tool"],+ "useCases": [+ "Footprint",+ "Investigate"+ ],+ "categories": ["Content Analysis"],+ "toolDetails": {+ "name": "Retire.js",+ "description": "Scanner detecting the use of JavaScript libraries with known vulnerabilities",+ "website": "http://retirejs.github.io/retire.js/",+ "repository": "https://github.com/RetireJS/retire.js"+ }+ }++ # Default options+ opts = {+ "retirejs_path": "",+ }++ # Option descriptions+ optdescs = {+ "retirejs_path": "Path to your retire binary. Must be set."+ }++ # Target+ results = None+ errorState = False++ def setup(self, sfc, userOpts=dict()):+ self.sf = sfc+ self.results = self.tempStorage()++ for opt in userOpts.keys():+ self.opts[opt] = userOpts[opt]++ def watchedEvents(self):+ return ["LINKED_URL_INTERNAL", "LINKED_URL_EXTERNAL"]++ def producedEvents(self):+ return [+ "VULNERABILITY_CVE_CRITICAL",+ "VULNERABILITY_CVE_HIGH",+ "VULNERABILITY_CVE_MEDIUM",+ "VULNERABILITY_CVE_LOW",+ "VULNERABILITY_GENERAL"+ ]++ # Handle events sent to this module+ def handleEvent(self, event):+ eventName = event.eventType+ srcModuleName = event.module+ eventData = event.data++ self.debug(f"Received event, {eventName}, from {srcModuleName}")++ if self.errorState:+ return++ if not self.opts['retirejs_path']:+ self.error("You enabled sfp_tool_retirejs but did not set a path to the tool!")+ self.errorState = True+ return++ exe = self.opts['retirejs_path']+ if self.opts['retirejs_path'].endswith('/'):+ exe = f"{exe}retire"++ if not os.path.isfile(exe):+ self.error(f"File does not exist: {exe}")+ self.errorState = True+ return++ if ".js" not in eventData:+ return++ # Don't look up stuff twice+ if eventData in self.results:+ self.debug(f"Skipping {eventData} as already scanned.")+ return+ self.results[eventData] = True++ # Store the javascript file being analyzed somewhere temporary+ tmpdirname = tempfile.mkdtemp()+ res = self.sf.fetchUrl(+ eventData,+ timeout=self.opts["_fetchtimeout"],+ useragent=self.opts["_useragent"],+ disableContentEncoding=True+ )++ if res["content"] is None:+ self.error(f"Unable to fetch {eventData}")+ return++ p = None+ try:+ with open(f"{tmpdirname}/lib.js", "wb") as f:+ f.write(res["content"])++ p = Popen(+ # Keep -j to avoid Deprecation warning. It could be removed in the future+ [exe, "--outputformat", "json", "-j"],+ cwd=tmpdirname,+ stdout=PIPE,+ stderr=PIPE,+ )+ stdout, stderr = p.communicate(input=None, timeout=60)+ if p.returncode == 0 or p.returncode == 13:+ content = stdout.decode(sys.stdin.encoding)+ else:+ self.error("Unable to read Retire.js content.")+ self.debug(f"Error running Retire.js: {stderr} - {stdout}")+ shutil.rmtree(tmpdirname)+ return+ except TimeoutExpired:+ if p:+ p.kill()+ stdout, stderr = p.communicate()+ self.debug("Timed out waiting for Retire.js to finish")+ shutil.rmtree(tmpdirname)+ return++ try:+ data = json.loads(content)+ for item in data.get("data", []):+ for result in item["results"]:+ for vuln in result["vulnerabilities"]:+ if "CVE" not in vuln["identifiers"]:+ text = f"{vuln['identifiers']['summary']}\n"+ text += f"Severity: {vuln['severity']}\n"+ text += f"Info: <SFURL>{vuln['info'][0]}</SFURL>"+ evt = SpiderFootEvent(+ "VULNERABILITY_GENERAL", text, self.__name__, event+ )+ self.notifyListeners(evt)+ else:+ for cve in vuln["identifiers"]["CVE"]:+ etype, cvetext = self.sf.cveInfo(cve)+ evt = SpiderFootEvent(+ etype, cvetext, self.__name__, event+ )+ self.notifyListeners(evt)+ shutil.rmtree(tmpdirname)+ except BaseException as e:+ self.error(f"Couldn't parse the JSON output of Retire.js: {e}")+ shutil.rmtree(tmpdirname)+ return++# End of sfp_tool_retirejs classdiff --git a/test/unit/modules/test_sfp_tool_retirejs.py b/test/unit/modules/test_sfp_tool_retirejs.pynew file mode 100644index 00000000..044dba5f--- /dev/null+++ b/test/unit/modules/test_sfp_tool_retirejs.py@@ -0,0 +1,60 @@+# test_sfp_tool_retirejs.py+import pytest+import unittest++from modules.sfp_tool_retirejs import sfp_tool_retirejs+from sflib import SpiderFoot+from spiderfoot import SpiderFootEvent, SpiderFootTarget+++class TestModuleRetirejs(unittest.TestCase):+ """+ Test modules.sfp_tool_retirejs+ """++ def test_opts(self):+ module = sfp_tool_retirejs()+ 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_tool_retirejs()+ module.setup(sf, dict())++ def test_watchedEvents_should_return_list(self):+ module = sfp_tool_retirejs()+ self.assertIsInstance(module.watchedEvents(), list)++ def test_producedEvents_should_return_list(self):+ module = sfp_tool_retirejs()+ self.assertIsInstance(module.producedEvents(), list)++ def test_handleEvent_no_tool_path_configured_should_set_errorState(self):+ """+ Test handleEvent(self, event)+ """+ sf = SpiderFoot(self.default_options)++ module = sfp_tool_retirejs()+ 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)+ self.assertTrue(module.errorState)