Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81bd9a2

Browse files
committed
minor refactoring
1 parent 0b54553 commit 81bd9a2

8 files changed

Lines changed: 33 additions & 57 deletions

File tree

lib/core/common.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,6 @@
101101
from lib.core.settings import DUMP_CR_MARKER
102102
from lib.core.settings import DUMP_DEL_MARKER
103103
from lib.core.settings import DUMP_TAB_MARKER
104-
from lib.core.settings import DUMP_START_MARKER
105-
from lib.core.settings import DUMP_STOP_MARKER
106104
from lib.core.settings import ML
107105
from lib.core.settings import MIN_TIME_RESPONSES
108106
from lib.core.settings import PAYLOAD_DELIMITER
@@ -1047,7 +1045,6 @@ def restoreDumpMarkedChars(inpStr, onlyNewlineTab=False):
10471045
replacedString = replacedString.replace(DUMP_NEWLINE_MARKER, "\n").replace(DUMP_CR_MARKER, "\r").replace(DUMP_TAB_MARKER, "\t")
10481046

10491047
if not onlyNewlineTab:
1050-
replacedString = replacedString.replace(DUMP_START_MARKER, "").replace(DUMP_STOP_MARKER, "")
10511048
replacedString = replacedString.replace(DUMP_DEL_MARKER, ", ")
10521049

10531050
return replacedString
@@ -1351,14 +1348,8 @@ def parseUnionPage(output, expression, partial=False, sort=True):
13511348

13521349
data = BigArray()
13531350

1354-
outCond1 = ( output.startswith(kb.chars.start) and output.endswith(kb.chars.stop) )
1355-
outCond2 = ( output.startswith(DUMP_START_MARKER) and output.endswith(DUMP_STOP_MARKER) )
1356-
1357-
if outCond1 or outCond2:
1358-
if outCond1:
1359-
regExpr = '%s(.*?)%s' % (kb.chars.start, kb.chars.stop)
1360-
elif outCond2:
1361-
regExpr = '%s(.*?)%s' % (DUMP_START_MARKER, DUMP_STOP_MARKER)
1351+
if output.startswith(kb.chars.start) and output.endswith(kb.chars.stop):
1352+
regExpr = '%s(.*?)%s' % (kb.chars.start, kb.chars.stop)
13621353

13631354
output = re.findall(regExpr, output, re.DOTALL | re.IGNORECASE)
13641355

@@ -2536,11 +2527,12 @@ def setOptimize():
25362527

25372528
def initTechnique(technique=None):
25382529
"""
2539-
Prepares proper page template and match ratio for technique specified
2530+
Prepares data for technique specified
25402531
"""
25412532

25422533
try:
25432534
data = getTechniqueData(technique)
2535+
resetCounter(technique)
25442536

25452537
if data:
25462538
kb.pageTemplate, kb.errorIsNone = getPageTemplate(data.templatePayload, kb.injection.place)
@@ -3172,3 +3164,11 @@ def unserializeObject(value):
31723164
if value:
31733165
retVal = pickle.loads(value.encode(UNICODE_ENCODING)) # pickle has problems with Unicode
31743166
return retVal
3167+
3168+
def resetCounter(counter):
3169+
kb.counters[counter] = 0
3170+
3171+
def incrementCounter(counter):
3172+
if counter not in kb.counters:
3173+
resetCounter(counter)
3174+
kb.counters[counter] += 1

lib/core/option.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,7 @@ def __setKnowledgeBaseAttributes(flushAll=True):
14021402
kb.cache.stdev = {}
14031403

14041404
kb.commonOutputs = None
1405+
kb.counters = {}
14051406
kb.data = AttribDict()
14061407
kb.dataOutputFlag = False
14071408

lib/core/settings.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@
5252
DUMP_CR_MARKER = "__CARRIAGE_RETURN__"
5353
DUMP_DEL_MARKER = "__DEL__"
5454
DUMP_TAB_MARKER = "__TAB__"
55-
DUMP_START_MARKER = "__START__"
56-
DUMP_STOP_MARKER = "__STOP__"
5755

