Path: blob/master/plugins/dbms/sqlite/connector.py
2992 views
#!/usr/bin/env python12"""3Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)4See the file 'LICENSE' for copying permission5"""67try:8import sqlite39except:10pass1112import logging1314from lib.core.common import getSafeExString15from lib.core.convert import getText16from lib.core.data import conf17from lib.core.data import logger18from lib.core.exception import SqlmapConnectionException19from lib.core.exception import SqlmapMissingDependence20from plugins.generic.connector import Connector as GenericConnector2122class Connector(GenericConnector):23"""24Homepage: http://pysqlite.googlecode.com/ and http://packages.ubuntu.com/quantal/python-sqlite25User guide: http://docs.python.org/release/2.5/lib/module-sqlite3.html26API: http://docs.python.org/library/sqlite3.html27Debian package: python-sqlite (SQLite 2), python-pysqlite3 (SQLite 3)28License: MIT2930Possible connectors: http://wiki.python.org/moin/SQLite31"""3233def __init__(self):34GenericConnector.__init__(self)35self.__sqlite = sqlite33637def connect(self):38self.initConnection()39self.checkFileDb()4041try:42self.connector = self.__sqlite.connect(database=self.db, check_same_thread=False, timeout=conf.timeout)4344cursor = self.connector.cursor()45cursor.execute("SELECT * FROM sqlite_master")46cursor.close()4748except (self.__sqlite.DatabaseError, self.__sqlite.OperationalError):49warnMsg = "unable to connect using SQLite 3 library, trying with SQLite 2"50logger.warning(warnMsg)5152try:53try:54import sqlite55except ImportError:56errMsg = "sqlmap requires 'python-sqlite' third-party library "57errMsg += "in order to directly connect to the database '%s'" % self.db58raise SqlmapMissingDependence(errMsg)5960self.__sqlite = sqlite61self.connector = self.__sqlite.connect(database=self.db, check_same_thread=False, timeout=conf.timeout)62except (self.__sqlite.DatabaseError, self.__sqlite.OperationalError) as ex:63raise SqlmapConnectionException(getSafeExString(ex))6465self.initCursor()66self.printConnected()6768def fetchall(self):69try:70return self.cursor.fetchall()71except self.__sqlite.OperationalError as ex:72logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) '%s'" % getSafeExString(ex))73return None7475def execute(self, query):76try:77self.cursor.execute(getText(query))78except self.__sqlite.OperationalError as ex:79logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) '%s'" % getSafeExString(ex))80except self.__sqlite.DatabaseError as ex:81raise SqlmapConnectionException(getSafeExString(ex))8283self.connector.commit()8485def select(self, query):86self.execute(query)87return self.fetchall()888990