Build a Python TLS Cipher Checker: Find Weak Ciphers on Your Own Servers


TLS is supposed to protect your traffic, but a server that still accepts old, broken ciphers is only as strong as its weakest one. An attacker who can force a connection down to a weak cipher can decrypt or tamper with traffic that looks encrypted. The fix starts with knowing what your server actually accepts — and a short Python script can tell you in seconds.

Why Weak Ciphers Still Exist

Servers support old ciphers for compatibility: some ancient client somewhere might still need them. The problem is that "might need" becomes "always enabled", and ciphers like RC4, 3DES and export-grade 40-bit encryption stay switched on for years after they were broken. RC4 was shown to be practically breakable in 2013. 3DES falls to the Sweet32 attack. TLS 1.0 and 1.1 are deprecated. Yet scans of the internet still find servers offering all of them.

The Script

Python's built-in ssl module can try a handshake with a specific cipher list and report whether the server accepts it. Save this as tls_cipher_check.py:

import socket
import ssl
import sys

# Cipher groups to test: (name, cipher string, max TLS version or None)
CIPHER_GROUPS = [
    ("NULL / export (broken)", "NULL:eNULL:EXP:LOW", None),
    ("RC4 (broken)", "RC4", None),
    ("3DES (weak - Sweet32)", "DES-CBC3-SHA", None),
    ("TLS 1.0 / 1.1 (deprecated)", "HIGH", ssl.TLSVersion.TLSv1_1),
    ("TLS 1.2 strong", "HIGH:!aNULL:!eNULL:!RC4:!3DES", None),
    ("TLS 1.3 only", "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256", None),
]

def test_group(host, port, cipher_string, max_version=None):
    ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    if max_version is not None:
        ctx.maximum_version = max_version
    try:
        ctx.set_ciphers(cipher_string)
        with socket.create_connection((host, port), timeout=5) as sock:
            with ctx.wrap_socket(sock, server_hostname=host) as ssock:
                cipher = ssock.cipher()
                version = ssock.version()
                return True, f"{version} with {cipher[0]}"
    except ssl.SSLError:
        return False, "rejected"
    except Exception as e:
        return False, f"error: {e}"

def main():
    host = sys.argv[1]
    port = int(sys.argv[2]) if len(sys.argv) > 2 else 443
    print(f"Checking {host}:{port}\n")
    problems = 0
    for name, ciphers, max_version in CIPHER_GROUPS:
        accepted, detail = test_group(host, port, ciphers, max_version)
        is_weak = any(w in name.lower()
                      for w in ("broken", "weak", "deprecated"))
        if accepted and is_weak:
            status = "ACCEPTED - FIX THIS"
            problems += 1
        elif accepted:
            status = "accepted"
        else:
            status = "rejected"
        print(f"[{status}] {name}: {detail}")
    print(f"\n{problems} weak cipher group(s) accepted." if problems
          else "\nNo weak ciphers accepted. Good.")

if __name__ == "__main__":
    main()

Run it against your own server:

python3 tls_cipher_check.py example.com 443

Reading the Results

The output tells you exactly where you stand. Any line marked "ACCEPTED - FIX THIS" is a real problem: your server will negotiate a broken cipher if a client asks for one, and an attacker can force that downgrade. A server that rejects RC4, 3DES and TLS 1.0/1.1 but accepts strong TLS 1.2 and 1.3 ciphers is in good shape.

The script uses CERT_NONE because it is testing cipher support, not certificate validity — check certificates separately with a proper validator. And it only tests your own servers: scanning other people's servers without permission is not your job.

Fixing What You Find

On nginx, restrict the protocol and ciphers in your server block:

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;

On Apache:

SSLProtocol -all +TLSv1.2 +TLSv1.3
SSLCipherSuite ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384

Then restart the service and run the script again. The weak groups should all show "rejected". Add the script to a weekly cron job and you will catch the next misconfiguration the moment someone re-enables a legacy cipher for "compatibility".

Remember the golden rule: only test systems you own or have written permission to test. Point this script at your own servers, fix what it finds, and your TLS will actually be as strong as it looks.

 

Comments

Popular Posts