5856
URI_QUESTION_MARKER = "__QUESTION_MARK__"
5957

lib/techniques/error/use.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from lib.core.common import dataToStdout
2020
from lib.core.common import extractRegexResult
2121
from lib.core.common import getUnicode
22+
from lib.core.common import incrementCounter
2223
from lib.core.common import initTechnique
2324
from lib.core.common import isNumPosStrValue
2425
from lib.core.common import listToStrValue
@@ -46,11 +47,7 @@
4647
from lib.request.connect import Connect as Request
4748
from lib.utils.resume import resume
4849

49-
reqCount = 0
50-
5150
def __oneShotErrorUse(expression, field):
52-
global reqCount
53-
5451
retVal = conf.hashDB.retrieve(expression) if not any([conf.flushSession, conf.freshQueries]) else None
5552

5653
threadData = getCurrentThreadData()
@@ -85,7 +82,7 @@ def __oneShotErrorUse(expression, field):
8582
# Perform the request
8683
page, headers = Request.queryPage(payload, content=True)
8784

88-
reqCount += 1
85+
incrementCounter(PAYLOAD.TECHNIQUE.ERROR)
8986

9087
# Parse the returned page to get the exact error-based
9188
# sql injection output
@@ -204,16 +201,13 @@ def errorUse(expression, expected=None, resumeValue=True, dump=False):
204201

205202
initTechnique(PAYLOAD.TECHNIQUE.ERROR)
206203

207-
global reqCount
208-
209204
count = None
210205
start = time.time()
211206
startLimit = 0
212207
stopLimit = None
213208
outputs = []
214209
untilLimitChar = None
215210
untilOrderChar = None
216-
reqCount = 0
217211

218212
if resumeValue:
219213
output = resume(expression, None)
@@ -392,7 +386,7 @@ def errorThread():
392386
duration = calculateDeltaSeconds(start)
393387

394388
if not kb.bruteMode:
395-
debugMsg = "performed %d queries in %d seconds" % (reqCount, duration)
389+
debugMsg = "performed %d queries in %d seconds" % (kb.counters[PAYLOAD.TECHNIQUE.ERROR], duration)
396390
logger.debug(debugMsg)
397391

398392
return outputs

lib/techniques/union/use.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from lib.core.common import extractRegexResult
2121
from lib.core.common import getConsoleWidth
2222
from lib.core.common import getUnicode
23+
from lib.core.common import incrementCounter
2324
from lib.core.common import initTechnique
2425
from lib.core.common import isNumPosStrValue
2526
from lib.core.common import listToStrValue
@@ -44,11 +45,7 @@
4445
from lib.request.connect import Connect as Request
4546
from lib.utils.resume import resume
4647

47-
reqCount = 0
48-
4948
def __oneShotUnionUse(expression, unpack=True, limited=False):
50-
global reqCount
51-
5249
retVal = conf.hashDB.retrieve(expression) if not any([conf.flushSession, conf.freshQueries]) else None
5350

5451
threadData = getCurrentThreadData()
@@ -59,13 +56,9 @@ def __oneShotUnionUse(expression, unpack=True, limited=False):
5956
trimcheck = "%s(?P<result>.*?)</" % (kb.chars.start)
6057

6158
# Prepare expression with delimiters
62-
injExpression = agent.concatQuery(expression, unpack)
63-
injExpression = unescaper.unescape(injExpression)
59+
injExpression = unescaper.unescape(agent.concatQuery(expression, unpack))
6460

65-
if conf.limitStart or conf.limitStop:
66-
where = PAYLOAD.WHERE.NEGATIVE
67-
else:
68-
where = None
61+
where = PAYLOAD.WHERE.NEGATIVE if conf.limitStart or conf.limitStop else None
6962

7063
# Forge the inband SQL injection request
7164
vector = kb.injection.data[PAYLOAD.TECHNIQUE.UNION].vector
@@ -75,7 +68,7 @@ def __oneShotUnionUse(expression, unpack=True, limited=False):
7568
# Perform the request
7669
page, headers = Request.queryPage(payload, content=True, raise404=False)
7770

78-
reqCount += 1
71+
incrementCounter(PAYLOAD.TECHNIQUE.UNION)
7972

