This change breaks existing behavior. Before, a missing ClientConfig.HostKeyCallback would cause host key checking to be disabled. In this configuration, establishing a connection to any host just works, so today, most SSH client code in the wild does not perform any host key checks. This makes it easy to perform a MITM attack: * SSH installations that use keyboard-interactive or password authentication can be attacked with MITM, thereby stealing passwords. * Clients that use public-key authentication with agent forwarding are also vulnerable: the MITM server could allow the login to succeed, and then immediately ask the agent to authenticate the login to the real server. * Clients that use public-key authentication without agent forwarding are harder to attack unnoticedly: an attacker cannot authenticate the login to the real server, so it cannot in general present a convincing server to the victim. Now, a missing HostKeyCallback will cause the handshake to fail. This change also provides InsecureIgnoreHostKey() and FixedHostKey(key) as ready made host checkers. A simplistic parser for OpenSSH's known_hosts file is given as an example. This change does not provide a full-fledged parser, as it has complexity (wildcards, revocation, hashed addresses) that will need further consideration. When introduced, the host checking feature maintained backward compatibility at the expense of security. We have decided this is not the right tradeoff for the SSH library. Fixes golang/go#19767 Change-Id: I45fc7ba9bd1ea29c31ec23f115cdbab99913e814 Reviewed-on: https://go-review.googlesource.com/38701 Run-TryBot: Han-Wen Nienhuys <hanwen@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
// Copyright 2014 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package ssh
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func testClientVersion(t *testing.T, config *ClientConfig, expected string) {
|
|
clientConn, serverConn := net.Pipe()
|
|
defer clientConn.Close()
|
|
receivedVersion := make(chan string, 1)
|
|
config.HostKeyCallback = InsecureIgnoreHostKey()
|
|
go func() {
|
|
version, err := readVersion(serverConn)
|
|
if err != nil {
|
|
receivedVersion <- ""
|
|
} else {
|
|
receivedVersion <- string(version)
|
|
}
|
|
serverConn.Close()
|
|
}()
|
|
NewClientConn(clientConn, "", config)
|
|
actual := <-receivedVersion
|
|
if actual != expected {
|
|
t.Fatalf("got %s; want %s", actual, expected)
|
|
}
|
|
}
|
|
|
|
func TestCustomClientVersion(t *testing.T) {
|
|
version := "Test-Client-Version-0.0"
|
|
testClientVersion(t, &ClientConfig{ClientVersion: version}, version)
|
|
}
|
|
|
|
func TestDefaultClientVersion(t *testing.T) {
|
|
testClientVersion(t, &ClientConfig{}, packageVersion)
|
|
}
|
|
|
|
func TestHostKeyCheck(t *testing.T) {
|
|
for _, tt := range []struct {
|
|
name string
|
|
wantError string
|
|
key PublicKey
|
|
}{
|
|
{"no callback", "must specify HostKeyCallback", nil},
|
|
{"correct key", "", testSigners["rsa"].PublicKey()},
|
|
{"mismatch", "mismatch", testSigners["ecdsa"].PublicKey()},
|
|
} {
|
|
c1, c2, err := netPipe()
|
|
if err != nil {
|
|
t.Fatalf("netPipe: %v", err)
|
|
}
|
|
defer c1.Close()
|
|
defer c2.Close()
|
|
serverConf := &ServerConfig{
|
|
NoClientAuth: true,
|
|
}
|
|
serverConf.AddHostKey(testSigners["rsa"])
|
|
|
|
go NewServerConn(c1, serverConf)
|
|
clientConf := ClientConfig{
|
|
User: "user",
|
|
}
|
|
if tt.key != nil {
|
|
clientConf.HostKeyCallback = FixedHostKey(tt.key)
|
|
}
|
|
|
|
_, _, _, err = NewClientConn(c2, "", &clientConf)
|
|
if err != nil {
|
|
if tt.wantError == "" || !strings.Contains(err.Error(), tt.wantError) {
|
|
t.Errorf("%s: got error %q, missing %q", err.Error(), tt.wantError)
|
|
}
|
|
} else if tt.wantError != "" {
|
|
t.Errorf("%s: succeeded, but want error string %q", tt.name, tt.wantError)
|
|
}
|
|
}
|
|
}
|