|
7 | 7 | See the file 'doc/COPYING' for copying permission |
8 | 8 | """ |
9 | 9 |
|
10 | | -try: |
11 | | - import sqlite3 |
12 | | -except ImportError, _: |
13 | | - pass |
| 10 | +import sqlite3 |
14 | 11 |
|
15 | 12 | from lib.core.convert import utf8encode |
16 | 13 | from lib.core.data import conf |
17 | 14 | from lib.core.data import logger |
18 | 15 | from lib.core.exception import sqlmapConnectionException |
| 16 | +from lib.core.exception import sqlmapMissingDependence |
19 | 17 |
|
20 | 18 | from plugins.generic.connector import Connector as GenericConnector |
21 | 19 |
|
| 20 | + |
22 | 21 | class Connector(GenericConnector): |
23 | 22 | """ |
24 | 23 | Homepage: http://pysqlite.googlecode.com/ |
25 | 24 | User guide: http://docs.python.org/release/2.5/lib/module-sqlite3.html |
26 | 25 | API: http://docs.python.org/library/sqlite3.html |
27 | | - Debian package: python-pysqlite2 |
| 26 | + Debian package: python-pysqlite2 (SQLite 2), python-pysqlite3 (SQLite 3) |
28 | 27 | License: MIT |
29 | 28 |
|
30 | 29 | Possible connectors: http://wiki.python.org/moin/SQLite |
31 | 30 | """ |
32 | 31 |
|
33 | 32 | def __init__(self): |
34 | 33 | GenericConnector.__init__(self) |
| 34 | + self.__sqlite = sqlite3 |
35 | 35 |
|
36 | 36 | def connect(self): |
37 | 37 | self.initConnection() |
38 | 38 | self.checkFileDb() |
39 | 39 |
|
40 | 40 | try: |
41 | | - self.connector = sqlite3.connect(database=self.db, check_same_thread=False, timeout=conf.timeout) |
42 | | - except (sqlite3.DatabaseError, sqlite3.OperationalError), msg: |
43 | | - raise sqlmapConnectionException, msg[0] |
| 41 | + self.connector = self.__sqlite.connect(database=self.db, check_same_thread=False, timeout=conf.timeout) |
| 42 | + |
| 43 | + cursor = self.connector.cursor() |
| 44 | + cursor.execute("SELECT * FROM sqlite_master") |
| 45 | + cursor.close() |
| 46 | + |
| 47 | + except (self.__sqlite.DatabaseError, self.__sqlite.OperationalError), msg: |
| 48 | + errMsg = "unable to connect using SQLite 3 library, trying with SQLite 2 (%s)" % msg[0] |
| 49 | + logger.error(errMsg) |
| 50 | + try: |
| 51 | + try: |
| 52 | + import sqlite |
| 53 | + except ImportError, _: |
| 54 | + errMsg = "sqlmap requires 'python-sqlite2' third-party library " |
| 55 | + errMsg += "in order to directly connect to the database '%s'" % self.db |
| 56 | + raise sqlmapMissingDependence, errMsg |
| 57 | + self.__sqlite = sqlite |
| 58 | + self.connector = self.__sqlite.connect(database=self.db, check_same_thread=False, timeout=conf.timeout) |
| 59 | + except (self.__sqlite.DatabaseError, self.__sqlite.OperationalError), msg: |
| 60 | + raise sqlmapConnectionException, msg[0] |
44 | 61 |
|
45 | 62 | self.setCursor() |
46 | 63 | self.connected() |
47 | 64 |
|
48 | 65 | def fetchall(self): |
49 | 66 | try: |
50 | 67 | return self.cursor.fetchall() |
51 | | - except sqlite3.OperationalError, msg: |
| 68 | + except self.__sqlite.OperationalError, msg: |
52 | 69 | logger.log(8, msg[0]) |
53 | 70 | return None |
54 | 71 |
|
55 | 72 | def execute(self, query): |
56 | 73 | try: |
57 | | - import pdb |
58 | | - pdb.set_trace() |
59 | 74 | self.cursor.execute(utf8encode(query)) |
60 | | - except sqlite3.OperationalError, msg: |
| 75 | + except self.__sqlite.OperationalError, msg: |
61 | 76 | logger.log(8, msg[0]) |
62 | | - except sqlite3.DatabaseError, msg: |
| 77 | + except self.__sqlite.DatabaseError, msg: |
63 | 78 | raise sqlmapConnectionException, msg[0] |
64 | 79 |
|
65 | 80 | self.connector.commit() |
|
0 commit comments