From 794700eb37212ba82c9c704d98d9cfcd518045f1 Mon Sep 17 00:00:00 2001 From: Bernardo Damele Date: Wed, 9 Jan 2013 22:08:50 +0000 Subject: [PATCH] preparing to handle logging calls by a separate file descriptor when sqlmap is executed by the REST API - issue #297 --- lib/core/convert.py | 4 ++++ lib/core/option.py | 30 ++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/core/convert.py b/lib/core/convert.py index ae1b1f201..ceae15931 100644 --- a/lib/core/convert.py +++ b/lib/core/convert.py @@ -11,6 +11,7 @@ except: import md5 import sha +import json import pickle import sys import struct @@ -126,3 +127,6 @@ def stdoutencode(data): retVal = data.encode(UNICODE_ENCODING) return retVal + +def jsonize(data): + return json.dumps(data, sort_keys=False, indent=4) diff --git a/lib/core/option.py b/lib/core/option.py index 77347d90b..e1c9f3b9b 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -52,7 +52,9 @@ from lib.core.common import singleTimeWarnMessage from lib.core.common import UnicodeRawConfigParser from lib.core.common import urldecode from lib.core.common import urlencode +from lib.core.convert import base64pickle from lib.core.convert import base64unpickle +from lib.core.convert import jsonize from lib.core.data import conf from lib.core.data import kb from lib.core.data import logger @@ -1804,6 +1806,31 @@ def _mergeOptions(inputOptions, overrideOptions): if hasattr(conf, key) and conf[key] is None: conf[key] = value +# Logger recorder object, which keeps the log structure +class LogRecorder(logging.StreamHandler): + """ + Logging handler class which only records CUSTOM_LOGGING.PAYLOAD entries + to a global list. + """ + loghist = [] + + def emit(self, record): + """ + Simply record the emitted events. + """ + self.loghist.append({'levelname': record.levelname, + 'text': record.message % record.args if record.args else record.message, + 'id': len(self.loghist)}) + + if conf.fdLog: + os.write(conf.fdLog, base64pickle(self.loghist)) + +def _setRestAPILog(): + if hasattr(conf, "fdLog") and conf.fdLog: + #logger.removeHandler(LOGGER_HANDLER) + LOGGER_RECORDER = LogRecorder() + logger.addHandler(LOGGER_RECORDER) + def _setTrafficOutputFP(): if conf.trafficFile: infoMsg = "setting file for logging HTTP traffic" @@ -2069,14 +2096,13 @@ def init(inputOptions=AttribDict(), overrideOptions=False): if not inputOptions.disableColoring: coloramainit() - elif hasattr(LOGGER_HANDLER, "disable_coloring"): - LOGGER_HANDLER.disable_coloring = True _setConfAttributes() _setKnowledgeBaseAttributes() _mergeOptions(inputOptions, overrideOptions) _useWizardInterface() setVerbosity() + _setRestAPILog() _saveCmdline() _setRequestFromFile() _cleanupOptions()