Build a Python Certificate Transparency Monitor That Spots Fake Certs for Your Domain


Here is a fact most website owners miss: every time anyone issues a TLS certificate for your domain — legitimate or not — it gets published in a public Certificate Transparency log within minutes. Attackers who want to impersonate your site need a certificate, and that certificate leaves a public paper trail. A small Python script can watch that trail for you.

Why This Matters

Phishing sites live or die by looking real. A fake login page for your bank or your company needs a valid certificate to show the padlock. Certificate Transparency (CT) logs record every certificate issued by public authorities, which means you can see a fake certificate for your domain the day it appears — often days before the phishing site goes live. Big companies run this monitoring constantly. You can run it from a ten-line script.

The Code

import json, sys, time, urllib.request

def fetch_certs(domain):
    url = f"https://crt.sh/?q=%25.{domain}&output=json"
    req = urllib.request.Request(url, headers={"User-Agent": "ct-monitor/1.0"})
    with urllib.request.urlopen(req, timeout=30) as r:
        return json.load(r)

def new_certs(domain, seen_file="seen_certs.txt"):
    try:
        with open(seen_file) as f:
            seen = set(f.read().splitlines())
    except FileNotFoundError:
        seen = set()

    certs = fetch_certs(domain)
    fresh = []
    for c in certs:
        cert_id = str(c["id"])
        if cert_id not in seen:
            fresh.append(c)
            seen.add(cert_id)

    with open(seen_file, "w") as f:
        f.write("\n".join(sorted(seen)))
    return fresh

if __name__ == "__main__":
    domain = sys.argv[1] if len(sys.argv) > 1 else "example.com"
    for cert in new_certs(domain):
        print(f"NEW CERT: {cert['common_name']} | "
              f"issuer: {cert['issuer_name']} | "
              f"issued: {cert['not_before']}")
    print("Check complete.")

Save it as ct_monitor.py and run python3 ct_monitor.py yourdomain.com. The first run records every certificate it sees; later runs only print new ones.

How It Works

  1. crt.sh is a free public search engine over Certificate Transparency logs. The query %25.yourdomain.com means "any subdomain of yourdomain.com".
  2. The seen-file trick stores certificate IDs in a text file, so each run only reports certificates that appeared since the last check.
  3. The fields that matter: common_name (the domain on the certificate), issuer_name (which authority issued it), and not_before (when it was issued). An issuer you have never used, or a subdomain you never created, is your red flag.

What to Do When You Spot One

  • Verify first. Check whether your team, your hosting provider, or a CDN issued it legitimately. Certificates from Let's Encrypt for subdomains you actually use are normal.
  • Look for lookalikes. A certificate for secure-yourbank-login.com or yourcompany-support.net is a phishing setup in progress. Report it to the issuing authority and to the registrar.
  • Act fast. A phishing site is most dangerous in its first hours, before blocklists catch it. Early detection is the whole point of this script.

Level Up

  • Run the script on a cron job once a day: 0 9 * * * python3 /path/to/ct_monitor.py yourdomain.com.
  • Send alerts to yourself with a simple email or Telegram message instead of printing to the terminal.
  • Monitor your employer's domain too — with permission. Catching a rogue certificate early is exactly the kind of thing security teams value.

You just built a threat-intelligence tool with the standard library alone. That is the whole idea: the best defense is often just paying attention, automatically.

Comments

Popular Posts