Cert Spotter

repository·master·Indexed 22 days ago

https://github.com/sslmate/certspotter

An open-source Certificate Transparency (CT) log monitor that alerts users when SSL/TLS certificates are issued for their domains. It includes the certspotter daemon for monitoring and the certspotter-authorize tool to suppress notifications for known legitimate certificates. Built in Go, it features robust encoding error handling and supports custom hooks for certificate detection.

Tokens
12K
Snippets
37
Records
49
Agent score
75%

What's inside certspotter

  1. How to determine if a certificate is authorized

    master

    When writing a hook script to filter out legitimate certificates, avoid using fingerprints, serial numbers, Subject Key Identifiers (SKI), or Issuer fields, as these can be spoofed or vary between precertificates and certificates.

    Instead, use one of these two methods:

    1. Compare TBS Hash: Compare the $TBS_SHA256 value against a list of authorized TBS hashes. The TBS hash is computed over the DER encoding of the TBSCertificate with any SCT extension removed. You can compute this using certspotter-authorize(8) -printhash.

    2. Compare Public Key Hash: Compare the $PUBKEY_SHA256 value against a list of authorized public key hashes. You can compute a public key hash manually using:

    openssl x509 -pubkey -noout | openssl pkey -pubin -outform DER | openssl sha256
  2. How to handle existing certificates and log resumption

    master

    Cert Spotter monitors logs continuously. Its behavior depends on whether it has previously monitored a log:

    Resuming Monitoring

    If Cert Spotter has previously monitored a log, it resumes from its last recorded position in $CERTSPOTTER_STATE_DIR/logs.

    Warning: If you add a new domain to your watchlist, Cert Spotter will not detect certificates that were logged before the domain was added.

    Detecting Pre-existing Certificates

    To force Cert Spotter to detect certificates that were logged before you added them to the watchlist, you must delete the contents of $CERTSPOTTER_STATE_DIR/logs. This causes Cert Spotter to restart monitoring from the very beginning of each log.

    Note: Restarting from the beginning can take days as it involves downloading hundreds of millions of certificates. For faster historical searches, use the Cert Spotter hosted service, the SSLMate CT Search API, or crt.sh.

  3. How Certspotter handles the watchlist

    master

    The watchlist defines the domain names that Certspotter will monitor in the CT logs.

    • File-based: Use the -watchlist <path> flag to point to a file. If no flag is provided, it defaults to ~/.certspotter/watchlist.
    • Stdin-based: Use -watchlist - to read the list of domains from standard input.
    • Requirement: A watchlist must be provided; if it is empty or not found, the program will exit with code 2.
    # Using a file
    ./certspotter -watchlist my_domains.txt
    
    # Using stdin
    cat my_domains.txt | ./certspotter -watchlist -
  4. Configure and use certspotter-script hooks

    master

    A certspotter-script is a program executed by certspotter(8) to notify you of events (like discovering a certificate for a watched domain).

    To use hooks, you can either:

    1. Place scripts in the $CERTSPOTTER_CONFIG_DIR/hooks.d directory (defaults to ~/.certspotter/hooks.d).
    2. Specify a script explicitly using the -script command-line argument.

    When a hook is triggered, certspotter sets several environment variables that your script can use to process the event.

    # Example of specifying a script via CLI
    certspotter -script /path/to/your/script.sh
  5. Set up notifications for matching certificates

    master

    Cert Spotter can notify you via email, scripts, or stdout when a matching certificate is discovered or an error occurs.

    Email Notifications

    • Command line: Use the -email ADDRESS flag. You can specify this multiple times for multiple recipients.
    • Configuration file: Any address listed in $CERTSPOTTER_CONFIG_DIR/email_recipients (defaults to ~/.certspotter/email_recipients) will be emailed. List one address per line.
    • Requirement: Your system must have a working sendmail(1) command.
    • Environment: Use the EMAIL environment variable to specify the sender address.

    Script Notifications

    • Command line: Use the -script COMMAND flag to execute a specific command.
    • Hooks directory: Cert Spotter automatically executes every executable file found in the $CERTSPOTTER_CONFIG_DIR/hooks.d directory (defaults to ~/.certspotter/hooks.d).

    Standard Output

    • Use the -stdout flag to write matching certificates and errors to stdout.
  6. Install and set up Cert Spotter

    master

    Cert Spotter is a Certificate Transparency log monitor that alerts you when an SSL/TLS certificate is issued for your domains. It requires Go version 1.21 or higher.

    Installation

    Install the certspotter command using Go:

    go install software.sslmate.com/src/certspotter/cmd/certspotter@latest

    Configuration

    Cert Spotter uses specific files in the $HOME/.certspotter/ directory for configuration:

    1. Watch List: Create $HOME/.certspotter/watchlist containing the DNS names you want to monitor, one per line.
      • To monitor an entire domain tree (the domain and all sub-domains), prefix the name with a dot (e.g., .example.com).
      • To monitor a single DNS name only, do not use a prefix.
    2. Email Recipients: Place one or more email addresses in $HOME/.certspotter/email_recipients (one per line). This requires a working sendmail command on your system.
    3. Hooks: Place executable scripts in the $HOME/.certspotter/hooks.d directory. These scripts will be executed when a certificate for a domain on your watch list is detected.

    Running as a Daemon

    Configure your system to run certspotter as a daemon. To save bandwidth and avoid being notified about certificates logged before you started monitoring, use the -start_at_end flag to start monitoring new logs from the end instead of the beginning.

  7. Authorize certificates with certspotter-authorize

    master

    Use certspotter-authorize to preemptively authorize certificates. This prevents certspotter(8) from sending notifications when these certificates (or their corresponding precertificates) are discovered in Certificate Transparency logs. This is ideal for integration into certificate issuance pipelines to prevent false alarms.

    How it works: certspotter-authorize computes the SHA-256 hash of the certificate's TBSCertificate (as per RFC 6962 Section 3.2). It then creates an empty .notified marker file in the certspotter state directory. When certspotter encounters a certificate with the same TBSCertificate hash, it will skip the notification.

    Important Note: Because it uses the TBSCertificate hash, authorizing a certificate also suppresses notifications for its precertificate. However, any changes to the certificate (different serial numbers, validity periods, etc.) will result in a different hash and will trigger a notification.

    # Authorize a single certificate from a file
    certspotter-authorize /path/to/cert.pem
    
    # Authorize multiple certificates
    certspotter-authorize cert-a.pem cert-b.pem
    
    # Authorize a certificate via stdin
    cat cert.pem | certspotter-authorize -
  8. Authorize known certificates with certspotter-authorize

    master

    To prevent false alarms, you can use certspotter-authorize to tell Cert Spotter about legitimate certificates issued by your certificate authority. Once authorized, Cert Spotter will not notify you when it discovers these certificates (or their corresponding precertificates) in Certificate Transparency logs.

    Installation

    go install software.sslmate.com/src/certspotter/cmd/certspotter-authorize@latest

    Usage

    Run the command against your certificate PEM file:

    certspotter-authorize /path/to/cert.pem
  9. Configure notification methods

    master

    Certspotter requires at least one way to notify you when a matching certificate is found. You can configure these via CLI flags or configuration files:

    1. Email:
      • Use the -email <address> flag (can be used multiple times).
      • Or, place email addresses (one per line) in the email_recipients file located in your configuration directory.
    2. Scripts:
      • Use the -script <path> flag to execute a specific program.
      • Or, place executable scripts in the hooks.d directory within your configuration directory.
    3. Stdout:
      • Use the -stdout flag to print matching certificates directly to the standard output.
  10. Configure the Cert Spotter watchlist

    master

    Cert Spotter monitors DNS names provided in a watchlist file.

    • Format: One DNS name per line.
    • Wildcard/Namespace monitoring: To monitor a domain and all its sub-domains, prefix the name with a dot (e.g., .example.com).
    • Single name monitoring: To monitor only a specific DNS name, do not use a prefix (e.g., example.com).
    • Input methods:
      • Use the -watchlist PATH flag to specify a file.
      • Use - to read the watchlist from stdin.
      • By default, it reads from $CERTSPOTTER_CONFIG_DIR/watchlist (defaults to ~/.certspotter/watchlist).

    Note: The watchlist is only read at startup. You must restart certspotter to apply changes.

    # Example watchlist file content
    .example.com
    api.example.com
    standalone.com
  11. Configure the state directory for certspotter-authorize

    master

    The certspotter-authorize utility must use the same state directory as the main certspotter(8) process to function correctly. You can specify this directory in two ways:

    1. Environment Variable: Set CERTSPOTTER_STATE_DIR to the desired path.
    2. CLI Flag: Use the -state_dir flag to override the environment variable.

    If neither is provided, it defaults to ~/.certspotter.

    # Using the environment variable
    export CERTSPOTTER_STATE_DIR=/var/lib/certspotter
    certspotter-authorize cert.pem
    
    # Using the CLI flag (overrides environment variable)
    certspotter-authorize -state_dir /var/lib/certspotter cert.pem
  12. Configure directory locations via environment variables

    master

    Certspotter uses several directories for state, configuration, and caching. You can override the default locations using the following environment variables:

    Environment VariablePurpose
    CERTSPOTTER_STATE_DIRDirectory for storing log position and discovered certificates
    STATE_DIRECTORYAlternative for state directory (used if started by a supervisor like systemd)
    CERTSPOTTER_CONFIG_DIRDirectory for configuration files (watchlist, email_recipients, hooks.d)
    CONFIGURATION_DIRECTORYAlternative for config directory (used if started by a supervisor)
    CERTSPOTTER_CACHE_DIRDirectory for caching data
    CACHE_DIRECTORYAlternative for cache directory (used if started by a supervisor)