Skip to content

Commit fcccf0c

Browse files
Return all the TLS handshake details we have even if handshake fails (#613)
* return TLS connection info even on handshake error. Fixes a regression introduced in 336455 and reported in issue 582 * Remove a todo
1 parent f9a3bfe commit fcccf0c

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

modules/tls.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package modules
33
import (
44
"context"
55
"errors"
6-
"fmt"
76

87
log "github.com/sirupsen/logrus"
98

@@ -85,22 +84,30 @@ func (s *TLSScanner) InitPerSender(senderID int) error {
8584
// heartbleed, if enabled).
8685
func (s *TLSScanner) Scan(ctx context.Context, dialerGroup *zgrab2.DialerGroup, target *zgrab2.ScanTarget) (zgrab2.ScanStatus, any, error) {
8786
conn, err := dialerGroup.Dial(ctx, target)
87+
if conn != nil {
88+
defer zgrab2.CloseConnAndHandleError(conn)
89+
}
8890
if err != nil {
89-
return zgrab2.TryGetScanStatus(err), nil, fmt.Errorf("failed to dial target %s: %w", target.String(), err)
91+
// Even on an error, we want to give the TLS Log if we have it.
92+
if conn != nil {
93+
if tlsConn, ok := conn.(*zgrab2.TLSConnection); ok {
94+
if tlsLog := tlsConn.GetLog(); tlsLog != nil {
95+
if tlsLog.HandshakeLog.ServerHello != nil {
96+
// If we got far enough to get a valid ServerHello, then
97+
// consider it to be a positive TLS detection.
98+
return zgrab2.TryGetScanStatus(err), tlsLog, err
99+
}
100+
// Otherwise, detection failed.
101+
}
102+
}
103+
}
104+
return zgrab2.TryGetScanStatus(err), nil, err
90105
}
91-
defer zgrab2.CloseConnAndHandleError(conn)
92106
tlsConn, ok := conn.(*zgrab2.TLSConnection)
93107
if !ok {
94-
return zgrab2.SCAN_INVALID_INPUTS, nil, errors.New("tls scanner requires a default dialer that creates TLS connections")
95-
}
96-
tlsLog := tlsConn.GetLog()
97-
if tlsLog != nil && tlsLog.HandshakeLog.ServerHello != nil {
98-
// If we got far enough to get a valid ServerHello, then
99-
// consider it to be a positive TLS detection.
100-
return zgrab2.SCAN_SUCCESS, tlsLog, nil
108+
return zgrab2.SCAN_UNKNOWN_ERROR, nil, errors.New("scan returned non-TLS connection")
101109
}
102-
// Otherwise detection failed
103-
return zgrab2.SCAN_HANDSHAKE_ERROR, nil, errors.New("tls handshake failed")
110+
return zgrab2.SCAN_SUCCESS, tlsConn.GetLog(), nil
104111
}
105112

106113
// Protocol returns the protocol identifer for the scanner.

processing.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,12 @@ func GetDefaultTLSWrapper(tlsFlags *TLSFlags) func(ctx context.Context, t *ScanT
139139
flags: tlsFlags,
140140
}
141141
err = tlsConn.Handshake()
142-
if err != nil {
142+
if err != nil && tlsConn.log == nil {
143+
// If the handshake fails and we have no log, just return error
143144
return nil, fmt.Errorf("could not perform tls handshake for target %s: %w", t.String(), err)
145+
} else if err != nil {
146+
// We'll return both the error and the connection so ZGrab can return the handshake log
147+
return &tlsConn, fmt.Errorf("could not successfully complete tls handshake for target %s: %w", t.String(), err)
144148
}
145149
return &tlsConn, err
146150
}

0 commit comments

Comments
 (0)