Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix resolved ip and other minor improvements #132

Merged
merged 3 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions pkg/tlsx/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (

"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/goflags"
ztls "github.com/zmap/zcrypto/tls"
stringsutil "github.com/projectdiscovery/utils/strings"

ztls "github.com/zmap/zcrypto/tls"
)

// Implementation is an interface implemented by TLSX client
Expand Down Expand Up @@ -313,10 +312,14 @@ func IsMisMatchedCert(host string, alternativeNames []string) bool {
return true
}

// IsTLSRevoked returns true if the certificate has been revoked
// IsTLSRevoked returns true if the certificate has been revoked or failed to parse
func IsTLSRevoked(cert *x509.Certificate) bool {
zcert, _ := zx509.ParseCertificate(cert.Raw)
return IsZTLSRevoked(zcert)
zcert, err := zx509.ParseCertificate(cert.Raw)
if err != nil {
return true
} else {
return IsZTLSRevoked(zcert)
}
}

// IsZTLSRevoked returns true if the certificate has been revoked
Expand Down
10 changes: 3 additions & 7 deletions pkg/tlsx/openssl/openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,9 @@ func (c *Client) ConnectWithOptions(hostname, ip, port string, options clients.C
}
defer rawConn.Close()

var resolvedIP string
if ip != "" {
resolvedIP = ip
} else if iputil.IsIP(hostname) {
resolvedIP = hostname
} else {
resolvedIP = c.dialer.GetDialedIP(hostname)
resolvedIP, _, err := net.SplitHostPort(rawConn.RemoteAddr().String())
if err != nil {
return nil, err
}

conn, err := openssl.Client(rawConn, opensslCtx)
Expand Down
10 changes: 3 additions & 7 deletions pkg/tlsx/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,9 @@ func (c *Client) ConnectWithOptions(hostname, ip, port string, options clients.C
return nil, fmt.Errorf("could not connect to %s", address)
}

var resolvedIP string
if ip != "" {
resolvedIP = ip
} else if iputil.IsIP(hostname) {
resolvedIP = hostname
} else {
resolvedIP = c.dialer.GetDialedIP(hostname)
resolvedIP, _, err := net.SplitHostPort(rawConn.RemoteAddr().String())
if err != nil {
return nil, err
}

config := c.tlsConfig
Expand Down
8 changes: 8 additions & 0 deletions pkg/tlsx/tlsx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strconv"

"github.com/pkg/errors"
"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/tlsx/pkg/tlsx/auto"
"github.com/projectdiscovery/tlsx/pkg/tlsx/clients"
"github.com/projectdiscovery/tlsx/pkg/tlsx/jarm"
Expand All @@ -24,6 +25,13 @@ func New(options *clients.Options) (*Service, error) {
service := &Service{
options: options,
}
if options.Fastdialer == nil {
var err error
options.Fastdialer, err = fastdialer.NewDialer(fastdialer.DefaultOptions)
if err != nil {
return nil, err
}
}
var err error
switch options.ScanMode {
case "ztls":
Expand Down
63 changes: 63 additions & 0 deletions pkg/tlsx/tlsx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tlsx_test

import (
"errors"
"testing"

"github.com/projectdiscovery/fastdialer/fastdialer"
"github.com/projectdiscovery/tlsx/pkg/tlsx"
"github.com/projectdiscovery/tlsx/pkg/tlsx/clients"
"github.com/projectdiscovery/tlsx/pkg/tlsx/openssl"
iputil "github.com/projectdiscovery/utils/ip"
)

func TestResolvedIP(t *testing.T) {

allmodes := []string{"ctls", "ztls", "openssl", "auto"}
targethostname := "projectdiscovery.io"
targets, err := getDNSdata(targethostname)
if err != nil {
t.Fatalf("failed to get dns data: %v", err)
}

for _, mode := range allmodes {
client, err := tlsx.New(&clients.Options{
ScanMode: mode,
Retries: 3,
})
if errors.Is(err, openssl.ErrNotSupported) {
t.Logf("openssl not available skipping..")
continue
}
if err != nil {
t.Fatalf("failed to create new client for %v mode: %v", mode, err)
}
for _, target := range targets {
resp, err := client.ConnectWithOptions(targethostname, target, "443", clients.ConnectOptions{})
if err != nil {
t.Fatalf("failed to get response from tlsx client: %v", err)
}
if !iputil.IsIP(resp.IP) {
t.Fatalf("expected ip address for %v but got %v for mode %v", target, resp.IP, mode)
}
}
}

}

func getDNSdata(hostname string) ([]string, error) {
targets := []string{}
fd, err := fastdialer.NewDialer(fastdialer.DefaultOptions)
if err != nil {
return targets, err
}
dnsData, err := fd.GetDNSData(hostname)
if err != nil {
return targets, err
}
targets = append(targets, hostname)
targets = append(targets, dnsData.A...)
targets = append(targets, dnsData.AAAA...)

return targets, nil
}
10 changes: 3 additions & 7 deletions pkg/tlsx/ztls/ztls.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,13 +139,9 @@ func (c *Client) ConnectWithOptions(hostname, ip, port string, options clients.C
return nil, fmt.Errorf("could not connect to %s", address)
}

var resolvedIP string
if ip != "" {
resolvedIP = ip
} else if iputil.IsIP(hostname) {
resolvedIP = hostname
} else {
resolvedIP = c.dialer.GetDialedIP(hostname)
resolvedIP, _, err := net.SplitHostPort(conn.RemoteAddr().String())
if err != nil {
return nil, err
}

config := c.tlsConfig
Expand Down