sqlmap/plugins/dbms/sqlite/connector.py

89 lines
2.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python2
"""
2013-01-18 18:07:51 +04:00
Copyright (c) 2006-2013 sqlmap developers (http://sqlmap.org/)
2010-10-15 03:18:29 +04:00
See the file 'doc/COPYING' for copying permission
"""
try:
import sqlite3
except ImportError:
pass
2010-03-28 03:48:12 +04:00
2012-10-23 17:34:59 +04:00
import logging
from lib.core.convert import utf8encode
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
2010-10-15 13:39:41 +04:00
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), msg:
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), msg:
raise SqlmapConnectionException(msg[0])
2010-03-28 03:48:12 +04:00
2013-01-18 14:21:23 +04:00
self.initCursor()
2010-03-28 03:48:12 +04:00
self.connected()
def fetchall(self):
2010-04-06 19:12:52 +04:00
try:
return self.cursor.fetchall()
2010-10-15 13:39:41 +04:00
except self.__sqlite.OperationalError, msg:
2012-10-23 17:34:59 +04:00
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[0])
2010-04-06 19:12:52 +04:00
return None
2010-03-28 03:48:12 +04:00
def execute(self, query):
try:
self.cursor.execute(utf8encode(query))
2010-10-15 13:39:41 +04:00
except self.__sqlite.OperationalError, msg:
2012-10-23 17:34:59 +04:00
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[0])
2010-10-15 13:39:41 +04:00
except self.__sqlite.DatabaseError, msg:
raise SqlmapConnectionException(msg[0])
2010-03-28 03:48:12 +04:00
self.connector.commit()
def select(self, query):
self.execute(query)
return self.fetchall()