sqlmap/plugins/dbms/sqlite/connector.py

89 lines
3.0 KiB
Python
Raw Normal View History

2019-05-08 13:47:52 +03:00
#!/usr/bin/env python
"""
2022-01-03 13:30:34 +03:00
Copyright (c) 2006-2022 sqlmap developers (https://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
"""
try:
import sqlite3
2017-09-04 18:05:48 +03:00
except:
pass
2010-03-28 03:48:12 +04:00
2012-10-23 17:34:59 +04:00
import logging
from lib.core.common import getSafeExString
2019-11-17 02:22:47 +03:00
from lib.core.convert import getText
2010-03-28 03:48:12 +04:00
from lib.core.data import conf
from lib.core.data import logger
from lib.core.exception import SqlmapConnectionException
from lib.core.exception import SqlmapMissingDependence
from plugins.generic.connector import Connector as GenericConnector
class Connector(GenericConnector):
"""
2013-01-22 22:56:25 +04:00
Homepage: http://pysqlite.googlecode.com/ and http://packages.ubuntu.com/quantal/python-sqlite
2010-03-28 03:48:12 +04:00
User guide: http://docs.python.org/release/2.5/lib/module-sqlite3.html
API: http://docs.python.org/library/sqlite3.html
2013-01-22 22:56:25 +04:00
Debian package: python-sqlite (SQLite 2), python-pysqlite3 (SQLite 3)
License: MIT
2010-03-28 03:48:12 +04:00
Possible connectors: http://wiki.python.org/moin/SQLite
"""
def __init__(self):
GenericConnector.__init__(self)
2010-10-15 13:39:41 +04:00
self.__sqlite = sqlite3
2010-03-28 03:48:12 +04:00
def connect(self):
2010-03-28 03:48:12 +04:00
self.initConnection()
self.checkFileDb()
2010-03-28 03:48:12 +04:00
try:
2010-10-15 13:39:41 +04:00
self.connector = self.__sqlite.connect(database=self.db, check_same_thread=False, timeout=conf.timeout)
cursor = self.connector.cursor()
cursor.execute("SELECT * FROM sqlite_master")
cursor.close()
except (self.__sqlite.DatabaseError, self.__sqlite.OperationalError):
2010-10-15 14:03:51 +04:00
warnMsg = "unable to connect using SQLite 3 library, trying with SQLite 2"
2010-12-08 17:46:07 +03:00
logger.warn(warnMsg)
2010-10-15 13:39:41 +04:00
try:
try:
import sqlite
except ImportError:
2013-01-22 22:56:25 +04:00
errMsg = "sqlmap requires 'python-sqlite' third-party library "
2010-10-15 13:39:41 +04:00
errMsg += "in order to directly connect to the database '%s'" % self.db
raise SqlmapMissingDependence(errMsg)
2010-10-15 13:39:41 +04:00
self.__sqlite = sqlite
self.connector = self.__sqlite.connect(database=self.db, check_same_thread=False, timeout=conf.timeout)
except (self.__sqlite.DatabaseError, self.__sqlite.OperationalError) as ex:
raise SqlmapConnectionException(getSafeExString(ex))
2010-03-28 03:48:12 +04:00
2013-01-18 14:21:23 +04:00
self.initCursor()
2013-04-15 16:31:27 +04:00
self.printConnected()
2010-03-28 03:48:12 +04:00
def fetchall(self):
2010-04-06 19:12:52 +04:00
try:
return self.cursor.fetchall()
except self.__sqlite.OperationalError as ex:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) '%s'" % getSafeExString(ex))
2010-04-06 19:12:52 +04:00
return None
2010-03-28 03:48:12 +04:00
def execute(self, query):
try:
2019-11-17 02:22:47 +03:00
self.cursor.execute(getText(query))
except self.__sqlite.OperationalError as ex:
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) '%s'" % getSafeExString(ex))
except self.__sqlite.DatabaseError as ex:
raise SqlmapConnectionException(getSafeExString(ex))
2010-03-28 03:48:12 +04:00
self.connector.commit()
def select(self, query):
self.execute(query)
return self.fetchall()