projectdiscovery / httpx

httpx is a fast and multi-purpose HTTP toolkit that allows running multiple probes using the retryablehttp library.
https://docs.projectdiscovery.io/tools/httpx
MIT License
7.53k stars 822 forks source link

Get the domain name information in the https protocol #1824

Closed badboycxcc closed 2 months ago

badboycxcc commented 2 months ago

Please describe your feature request:

I need this information.Request https url, parse the CN information and print it.

Subject: CN=www.google.com
openssl x509 -in www.google.com.pem -noout -text
Certificate:
    Data:
        Version: 3 (0x2)
        Serial Number:
            28:2a:dd:41:18:16:c7:14:0a:a2:cf:15:0a:a2:e0:2d
        Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=US, O=Google Trust Services, CN=WR2
        Validity
            Not Before: Jun 24 07:42:34 2024 GMT
            Not After : Sep 16 07:42:33 2024 GMT
        Subject: CN=www.google.com
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                pub:
                    04:c9:f6:99:d5:49:ca:c7:30:96:b3:71:64:f8:57:
                    ef:df:2c:f3:23:5d:32:2b:19:51:a2:c7:7a:b0:f7:
                    73:b9:d4:99:35:74:37:47:1b:26:c8:d4:25:85:86:
                    30:4e:51:12:75:da:40:3f:05:b1:68:40:67:f1:2f:
                    d2:e2:92:d6:82
                ASN1 OID: prime256v1
                NIST CURVE: P-256
        X509v3 extensions:
            X509v3 Key Usage: critical
                Digital Signature
            X509v3 Extended Key Usage: 
                TLS Web Server Authentication
            X509v3 Basic Constraints: critical
                CA:FALSE
            X509v3 Subject Key Identifier: 
                88:61:D3:44:6D:E7:48:34:94:B5:A2:DA:CD:C3:36:42:06:63:51:39
            X509v3 Authority Key Identifier: 
                DE:1B:1E:ED:79:15:D4:3E:37:24:C3:21:BB:EC:34:39:6D:42:B2:30
            Authority Information Access: 
                OCSP - URI:http://o.pki.goog/wr2
                CA Issuers - URI:http://i.pki.goog/wr2.crt
            X509v3 Subject Alternative Name: 
                DNS:www.google.com
            X509v3 Certificate Policies: 
                Policy: 2.23.140.1.2.1
            X509v3 CRL Distribution Points: 
                Full Name:
                  URI:http://c.pki.goog/wr2/oBFYYahzgVI.crl
            CT Precertificate SCTs: 
                Signed Certificate Timestamp:
                    Version   : v1 (0x0)
                    Log ID    : EE:CD:D0:64:D5:DB:1A:CE:C5:5C:B7:9D:B4:CD:13:A2:
                                32:87:46:7C:BC:EC:DE:C3:51:48:59:46:71:1F:B5:9B
                    Timestamp : Jun 24 08:42:35.524 2024 GMT
                    Extensions: none
                    Signature : ecdsa-with-SHA256
                                30:45:02:21:00:F8:BD:57:A8:8D:67:8E:F7:ED:DB:25:
                                A2:8E:32:BB:44:16:97:FF:69:3A:7C:04:2E:CF:8D:14:
                                D8:AD:25:04:DD:02:20:3A:55:09:FB:D9:3E:31:2E:0A:
                                BC:22:5E:00:3F:43:44:76:6D:96:B1:74:16:D5:4F:76:
                                4B:AA:7C:76:C8:30:76
                Signed Certificate Timestamp:
                    Version   : v1 (0x0)
                    Log ID    : 48:B0:E3:6B:DA:A6:47:34:0F:E5:6A:02:FA:9D:30:EB:
                                1C:52:01:CB:56:DD:2C:81:D9:BB:BF:AB:39:D8:84:73
                    Timestamp : Jun 24 08:42:35.562 2024 GMT
                    Extensions: none
                    Signature : ecdsa-with-SHA256
                                30:44:02:20:42:DF:DD:70:C7:A1:80:C7:72:5A:64:6C:
                                0C:1E:B1:44:CA:74:1E:B4:F3:5C:26:A4:B0:9A:E6:89:
                                1D:A4:33:C0:02:20:15:B0:A1:EE:46:36:1A:A2:0F:6D:
                                06:63:27:F1:EB:DF:E4:F4:C0:DA:9E:1C:C9:FE:FE:57:
                                66:CC:D9:35:FB:8A
    Signature Algorithm: sha256WithRSAEncryption
    Signature Value:
        77:ff:6c:9b:67:06:37:8c:52:3d:47:04:b2:76:f1:35:a1:22:
        50:72:33:3f:e8:bb:9e:61:8b:c0:25:1b:97:cb:16:a0:e8:e7:
        4d:44:76:9f:73:1e:52:2d:b1:b5:53:ff:27:3f:5a:88:c8:96:
        2a:05:c9:d2:58:9e:b2:d5:81:94:9a:78:98:42:be:a5:49:de:
        c8:e8:da:b2:1d:0e:bc:d6:4e:ee:88:f1:e4:8f:a6:51:58:2a:
        ed:d0:d8:f5:c7:e9:f7:22:bf:8b:af:c8:40:da:a6:00:17:5d:
        00:c3:41:25:a3:aa:1c:a3:f8:8c:92:26:e1:b9:da:79:0c:82:
        93:91:fc:6a:05:ff:7c:6e:65:51:fc:32:40:1e:77:60:6f:f3:
        8d:f1:a8:ce:20:36:d0:a7:7e:bc:29:c6:56:ad:02:e7:e8:ee:
        ad:e5:22:7e:3e:1b:8f:b0:6e:07:25:7f:e4:03:7c:e0:87:d4:
        ba:3e:83:a2:68:0a:4e:9d:ea:93:88:f9:b7:08:a8:a7:cc:af:
        ca:c7:71:38:6a:3b:47:ef:57:47:39:a3:d0:f9:d8:af:c3:b9:
        72:d4:a6:d6:ed:ab:25:1c:6d:07:03:47:da:14:e1:fb:34:2d:
        60:dd:18:14:7a:36:35:09:f5:48:e0:5d:05:ed:15:9e:03:42:
        21:0b:11:08

