Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

Logging: Use exc_info=True rather than printing traceback.format_exc (#1696)

bcoles983c836af81d
diff --git a/sflib.py b/sflib.py
index b13e25ac..0f077c30 100644
--- a/sflib.py
+++ b/sflib.py
@@ -22,7 +22,6 @@ import socket
import ssl
import sys
import time
-import traceback
import urllib.error
import urllib.parse
import urllib.request
@@ -253,7 +252,7 @@ class SpiderFoot:
return hashlib.sha256(s.encode('raw_unicode_escape')).hexdigest()
def cachePut(self, label: str, data: str) -> None:
- """Store data to the cache
+ """Store data to the cache.
Args:
label (str): Name of the cached data to be used when retrieving the cached data.
@@ -275,7 +274,7 @@ class SpiderFoot:
fp.write(data)
def cacheGet(self, label: str, timeoutHrs: int) -> str:
- """Retreive data from the cache
+ """Retreive data from the cache.
Args:
label (str): Name of the cached data to retrieve
@@ -285,7 +284,7 @@ class SpiderFoot:
Returns:
str: cached data
"""
- if label is None:
+ if not label:
return None
pathLabel = hashlib.sha224(label.encode('utf-8')).hexdigest()
@@ -1320,11 +1319,9 @@ class SpiderFoot:
)
except Exception as e:
if noLog:
- self.debug(f"Unexpected exception ({e}) occurred fetching (HEAD only) URL: {url}")
- self.debug(traceback.format_exc())
+ self.debug(f"Unexpected exception ({e}) occurred fetching (HEAD only) URL: {url}", exc_info=True)
else:
- self.error(f"Unexpected exception ({e}) occurred fetching (HEAD only) URL: {url}")
- self.error(traceback.format_exc())
+ self.error(f"Unexpected exception ({e}) occurred fetching (HEAD only) URL: {url}", exc_info=True)
return result
@@ -1366,11 +1363,9 @@ class SpiderFoot:
except Exception as e:
if noLog:
- self.debug(f"Unexpected exception ({e}) occurred fetching (HEAD only) URL: {result['realurl']}")
- self.debug(traceback.format_exc())
+ self.debug(f"Unexpected exception ({e}) occurred fetching (HEAD only) URL: {result['realurl']}", exc_info=True)
else:
- self.error(f"Unexpected exception ({e}) occurred fetching (HEAD only) URL: {result['realurl']}")
- self.error(traceback.format_exc())
+ self.error(f"Unexpected exception ({e}) occurred fetching (HEAD only) URL: {result['realurl']}", exc_info=True)
return result
@@ -1409,11 +1404,9 @@ class SpiderFoot:
return result
except Exception as e:
if noLog:
- self.debug(f"Unexpected exception ({e}) occurred fetching URL: {url}")
- self.debug(traceback.format_exc())
+ self.debug(f"Unexpected exception ({e}) occurred fetching URL: {url}", exc_info=True)
else:
- self.error(f"Unexpected exception ({e}) occurred fetching URL: {url}")
- self.error(traceback.format_exc())
+ self.error(f"Unexpected exception ({e}) occurred fetching URL: {url}", exc_info=True)
return result
@@ -1467,9 +1460,7 @@ class SpiderFoot:
result["content"] = res.content
except Exception as e:
- self.error(f"Unexpected exception ({e}) occurred parsing response for URL: {url}")
- self.error(traceback.format_exc())
-
+ self.error(f"Unexpected exception ({e}) occurred parsing response for URL: {url}", exc_info=True)
result['content'] = None
result['status'] = str(e)
diff --git a/sfscan.py b/sfscan.py
index d21ebb74..702ea579 100644
--- a/sfscan.py
+++ b/sfscan.py
@@ -10,10 +10,8 @@
# License: MIT
# -----------------------------------------------------------------
import socket
-import sys
import time
import queue
-import traceback
from time import sleep
from copy import deepcopy
from contextlib import suppress
@@ -296,7 +294,7 @@ class SpiderFootScanner():
mod = getattr(module, modName)()
mod.__name__ = modName
except Exception:
- self.__sf.error(f"Module {modName} initialization failed: {traceback.format_exc()}")
+ self.__sf.error(f"Module {modName} initialization failed", exc_info=True)
continue
# Set up the module options, scan ID, database handle and listeners
@@ -313,7 +311,7 @@ class SpiderFootScanner():
mod.setDbh(self.__dbh)
mod.setup(self.__sf, self.__modconfig[modName])
except Exception:
- self.__sf.error(f"Module {modName} initialization failed: {traceback.format_exc()}")
+ self.__sf.error(f"Module {modName} initialization failed", exc_info=True)
mod.errorState = True
continue
@@ -415,11 +413,9 @@ class SpiderFootScanner():
self.__setStatus("ABORTED", None, time.time() * 1000)
except BaseException as e:
- exc_type, exc_value, exc_traceback = sys.exc_info()
self.__sf.error(
- f"Unhandled exception ({e.__class__.__name__}) encountered during scan."
- + "Please report this as a bug: " +
- + repr(traceback.format_exception(exc_type, exc_value, exc_traceback))
+ f"Unhandled exception ({e.__class__.__name__}) encountered during scan. Please report this as a bug",
+ exc_info=True
)
self.__sf.status(f"Scan [{self.__scanId}] failed: {e}")
self.__setStatus("ERROR-FAILED", None, time.time() * 1000)
@@ -434,7 +430,7 @@ class SpiderFootScanner():
def runCorrelations(self) -> None:
"""Run correlation rules."""
- self.__sf.status(f"Running {len(self.__config['__correlationrules__'])} correlation rules.")
+ self.__sf.status(f"Running {len(self.__config['__correlationrules__'])} correlation rules on scan {self.__scanId}.")
ruleset = dict()
for rule in self.__config['__correlationrules__']:
ruleset[rule['id']] = rule['rawYaml']
@@ -448,13 +444,12 @@ class SpiderFootScanner():
TypeError: queue tried to process a malformed event
AssertionError: scan halted for some reason
"""
+ if not self.eventQueue:
+ return
counter = 0
try:
- if not self.eventQueue:
- return
-
# start one thread for each module
for mod in self.__moduleInstances.values():
mod.start()