mysql_mit_ssl_verschluesselten_verbindungen
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
| mysql_mit_ssl_verschluesselten_verbindungen [2020-08-21 13:08:23] – manfred | mysql_mit_ssl_verschluesselten_verbindungen [2026-03-02 15:58:52] (aktuell) – manfred | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | ====== MySQL mit SSL verschlüsselten Verbindungen ====== | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | > mysqlshow --ssl-mode=VERIFY_IDENTITY --ssl-key='/ | ||
| + | |||
| + | > echo "SHOW STATUS WHERE variable_name IN (' | ||
| + | +-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
| + | | Variable_name | ||
| + | +-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
| + | | Ssl_cipher | ||
| + | | Ssl_cipher_list | ECDHE-ECDSA-AES256-GCM-SHA384: | ||
| + | +-----------------+-------------------------------------------------------------------------------------------------------------------------------------------------------+ | ||
| + | |||
| + | |||
| + | === Ubuntu 16.04 === | ||
| + | |||
| + | Welche CIPHER es in openssl gibt, kann man beispielsweise so ermitteln: | ||
| + | > openssl ciphers -V TLSv1.2 | ||
| + | 0xC0,0x28 - ECDHE-RSA-AES256-SHA384 TLSv1.2 Kx=ECDH | ||
| + | 0x00,0x6B - DHE-RSA-AES256-SHA256 | ||
| + | 0x00,0x69 - DH-RSA-AES256-SHA256 | ||
| + | 0x00,0x6D - ADH-AES256-SHA256 | ||
| + | 0xC0,0x2A - ECDH-RSA-AES256-SHA384 | ||
| + | 0x00,0x3D - AES256-SHA256 | ||
| + | |||
| + | Leider können nicht immer alle openssl-CIPHER verwendet werden. | ||
| + | |||
| + | Beispielsweise funktionieren nur diese 6 Cipher auf "'' | ||
| + | AES128-SHA | ||
| + | AES256-SHA | ||
| + | DHE-RSA-AES128-SHA | ||
| + | DHE-RSA-AES256-SHA | ||
| + | ECDHE-RSA-AES128-SHA | ||
| + | ECDHE-RSA-AES256-SHA | ||
| + | |||
| + | |||
| + | === Ubuntu 18.04 === | ||
| + | |||
| + | < | ||
| + | # openssl version | ||
| + | OpenSSL 1.1.0h | ||
| + | |||
| + | # openssl ciphers --help | ||
| + | Usage: ciphers [options] | ||
| + | Valid options are: | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | |||
| + | # openssl ciphers -s | ||
| + | TLS_AES_256_GCM_SHA384: | ||
| + | |||
| + | # openssl ciphers -s | tr -s ':' | ||
| + | TLS_AES_256_GCM_SHA384 | ||
| + | TLS_CHACHA20_POLY1305_SHA256 | ||
| + | TLS_AES_128_GCM_SHA256 | ||
| + | ECDHE-ECDSA-AES256-GCM-SHA384 | ||
| + | ECDHE-RSA-AES256-GCM-SHA384 | ||
| + | DHE-RSA-AES256-GCM-SHA384 | ||
| + | ECDHE-ECDSA-CHACHA20-POLY1305 | ||
| + | ECDHE-RSA-CHACHA20-POLY1305 | ||
| + | DHE-RSA-CHACHA20-POLY1305 | ||
| + | ECDHE-ECDSA-AES128-GCM-SHA256 | ||
| + | ECDHE-RSA-AES128-GCM-SHA256 | ||
| + | DHE-RSA-AES128-GCM-SHA256 | ||
| + | ECDHE-ECDSA-AES256-SHA384 | ||
| + | ECDHE-RSA-AES256-SHA384 | ||
| + | DHE-RSA-AES256-SHA256 | ||
| + | ECDHE-ECDSA-AES128-SHA256 | ||
| + | ECDHE-RSA-AES128-SHA256 | ||
| + | DHE-RSA-AES128-SHA256 | ||
| + | ECDHE-ECDSA-AES256-SHA | ||
| + | ECDHE-RSA-AES256-SHA | ||
| + | DHE-RSA-AES256-SHA | ||
| + | ECDHE-ECDSA-AES128-SHA | ||
| + | ECDHE-RSA-AES128-SHA | ||
| + | DHE-RSA-AES128-SHA | ||
| + | AES256-GCM-SHA384 | ||
| + | AES128-GCM-SHA256 | ||
| + | AES256-SHA256 | ||
| + | AES128-SHA256 | ||
| + | AES256-SHA | ||
| + | AES128-SHA | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== SSL-Verschlüsselung auf dem Server aktivieren ===== | ||
| + | |||
| + | in einem separaten Terminal-Fenster den Status überwachen: | ||
| + | # watch -n1 'echo "SHOW SLAVE STATUS \G;" | mysql -hlocalhost -P3306 | egrep " | ||
| + | |||
| + | die Benutzer auf verschlüsselte Verbindungen umstellen: | ||
| + | # echo " | ||
| + | | ||
| + | # echo "ALTER USER ' | ||
| + | # echo "ALTER USER ' | ||
| + | | ||
| + | # echo " | ||
| + | |||
| + | die SSL-Verschlüsselung der Replikationsverbindungen muss durch **'' | ||
| + | # echo "STOP SLAVE;" | ||
| + | |||
| + | die Schlüssel und Zertifikate müssen an ihrem vorbestimmten Ort liegen: | ||
| + | # ll / | ||
| + | -rw-r--r-- 1 mysql mysql 1119 Mär 6 12:02 / | ||
| + | -rw------- 1 mysql mysql 1676 Jun 29 11:49 / | ||
| + | -rw-r--r-- 1 mysql mysql 452 Jun 29 11:49 / | ||
| + | -rw-r--r-- 1 mysql mysql 1119 Mär 6 11:58 / | ||
| + | -rw------- 1 mysql mysql 1675 Mär 6 11:58 / | ||
| + | |||
| + | Es ist aber auch möglich, die MySQL-Client-Konfiguration so anzupassen, das man sich die CLI-Parameter sparen kann: | ||
| + | # cat / | ||
| + | [mysqld] | ||
| + | ssl = 1 | ||
| + | # | ||
| + | ssl_ca | ||
| + | ssl_cert | ||
| + | ssl_key | ||
| + | ssl-cipher | ||
| + | require_secure_transport | ||
| + | |||
| + | # service mysql restart | ||
| + | |||
| + | jetzt kann jeder unterstützte Cipher verwendet werden: | ||
| + | # echo "SHOW VARIABLES LIKE ' | ||
| + | +---------------+-----------------+ | ||
| + | | Variable_name | Value | | ||
| + | +---------------+-----------------+ | ||
| + | | have_openssl | ||
| + | | have_ssl | ||
| + | | ssl_ca | ||
| + | | ssl_capath | ||
| + | | ssl_cert | ||
| + | | ssl_cipher | ||
| + | | ssl_crl | ||
| + | | ssl_crlpath | ||
| + | | ssl_key | ||
| + | +---------------+-----------------+ | ||
| + | |||
| + | jetzt können maximal nur die unten ausgewählten Cipher verwendet werden: | ||
| + | # echo "SHOW VARIABLES LIKE ' | ||
| + | +---------------+-------------------------------------------------------------------------------------------------------+ | ||
| + | | Variable_name | Value | | ||
| + | +---------------+-------------------------------------------------------------------------------------------------------+ | ||
| + | | have_openssl | ||
| + | | have_ssl | ||
| + | | ssl_ca | ||
| + | | ssl_capath | ||
| + | | ssl_cert | ||
| + | | ssl_cipher | ||
| + | | ssl_crl | ||
| + | | ssl_crlpath | ||
| + | | ssl_key | ||
| + | +---------------+-------------------------------------------------------------------------------------------------------+ | ||
| + | |||
| + | jetzt sind noch unverschlüsselte Verbindungen möglich: | ||
| + | # echo "SHOW VARIABLES LIKE ' | ||
| + | +--------------------------+-----------------------+ | ||
| + | | Variable_name | ||
| + | +--------------------------+-----------------------+ | ||
| + | | require_secure_transport | OFF | | ||
| + | | secure_auth | ||
| + | | secure_file_priv | ||
| + | +--------------------------+-----------------------+ | ||
| + | |||
| + | jetzt sind nur noch verschlüsselte Verbindungen möglich: | ||
| + | # echo "SHOW VARIABLES LIKE ' | ||
| + | +--------------------------+-----------------------+ | ||
| + | | Variable_name | ||
| + | +--------------------------+-----------------------+ | ||
| + | | require_secure_transport | ON | | ||
| + | | secure_auth | ||
| + | | secure_file_priv | ||
| + | +--------------------------+-----------------------+ | ||
| + | |||
| + | # echo "SHOW STATUS LIKE ' | ||
| + | +-----------------------+--------------------------+ | ||
| + | | Variable_name | ||
| + | +-----------------------+--------------------------+ | ||
| + | | Ssl_server_not_after | ||
| + | | Ssl_server_not_before | Mar 2 11:24:36 2020 GMT | | ||
| + | +-----------------------+--------------------------+ | ||
| + | |||
| + | |||
| + | ===== Mit dem MySQL-Client auf MySQL-Datenbanken über eine SSL/TLS verschlüsselte Verbindung zugreifen ===== | ||
| + | |||
| + | siehe auch: __Mit PHP [[:: | ||
| + | |||
| + | relevante PEM-Dateien auf dem Server-System, | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | |||
| + | benötigte PEM-Dateien auf dem Server-System in die MySQL-Konfiguration einbinden: | ||
| + | / | ||
| + | / | ||
| + | / | ||
| + | |||
| + | Der Aufruf mit Parametern auf der CLI: | ||
| + | > mysqlshow -uroot -pgeheim -hdbserver01 -P3306 --ssl-mode=required --tls-version=TLSv1.2 | ||
| + | > mysqlshow -uroot -pgeheim -hdbserver01 -P3306 --ssl-mode=required | ||
| + | +------------------------------------+ | ||
| + | | | ||
| + | +------------------------------------+ | ||
| + | | information_schema | ||
| + | | mysql | | ||
| + | | performance_schema | ||
| + | | sys | | ||
| + | +------------------------------------+ | ||
| + | |||
| + | Verbindungstest: | ||
| + | > mysqlshow -uroot -pgeheim -hdbserver01 -P3306 | ||
| + | +------------------------------------+ | ||
| + | | | ||
| + | +------------------------------------+ | ||
| + | | information_schema | ||
| + | | mysql | | ||
| + | | performance_schema | ||
| + | | sys | | ||
| + | +------------------------------------+ | ||
| + | |||
| + | |||
| + | ===== MySQL sein SSL-Zertifikat prüfen ===== | ||
| + | |||
| + | <code bash / | ||
| + | #!/bin/bash | ||
| + | |||
| + | # diese Methode ist universell und funktioniert bei MySQL und bei MariaDB; | ||
| + | |||
| + | # der Pfad zum Zertifikat wird aus der DB ausgelesen; | ||
| + | # die Informationen werden dann direkt aus der PEM-Datei gelesen; | ||
| + | |||
| + | MYSQL_LOGIN=" | ||
| + | # | ||
| + | |||
| + | ( | ||
| + | cd $(echo " | ||
| + | # | ||
| + | SSL_ZERTIFIKAT=" | ||
| + | |||
| + | BIS=" | ||
| + | echo " | ||
| + | echo " | ||
| + | echo " | ||
| + | openssl x509 -noout -subject -issuer -in " | ||
| + | ) | tr -s ' | ||
| + | echo | ||
| + | </ | ||
| + | |||
| + | <code py / | ||
| + | # | ||
| + | |||
| + | import glob | ||
| + | import subprocess | ||
| + | import datetime | ||
| + | import os | ||
| + | |||
| + | WARN_DAYS = 30 | ||
| + | CRIT_DAYS = 10 | ||
| + | |||
| + | now = datetime.datetime.now() | ||
| + | |||
| + | sockets = sorted(glob.glob("/ | ||
| + | |||
| + | for sock in sockets: | ||
| + | if not os.path.exists(sock): | ||
| + | continue | ||
| + | try: | ||
| + | ssl_cert = subprocess.check_output( | ||
| + | [" | ||
| + | input=" | ||
| + | text=True, | ||
| + | stderr=subprocess.DEVNULL | ||
| + | ).strip() | ||
| + | |||
| + | ssl_ca = subprocess.check_output( | ||
| + | [" | ||
| + | input=" | ||
| + | text=True, | ||
| + | stderr=subprocess.DEVNULL | ||
| + | ).strip() | ||
| + | |||
| + | if not ssl_cert.startswith("/" | ||
| + | datadir = subprocess.check_output( | ||
| + | [" | ||
| + | input=" | ||
| + | text=True, | ||
| + | stderr=subprocess.DEVNULL | ||
| + | ).strip() | ||
| + | |||
| + | cert_path = datadir + ssl_cert | ||
| + | else: | ||
| + | cert_path = ssl_cert | ||
| + | |||
| + | except Exception: | ||
| + | print(f" | ||
| + | continue | ||
| + | |||
| + | try: | ||
| + | out = subprocess.check_output( | ||
| + | [" | ||
| + | " | ||
| + | text=True, | ||
| + | stderr=subprocess.DEVNULL | ||
| + | ) | ||
| + | except Exception: | ||
| + | print(f" | ||
| + | continue | ||
| + | |||
| + | expiry = None | ||
| + | subject = "" | ||
| + | issuer = "" | ||
| + | |||
| + | for line in out.splitlines(): | ||
| + | if line.startswith(" | ||
| + | raw = line.split(" | ||
| + | expiry = datetime.datetime.strptime(raw, | ||
| + | |||
| + | elif line.startswith(" | ||
| + | subject = line.replace(" | ||
| + | |||
| + | elif line.startswith(" | ||
| + | issuer = line.replace(" | ||
| + | |||
| + | if not expiry: | ||
| + | print(f" | ||
| + | continue | ||
| + | |||
| + | # ----------- days_left -------------- | ||
| + | days_left = (expiry - now).days | ||
| + | |||
| + | if days_left < CRIT_DAYS: | ||
| + | status = 2 | ||
| + | elif days_left < WARN_DAYS: | ||
| + | status = 1 | ||
| + | else: | ||
| + | status = 0 | ||
| + | |||
| + | expiry_str = expiry.strftime(" | ||
| + | |||
| + | svc = f" | ||
| + | |||
| + | summary = f" | ||
| + | |||
| + | if subject: | ||
| + | summary += f" {subject};" | ||
| + | if issuer: | ||
| + | summary += f" {issuer};" | ||
| + | |||
| + | print(f" | ||
| + | </ | ||
| + | |||
