Discovery Scan - The certificate chain was issued by an authority that is not trusted
Symptom
When we do a discovery scan against MS SQL Servers in the environment, we got errors in the log:
ERROR - Failed to initialize connection with target QAWINDSQL Traceback (most recent call last): File "scan_logic\db_scan.py", line 50, in db_initialize_connection pyodbc.OperationalError: ('08001', '[08001] [Microsoft][ODBC Driver 18 for SQL Server]SSL Provider: The certificate chain was issued by an authority that is not trusted.\r\n (-2146893019) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 18 for SQL Server]Invalid connection string attribute (0); [08001] [Microsoft][ODBC Driver 18 for SQL Server]Client unable to establish connection. For solutions related to encryption errors, see https://go.microsoft.com/fwlink/?linkid=2226722 (-2146893019)')
ERROR - Failed to query the database for target
Environment
Privilege Cloud Discovery
Discovery (New)
Cause
The DB has to have a certificate that is signed by CA
When an application connects to SQL Server (or any database) over ODBC, the data (including usernames, passwords, and queries) would otherwise travel across the network in plain text. Enabling SSL/TLS mode in ODBC ensures the connection is encrypted end-to-end, so sensitive data cannot be intercepted or modified in transit.
It also allows the client to validate the server’s identity using its certificate, which protects against man-in-the-middle attacks. In short, SSL mode in ODBC provides confidentiality, integrity, and authentication for database connections.
If the client does not have a certificate or the certificate is not recognized by the client (the connector machine which the scan runs from), the above error will throw.
Resolution
The client needs to make sure that the DB has a certificate that is signed by CA. When a SQL Server uses a certificate that is signed by a trusted Certificate Authority (CA), most clients already have that CA’s root certificate in their operating system’s trust store. This means the client can automatically verify the server’s identity during the SSL/TLS handshake without manually installing the server’s certificate. As long as the certificate matches the server’s hostname and is within its validity period, the connection will be encrypted and trusted by default.
Another option, if the SQL Server uses a self-signed certificate, there is no trusted CA to vouch for it. In this case, clients will not recognize the certificate automatically. To establish trust, the server’s certificate (or its own self-signed root) must be manually installed into the client’s trust store. Without this step, the connection will fail validation.
In our testing we use self signed certificates, here is how to set this up (ONLY FOR TESTING NOT FOR CLIENTS):
- Connect to the machine the DB is located on
- Run the following command (make sure to replace db-hostname with your relevant db host name and db-ip with the ip of the db):
- sudo openssl req -x509 -newkey rsa:2048 -nodes -days 825 \
-keyout /var/opt/mssql/security/sql.key \
-out /var/opt/mssql/security/sql.crt \
-subj "/CN=<db-hostname>" \
-addext "subjectAltName=DNS:<db-hostname>,IP:<db-ip>" \
-addext "keyUsage=digitalSignature,keyEncipherment" \
-addext "extendedKeyUsage=serverAuth" - copy the certificate to a tmp folder in order to download it to you machine and copy it to the connector machine
-
sudo cp /var/opt/mssql/security/sql.crt /tmp/sql.crt
-
- sudo chown mssql:mssql /var/opt/mssql/security/sql.*
sudo chmod 600 /var/opt/mssql/security/sql.key
sudo chmod 644 /var/opt/mssql/security/sql.crt - sudo /opt/mssql/bin/mssql-conf set network.tlscert /var/opt/mssql/security/sql.crt
sudo /opt/mssql/bin/mssql-conf set network.tlskey /var/opt/mssql/security/sql.key
sudo systemctl restart mssql-server
- sudo openssl req -x509 -newkey rsa:2048 -nodes -days 825 \
- The certificate should be copied to the connector machine and installed there
- From terminal in mac run the command:
scp -i "db-plugin-key.pem" db-hostname:/tmp/sql.crt ~/Downloads/sql.crt
- copy the sql.crt certificate from the download folder to the connector machine which the scan runs from
- double click on the certificate and chose install
- From terminal in mac run the command: