$ ssh -o PreferredAuthentications=none -o NoHostAuthenticationForLocalhost=yes ssh_login
...: Permission denied (publickey,password,keyboard-interactive).
The above command can test for available auth methods from an ssh server. But I don't see how to obtain the same result in Go. Is there a way to do so?
https://pkg.go.dev/golang.org/x/crypto/ssh
I think go ssh client cannot be implemented because the related methods are all private, and a userAuthRequestMsg cannot be created and sent.
The methods variable returned on line 44 of ssh/client_auth.go is the list of authentication methods.
The first method of go ssh client authentication is noneAuth, and the userAuthRequestMsg request is sent to obtain authentication Method list, then use config.Auth as the next method to verify and check whether it is in methods.
Modify the source code /root/go/src/golang.org/x/crypto/ssh/client_auth.go and insert fmt.Println(methods) on line 45, and the methods output from the execution code is the supported authentication In the method list, port 22 is the sshd installed by linux, and port 8089 is the content of the ssh server connection protocol implemented by myself.
package main
import (
"fmt"
"golang.org/x/crypto/ssh"
)
func main() {
auth("127.0.0.1:22")
auth("127.0.0.1:8089")
}
func auth(host string) {
client, err := ssh.Dial("tcp", host, &ssh.ClientConfig{
User: "root",
Auth: []ssh.AuthMethod{
ssh.Password(""),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
})
fmt.Println(host)
fmt.Println(client)
fmt.Println(err)
}
[root@izuf6b7o9hu1q8vzyvkyznz ~]# go run /tmp/03.go
[publickey gssapi-keyex gssapi-with-mic password]
[publickey gssapi-keyex gssapi-with-mic password]
127.0.0.1:22
<nil>
ssh: handshake failed: ssh: unable to authenticate, attempted methods [none password], no supported methods remain
[password publickey]
[password publickey]
127.0.0.1:8089
<nil>
ssh: handshake failed: ssh: unable to authenticate, attempted methods [none password], no supported methods remain
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With