Path: blob/master/plugins/dbms/postgresql/connector.py
2992 views
#!/usr/bin/env python12"""3Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)4See the file 'LICENSE' for copying permission5"""67try:8import psycopg29import psycopg2.extensions10psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)11psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)12except:13pass1415from lib.core.common import getSafeExString16from lib.core.data import logger17from lib.core.exception import SqlmapConnectionException18from plugins.generic.connector import Connector as GenericConnector1920class Connector(GenericConnector):21"""22Homepage: http://initd.org/psycopg/23User guide: http://initd.org/psycopg/docs/24API: http://initd.org/psycopg/docs/genindex.html25Debian package: python-psycopg226License: GPL2728Possible connectors: http://wiki.python.org/moin/PostgreSQL29"""3031def connect(self):32self.initConnection()3334try:35self.connector = psycopg2.connect(host=self.hostname, user=self.user, password=self.password, database=self.db, port=self.port)36except (psycopg2.OperationalError, UnicodeDecodeError) as ex:37raise SqlmapConnectionException(getSafeExString(ex))3839self.connector.set_client_encoding('UNICODE')4041self.initCursor()42self.printConnected()4344def fetchall(self):45try:46return self.cursor.fetchall()47except psycopg2.ProgrammingError as ex:48logger.warning(getSafeExString(ex))49return None5051def execute(self, query):52retVal = False5354try:55self.cursor.execute(query)56retVal = True57except (psycopg2.OperationalError, psycopg2.ProgrammingError) as ex:58logger.warning(("(remote) '%s'" % getSafeExString(ex)).strip())59except psycopg2.InternalError as ex:60raise SqlmapConnectionException(getSafeExString(ex))6162self.connector.commit()6364return retVal6566def select(self, query):67retVal = None6869if self.execute(query):70retVal = self.fetchall()7172return retVal737475