SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.
Python attacksurfacecticybersecurityfootprintinghackinginformation-gatheringinformation-securityinfosecintelligence-gatheringosintosint-frameworkosint-reconnaissanceosint-toolpentestingpythonreconsecurity-toolsthreat-intelligencethreatintel
sf: Add logging
Brendan Coles9dac99fdcf07
diff --git a/sf.py b/sf.pyindex f62f456a..fbd455a3 100755--- a/sf.py+++ b/sf.py@@ -18,6 +18,7 @@ if sys.version_info < (3, 6):sys.exit(-1)import argparse+import loggingimport multiprocessing as mpimport osimport os.path@@ -25,6 +26,7 @@ import randomimport signalimport timefrom copy import deepcopy+from logging import handlersimport cherrypyfrom cherrypy.lib import auth_digest@@ -34,6 +36,24 @@ from sflib import SpiderFootfrom sfscan import SpiderFootScannerfrom sfwebui import SpiderFootWebUi+log = logging.getLogger()+log.setLevel(logging.DEBUG)+log_format = logging.Formatter("%(asctime)s [%(levelname)s] %(message)s")++console_handler = logging.StreamHandler(sys.stderr)+console_handler.setFormatter(log_format)+log.addHandler(console_handler)++debug_handler = handlers.RotatingFileHandler("spiderfoot.debug.log", maxBytes=(1048576*5), backupCount=7)+debug_handler.setLevel(logging.DEBUG)+debug_handler.setFormatter(log_format)+log.addHandler(debug_handler)++error_handler = handlers.RotatingFileHandler("spiderfoot.error.log", maxBytes=(1048576*5), backupCount=7)+error_handler.setLevel(logging.WARN)+error_handler.setFormatter(log_format)+log.addHandler(error_handler)+# 'Global' configuration options# These can be overriden on a per-module basis, and some will# be overridden from saved configuration settings stored in the DB.@@ -130,10 +150,13 @@ if __name__ == '__main__':if args.debug:sfConfig['_debug'] = True+ log.setLevel(logging.DEBUG)else:+ log.setLevel(logging.INFO)sfConfig['_debug'] = Falseif args.q or args.o == "json":+ log.setLevel(logging.NOTSET)sfConfig['__logging'] = Falseif args.l:diff --git a/sflib.py b/sflib.pyindex 44762124..df696426 100644--- a/sflib.py+++ b/sflib.py@@ -15,6 +15,7 @@ import htmlimport inspectimport ioimport json+import loggingimport osimport randomimport re@@ -61,6 +62,7 @@ class SpiderFoot:_scanId = None_socksProxy = Noneopts = dict()+ log = logging.getLogger(__name__)def __init__(self, options):"""Initialize SpiderFoot object.@@ -443,12 +445,15 @@ class SpiderFoot:print(f"[Error] {message}")else:self._dblog("ERROR", message)+if self.opts.get('__logstdout'):print(f"[Error] {message}")+if exception:+ self.log.exception(message)raise BaseException(f"Internal Error Encountered: {message}")-- return None+ else:+ self.log.error(message)def fatal(self, error):"""Print an error message and stacktrace then exit.@@ -464,7 +469,11 @@ class SpiderFoot:print('[Fatal] %s' % error)else:self._dblog("FATAL", error)+print(str(inspect.stack()))++ self.log.critical(error)+sys.exit(-1)def status(self, message):@@ -487,6 +496,8 @@ class SpiderFoot:if self.opts.get('__logstdout'):print("[*] %s" % message)+ self.log.info(message)+def info(self, message):"""Log and print an info message.@@ -522,7 +533,8 @@ class SpiderFoot:self._dblog("INFO", message, modName)if self.opts.get('__logstdout'):print("[*] %s" % message)- return++ self.log.info(message)def debug(self, message):"""Log and print a debug message.@@ -560,7 +572,8 @@ class SpiderFoot:self._dblog("DEBUG", message, modName)if self.opts.get('__logstdout'):print("[d:%s] %s" % (modName, message))- return++ self.log.debug(f"{modName} : {message}")@staticmethoddef myPath():diff --git a/spiderfoot/plugin.py b/spiderfoot/plugin.pyindex 7a4ae9c9..88eb74aa 100644--- a/spiderfoot/plugin.py+++ b/spiderfoot/plugin.py@@ -1,3 +1,4 @@+import loggingimport osimport sysimport time@@ -23,6 +24,8 @@ class SpiderFootPlugin():socksProxy (str): SOCKS proxy"""+ log = logging.getLogger(__name__)+# Will be set to True by the controller if the user aborts scanning_stopScanning = False# Modules that will be notified when this module produces events@@ -226,12 +229,7 @@ class SpiderFootPlugin():try:listener.handleEvent(sfEvent)except BaseException as e:- from sflib import SpiderFoot-- with open(os.path.join(SpiderFoot.dataPath(), "sferror.log"), "a") as f:- f.write(f"[{time.ctime()}]: Module ({listener.__module__}) encountered an error: {e}\n")- exc_type, exc_value, exc_traceback = sys.exc_info()- f.write(repr(traceback.format_exception(exc_type, exc_value, exc_traceback)))+ self.log.exception(f"Module ({listener.__module__}) encountered an error: {e}")def checkForStop(self):"""For modules to use to check for when they should give back control.