sqlmap/lib/utils/sqlalchemy.py

111 lines
4.1 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 imp
2013-05-29 17:49:09 +04:00
import logging
2013-04-15 17:36:10 +04:00
import os
import sys
2016-12-06 17:43:09 +03:00
import traceback
import warnings
_sqlalchemy = None
try:
f, pathname, desc = imp.find_module("sqlalchemy", sys.path[1:])
2013-08-20 20:54:32 +04:00
_ = imp.load_module("sqlalchemy", f, pathname, desc)
if hasattr(_, "dialects"):
_sqlalchemy = _
warnings.simplefilter(action="ignore", category=_sqlalchemy.exc.SAWarning)
except ImportError:
pass
try:
import MySQLdb # used by SQLAlchemy in case of MySQL
warnings.filterwarnings("error", category=MySQLdb.Warning)
except ImportError:
pass
from lib.core.data import conf
from lib.core.data import logger
from lib.core.exception import SqlmapConnectionException
2013-04-15 17:36:10 +04:00
from lib.core.exception import SqlmapFilePathException
2018-12-23 11:57:50 +03:00
from lib.core.exception import SqlmapMissingDependence
from plugins.generic.connector import Connector as GenericConnector
2019-05-19 08:52:38 +03:00
def getSafeExString(ex, encoding=None): # Cross-referenced function
raise NotImplementedError
class SQLAlchemy(GenericConnector):
2013-04-15 16:20:21 +04:00
def __init__(self, dialect=None):
GenericConnector.__init__(self)
2013-04-15 16:20:21 +04:00
self.dialect = dialect
def connect(self):
2013-04-15 16:20:21 +04:00
if _sqlalchemy:
self.initConnection()
try:
if not self.port and self.db:
2013-04-15 17:36:10 +04:00
if not os.path.exists(self.db):
2018-03-13 13:13:38 +03:00
raise SqlmapFilePathException("the provided database file '%s' does not exist" % self.db)
2013-04-15 17:36:10 +04:00
_ = conf.direct.split("//", 1)
conf.direct = "%s////%s" % (_[0], os.path.abspath(self.db))
2013-04-15 16:20:21 +04:00
if self.dialect:
2014-05-10 03:11:19 +04:00
conf.direct = conf.direct.replace(conf.dbms, self.dialect, 1)
2013-04-15 17:36:10 +04:00
2018-01-25 14:29:56 +03:00
if self.dialect == "sqlite":
engine = _sqlalchemy.create_engine(conf.direct, connect_args={"check_same_thread": False})
elif self.dialect == "oracle":
2018-05-09 14:38:39 +03:00
engine = _sqlalchemy.create_engine(conf.direct)
2018-01-25 14:29:56 +03:00
else:
engine = _sqlalchemy.create_engine(conf.direct, connect_args={})
self.connector = engine.connect()
2016-12-04 00:06:18 +03:00
except (TypeError, ValueError):
2016-12-06 17:43:09 +03:00
if "_get_server_version_info" in traceback.format_exc():
try:
import pymssql
if int(pymssql.__version__[0]) < 2:
raise SqlmapConnectionException("SQLAlchemy connection issue (obsolete version of pymssql ('%s') is causing problems)" % pymssql.__version__)
except ImportError:
pass
2017-09-01 15:29:52 +03:00
elif "invalid literal for int() with base 10: '0b" in traceback.format_exc():
raise SqlmapConnectionException("SQLAlchemy connection issue ('https://bitbucket.org/zzzeek/sqlalchemy/issues/3975')")
2019-09-09 12:15:13 +03:00
else:
pass
2013-04-15 17:36:10 +04:00
except SqlmapFilePathException:
raise
2019-01-22 03:20:27 +03:00
except Exception as ex:
2019-05-19 08:52:38 +03:00
raise SqlmapConnectionException("SQLAlchemy connection issue ('%s')" % getSafeExString(ex))
2013-04-15 16:20:21 +04:00
2013-04-15 16:31:27 +04:00
self.printConnected()
2018-12-23 11:57:50 +03:00
else:
raise SqlmapMissingDependence("SQLAlchemy not available")
def fetchall(self):
try:
retVal = []
for row in self.cursor.fetchall():
retVal.append(tuple(row))
return retVal
2019-01-22 03:20:27 +03:00
except _sqlalchemy.exc.ProgrammingError as ex:
2019-05-19 08:52:38 +03:00
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % getSafeExString(ex))
return None
def execute(self, query):
try:
self.cursor = self.connector.execute(query)
2019-01-22 03:20:27 +03:00
except (_sqlalchemy.exc.OperationalError, _sqlalchemy.exc.ProgrammingError) as ex:
2019-05-19 08:52:38 +03:00
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % getSafeExString(ex))
2019-01-22 03:20:27 +03:00
except _sqlalchemy.exc.InternalError as ex:
2019-05-19 08:52:38 +03:00
raise SqlmapConnectionException(getSafeExString(ex))
def select(self, query):
self.execute(query)
return self.fetchall()