From 1d6d2c1892f91a450b97c41c46c416bc397e0ab1 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Wed, 29 Sep 2021 16:13:13 +0200 Subject: [PATCH] Some refactoring (isMultiThreadMode fuzz) --- lib/core/common.py | 21 +----------------- lib/core/dump.py | 3 +-- lib/core/option.py | 1 + lib/core/settings.py | 2 +- lib/core/threads.py | 50 +++++++++++++++++++++++------------------- lib/request/connect.py | 3 +-- 6 files changed, 32 insertions(+), 48 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index 82de0f3f6..7de61f0ff 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -1016,7 +1016,7 @@ def dataToStdout(data, forceOutput=False, bold=False, contentType=None, status=C if not kb.get("threadException"): if forceOutput or not (getCurrentThreadData().disableStdOut or kb.get("wizardMode")): - multiThreadMode = isMultiThreadMode() + multiThreadMode = kb.get("multiThreadMode") if multiThreadMode: logging._acquireLock() @@ -2266,25 +2266,6 @@ def isHexEncodedString(subject): return re.match(r"\A[0-9a-fA-Fx]+\Z", subject) is not None -def isMultiThreadMode(): - """ - Checks if running in multi-thread(ing) mode - - >>> import time - >>> threading.activeCount() - 1 - >>> isMultiThreadMode() - False - >>> _ = lambda: time.sleep(0.1) - >>> thread = threading.Thread(target=_) - >>> thread.daemon = True - >>> thread.start() - >>> isMultiThreadMode() - True - """ - - return threading.activeCount() > 1 - @cachedmethod def getConsoleWidth(default=80): """ diff --git a/lib/core/dump.py b/lib/core/dump.py index 6e02df5b8..d20584450 100644 --- a/lib/core/dump.py +++ b/lib/core/dump.py @@ -19,7 +19,6 @@ from lib.core.common import dataToStdout from lib.core.common import filterNone from lib.core.common import getSafeExString from lib.core.common import isListLike -from lib.core.common import isMultiThreadMode from lib.core.common import isNoneValue from lib.core.common import normalizeUnicode from lib.core.common import openFile @@ -80,7 +79,7 @@ class Dump(object): elif console: dataToStdout(text) - multiThreadMode = isMultiThreadMode() + multiThreadMode = kb.multiThreadMode if multiThreadMode: self._lock.acquire() diff --git a/lib/core/option.py b/lib/core/option.py index 3b6a1ceab..e23f51347 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -2090,6 +2090,7 @@ def _setKnowledgeBaseAttributes(flushAll=True): kb.matchRatio = None kb.maxConnectionsFlag = False kb.mergeCookies = None + kb.multiThreadMode = False kb.multipleCtrlC = False kb.negativeLogic = False kb.nchar = True diff --git a/lib/core/settings.py b/lib/core/settings.py index 9f113c5c9..eae0324c5 100644 --- a/lib/core/settings.py +++ b/lib/core/settings.py @@ -20,7 +20,7 @@ from thirdparty import six from thirdparty.six import unichr as _unichr # sqlmap version (...) -VERSION = "1.5.9.11" +VERSION = "1.5.9.12" TYPE = "dev" if VERSION.count('.') > 2 and VERSION.split('.')[-1] != '0' else "stable" TYPE_COLORS = {"dev": 33, "stable": 90, "pip": 34} VERSION_STRING = "sqlmap/%s#%s" % ('.'.join(VERSION.split('.')[:-1]) if VERSION.count('.') > 2 and VERSION.split('.')[-1] == '0' else VERSION, TYPE) diff --git a/lib/core/threads.py b/lib/core/threads.py index e8e4f5cdc..be1f6d622 100644 --- a/lib/core/threads.py +++ b/lib/core/threads.py @@ -123,31 +123,32 @@ def runThreads(numThreads, threadFunction, cleanupFunction=None, forwardExceptio kb.threadContinue = True kb.threadException = False kb.technique = ThreadData.technique - - if threadChoice and conf.threads == numThreads == 1 and not (kb.injection.data and not any(_ not in (PAYLOAD.TECHNIQUE.TIME, PAYLOAD.TECHNIQUE.STACKED) for _ in kb.injection.data)): - while True: - message = "please enter number of threads? [Enter for %d (current)] " % numThreads - choice = readInput(message, default=str(numThreads)) - if choice: - skipThreadCheck = False - - if choice.endswith('!'): - choice = choice[:-1] - skipThreadCheck = True - - if isDigit(choice): - if int(choice) > MAX_NUMBER_OF_THREADS and not skipThreadCheck: - errMsg = "maximum number of used threads is %d avoiding potential connection issues" % MAX_NUMBER_OF_THREADS - logger.critical(errMsg) - else: - conf.threads = numThreads = int(choice) - break - - if numThreads == 1: - warnMsg = "running in a single-thread mode. This could take a while" - logger.warn(warnMsg) + kb.multiThreadMode = False try: + if threadChoice and conf.threads == numThreads == 1 and not (kb.injection.data and not any(_ not in (PAYLOAD.TECHNIQUE.TIME, PAYLOAD.TECHNIQUE.STACKED) for _ in kb.injection.data)): + while True: + message = "please enter number of threads? [Enter for %d (current)] " % numThreads + choice = readInput(message, default=str(numThreads)) + if choice: + skipThreadCheck = False + + if choice.endswith('!'): + choice = choice[:-1] + skipThreadCheck = True + + if isDigit(choice): + if int(choice) > MAX_NUMBER_OF_THREADS and not skipThreadCheck: + errMsg = "maximum number of used threads is %d avoiding potential connection issues" % MAX_NUMBER_OF_THREADS + logger.critical(errMsg) + else: + conf.threads = numThreads = int(choice) + break + + if numThreads == 1: + warnMsg = "running in a single-thread mode. This could take a while" + logger.warn(warnMsg) + if numThreads > 1: if startThreadMsg: infoMsg = "starting %d threads" % numThreads @@ -156,6 +157,8 @@ def runThreads(numThreads, threadFunction, cleanupFunction=None, forwardExceptio threadFunction() return + kb.multiThreadMode = True + # Start the threads for numThread in xrange(numThreads): thread = threading.Thread(target=exceptionHandledFunction, name=str(numThread), args=[threadFunction]) @@ -225,6 +228,7 @@ def runThreads(numThreads, threadFunction, cleanupFunction=None, forwardExceptio traceback.print_exc() finally: + kb.multiThreadMode = False kb.threadContinue = True kb.threadException = False kb.technique = None diff --git a/lib/request/connect.py b/lib/request/connect.py index 317990af4..f88be5783 100644 --- a/lib/request/connect.py +++ b/lib/request/connect.py @@ -43,7 +43,6 @@ from lib.core.common import getHeader from lib.core.common import getHostHeader from lib.core.common import getRequestHeader from lib.core.common import getSafeExString -from lib.core.common import isMultiThreadMode from lib.core.common import logHTTPTraffic from lib.core.common import openFile from lib.core.common import popValue @@ -884,7 +883,7 @@ class Connect(object): else: logger.debug(warnMsg) return Connect._retryProxy(**kwargs) - elif kb.testMode or isMultiThreadMode(): + elif kb.testMode or kb.multiThreadMode: logger.critical(warnMsg) return None, None, None else: