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>
42 lines
958 B
Go
42 lines
958 B
Go
// Copyright 2016 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 agent_test
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"os"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
"golang.org/x/crypto/ssh/agent"
|
|
)
|
|
|
|
func ExampleClientAgent() {
|
|
// ssh-agent has a UNIX socket under $SSH_AUTH_SOCK
|
|
socket := os.Getenv("SSH_AUTH_SOCK")
|
|
conn, err := net.Dial("unix", socket)
|
|
if err != nil {
|
|
log.Fatalf("net.Dial: %v", err)
|
|
}
|
|
agentClient := agent.NewClient(conn)
|
|
config := &ssh.ClientConfig{
|
|
User: "username",
|
|
Auth: []ssh.AuthMethod{
|
|
// Use a callback rather than PublicKeys
|
|
// so we only consult the agent once the remote server
|
|
// wants it.
|
|
ssh.PublicKeysCallback(agentClient.Signers),
|
|
},
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
}
|
|
|
|
sshc, err := ssh.Dial("tcp", "localhost:22", config)
|
|
if err != nil {
|
|
log.Fatalf("Dial: %v", err)
|
|
}
|
|
// .. use sshc
|
|
sshc.Close()
|
|
}
|