Describe the use case of this feature:

Usage scenarios I have a lot of urls. I need to extract the certificate CN information in batches to obtain the domain name to confirm my company.

badboycxcc commented 2 months ago

Function implementation code, very simple but very practical (my code is not good)

func GetRemoteCerts(addr string) (certs *x509.Certificate, err error) {

        conn, err := tls.DialWithDialer(&net.Dialer{
                Timeout:  time.Second * 10,
                Deadline: time.Now().Add(time.Second * 9),
        }, "tcp", addr, &tls.Config{
                InsecureSkipVerify: true,
        })
        if err != nil {
                // fmt.Println(err)
                return
        }
        defer conn.Close()
        stats := conn.ConnectionState()
        // fmt.Println(stats.HandshakeComplete)

        // fmt.Printf("SSl Info: %s\n", stats.PeerCertificates[0].Subject.String())
        // fmt.Printf("SSl Info: %s\n", stats.PeerCertificates[0].Subject.CommonName)
        certs = stats.PeerCertificates[0]

        return
}

func GetSSl(target string) (sslinfo string, err error) {
        if strings.HasPrefix(target, "https://") {
                target = strings.ReplaceAll(target, "https://", "")
        }
        if !strings.Contains(target, ":") {
                fmt.Println("Please input target's port number")
                return "", errors.New("Please input target's port number")
        }
        // args := os.Args
        certs, err := GetRemoteCerts(target)
        if err != nil {
                fmt.Println(err)
                return "", err
        }
        return certs.Subject.CommonName, nil
        // fmt.Printf("Host: %s,SSl Info Subject: %s\n", target, certs.Subject)
}
ehsandeep commented 2 months ago

@badboycxcc this is already supported with -tls-grab option, for example echo google.com | httpx -tls-grab -j | jq .

badboycxcc commented 2 months ago

image

Cool

../Tools/httpx -l a.txt -tls-grab -j | jq '.tls | {host, subject_cn}'
Mzack9999 commented 2 months ago

Closing the issue as the functionality is already supported