From 3a90105fbbeace091019715b14e2ae647753fb09 Mon Sep 17 00:00:00 2001 From: Miroslav Stampar Date: Thu, 14 Jun 2012 13:38:53 +0000 Subject: [PATCH] minor refactoring --- lib/core/common.py | 21 ++++++++++++++------- lib/core/dump.py | 9 +++++---- lib/core/option.py | 3 ++- lib/request/direct.py | 3 ++- lib/techniques/error/use.py | 3 ++- plugins/generic/enumeration.py | 7 ++++--- plugins/generic/filesystem.py | 5 +++-- 7 files changed, 32 insertions(+), 19 deletions(-) diff --git a/lib/core/common.py b/lib/core/common.py index b29477330..f1ce9216e 100644 --- a/lib/core/common.py +++ b/lib/core/common.py @@ -788,7 +788,7 @@ def readInput(message, default=None, checkBatch=True): message += " " if checkBatch and conf.batch: - if isinstance(default, (list, tuple, set)): + if isListLike(default): options = ",".join(getUnicode(opt, UNICODE_ENCODING) for opt in default) elif default: options = getUnicode(default, UNICODE_ENCODING) @@ -1888,7 +1888,7 @@ def getUnicode(value, encoding=None, system=False, noneToNull=False): if noneToNull and value is None: return NULL - if isinstance(value, (list, tuple)): + if isListLike(value): value = list(getUnicode(_, encoding, system, noneToNull) for _ in value) return value @@ -2425,7 +2425,7 @@ def arrayizeValue(value): Makes a list out of value if it is not already a list or tuple itself """ - if not isinstance(value, (list, tuple)): + if not isListLike(value): value = [value] return value @@ -2435,7 +2435,7 @@ def unArrayizeValue(value): Makes a value out of iterable if it is a list or tuple itself """ - if isinstance(value, (list, tuple)): + if isListLike(value): value = value[0] if len(value) > 0 else None return value @@ -2446,12 +2446,19 @@ def flattenValue(value): """ for i in iter(value): - if isinstance(i, (list, tuple)): + if isListLike(i): for j in flattenValue(i): yield j else: yield i +def isListLike(value): + """ + Returns True if the given value is a list-like instance + """ + + return isinstance(value, (list, tuple, set, BigArray)) + def getSortedInjectionTests(): """ Returns prioritized test list by eventually detected DBMS from error @@ -2784,7 +2791,7 @@ def isNoneValue(value): if isinstance(value, basestring): return value in ("None", "") - elif isinstance(value, (list, tuple)): + elif isListLike(value): return all(isNoneValue(_) for _ in value) elif isinstance(value, dict): return not any(value) @@ -3127,7 +3134,7 @@ def applyFunctionRecursively(value, function): Applies function recursively through list-like structures """ - if isinstance(value, (list, tuple, set, BigArray)): + if isListLike(value): retVal = [applyFunctionRecursively(_, function) for _ in value] else: retVal = function(value) diff --git a/lib/core/dump.py b/lib/core/dump.py index 1b2517179..4b4ac2cd4 100644 --- a/lib/core/dump.py +++ b/lib/core/dump.py @@ -17,6 +17,7 @@ from lib.core.common import Backend from lib.core.common import dataToDumpFile from lib.core.common import dataToStdout from lib.core.common import getUnicode +from lib.core.common import isListLike from lib.core.common import normalizeUnicode from lib.core.common import openFile from lib.core.common import prioritySortColumns @@ -72,7 +73,7 @@ class Dump: return self._outputFile def string(self, header, data, sort=True): - if isinstance(data, (list, tuple, set)): + if isListLike(data): self.lister(header, data, sort) elif data: data = self._formatString(getUnicode(data)) @@ -102,7 +103,7 @@ class Dump: for element in elements: if isinstance(element, basestring): self._write("[*] %s" % element) - elif isinstance(element, (list, tuple, set)): + elif isListLike(element): self._write("[*] " + ", ".join(getUnicode(e) for e in element)) if elements: @@ -173,7 +174,7 @@ class Dump: for tables in dbTables.values(): for table in tables: - if isinstance(table, (list, tuple, set)): + if table and isListLike(table): table = table[0] maxlength = max(maxlength, len(normalizeUnicode(table) or str(table))) @@ -193,7 +194,7 @@ class Dump: self._write("+%s+" % lines) for table in tables: - if isinstance(table, (list, tuple, set)): + if table and isListLike(table): table = table[0] blank = " " * (maxlength - len(normalizeUnicode(table) or str(table))) diff --git a/lib/core/option.py b/lib/core/option.py index bfe5282eb..cba15af41 100644 --- a/lib/core/option.py +++ b/lib/core/option.py @@ -35,6 +35,7 @@ from lib.core.common import findPageForms from lib.core.common import getConsoleWidth from lib.core.common import getFileItems from lib.core.common import getFileType +from lib.core.common import isListLike from lib.core.common import normalizePath from lib.core.common import ntToPosixSlashes from lib.core.common import openFile @@ -1630,7 +1631,7 @@ def __saveCmdline(): optionData.sort() for option, value, datatype in optionData: - if isinstance(datatype, (list, tuple, set)): + if datatype and isListLike(datatype): datatype = datatype[0] if value is None: diff --git a/lib/request/direct.py b/lib/request/direct.py index 02724a039..85db6ba2b 100644 --- a/lib/request/direct.py +++ b/lib/request/direct.py @@ -17,6 +17,7 @@ from lib.core.common import getCurrentThreadData from lib.core.common import getUnicode from lib.core.common import hashDBRetrieve from lib.core.common import hashDBWrite +from lib.core.common import isListLike from lib.core.data import conf from lib.core.data import kb from lib.core.data import logger @@ -61,7 +62,7 @@ def direct(query, content=True): if not output: return output elif content: - if output and isinstance(output, (list, tuple)): + if output and isListLike(output): if len(output[0]) == 1: if len(output) > 1: output = map(lambda _: _[0], output) diff --git a/lib/techniques/error/use.py b/lib/techniques/error/use.py index 5bb18b785..8a94c9b6b 100644 --- a/lib/techniques/error/use.py +++ b/lib/techniques/error/use.py @@ -23,6 +23,7 @@ from lib.core.common import hashDBRetrieve from lib.core.common import hashDBWrite from lib.core.common import incrementCounter from lib.core.common import initTechnique +from lib.core.common import isListLike from lib.core.common import isNumPosStrValue from lib.core.common import listToStrValue from lib.core.common import readInput @@ -367,7 +368,7 @@ def errorUse(expression, expected=None, dump=False): if not outputs and not abortedFlag: outputs = __errorFields(expression, expressionFields, expressionFieldsList) - if outputs and isinstance(outputs, list) and len(outputs) == 1 and isinstance(outputs[0], basestring): + if outputs and isListLike(outputs) and len(outputs) == 1 and isinstance(outputs[0], basestring): outputs = outputs[0] duration = calculateDeltaSeconds(start) diff --git a/plugins/generic/enumeration.py b/plugins/generic/enumeration.py index 874d45971..c81f15dd7 100644 --- a/plugins/generic/enumeration.py +++ b/plugins/generic/enumeration.py @@ -20,6 +20,7 @@ from lib.core.common import filterPairValues from lib.core.common import getLimitRange from lib.core.common import getUnicode from lib.core.common import isInferenceAvailable +from lib.core.common import isListLike from lib.core.common import isNoneValue from lib.core.common import isNumPosStrValue from lib.core.common import isTechniqueAvailable @@ -879,7 +880,7 @@ class Enumeration: if not isNoneValue(value): value = filter(None, arrayizeValue(value)) - if len(value) > 0 and not isinstance(value[0], (list, tuple)): + if len(value) > 0 and not isListLike(value[0]): value = map(lambda x: (dbs[0], x), value) for db, table in filterPairValues(value): @@ -1620,7 +1621,7 @@ class Enumeration: entries = [] elif isinstance(entries, basestring): entries = [entries] - elif not isinstance(entries, (list, tuple)): + elif not isListLike(entries): entries = [] entriesCount = len(entries) @@ -1630,7 +1631,7 @@ class Enumeration: colLen = len(column) if column not in kb.data.dumpedTable: - kb.data.dumpedTable[column] = {"length": colLen, "values": []} + kb.data.dumpedTable[column] = {"length": colLen, "values": BigArray()} for entry in entries: if entry is None or len(entry) == 0: diff --git a/plugins/generic/filesystem.py b/plugins/generic/filesystem.py index a17fc61f7..0630fdeb0 100644 --- a/plugins/generic/filesystem.py +++ b/plugins/generic/filesystem.py @@ -15,6 +15,7 @@ from lib.core.agent import agent from lib.core.common import dataToOutFile from lib.core.common import Backend from lib.core.common import isNumPosStrValue +from lib.core.common import isListLike from lib.core.common import isTechniqueAvailable from lib.core.common import randomStr from lib.core.common import readInput @@ -213,11 +214,11 @@ class Filesystem: self.cleanup(onlyFileTbl=True) return - elif isinstance(fileContent, (list, tuple, set)): + elif isListLike(fileContent): newFileContent = "" for chunk in fileContent: - if isinstance(chunk, (list, tuple, set)): + if isListLike(chunk): if len(chunk) > 0: chunk = chunk[0] else: