From c20546dcaad177659bbbc5f0602772bca9204791 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Mon, 26 Dec 2011 12:24:39 +0000 Subject: [PATCH] minor refactoring --- lib/core/common.py | 14 +++++--------- lib/core/enums.py | 5 +++++ lib/core/option.py | 7 ++++--- lib/core/settings.py | 7 ++++--- lib/request/connect.py | 9 +++++---- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index 60d8eeda3..426bf0b06 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -1804,19 +1804,12 @@ def stdev(values): key = (values[0], values[-1], len(values)) - retVal = None - if key in kb.cache.stdev: retVal = kb.cache.stdev[key] else: - summa = 0.0 avg = average(values) - - for value in values: - value = value or 0 - summa += pow(value - avg, 2) - - retVal = sqrt(summa/(len(values) - 1)) + _ = reduce(lambda x, y: x + pow((y or 0) - avg, 2), values, 0.0) + retVal = sqrt(_/(len(values) - 1)) kb.cache.stdev[key] = retVal return retVal @@ -2241,6 +2234,9 @@ def logHTTPTraffic(requestLogMsg, responseLogMsg): Logs HTTP traffic to the output file """ + if not conf.trafficFile: + return + kb.locks.logLock.acquire() dataToTrafficFile("%s%s" % (requestLogMsg, os.linesep)) diff --git a/lib/core/enums.py b/lib/core/enums.py index 9b22b48e5..b97e1af4d 100644 --- a/lib/core/enums.py +++ b/lib/core/enums.py @@ -36,6 +36,11 @@ class DBMS: SYBASE = "Sybase" DB2 = "IBM DB2" +class CUSTOM_LOGGING: + PAYLOAD = 9 + TRAFFIC_OUT = 8 + TRAFFIC_IN = 7 + class OS: LINUX = "Linux" WINDOWS = "Windows" diff --git a/lib/core/option.py b/lib/core/option.py index a2a072276..653768d83 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -60,6 +60,7 @@ from lib.core.data import queries from lib.core.datatype import AttribDict from lib.core.datatype import InjectionDict from lib.core.defaults import defaults +from lib.core.enums import CUSTOM_LOGGING from lib.core.enums import DBMS from lib.core.enums import HTTPHEADER from lib.core.enums import HTTPMETHOD @@ -1648,11 +1649,11 @@ def __setVerbosity(): elif conf.verbose == 2: logger.setLevel(logging.DEBUG) elif conf.verbose == 3: - logger.setLevel(9) + logger.setLevel(CUSTOM_LOGGING.PAYLOAD) elif conf.verbose == 4: - logger.setLevel(8) + logger.setLevel(CUSTOM_LOGGING.TRAFFIC_OUT) elif conf.verbose >= 5: - logger.setLevel(7) + logger.setLevel(CUSTOM_LOGGING.TRAFFIC_IN) def __mergeOptions(inputOptions, overrideOptions): """ diff --git a/lib/core/settings.py b/lib/core/settings.py index 9d93e5f31..8e8133a4e 100644 --- a/lib/core/settings.py +++ b/lib/core/settings.py @@ -12,6 +12,7 @@ import os import subprocess import sys +from lib.core.enums import CUSTOM_LOGGING from lib.core.enums import DBMS from lib.core.enums import PLACE from lib.core.revision import getRevisionNumber @@ -33,9 +34,9 @@ LOWER_RATIO_BOUND = 0.02 UPPER_RATIO_BOUND = 0.98 # sqlmap logger -logging.addLevelName(9, "PAYLOAD") -logging.addLevelName(8, "TRAFFIC OUT") -logging.addLevelName(7, "TRAFFIC IN") +logging.addLevelName(CUSTOM_LOGGING.PAYLOAD, "PAYLOAD") +logging.addLevelName(CUSTOM_LOGGING.TRAFFIC_OUT, "TRAFFIC OUT") +logging.addLevelName(CUSTOM_LOGGING.TRAFFIC_IN, "TRAFFIC IN") LOGGER = logging.getLogger("sqlmapLog") LOGGER_HANDLER = logging.StreamHandler(sys.stdout) diff --git a/lib/request/connect.py b/lib/request/connect.py index 5c4bf3508..9cb0f1abf 100644 --- a/lib/request/connect.py +++ b/lib/request/connect.py @@ -44,6 +44,7 @@ from lib.core.convert import urlencode from lib.core.data import conf from lib.core.data import kb from lib.core.data import logger +from lib.core.enums import CUSTOM_LOGGING from lib.core.enums import HTTPHEADER from lib.core.enums import HTTPMETHOD from lib.core.enums import NULLCONNECTION @@ -300,7 +301,7 @@ class Connect: threadData.lastRequestMsg = requestMsg - logger.log(8, requestMsg) + logger.log(CUSTOM_LOGGING.TRAFFIC_OUT, requestMsg) conn = urllib2.urlopen(req) @@ -426,7 +427,7 @@ class Connect: elif conf.verbose > 5: responseMsg += "%s\n\n%s\n" % (logHeaders, page) - logger.log(7, responseMsg) + logger.log(CUSTOM_LOGGING.TRAFFIC_IN, responseMsg) if e.code == httplib.UNAUTHORIZED: errMsg = "not authorized, try to provide right HTTP " @@ -516,7 +517,7 @@ class Connect: elif conf.verbose > 5: responseMsg += "%s\n\n%s\n" % (logHeaders, page) - logger.log(7, responseMsg) + logger.log(CUSTOM_LOGGING.TRAFFIC_IN, responseMsg) return page, responseHeaders, code @@ -556,7 +557,7 @@ class Connect: value = agent.replacePayload(value, payload) - logger.log(9, payload) + logger.log(CUSTOM_LOGGING.PAYLOAD, payload) if place == PLACE.COOKIE and conf.cookieUrlencode: value = agent.removePayloadDelimiters(value)