2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2010-03-27 02:23:25 +03:00
|
|
|
|
|
|
|
"""
|
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
|
2010-03-27 02:23:25 +03:00
|
|
|
"""
|
|
|
|
|
2010-11-17 20:20:32 +03:00
|
|
|
try:
|
|
|
|
import sqlite3
|
2012-12-06 13:21:53 +04:00
|
|
|
except ImportError:
|
2010-11-17 20:20:32 +03:00
|
|
|
pass
|
2010-03-28 03:48:12 +04:00
|
|
|
|
2012-10-23 17:34:59 +04:00
|
|
|
import logging
|
|
|
|
|
2010-05-29 19:35:38 +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
|
2012-12-06 17:14:19 +04:00
|
|
|
from lib.core.exception import SqlmapConnectionException
|
|
|
|
from lib.core.exception import SqlmapMissingDependence
|
2010-03-27 02:23:25 +03:00
|
|
|
from plugins.generic.connector import Connector as GenericConnector
|
|
|
|
|
2010-10-15 13:39:41 +04:00
|
|
|
|
2010-03-27 02:23:25 +03: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)
|
2010-03-31 14:50:47 +04:00
|
|
|
License: MIT
|
2010-03-28 03:48:12 +04:00
|
|
|
|
|
|
|
Possible connectors: http://wiki.python.org/moin/SQLite
|
2010-03-27 02:23:25 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-03-31 14:50:47 +04:00
|
|
|
def connect(self):
|
2010-03-28 03:48:12 +04:00
|
|
|
self.initConnection()
|
2010-03-31 14:50:47 +04:00
|
|
|
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-27 14:37:54 +04:00
|
|
|
|
2010-10-15 13:39:41 +04:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
import sqlite
|
2012-12-06 13:21:53 +04:00
|
|
|
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
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapMissingDependence(errMsg)
|
2010-10-27 14:37:54 +04:00
|
|
|
|
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:
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapConnectionException(msg[0])
|
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()
|
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:
|
2010-05-29 19:35:38 +04:00
|
|
|
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:
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapConnectionException(msg[0])
|
2010-03-28 03:48:12 +04:00
|
|
|
|
|
|
|
self.connector.commit()
|
|
|
|
|
|
|
|
def select(self, query):
|
2010-03-31 14:50:47 +04:00
|
|
|
self.execute(query)
|
|
|
|
return self.fetchall()
|