2013-02-14 15:32:17 +04:00
|
|
|
#!/usr/bin/env python
|
2010-03-27 02:23:25 +03:00
|
|
|
|
|
|
|
"""
|
2016-01-06 02:06:12 +03:00
|
|
|
Copyright (c) 2006-2016 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
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
import psycopg2
|
2010-05-29 15:46:41 +04:00
|
|
|
import psycopg2.extensions
|
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
|
|
|
|
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
|
2012-12-06 13:21:53 +04:00
|
|
|
except ImportError:
|
2010-03-27 02:23:25 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
from lib.core.data import logger
|
2012-12-06 17:14:19 +04:00
|
|
|
from lib.core.exception import SqlmapConnectionException
|
2010-03-27 02:23:25 +03:00
|
|
|
from plugins.generic.connector import Connector as GenericConnector
|
|
|
|
|
|
|
|
class Connector(GenericConnector):
|
|
|
|
"""
|
|
|
|
Homepage: http://initd.org/psycopg/
|
|
|
|
User guide: http://initd.org/psycopg/docs/
|
|
|
|
API: http://initd.org/psycopg/docs/genindex.html
|
|
|
|
Debian package: python-psycopg2
|
|
|
|
License: GPL
|
|
|
|
|
|
|
|
Possible connectors: http://wiki.python.org/moin/PostgreSQL
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
GenericConnector.__init__(self)
|
|
|
|
|
2010-03-31 14:50:47 +04:00
|
|
|
def connect(self):
|
2010-03-27 02:23:25 +03:00
|
|
|
self.initConnection()
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.connector = psycopg2.connect(host=self.hostname, user=self.user, password=self.password, database=self.db, port=self.port)
|
|
|
|
except psycopg2.OperationalError, msg:
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapConnectionException(msg)
|
2010-03-27 02:23:25 +03:00
|
|
|
|
2010-05-29 15:46:41 +04:00
|
|
|
self.connector.set_client_encoding('UNICODE')
|
|
|
|
|
2013-01-18 14:21:23 +04:00
|
|
|
self.initCursor()
|
2013-04-15 16:31:27 +04:00
|
|
|
self.printConnected()
|
2010-03-27 02:23:25 +03:00
|
|
|
|
|
|
|
def fetchall(self):
|
2010-04-06 19:12:52 +04:00
|
|
|
try:
|
|
|
|
return self.cursor.fetchall()
|
|
|
|
except psycopg2.ProgrammingError, msg:
|
2010-11-08 01:14:06 +03:00
|
|
|
logger.warn(msg)
|
2010-04-06 19:12:52 +04:00
|
|
|
return None
|
2010-03-27 02:23:25 +03:00
|
|
|
|
|
|
|
def execute(self, query):
|
2012-01-13 18:10:53 +04:00
|
|
|
retVal = False
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
try:
|
|
|
|
self.cursor.execute(query)
|
2012-01-13 18:10:53 +04:00
|
|
|
retVal = True
|
2010-03-27 02:23:25 +03:00
|
|
|
except (psycopg2.OperationalError, psycopg2.ProgrammingError), msg:
|
2012-02-09 14:16:58 +04:00
|
|
|
logger.warn(("(remote) %s" % msg).strip())
|
2010-03-27 02:23:25 +03:00
|
|
|
except psycopg2.InternalError, msg:
|
2013-01-04 02:20:55 +04:00
|
|
|
raise SqlmapConnectionException(msg)
|
2010-03-27 02:23:25 +03:00
|
|
|
|
|
|
|
self.connector.commit()
|
|
|
|
|
2012-01-13 18:10:53 +04:00
|
|
|
return retVal
|
|
|
|
|
2010-03-27 02:23:25 +03:00
|
|
|
def select(self, query):
|
2012-01-13 18:10:53 +04:00
|
|
|
retVal = None
|
|
|
|
|
|
|
|
if self.execute(query):
|
|
|
|
retVal = self.fetchall()
|
|
|
|
|
|
|
|
return retVal
|