Skip to content

Commit 1d11c40

Browse files
mzpqnxowconstantinsander
authored andcommitted
[BUGFIX] Set SNI on each redirect to avoid handshake failures / incorrect server name (#306)
* Set SNI explicitly, in case it's a redirect (fix for #300) * Fix the SNI issue correctly, using the host portion of addr, while respecting --server-name and --no-sni * Clean up double error logging pointed out by dadrien * Comply with RFC4366, do not set SNI server name for IP address Co-authored-by: Adam Greene <[email protected]> zmap/zgrab2#306
1 parent 3613392 commit 1d11c40

File tree

1 file changed

+36
-10
lines changed

1 file changed

+36
-10
lines changed

modules/http/scanner.go

+36-10
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ func (flags *Flags) Help() string {
132132
}
133133

134134
// Protocol returns the protocol identifer for the scanner.
135-
func (s *Scanner) Protocol() string {
135+
func (scanner *Scanner) Protocol() string {
136136
return "http"
137137
}
138138

@@ -143,13 +143,13 @@ func (scanner *Scanner) Init(flags zgrab2.ScanFlags) error {
143143

144144
if fl.ComputeDecodedBodyHashAlgorithm == "sha1" {
145145
scanner.decodedHashFn = func(body []byte) string {
146-
raw_hash := sha1.Sum(body)
147-
return fmt.Sprintf("sha1:%s", hex.EncodeToString(raw_hash[:]))
146+
rawHash := sha1.Sum(body)
147+
return fmt.Sprintf("sha1:%s", hex.EncodeToString(rawHash[:]))
148148
}
149149
} else if fl.ComputeDecodedBodyHashAlgorithm == "sha256" {
150150
scanner.decodedHashFn = func(body []byte) string {
151-
raw_hash := sha256.Sum256(body)
152-
return fmt.Sprintf("sha256:%s", hex.EncodeToString(raw_hash[:]))
151+
rawHash := sha256.Sum256(body)
152+
return fmt.Sprintf("sha256:%s", hex.EncodeToString(rawHash[:]))
153153
}
154154
} else if fl.ComputeDecodedBodyHashAlgorithm != "" {
155155
log.Panicf("Invalid ComputeDecodedBodyHashAlgorithm choice made it through zflags: %s", scanner.config.ComputeDecodedBodyHashAlgorithm)
@@ -239,18 +239,35 @@ func (scan *scan) dialContext(ctx context.Context, network string, addr string)
239239

240240
// getTLSDialer returns a Dial function that connects using the
241241
// zgrab2.GetTLSConnection()
242-
func (scan *scan) getTLSDialer(t *zgrab2.ScanTarget) func(net, addr string) (net.Conn, error) {
243-
return func(net, addr string) (net.Conn, error) {
244-
outer, err := scan.dialContext(context.Background(), net, addr)
242+
func (scan *scan) getTLSDialer(t *zgrab2.ScanTarget) func(network, addr string) (net.Conn, error) {
243+
return func(network, addr string) (net.Conn, error) {
244+
outer, err := scan.dialContext(context.Background(), network, addr)
245245
if err != nil {
246246
return nil, err
247247
}
248-
249248
cfg, err := scan.scanner.config.TLSFlags.GetTLSConfigForTarget(t)
250249
if err != nil {
251250
return nil, err
252251
}
253252

253+
// Set SNI server name on redirects unless --server-name was used (issue #300)
254+
// - t.Domain is always set to the *original* Host so it's not useful for setting SNI
255+
// - host is the current target of the request in this context; this is true for the
256+
// initial request as well as subsequent requests caused by redirects
257+
// - scan.scanner.config.ServerName is the value from --server-name if one was specified
258+
259+
// If SNI is enabled and --server-name is not set, use the target host for the SNI server name
260+
if !scan.scanner.config.NoSNI && scan.scanner.config.ServerName == "" {
261+
host, _, err := net.SplitHostPort(addr)
262+
if err != nil {
263+
log.Errorf("getTLSDialer(): Something went wrong splitting host/port '%s': %s", addr, err)
264+
}
265+
// RFC4366: Literal IPv4 and IPv6 addresses are not permitted in "HostName"
266+
if i := net.ParseIP(host); i == nil {
267+
cfg.ServerName = host
268+
}
269+
}
270+
254271
if scan.scanner.config.OverrideSH {
255272
cfg.SignatureAndHashes = []tls.SigAndHash{
256273
{0x01, 0x04}, // rsa, sha256
@@ -262,7 +279,6 @@ func (scan *scan) getTLSDialer(t *zgrab2.ScanTarget) func(net, addr string) (net
262279
{0x01, 0x06}, // rsa, sha512
263280
}
264281
}
265-
266282
tlsConn := scan.scanner.config.TLSFlags.GetWrappedConnection(outer, cfg)
267283

268284
// lib/http/transport.go fills in the TLSLog in the http.Request instance(s)
@@ -437,6 +453,16 @@ func (scan *scan) Grab() *zgrab2.ScanError {
437453
encoder, _, _ := charset.DetermineEncoding(buf.Bytes(), resp.Header.Get("content_type"))
438454
decoder := encoder.NewDecoder()
439455

456+
//"windows-1252" is the default value and will likely not decode correctly
457+
if certain || encoding != "windows-1252" {
458+
decoded, decErr := decoder.Bytes(buf.Bytes())
459+
460+
if decErr == nil {
461+
bodyText = string(decoded)
462+
decodedSuccessfully = true
463+
}
464+
}
465+
440466
decoded, decErr := decoder.String(bufAsString)
441467

442468
// if the decoder errors out just use the buffer as a string

0 commit comments

Comments
 (0)