8073
# Parse the returned page to get the exact union-based
8174
# sql injection output
@@ -129,8 +122,7 @@ def __configUnionCols(columns):
129122
if not colsStart.isdigit() or not colsStop.isdigit():
130123
raise sqlmapSyntaxException, "--union-cols must be a range of integers"
131124

132-
conf.uColsStart = int(colsStart)
133-
conf.uColsStop = int(colsStop)
125+
conf.uColsStart, conf.uColsStop = int(colsStart), int(colsStop)
134126

135127
if conf.uColsStart > conf.uColsStop:
136128
errMsg = "--union-cols range has to be from lower to "
@@ -149,15 +141,13 @@ def unionUse(expression, unpack=True, dump=False):
149141

150142
initTechnique(PAYLOAD.TECHNIQUE.UNION)
151143

152-
global reqCount
153-
154144
count = None
155145
origExpr = expression
156146
startLimit = 0
157147
stopLimit = None
158148
test = True
159149
value = ""
160-
reqCount = 0
150+
161151
width = getConsoleWidth()
162152
start = time.time()
163153

@@ -362,7 +352,7 @@ def unionThread():
362352
duration = calculateDeltaSeconds(start)
363353

364354
if not kb.bruteMode:
365-
debugMsg = "performed %d queries in %d seconds" % (reqCount, duration)
355+
debugMsg = "performed %d queries in %d seconds" % (kb.counters[PAYLOAD.TECHNIQUE.UNION], duration)
366356
logger.debug(debugMsg)
367357

368358
return value

lib/utils/hashdb.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,9 @@ def beginTransaction(self):
116116
def endTransaction(self):
117117
threadData = getCurrentThreadData()
118118
if threadData.inTransaction:
119-
self.cursor.execute('END TRANSACTION')
120-
threadData.inTransaction = False
119+
try:
120+
self.cursor.execute('END TRANSACTION')
121+
except sqlite3.OperationalError, ex:
122+
pass
123+
finally:
124+
threadData.inTransaction = False

lib/utils/resume.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
from lib.core.enums import PAYLOAD
2828
from lib.core.unescaper import unescaper
2929
from lib.techniques.blind.inference import bisection
30-
from lib.core.settings import DUMP_START_MARKER
31-
from lib.core.settings import DUMP_STOP_MARKER
3230
from lib.core.settings import DUMP_DEL_MARKER
3331

3432
def queryOutputLength(expression, payload):
@@ -120,20 +118,11 @@ def resume(expression, payload):
120118
resumedValue = resumedValue[:-1]
121119

122120
infoMsg = "read from file '%s': " % conf.sessionFile
123-
logValue = getCompiledRegex("%s(.*?)%s" % (DUMP_START_MARKER, DUMP_STOP_MARKER), re.S).findall(resumedValue)
124121

125-
if logValue:
126-
if kb.technique == PAYLOAD.TECHNIQUE.UNION:
127-
logValue = ", ".join(value.replace(DUMP_DEL_MARKER, ", ") for value in logValue)
128-
else:
129-
return None
122+
if "\n" in resumedValue:
123+
infoMsg += "%s..." % resumedValue.split("\n")[0]
130124
else:
131-
logValue = resumedValue
132-
133-
if "\n" in logValue:
134-
infoMsg += "%s..." % logValue.split("\n")[0]
135-
else:
136-
infoMsg += logValue
125+
infoMsg += resumedValue
137126

138127
if not kb.suppressResumeInfo:
139128
dataToStdout("[%s] [INFO] %s\n" % (time.strftime("%X"), infoMsg))

plugins/generic/enumeration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,7 +1444,7 @@ def __pivotDumpTable(self, table, colList, count=None, blind=True):
14441444

14451445
if not validPivotValue:
14461446
warnMsg = "no proper pivot column provided (with unique values)."
1447-
warnMsg += " all rows can't be retrieved."
1447+
warnMsg += " It's not possible to retrieve all rows."
14481448
logger.warn(warnMsg)
14491449

14501450
pivotValue = " "

0 commit comments

Comments
 (0)