2019-05-08 13:47:52 +03:00
|
|
|
#!/usr/bin/env python
|
2010-03-31 14:50:47 +04:00
|
|
|
|
|
|
|
"""
|
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
|
2010-03-31 14:50:47 +04:00
|
|
|
"""
|
|
|
|
|
2012-01-13 16:45:02 +04:00
|
|
|
import time
|
|
|
|
|
2012-02-27 16:55:28 +04:00
|
|
|
from extra.safe2bin.safe2bin import safecharencode
|
2010-03-31 14:50:47 +04:00
|
|
|
from lib.core.agent import agent
|
2011-01-28 19:36:09 +03:00
|
|
|
from lib.core.common import Backend
|
2012-01-13 16:45:02 +04:00
|
|
|
from lib.core.common import calculateDeltaSeconds
|
2012-10-15 20:41:41 +04:00
|
|
|
from lib.core.common import extractExpectedValue
|
2012-01-13 16:45:02 +04:00
|
|
|
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
|
2010-03-31 14:50:47 +04:00
|
|
|
from lib.core.data import conf
|
|
|
|
from lib.core.data import kb
|
2010-04-12 13:35:20 +04:00
|
|
|
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
|
2010-11-08 12:20:02 +03:00
|
|
|
from lib.core.enums import DBMS
|
2012-10-15 20:41:41 +04:00
|
|
|
from lib.core.enums import EXPECTED
|
2016-10-17 23:55:07 +03:00
|
|
|
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
|
2010-03-31 14:50:47 +04:00
|
|
|
|
|
|
|
def direct(query, content=True):
|
2011-01-21 00:59:47 +03:00
|
|
|
select = True
|
2010-03-31 14:50:47 +04:00
|
|
|
query = agent.payloadDirect(query)
|
2012-05-22 13:33:22 +04:00
|
|
|
query = agent.adjustLateValues(query)
|
2012-01-13 16:45:02 +04:00
|
|
|
threadData = getCurrentThreadData()
|
2010-03-31 14:50:47 +04:00
|
|
|
|
2014-04-03 11:00:14 +04:00
|
|
|
if Backend.isDbms(DBMS.ORACLE) and query.upper().startswith("SELECT ") and " FROM " not in query.upper():
|
2010-03-31 14:50:47 +04:00
|
|
|
query = "%s FROM DUAL" % query
|
|
|
|
|
|
|
|
for sqlTitle, sqlStatements in SQL_STATEMENTS.items():
|
|
|
|
for sqlStatement in sqlStatements:
|
2011-01-21 00:59:47 +03:00
|
|
|
if query.lower().startswith(sqlStatement) and sqlTitle != "SQL SELECT statement":
|
|
|
|
select = False
|
2010-03-31 14:50:47 +04:00
|
|
|
break
|
|
|
|
|
2011-01-21 00:59:47 +03:00
|
|
|
if select and not query.upper().startswith("SELECT "):
|
2012-10-15 20:45:13 +04:00
|
|
|
query = "SELECT %s" % query
|
2011-01-21 00:59:47 +03:00
|
|
|
|
2012-12-07 14:54:34 +04:00
|
|
|
logger.log(CUSTOM_LOGGING.PAYLOAD, query)
|
2010-04-12 13:35:20 +04:00
|
|
|
|
2012-02-27 16:14:01 +04:00
|
|
|
output = hashDBRetrieve(query, True, True)
|
2012-01-13 16:45:02 +04:00
|
|
|
start = time.time()
|
2012-07-02 05:04:19 +04:00
|
|
|
|
2014-04-03 11:00:14 +04:00
|
|
|
if not select and "EXEC " not in query.upper():
|
2016-10-17 23:55:07 +03:00
|
|
|
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):
|
2016-10-17 23:55:07 +03:00
|
|
|
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
|
|
|
|
2012-01-13 16:45:02 +04:00
|
|
|
threadData.lastQueryDuration = calculateDeltaSeconds(start)
|
2010-03-31 14:50:47 +04:00
|
|
|
|
2012-01-14 01:03:50 +04:00
|
|
|
if not output:
|
|
|
|
return output
|
2010-03-31 14:50:47 +04:00
|
|
|
elif content:
|
2012-06-14 17:38:53 +04:00
|
|
|
if output and isListLike(output):
|
2010-03-31 14:50:47 +04:00
|
|
|
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
|
2010-03-31 14:50:47 +04:00
|
|
|
else:
|
2012-10-15 20:41:41 +04:00
|
|
|
return extractExpectedValue(output, EXPECTED.BOOL)
|