sqlmap/lib/utils/sqlalchemy.py

86 lines
2.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
2014-01-13 21:24:49 +04:00
Copyright (c) 2006-2014 sqlmap developers (http://sqlmap.org/)
See the file 'doc/COPYING' 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
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
from plugins.generic.connector import Connector as GenericConnector
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):
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:
conf.direct = conf.direct.replace(conf.dbms, self.dialect)
2013-04-15 17:36:10 +04:00
2013-04-15 16:20:21 +04:00
engine = _sqlalchemy.create_engine(conf.direct, connect_args={'check_same_thread':False} if self.dialect == "sqlite" else {})
self.connector = engine.connect()
2013-04-15 17:36:10 +04:00
except SqlmapFilePathException:
raise
2013-04-15 16:20:21 +04:00
except Exception, msg:
2013-05-19 03:19:54 +04:00
raise SqlmapConnectionException("SQLAlchemy connection issue ('%s')" % msg[0])
2013-04-15 16:20:21 +04:00
2013-04-15 16:31:27 +04:00
self.printConnected()
def fetchall(self):
try:
retVal = []
for row in self.cursor.fetchall():
retVal.append(tuple(row))
return retVal
except _sqlalchemy.exc.ProgrammingError, msg:
2013-10-15 11:49:27 +04:00
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg.message if hasattr(msg, "message") else msg)
return None
def execute(self, query):
try:
self.cursor = self.connector.execute(query)
except (_sqlalchemy.exc.OperationalError, _sqlalchemy.exc.ProgrammingError), msg:
2013-10-15 11:49:27 +04:00
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg.message if hasattr(msg, "message") else msg)
except _sqlalchemy.exc.InternalError, msg:
raise SqlmapConnectionException(msg[1])
def select(self, query):
self.execute(query)
return self.fetchall()