Code clean up

This commit is contained in:
Shreya 2020-11-21 13:19:44 +05:30
parent 6cb9d4808f
commit e8a2a37690
2 changed files with 48 additions and 41 deletions

View File

@ -1,7 +1,3 @@
"""
Implementation from https://github.com/SecuraBV/CVE-2020-1472
"""
import logging
import psycopg2
@ -18,8 +14,8 @@ class PostgreSQLFinger(HostFinger):
# Class related consts
_SCANNED_SERVICE = 'PostgreSQL'
POSTGRESQL_DEFAULT_PORT = 5432
CREDS = {'username': 'monkeySaysHello',
'password': 'monkeySaysXXX'}
CREDS = {'username': "monkeySaysHello",
'password': "monkeySaysXXX"}
def get_host_fingerprint(self, host):
try:
@ -31,6 +27,7 @@ class PostgreSQLFinger(HostFinger):
except psycopg2.OperationalError as ex:
# try block will throw an OperationalError since the credentials are wrong, which we then analyze
try:
self.relevant_ex_substrings = ["password authentication failed",
"entry for host"] # "no pg_hba.conf entry for host" but filename may be diff
exception_string = str(ex)
@ -39,43 +36,52 @@ class PostgreSQLFinger(HostFinger):
# OperationalError due to some other reason
return False
# all's well; start analysing errors
self.init_service(host.services, self._SCANNED_SERVICE, self.POSTGRESQL_DEFAULT_PORT)
ssl_connection_details = []
exceptions = exception_string.split("\n")
ssl_conf_on_server = self.is_ssl_configured(exceptions)
connection_details = {'ssl_conf': "SSL is configured on the PostgreSQL server.\n",
'ssl_not_conf': "SSL is NOT configured on the PostgreSQL server.\n",
'all_ssl': "SSL connections can be made by all.\n",
'all_non_ssl': "Non-SSL connections can be made by all.\n",
'selected_ssl': "SSL connections can be made by selected hosts only OR "
"non-SSL usage is forced.\n",
'selected_non_ssl': "Non-SSL connections can be made by selected hosts only OR "
"SSL usage is forced.\n"}
""" Make this part cleaner and better! """
ssl_connection_details = []
ssl_conf_on_server = self.is_ssl_configured(exceptions)
# SSL configured
if ssl_conf_on_server:
ssl_connection_details.append("SSL is configured on the PostgreSQL server.\n")
ssl_connection_details.append(connection_details['ssl_conf'])
# SSL
if self.found_entry_for_host_but_pwd_auth_failed(exceptions[0]):
ssl_connection_details.append("SSL connections can be made by all.\n")
ssl_connection_details.append(connection_details['all_ssl'])
else:
ssl_connection_details.append(
"SSL connections can be made by selected hosts only OR non-SSL usage is forced.\n")
ssl_connection_details.append(connection_details['selected_ssl'])
# non-SSL
if self.found_entry_for_host_but_pwd_auth_failed(exceptions[1]):
ssl_connection_details.append("Non-SSL connections can be made by all.\n")
ssl_connection_details.append(connection_details['all_non_ssl'])
else:
ssl_connection_details.append(
"Non-SSL connections can be made by selected hosts only OR SSL usage is forced.\n")
ssl_connection_details.append(connection_details['selected_non_ssl'])
# SSL not configured
else:
ssl_connection_details.append("SSL is NOT configured on the PostgreSQL server.\n")
ssl_connection_details.append(connection_details['ssl_not_conf'])
if self.found_entry_for_host_but_pwd_auth_failed(exceptions[0]):
ssl_connection_details.append("Non-SSL connections can be made by all.\n")
ssl_connection_details.append(connection_details['all_non_ssl'])
else:
ssl_connection_details.append(
"Non-SSL connections can be made by selected hosts only OR SSL usage is forced.\n")
ssl_connection_details.append(connection_details['selected_non_ssl'])
host.services[self._SCANNED_SERVICE]['communication_encryption_details'] = ''.join(ssl_connection_details)
return True
except Exception as err:
LOG.debug("Error getting PostgreSQL fingerprint: %s", err)
return False
def is_ssl_configured(self, exceptions):
# when trying to authenticate, it checks pg_hba.conf file:
# first, for a record where it can connect with SSL and second, without SSL

View File

@ -8,6 +8,7 @@ netifaces>=0.10.9
odict==1.7.0
paramiko>=2.7.1
psutil>=5.7.0
psycopg2-binary==2.8.6
pycryptodome==3.9.8
pyftpdlib==1.5.6
pymssql<3.0