sqlmap/plugins/dbms/sqlite/connector.py

90 lines
2.7 KiB
Python
Raw Normal View History

#!/usr/bin/env python
"""
$Id$
2011-07-08 00:10:03 +04:00
Copyright (c) 2006-2011 sqlmap developers (http://www.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
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
2010-10-15 13:39:41 +04:00
from lib.core.exception import sqlmapMissingDependence
2010-03-28 03:48:12 +04:00
from plugins.generic.connector import Connector as GenericConnector
2010-10-15 13:39:41 +04:00
class Connector(GenericConnector):
"""
2010-03-28 03:48:12 +04:00
Homepage: http://pysqlite.googlecode.com/
User guide: http://docs.python.org/release/2.5/lib/module-sqlite3.html
API: http://docs.python.org/library/sqlite3.html
2010-10-15 13:39:41 +04:00
Debian package: python-pysqlite2 (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, _:
2011-04-30 17:20:05 +04:00
errMsg = "sqlmap requires 'python-sqlite2' 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
self.setCursor()
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:
logger.warn(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:
logger.warn(msg[0])
2010-10-15 13:39:41 +04:00
except self.__sqlite.DatabaseError, msg:
2010-03-30 17:36:23 +04:00
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()