|
| 1 | +/* |
| 2 | +·━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━· |
| 3 | +: : |
| 4 | +: █▀ █ █▀▀ · Blazing-fast pentesting suite : |
| 5 | +: ▄█ █ █▀ · BSD 3-Clause License : |
| 6 | +: : |
| 7 | +: (c) 2022-2026 vmfunc, xyzeva, : |
| 8 | +: lunchcat alumni & contributors : |
| 9 | +: : |
| 10 | +·━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━· |
| 11 | +*/ |
| 12 | + |
| 13 | +package scan |
| 14 | + |
| 15 | +import ( |
| 16 | + "bytes" |
| 17 | + "crypto/tls" |
| 18 | + "crypto/x509" |
| 19 | + "fmt" |
| 20 | + "net" |
| 21 | + "net/url" |
| 22 | + "strconv" |
| 23 | + "time" |
| 24 | + |
| 25 | + "github.com/vmfunc/sif/internal/logger" |
| 26 | + "github.com/vmfunc/sif/internal/output" |
| 27 | +) |
| 28 | + |
| 29 | +// tlsCertExpirySoonWindow flags a leaf certificate as "expiring soon" inside |
| 30 | +// this window, mirroring the threshold nmap's ssl-cert and testssl.sh use. |
| 31 | +const tlsCertExpirySoonWindow = 30 * 24 * time.Hour |
| 32 | + |
| 33 | +// TLSCertResult holds what the target's leaf certificate reveals: extra |
| 34 | +// hostnames from the SAN list, issuer/validity metadata, and posture flags a |
| 35 | +// human would want surfaced without reading the cert by hand. |
| 36 | +type TLSCertResult struct { |
| 37 | + Subject string `json:"subject"` |
| 38 | + Issuer string `json:"issuer"` |
| 39 | + SANs []string `json:"sans"` |
| 40 | + NotBefore string `json:"not_before"` |
| 41 | + NotAfter string `json:"not_after"` |
| 42 | + SerialNumber string `json:"serial_number"` |
| 43 | + SelfSigned bool `json:"self_signed"` |
| 44 | + Expired bool `json:"expired"` |
| 45 | + ExpiringSoon bool `json:"expiring_soon"` |
| 46 | + Wildcard bool `json:"wildcard"` |
| 47 | + NewSubdomains []string `json:"new_subdomains"` // SANs not already known to be the target host |
| 48 | + ChainLength int `json:"chain_length"` |
| 49 | +} |
| 50 | + |
| 51 | +func (r *TLSCertResult) ResultType() string { return "tlscert" } |
| 52 | + |
| 53 | +var _ ScanResult = (*TLSCertResult)(nil) |
| 54 | + |
| 55 | +// tlsDial is a var so tests can substitute a fake dialer without touching the |
| 56 | +// network. |
| 57 | +var tlsDial = func(addr string, timeout time.Duration) (*tls.Conn, error) { |
| 58 | + dialer := &net.Dialer{Timeout: timeout} |
| 59 | + // InsecureSkipVerify is deliberate: this module mines whatever certificate |
| 60 | + // the target presents, self-signed or expired included, rather than only |
| 61 | + // certs that pass validation like the main http client requires. |
| 62 | + return tls.DialWithDialer(dialer, "tcp", addr, &tls.Config{InsecureSkipVerify: true}) //nolint:gosec // recon target, not a trust decision |
| 63 | +} |
| 64 | + |
| 65 | +// TLSCert connects to the target on the given port (443 when unset) and mines |
| 66 | +// its leaf certificate: SANs become candidate subdomains, issuer/validity feed |
| 67 | +// posture flags (self-signed, expired, expiring soon). Unlike Passive's crt.sh |
| 68 | +// and certspotter feeds this is an active probe against the target itself, so |
| 69 | +// it catches certs that were never logged to a public CT log (short-lived |
| 70 | +// certs not yet propagated, internal CAs) at the cost of touching the target. |
| 71 | +func TLSCert(targetURL string, port int, timeout time.Duration, logdir string) (*TLSCertResult, error) { |
| 72 | + log := output.Module("TLSCERT") |
| 73 | + log.Start() |
| 74 | + |
| 75 | + parsed, err := url.Parse(targetURL) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("parse target url %q: %w", targetURL, err) |
| 78 | + } |
| 79 | + host := parsed.Hostname() |
| 80 | + if host == "" { |
| 81 | + return nil, fmt.Errorf("target url %q has no host", targetURL) |
| 82 | + } |
| 83 | + if port == 0 { |
| 84 | + port = 443 |
| 85 | + } |
| 86 | + addr := net.JoinHostPort(host, strconv.Itoa(port)) |
| 87 | + |
| 88 | + sanitizedURL := stripScheme(targetURL) |
| 89 | + if logdir != "" { |
| 90 | + if err := logger.WriteHeader(sanitizedURL, logdir, "tls certificate recon"); err != nil { |
| 91 | + log.Error("error creating log file: %v", err) |
| 92 | + return nil, fmt.Errorf("create tlscert log: %w", err) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + conn, err := tlsDial(addr, timeout) |
| 97 | + if err != nil { |
| 98 | + log.Warn("tls dial %s failed: %v", addr, err) |
| 99 | + return nil, fmt.Errorf("tls dial %q: %w", addr, err) |
| 100 | + } |
| 101 | + defer func() { _ = conn.Close() }() |
| 102 | + |
| 103 | + state := conn.ConnectionState() |
| 104 | + if len(state.PeerCertificates) == 0 { |
| 105 | + return nil, fmt.Errorf("tls handshake with %q presented no certificates", addr) |
| 106 | + } |
| 107 | + leaf := state.PeerCertificates[0] |
| 108 | + |
| 109 | + result := buildTLSCertResult(leaf, state.PeerCertificates, host) |
| 110 | + |
| 111 | + logTLSCertResult(log, sanitizedURL, logdir, result) |
| 112 | + |
| 113 | + log.Complete(len(result.SANs), "san entries") |
| 114 | + return result, nil |
| 115 | +} |
| 116 | + |
| 117 | +// buildTLSCertResult turns a parsed leaf certificate into the recon-facing |
| 118 | +// result: SAN-derived hostnames, issuer/validity, and posture flags. |
| 119 | +func buildTLSCertResult(leaf *x509.Certificate, chain []*x509.Certificate, targetHost string) *TLSCertResult { |
| 120 | + now := time.Now() |
| 121 | + |
| 122 | + sanSet := make(map[string]struct{}, len(leaf.DNSNames)) |
| 123 | + for _, name := range leaf.DNSNames { |
| 124 | + sanSet[normalizeHost(name)] = struct{}{} |
| 125 | + } |
| 126 | + sans := sortedKeys(sanSet) |
| 127 | + |
| 128 | + var newSubs []string |
| 129 | + for _, san := range sans { |
| 130 | + if san != normalizeHost(targetHost) { |
| 131 | + newSubs = append(newSubs, san) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + wildcard := false |
| 136 | + for _, name := range leaf.DNSNames { |
| 137 | + if len(name) > 1 && name[0] == '*' { |
| 138 | + wildcard = true |
| 139 | + break |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + // self-signed: issuer == subject (raw DER, not the human-readable string) |
| 144 | + // and the cert's own signature verifies against its own TBS bytes. |
| 145 | + // CheckSignatureFrom(leaf) is the wrong tool here - it additionally |
| 146 | + // requires CA key-usage/basic-constraints bits a self-signed leaf usually |
| 147 | + // doesn't set, so it false-negatives on exactly the certs this flag exists |
| 148 | + // to catch. |
| 149 | + selfSigned := bytes.Equal(leaf.RawIssuer, leaf.RawSubject) && |
| 150 | + leaf.CheckSignature(leaf.SignatureAlgorithm, leaf.RawTBSCertificate, leaf.Signature) == nil |
| 151 | + |
| 152 | + return &TLSCertResult{ |
| 153 | + Subject: leaf.Subject.String(), |
| 154 | + Issuer: leaf.Issuer.String(), |
| 155 | + SANs: sans, |
| 156 | + NotBefore: leaf.NotBefore.UTC().Format(time.RFC3339), |
| 157 | + NotAfter: leaf.NotAfter.UTC().Format(time.RFC3339), |
| 158 | + SerialNumber: leaf.SerialNumber.String(), |
| 159 | + SelfSigned: selfSigned, |
| 160 | + Expired: now.After(leaf.NotAfter), |
| 161 | + ExpiringSoon: !now.After(leaf.NotAfter) && leaf.NotAfter.Sub(now) < tlsCertExpirySoonWindow, |
| 162 | + Wildcard: wildcard, |
| 163 | + NewSubdomains: newSubs, |
| 164 | + ChainLength: len(chain), |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +func logTLSCertResult(log *output.ModuleLogger, sanitizedURL, logdir string, result *TLSCertResult) { |
| 169 | + log.Info("subject: %s", result.Subject) |
| 170 | + log.Info("issuer: %s", result.Issuer) |
| 171 | + if result.SelfSigned { |
| 172 | + log.Warn("certificate is self-signed") |
| 173 | + } |
| 174 | + if result.Expired { |
| 175 | + log.Warn("certificate expired %s", result.NotAfter) |
| 176 | + } else if result.ExpiringSoon { |
| 177 | + log.Warn("certificate expires soon: %s", result.NotAfter) |
| 178 | + } |
| 179 | + for _, san := range result.NewSubdomains { |
| 180 | + log.Success("san: %s", output.Highlight.Render(san)) |
| 181 | + } |
| 182 | + |
| 183 | + if logdir == "" { |
| 184 | + return |
| 185 | + } |
| 186 | + |
| 187 | + sb := fmt.Sprintf("Subject: %s\nIssuer: %s\nNotBefore: %s\nNotAfter: %s\nSelfSigned: %v\nExpired: %v\nWildcard: %v\n", |
| 188 | + result.Subject, result.Issuer, result.NotBefore, result.NotAfter, result.SelfSigned, result.Expired, result.Wildcard) |
| 189 | + if len(result.SANs) > 0 { |
| 190 | + sb += "\nSANs:\n" |
| 191 | + for _, san := range result.SANs { |
| 192 | + sb += " " + san + "\n" |
| 193 | + } |
| 194 | + } |
| 195 | + _ = logger.Write(sanitizedURL, logdir, sb) |
| 196 | +} |
0 commit comments