Add SSH outbound host key validation

This commit is contained in:
lyc8503 2023-01-14 20:39:59 +08:00
parent f32c149738
commit 51f8d36daf
4 changed files with 23 additions and 0 deletions

View File

@ -14,6 +14,7 @@
"private_key_passphrase": "", "private_key_passphrase": "",
"host_key_algorithms": [], "host_key_algorithms": [],
"client_version": "SSH-2.0-OpenSSH_7.4p1", "client_version": "SSH-2.0-OpenSSH_7.4p1",
"host_key": "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdH...",
... // Dial Fields ... // Dial Fields
} }
@ -59,6 +60,10 @@ Host key algorithms.
Client version. Random version will be used if empty. Client version. Random version will be used if empty.
#### host_key
Remote SSH host key. Accept any host key if empty.
### Dial Fields ### Dial Fields
See [Dial Fields](/configuration/shared/dial) for details. See [Dial Fields](/configuration/shared/dial) for details.

View File

@ -14,6 +14,7 @@
"private_key_passphrase": "", "private_key_passphrase": "",
"host_key_algorithms": [], "host_key_algorithms": [],
"client_version": "SSH-2.0-OpenSSH_7.4p1", "client_version": "SSH-2.0-OpenSSH_7.4p1",
"host_key": "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdH...",
... // 拨号字段 ... // 拨号字段
} }
@ -59,6 +60,10 @@ SSH 用户, 默认使用 root。
客户端版本,默认使用随机值。 客户端版本,默认使用随机值。
#### host_key
指定主机密钥,留空接受所有主机密钥.
### 拨号字段 ### 拨号字段
参阅 [拨号字段](/zh/configuration/shared/dial/)。 参阅 [拨号字段](/zh/configuration/shared/dial/)。

View File

@ -10,4 +10,5 @@ type SSHOutboundOptions struct {
PrivateKeyPassphrase string `json:"private_key_passphrase,omitempty"` PrivateKeyPassphrase string `json:"private_key_passphrase,omitempty"`
HostKeyAlgorithms Listable[string] `json:"host_key_algorithms,omitempty"` HostKeyAlgorithms Listable[string] `json:"host_key_algorithms,omitempty"`
ClientVersion string `json:"client_version,omitempty"` ClientVersion string `json:"client_version,omitempty"`
HostKey string `json:"host_key,omitempty"`
} }

View File

@ -2,7 +2,9 @@ package outbound
import ( import (
"context" "context"
"encoding/base64"
"math/rand" "math/rand"
"errors"
"net" "net"
"os" "os"
"strconv" "strconv"
@ -38,6 +40,7 @@ type SSH struct {
clientAccess sync.Mutex clientAccess sync.Mutex
clientConn net.Conn clientConn net.Conn
client *ssh.Client client *ssh.Client
hostKey string
} }
func NewSSH(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SSHOutboundOptions) (*SSH, error) { func NewSSH(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SSHOutboundOptions) (*SSH, error) {
@ -55,6 +58,7 @@ func NewSSH(ctx context.Context, router adapter.Router, logger log.ContextLogger
user: options.User, user: options.User,
hostKeyAlgorithms: options.HostKeyAlgorithms, hostKeyAlgorithms: options.HostKeyAlgorithms,
clientVersion: options.ClientVersion, clientVersion: options.ClientVersion,
hostKey: options.HostKey,
} }
if outbound.serverAddr.Port == 0 { if outbound.serverAddr.Port == 0 {
outbound.serverAddr.Port = 22 outbound.serverAddr.Port = 22
@ -104,6 +108,11 @@ func randomVersion() string {
return version return version
} }
func keyString(k ssh.PublicKey) string {
// e.g. "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTY...."
return k.Type() + " " + base64.StdEncoding.EncodeToString(k.Marshal())
}
func (s *SSH) connect() (*ssh.Client, error) { func (s *SSH) connect() (*ssh.Client, error) {
if s.client != nil { if s.client != nil {
return s.client, nil return s.client, nil
@ -126,6 +135,9 @@ func (s *SSH) connect() (*ssh.Client, error) {
ClientVersion: s.clientVersion, ClientVersion: s.clientVersion,
HostKeyAlgorithms: s.hostKeyAlgorithms, HostKeyAlgorithms: s.hostKeyAlgorithms,
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error { HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
if s.hostKey != "" && s.hostKey != keyString(key) {
return errors.New("SSH Host key mismatch, expected: " + keyString(key) + " got: " + s.hostKey)
}
return nil return nil
}, },
} }