Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sqlmapproject
GitHub Repository: sqlmapproject/sqlmap
Path: blob/master/plugins/dbms/postgresql/connector.py
2992 views
1
#!/usr/bin/env python
2
3
"""
4
Copyright (c) 2006-2025 sqlmap developers (https://sqlmap.org)
5
See the file 'LICENSE' for copying permission
6
"""
7
8
try:
9
import psycopg2
10
import psycopg2.extensions
11
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
12
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)
13
except:
14
pass
15
16
from lib.core.common import getSafeExString
17
from lib.core.data import logger
18
from lib.core.exception import SqlmapConnectionException
19
from plugins.generic.connector import Connector as GenericConnector
20
21
class Connector(GenericConnector):
22
"""
23
Homepage: http://initd.org/psycopg/
24
User guide: http://initd.org/psycopg/docs/
25
API: http://initd.org/psycopg/docs/genindex.html
26
Debian package: python-psycopg2
27
License: GPL
28
29
Possible connectors: http://wiki.python.org/moin/PostgreSQL
30
"""
31
32
def connect(self):
33
self.initConnection()
34
35
try:
36
self.connector = psycopg2.connect(host=self.hostname, user=self.user, password=self.password, database=self.db, port=self.port)
37
except (psycopg2.OperationalError, UnicodeDecodeError) as ex:
38
raise SqlmapConnectionException(getSafeExString(ex))
39
40
self.connector.set_client_encoding('UNICODE')
41
42
self.initCursor()
43
self.printConnected()
44
45
def fetchall(self):
46
try:
47
return self.cursor.fetchall()
48
except psycopg2.ProgrammingError as ex:
49
logger.warning(getSafeExString(ex))
50
return None
51
52
def execute(self, query):
53
retVal = False
54
55
try:
56
self.cursor.execute(query)
57
retVal = True
58
except (psycopg2.OperationalError, psycopg2.ProgrammingError) as ex:
59
logger.warning(("(remote) '%s'" % getSafeExString(ex)).strip())
60
except psycopg2.InternalError as ex:
61
raise SqlmapConnectionException(getSafeExString(ex))
62
63
self.connector.commit()
64
65
return retVal
66
67
def select(self, query):
68
retVal = None
69
70
if self.execute(query):
71
retVal = self.fetchall()
72
73
return retVal
74
75