SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sfp_punkspider: Set errorState upon error response (#1707)
bcolesb32d2f75a65e
diff --git a/modules/sfp_punkspider.py b/modules/sfp_punkspider.pyindex 8976866d..b578c5fd 100644--- a/modules/sfp_punkspider.py+++ b/modules/sfp_punkspider.py@@ -44,6 +44,7 @@ class sfp_punkspider(SpiderFootPlugin):# or you run the risk of data persisting between scan runs.results = None+ errorState = Falsedef setup(self, sfc, userOpts=dict()):self.sf = sfc@@ -63,29 +64,48 @@ class sfp_punkspider(SpiderFootPlugin):def producedEvents(self):return ["VULNERABILITY_GENERAL"]- def query(self, qry):- qryhash = hashlib.md5(qry.encode('utf-8', errors='replace').lower()).hexdigest() # noqa: DUO130- url = f"https://api.punkspider.org/api/partial-hash/{qryhash}"-+ def query(self, domain: str):+ domain_hash = hashlib.md5(domain.encode('utf-8', errors='replace').lower()).hexdigest() # noqa: DUO130+ url = f"https://api.punkspider.org/api/partial-hash/{domain_hash}"res = self.sf.fetchUrl(url, timeout=30, useragent=self.opts['_useragent'])++ return self.parseApiResponse(res)++ def parseApiResponse(self, res: dict):+ if not res:+ self.error("No response from PunkSpider.")+ return None++ if res['code'] == '404':+ self.debug("No results from PunkSpider.")+ return None++ if res['code'] == '500' or res['code'] == '502' or res['code'] == '503':+ self.error("PunkSpider service is unavailable.")+ self.errorState = True+ return None++ # Catch all non-200 status codes, and presume something went wrong+ if res['code'] != '200':+ self.error(f"Unexpected reply from PunkSpider: {res['code']}")+ self.errorState = True+ return None+if res['content'] is None:- self.debug("No content returned from PunkSpider")return Nonetry:return json.loads(res['content'])except Exception as e:- self.error(f"Error processing response from PunkSpider: {e}")+ self.debug(f"Error processing JSON response from PunkSpider: {e}")return None- # Handle events sent to this moduledef handleEvent(self, event):eventName = event.eventType- srcModuleName = event.moduleeventData = event.data- self.debug(f"Received event, {eventName}, from {srcModuleName}")+ self.debug(f"Received event, {eventName}, from {event.module}")if eventData in self.results:self.debug(f"Skipping {eventData}, already checked.")diff --git a/test/unit/modules/test_sfp_punkspider.py b/test/unit/modules/test_sfp_punkspider.pyindex ad36a5ce..da809e52 100644--- a/test/unit/modules/test_sfp_punkspider.py+++ b/test/unit/modules/test_sfp_punkspider.py@@ -24,3 +24,27 @@ class TestModulePunkspider(unittest.TestCase):def test_producedEvents_should_return_list(self):module = sfp_punkspider()self.assertIsInstance(module.producedEvents(), list)++ def test_parseApiResponse_nonfatal_http_response_code_should_not_set_errorState(self):+ sf = SpiderFoot(self.default_options)++ http_codes = ["200", "404"]+ for code in http_codes:+ with self.subTest(code=code):+ module = sfp_punkspider()+ module.setup(sf, dict())+ result = module.parseApiResponse({"code": code, "content": None})+ self.assertIsNone(result)+ self.assertFalse(module.errorState)++ def test_parseApiResponse_fatal_http_response_error_code_should_set_errorState(self):+ sf = SpiderFoot(self.default_options)++ http_codes = ["401", "402", "403", "429", "500", "502", "503"]+ for code in http_codes:+ with self.subTest(code=code):+ module = sfp_punkspider()+ module.setup(sf, dict())+ result = module.parseApiResponse({"code": code, "content": None})+ self.assertIsNone(result)+ self.assertTrue(module.errorState)