Key exchange without server identity verification

Key exchange without server identity verification

Description

ssh.InsecureIgnoreHostKey() accepts the host key of any server the client reaches. Someone on the network path could impersonate the intended SSH server.

Potential impact

  • A server impersonating the intended host can read SSH passwords, tokens and commands sent to it, and alter the responses it returns.
  • With public-key authentication, the client keeps its private key. Data exchanged over the session can still be exposed.

Remediation

  • Use knownhosts.New from golang.org/x/crypto/ssh/knownhosts to read a trusted OpenSSH known_hosts file. Assign the returned function to HostKeyCallback. For a single configured destination, you can pin that server’s verified public key with ssh.FixedHostKey().
  • Distribute the file or pinned keys through a trusted channel. Verify replacement keys independently before updating them. Give tests their own known_hosts file or pinned key.

Examples

The examples compare host-key checks. In production, read the password from protected configuration and handle errors from os.UserHomeDir().

Before

go
package main

import (
    "log"

    "golang.org/x/crypto/ssh"
)

func newSSHClientInsecure(addr, user, password string) (*ssh.Client, error) {
    config := &ssh.ClientConfig{
        User: user,
        Auth: []ssh.AuthMethod{
            ssh.Password(password),
        },
        // Server host key is not verified
        HostKeyCallback: ssh.InsecureIgnoreHostKey(),
    }

    client, err := ssh.Dial("tcp", addr, config)
    if err != nil {
        return nil, err
    }
    return client, nil
}

func main() {
    client, err := newSSHClientInsecure("example.com:22", "deploy", "secret-password")
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    // ... Perform important operations ...
}

After

go
package main

import (
    "log"
    "os"

    "golang.org/x/crypto/ssh"
    "golang.org/x/crypto/ssh/knownhosts"
)

func newSSHClientSecure(addr, user, password, knownHostsPath string) (*ssh.Client, error) {
    // Configure host-key verification using known_hosts
    hostKeyCallback, err := knownhosts.New(knownHostsPath)
    if err != nil {
        return nil, err
    }

    config := &ssh.ClientConfig{
        User: user,
        Auth: []ssh.AuthMethod{
            ssh.Password(password),
        },
        HostKeyCallback: hostKeyCallback,
    }

    client, err := ssh.Dial("tcp", addr, config)
    if err != nil {
        return nil, err
    }
    return client, nil
}

func main() {
    home, _ := os.UserHomeDir()
    knownHosts := home + "/.ssh/known_hosts"

    client, err := newSSHClientSecure("example.com:22", "deploy", "secret-password", knownHosts)
    if err != nil {
        log.Fatal(err)
    }
    defer client.Close()

    // ... Perform SSH operations after verifying the host key ...
}

The callback returned by knownhosts.New checks the server key against known_hosts. ssh.Dial fails if the key differs or the host has no entry.

References