ncouture / MockSSH

Mock an SSH server and define all commands it supports (Python, Twisted)
Other
123 stars 23 forks source link

Add public key authentication for clients #14

Open greatestape opened 8 years ago

greatestape commented 8 years ago

How do I leverage the private.key that MockSSH generates to authenticate using key-exchange instead of password? There's probably something obvious that I'm misunderstanding, but so far I'm stumped.

I tried ssh -i private.key <user>@127.0.0.1 -p <port> but it still prompts for password. I've tried running with -vvv and it looks like it's loaded private.key as the identity file, but later on it ends up saying Authentications that can continue: password.

Thanks for making this tool! I'm excited to start using it in our test suite.

ncouture commented 7 years ago

As far as I remember there is no support for SSH keys by the SSH server in MockSSH and the keys you are seeing are the server's and not the client's.

If you want this feature I would be willing to implement it but please see if you can contribute a patch first by integrating the following into something reusable:

(mockssh)jehjohnson ~/git/MockSSH $ ckeygen -t rsa -f id_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in id_rsa
Your public key has been saved in id_rsa.pub
The key fingerprint is:
fc:b8:57:88:ed:b6:46:df:71:f3:65:67:19:bd:41:45

In MockSSH.py under the password checker:

    sshFactory.portal.registerChecker(
        checkers.InMemoryUsernamePasswordDatabaseDontUse(**users))

Add the following to add support for the new keys created, for the user me:

    sshFactory.portal.registerChecker(
        SSHPublicKeyChecker(
            InMemorySSHKeyDB({
                'me': [keys.Key.fromFile('id_rsa.pub')]})))

Run a MockSSH server:

$ python MockSSH.py

Test public key authentication:

jehjohnson ~ $ ssh me@0 -p 2222 -v -i ~jehjohnson/git/MockSSH/id_rsa
OpenSSH_6.7p1 Debian-5+deb8u2, OpenSSL 1.0.1t  3 May 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to 0 [0.0.0.0] port 2222.
debug1: Connection established.
debug1: identity file /home/jehjohnson/git/MockSSH/keys/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/jehjohnson/git/MockSSH/keys/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_6.7p1 Debian-5+deb8u2
debug1: Remote protocol version 2.0, remote software version OpenSSH_Mock MockSSH.py
debug1: match: OpenSSH_Mock MockSSH.py pat OpenSSH* compat 0x04000000
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-sha2-256 none
debug1: kex: client->server aes128-ctr hmac-sha2-256 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<8192<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA e6:2f:4f:c3:8e:44:ba:7d:2d:83:c3:b8:11:79:3d:60
debug1: Host '[0]:2222' is known and matches the RSA host key.
debug1: Found key in /home/jehjohnson/.ssh/known_hosts:21
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: password,publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/janedoe/git/MockSSH/keys/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 151
debug1: Authentication succeeded (publickey).
Authenticated to 0 ([127.0.0.1]:2222).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_US.UTF-8
$ exit
debug1: SSH2_MSG_KEXINIT sent
debug1: channel 0: free: client-session, nchannels 1
Connection to 0 closed.
Transferred: sent 4440, received 3496 bytes, in 167.7 seconds
Bytes per second: sent 26.5, received 20.8
debug1: Exit status -1
janedoe ~ $