sqlmap/lib/request/direct.py

87 lines
3.1 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
"""
2020-01-01 15:25:15 +03:00
Copyright (c) 2006-2020 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
2019-11-17 02:21:55 +03: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 20:41:41 +04:00
from lib.core.common import extractExpectedValue
from lib.core.common import getCurrentThreadData
2012-02-27 16:14:01 +04:00
from lib.core.common import hashDBRetrieve
from lib.core.common import hashDBWrite
2012-06-14 17:38:53 +04:00
from lib.core.common import isListLike
2019-05-06 01:54:21 +03: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 13:19:15 +04:00
from lib.core.dicts import SQL_STATEMENTS
2012-12-07 14:54:34 +04:00
from lib.core.enums import CUSTOM_LOGGING
from lib.core.enums import DBMS
2012-10-15 20:41:41 +04:00
from lib.core.enums import EXPECTED
from lib.core.enums import TIMEOUT_STATE
2011-01-30 14:36:03 +03:00
from lib.core.settings import UNICODE_ENCODING
2019-09-11 15:05:25 +03:00
from lib.utils.safe2bin import safecharencode
2010-04-06 19:12:52 +04: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 11:00:14 +04: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 02:21:55 +03:00
if select:
if not query.upper().startswith("SELECT "):
query = "SELECT %s" % query
if conf.binaryFields:
2019-11-17 02:52:04 +03:00
for field in conf.binaryFields:
2019-11-17 02:21:55 +03: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 14:54:34 +04:00
logger.log(CUSTOM_LOGGING.PAYLOAD, query)
2012-02-27 16:14:01 +04:00
output = hashDBRetrieve(query, True, True)
start = time.time()
2014-04-03 11:00:14 +04:00
if not select and "EXEC " not in query.upper():
timeout(func=conf.dbmsConnector.execute, args=(query,), duration=conf.timeout, default=None)
2019-08-13 16:22:02 +03: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 16:14:01 +04:00
elif output:
infoMsg = "resumed: %s..." % getUnicode(output, UNICODE_ENCODING)[:20]
logger.info(infoMsg)
2016-12-20 01:47:39 +03:00
threadData.lastQueryDuration = calculateDeltaSeconds(start)
2012-01-14 01:03:50 +04:00
if not output:
return output
elif content:
2012-06-14 17:38:53 +04:00
if output and isListLike(output):
if len(output[0]) == 1:
2012-10-15 20:41:41 +04:00
output = [_[0] for _ in output]
2012-02-07 14:46:55 +04:00
2012-02-27 16:55:28 +04:00
retVal = getUnicode(output, noneToNull=True)
return safecharencode(retVal) if kb.safeCharEncode else retVal
else:
2012-10-15 20:41:41 +04:00
return extractExpectedValue(output, EXPECTED.BOOL)