sqlmap/lib/request/direct.py

80 lines
2.8 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
"""
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
import time
2012-02-27 16:55:28 +04:00
from extra.safe2bin.safe2bin import safecharencode
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
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
if select and not query.upper().startswith("SELECT "):
2012-10-15 20:45:13 +04:00
query = "SELECT %s" % 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)