From 385e208f38858f79134e326d8c3aaca691aaa0af Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Tue, 21 Dec 2010 14:21:24 +0000 Subject: [PATCH] code refactoring regarding standard output suppression and some threading issues --- lib/core/common.py | 4 +++- lib/core/option.py | 3 ++- lib/request/connect.py | 2 +- lib/request/inject.py | 5 ++--- lib/techniques/blind/inference.py | 8 ++++---- lib/techniques/brute/use.py | 12 +++++++++++- lib/techniques/error/use.py | 6 ++---- plugins/generic/enumeration.py | 5 ++--- 8 files changed, 27 insertions(+), 18 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index 0b553b56e..f132a1ef9 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -427,7 +427,9 @@ def filePathToString(filePath): return strRepl def dataToStdout(data, forceOutput=False): - if forceOutput or conf.verbose > 0: + if (forceOutput or conf.verbose > 0)\ + and not ('threadException' in kb and kb.threadException)\ + and not ('disableStdOut' in kb and kb.disableStdOut): try: sys.stdout.write(data) sys.stdout.flush() diff --git a/lib/core/option.py b/lib/core/option.py index 5e5a85527..7ee26215b 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -1119,7 +1119,6 @@ def __setConfAttributes(): conf.sessionFP = None conf.start = True conf.tests = [] - conf.threadException = False conf.trafficFP = None conf.wFileType = None @@ -1144,6 +1143,7 @@ def __setKnowledgeBaseAttributes(flushAll=True): kb.commonOutputs = None kb.data = advancedDict() + kb.disableStdOut = False # Old style injection flag kb.unionTest = None @@ -1196,6 +1196,7 @@ def __setKnowledgeBaseAttributes(flushAll=True): kb.testMode = False kb.testQueryCount = 0 kb.threadContinue = True + kb.threadException = False kb.threadData = {} kb.unionComment = "" kb.unionCount = None diff --git a/lib/request/connect.py b/lib/request/connect.py index 6c3206d5f..90245d92f 100644 --- a/lib/request/connect.py +++ b/lib/request/connect.py @@ -288,7 +288,7 @@ class Connect: if silent or (ignoreTimeout and "timeout" in tbMsg): return None, None - elif kb.retriesCount < conf.retries and not conf.threadException and not conf.scriptKiddie: + elif kb.retriesCount < conf.retries and not kb.threadException and not conf.scriptKiddie: kb.retriesCount += 1 warnMsg += ", sqlmap is going to retry the request" diff --git a/lib/request/inject.py b/lib/request/inject.py index b9488b3e4..630d28839 100644 --- a/lib/request/inject.py +++ b/lib/request/inject.py @@ -390,8 +390,7 @@ def getValue(expression, blind=True, inband=True, error=True, time=True, fromUse """ if suppressOutput: - pushValue(conf.verbose) - conf.verbose = 0 + kb.disableStdOut = True try: if conf.direct: @@ -467,7 +466,7 @@ def getValue(expression, blind=True, inband=True, error=True, time=True, fromUse finally: if suppressOutput: - conf.verbose = popValue() + kb.disableStdOut = False if value and expected == EXPECTED.BOOL: if isinstance(value, basestring): diff --git a/lib/techniques/blind/inference.py b/lib/techniques/blind/inference.py index 0a2a5ecc8..7362d7815 100644 --- a/lib/techniques/blind/inference.py +++ b/lib/techniques/blind/inference.py @@ -341,11 +341,11 @@ def bisection(payload, expression, length=None, charsetType=None, firstChar=None except (sqlmapConnectionException, sqlmapValueException), errMsg: print - conf.threadException = True + kb.threadException = True logger.error("thread %d: %s" % (numThread + 1, errMsg)) except KeyboardInterrupt: - conf.threadException = True + kb.threadException = True print logger.debug("waiting for threads to finish") @@ -359,7 +359,7 @@ def bisection(payload, expression, length=None, charsetType=None, firstChar=None except: print - conf.threadException = True + kb.threadException = True errMsg = unhandledException() logger.error("thread %d: %s" % (numThread + 1, errMsg)) traceback.print_exc() @@ -505,7 +505,7 @@ def bisection(payload, expression, length=None, charsetType=None, firstChar=None if not partialValue: dataToSessionFile("]\n") - if conf.threadException: + if kb.threadException: raise sqlmapThreadException, "something unexpected happened inside the threads" return queriesCount[0], finalValue diff --git a/lib/techniques/brute/use.py b/lib/techniques/brute/use.py index dfdfbbe12..2bf746557 100644 --- a/lib/techniques/brute/use.py +++ b/lib/techniques/brute/use.py @@ -80,7 +80,17 @@ def tableExists(tableFile): thread.join(5) except KeyboardInterrupt: kb.threadContinue = False - raise + kb.threadException = True + + print + logger.debug("waiting for threads to finish") + + try: + while (threading.activeCount() > 1): + pass + + except KeyboardInterrupt: + raise sqlmapThreadException, "user aborted" finally: kb.locks.seqLock = None diff --git a/lib/techniques/error/use.py b/lib/techniques/error/use.py index 22406aa4d..1ccfc1253 100644 --- a/lib/techniques/error/use.py +++ b/lib/techniques/error/use.py @@ -11,6 +11,7 @@ import re import time from lib.core.agent import agent +from lib.core.common import dataToStdout from lib.core.common import extractRegexResult from lib.core.common import getUnicode from lib.core.common import initTechnique @@ -60,9 +61,6 @@ def errorUse(expression): if output: output = output.replace(kb.misc.space, " ") - - if conf.verbose > 0: - infoMsg = "retrieved: %s" % replaceNewlineTabs(output, stdout=True) - logger.info(infoMsg) + dataToStdout("\r[%s] [INFO] retrieved: %s" % (time.strftime("%X"), replaceNewlineTabs(output, stdout=True))) return output diff --git a/plugins/generic/enumeration.py b/plugins/generic/enumeration.py index 7a71a2215..6d8f1613f 100644 --- a/plugins/generic/enumeration.py +++ b/plugins/generic/enumeration.py @@ -299,8 +299,7 @@ class Enumeration: break else: query = rootQuery.blind.query % user - pushValue(conf.verbose) - conf.verbose = 0 + kb.disableStdOut = True elif kb.dbms == DBMS.MSSQL: if kb.dbmsVersion[0] in ( "2005", "2008" ): query = rootQuery.blind.query2 % (user, index, user) @@ -310,7 +309,7 @@ class Enumeration: query = rootQuery.blind.query % (user, index) password = inject.getValue(query, inband=False) if kb.dbms == DBMS.SYBASE: - conf.verbose = popValue() + kb.disableStdOut = False password = "0x%s" % strToHex(password) infoMsg = "retrieved: %s" % password logger.info(infoMsg)