SIGN IN SIGN UP
sqlmapproject / sqlmap UNCLAIMED

Automatic SQL injection and database takeover tool

36950 0 0 Python
2019-05-08 12:47:52 +02:00
#!/usr/bin/env python
"""
2024-01-03 23:11:52 +01:00
Copyright (c) 2006-2024 sqlmap developers (https://sqlmap.org/)
2017-10-11 14:50:46 +02:00
See the file 'LICENSE' for copying permission
"""
2019-11-17 00:21:55 +01:00
import re
import time
from lib.core.agent import agent
from lib.core.common import Backend
from lib.core.common import calculateDeltaSeconds
2012-10-15 18:41:41 +02:00
from lib.core.common import extractExpectedValue
from lib.core.common import getCurrentThreadData
2012-02-27 12:14:01 +00:00
from lib.core.common import hashDBRetrieve
from lib.core.common import hashDBWrite
2012-06-14 13:38:53 +00:00
from lib.core.common import isListLike
2019-05-06 00:54:21 +02:00
from lib.core.convert import getUnicode
from lib.core.data import conf
from lib.core.data import kb
from lib.core.data import logger
2012-08-21 11:19:15 +02:00
from lib.core.dicts import SQL_STATEMENTS
2012-12-07 11:54:34 +01:00
from lib.core.enums import CUSTOM_LOGGING
from lib.core.enums import DBMS
2012-10-15 18:41:41 +02:00
from lib.core.enums import EXPECTED
from lib.core.enums import TIMEOUT_STATE
2011-01-30 11:36:03 +00:00
from lib.core.settings import UNICODE_ENCODING
2019-09-11 14:05:25 +02:00
from lib.utils.safe2bin import safecharencode
2010-04-06 15:12:52 +00:00
from lib.utils.timeout import timeout
def direct(query, content=True):
select = True
query = agent.payloadDirect(query)
query = agent.adjustLateValues(query)
threadData = getCurrentThreadData()
2014-04-03 09:00:14 +02:00
if Backend.isDbms(DBMS.ORACLE) and query.upper().startswith("SELECT ") and " FROM " not in query.upper():
query = "%s FROM DUAL" % query
for sqlTitle, sqlStatements in SQL_STATEMENTS.items():
for sqlStatement in sqlStatements:
if query.lower().startswith(sqlStatement) and sqlTitle != "SQL SELECT statement":
select = False
break
2019-11-17 00:21:55 +01:00
if select:
2020-11-30 23:33:08 +01:00
if re.search(r"(?i)\ASELECT ", query) is None:
2019-11-17 00:21:55 +01:00
query = "SELECT %s" % query
2020-11-30 23:33:08 +01:00
2019-11-17 00:21:55 +01:00
if conf.binaryFields:
2019-11-17 00:52:04 +01:00
for field in conf.binaryFields:
2019-11-17 00:21:55 +01:00
field = field.strip()
if re.search(r"\b%s\b" % re.escape(field), query):
query = re.sub(r"\b%s\b" % re.escape(field), agent.hexConvertField(field), query)
2012-12-07 11:54:34 +01:00
logger.log(CUSTOM_LOGGING.PAYLOAD, query)
2012-02-27 12:14:01 +00:00
output = hashDBRetrieve(query, True, True)
start = time.time()
2020-11-30 23:33:08 +01:00
if not select and re.search(r"(?i)\bEXEC ", query) is None:
timeout(func=conf.dbmsConnector.execute, args=(query,), duration=conf.timeout, default=None)
2019-08-13 15:22:02 +02:00
elif not (output and ("%soutput" % conf.tablePrefix) not in query and ("%sfile" % conf.tablePrefix) not in query):
output, state = timeout(func=conf.dbmsConnector.select, args=(query,), duration=conf.timeout, default=None)
if state == TIMEOUT_STATE.NORMAL:
hashDBWrite(query, output, True)
elif state == TIMEOUT_STATE.TIMEOUT:
conf.dbmsConnector.close()
conf.dbmsConnector.connect()
2012-02-27 12:14:01 +00:00
elif output:
infoMsg = "resumed: %s..." % getUnicode(output, UNICODE_ENCODING)[:20]
logger.info(infoMsg)
2016-12-19 23:47:39 +01:00
threadData.lastQueryDuration = calculateDeltaSeconds(start)
2012-01-13 21:03:50 +00:00
if not output:
return output
elif content:
2012-06-14 13:38:53 +00:00
if output and isListLike(output):
if len(output[0]) == 1:
2012-10-15 18:41:41 +02:00
output = [_[0] for _ in output]
2012-02-07 10:46:55 +00:00
2012-02-27 12:55:28 +00:00
retVal = getUnicode(output, noneToNull=True)
return safecharencode(retVal) if kb.safeCharEncode else retVal
else:
2012-10-15 18:41:41 +02:00
return extractExpectedValue(output, EXPECTED.BOOL)