Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e65037

Browse files
committed
Introducing lib/utils/sqlalchemy.py (Issue #361)
1 parent b6fee63 commit 3e65037

5 files changed

Lines changed: 74 additions & 13 deletions

File tree

lib/controller/handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from lib.core.settings import MAXDB_ALIASES
1919
from lib.core.settings import SYBASE_ALIASES
2020
from lib.core.settings import DB2_ALIASES
21+
from lib.utils.sqlalchemy import SQLAlchemy
2122

2223
from plugins.dbms.mssqlserver import MSSQLServerMap
2324
from plugins.dbms.mssqlserver.connector import Connector as MSSQLServerConn

lib/core/dicts.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@
127127
DUMP_REPLACEMENTS = {" ": NULL, "": BLANK}
128128

129129
DBMS_DICT = {
130-
DBMS.MSSQL: (MSSQL_ALIASES, "python-pymssql", "http://pymssql.sourceforge.net/"),
131-
DBMS.MYSQL: (MYSQL_ALIASES, "python pymysql", "https://github.com/petehunt/PyMySQL/"),
132-
DBMS.PGSQL: (PGSQL_ALIASES, "python-psycopg2", "http://initd.org/psycopg/"),
133-
DBMS.ORACLE: (ORACLE_ALIASES, "python cx_Oracle", "http://cx-oracle.sourceforge.net/"),
134-
DBMS.SQLITE: (SQLITE_ALIASES, "python-sqlite", "http://packages.ubuntu.com/quantal/python-sqlite"),
135-
DBMS.ACCESS: (ACCESS_ALIASES, "python-pyodbc", "http://pyodbc.googlecode.com/"),
136-
DBMS.FIREBIRD: (FIREBIRD_ALIASES, "python-kinterbasdb", "http://kinterbasdb.sourceforge.net/"),
137-
DBMS.MAXDB: (MAXDB_ALIASES, None, None),
138-
DBMS.SYBASE: (SYBASE_ALIASES, "python-pymssql", "http://pymssql.sourceforge.net/"),
139-
DBMS.DB2: (DB2_ALIASES, "python ibm-db", "http://code.google.com/p/ibm-db/"),
130+
DBMS.MSSQL: (MSSQL_ALIASES, "python-pymssql", "http://pymssql.sourceforge.net/", "mssql"),
131+
DBMS.MYSQL: (MYSQL_ALIASES, "python pymysql", "https://github.com/petehunt/PyMySQL/", "mysql"),
132+
DBMS.PGSQL: (PGSQL_ALIASES, "python-psycopg2", "http://initd.org/psycopg/", "postgresql"),
133+
DBMS.ORACLE: (ORACLE_ALIASES, "python cx_Oracle", "http://cx-oracle.sourceforge.net/", "oracle"),
134+
DBMS.SQLITE: (SQLITE_ALIASES, "python-sqlite", "http://packages.ubuntu.com/quantal/python-sqlite", "sqlite"),
135+
DBMS.ACCESS: (ACCESS_ALIASES, "python-pyodbc", "http://pyodbc.googlecode.com/", "access"),
136+
DBMS.FIREBIRD: (FIREBIRD_ALIASES, "python-kinterbasdb", "http://kinterbasdb.sourceforge.net/", "firebird"),
137+
DBMS.MAXDB: (MAXDB_ALIASES, None, None, "maxdb"),
138+
DBMS.SYBASE: (SYBASE_ALIASES, "python-pymssql", "http://pymssql.sourceforge.net/", "sybase"),
139+
DBMS.DB2: (DB2_ALIASES, "python ibm-db", "http://code.google.com/p/ibm-db/", "ibm_db_sa"),
140140
}
141141

142142
FROM_DUMMY_TABLE = {

lib/core/target.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ def _resumeDBMS():
356356

357357
if conf.dbms:
358358
check = True
359-
for aliases, _, _ in DBMS_DICT.values():
359+
for aliases, _, _, _ in DBMS_DICT.values():
360360
if conf.dbms.lower() in aliases and dbms not in aliases:
361361
check = False
362362
break

lib/utils/sqlalchemy.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Copyright (c) 2006-2013 sqlmap developers (http://sqlmap.org/)
5+
See the file 'doc/COPYING' for copying permission
6+
"""
7+
8+
import imp
9+
import sys
10+
11+
_sqlalchemy = None
12+
try:
13+
f, pathname, desc = imp.find_module("sqlalchemy", sys.path[1:])
14+
_sqlalchemy = imp.load_module("sqlalchemy", f, pathname, desc)
15+
except ImportError:
16+
pass
17+
18+
from lib.core.data import conf
19+
from lib.core.data import logger
20+
from lib.core.exception import SqlmapConnectionException
21+
from plugins.generic.connector import Connector as GenericConnector
22+
23+
class SQLAlchemy(GenericConnector):
24+
def __init__(self):
25+
GenericConnector.__init__(self)
26+
27+
def connect(self):
28+
self.initConnection()
29+
try:
30+
#_sqlalchemy.dialects.__all__
31+
if not self.port and self.db:
32+
if "///" not in conf.direct:
33+
conf.direct = conf.direct.replace("//", "///")
34+
engine = _sqlalchemy.create_engine(conf.direct, connect_args={'check_same_thread':False})
35+
self.connection = engine.connect()
36+
except _sqlalchemy.exc.OperationalError, msg:
37+
raise SqlmapConnectionException(msg[0])
38+
39+
self.connected()
40+
41+
def fetchall(self):
42+
try:
43+
return self.cursor.fetchall()
44+
except _sqlalchemy.exc.ProgrammingError, msg:
45+
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[1])
46+
return None
47+
48+
def execute(self, query):
49+
try:
50+
self.cursor = self.connection.execute(query)
51+
except (_sqlalchemy.exc.OperationalError, _sqlalchemy.exc.ProgrammingError), msg:
52+
logger.log(logging.WARN if conf.dbmsHandler else logging.DEBUG, "(remote) %s" % msg[1])
53+
except _sqlalchemy.exc.InternalError, msg:
54+
raise SqlmapConnectionException(msg[1])
55+
56+
def select(self, query):
57+
self.execute(query)
58+
return self.fetchall()

plugins/generic/connector.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,10 @@ def initCursor(self):
4646

4747
def close(self):
4848
try:
49-
self.cursor.close()
50-
self.connector.close()
49+
if self.cursor:
50+
self.cursor.close()
51+
if self.connector:
52+
self.connector.close()
5153
except Exception, msg:
5254
logger.debug(msg)
5355
finally:

0 commit comments

Comments
 (0)