TheOddPerson
TheOddPerson

Reputation: 159

Getting SSH host key from WinSCP .NET assembly connection

I am building a SFTP based application using WinSCP .NET assembly.

One 'key' thing that I seem to be missing is that there doesn't appear to be a way for the application to validate the host it's connected to using the SSH-Key unless the user is able to provide it.

As we know this isn't normally the case and I would like to still use the SSH key without the user using a 3rd party program to acquire it.

Before making a connection you either need to set:

ConnectionOption.SshHostKeyFingerprint= SSHkeyString

OR

ConnectionOptions.GiveUpSecurityAndAcceptAnySshHostKey = True

and if you don't know what the key is yet you have no choice to do the latter, and I assume you would be able to get the property of the SSH host key by calling this after the connection is made:

MessageBox.Show(ConnectionOption.SshHostKeyFingerprint)

but that returns an empty result.

I've looked over the documentation for the .NET connector and there doesn't seem a way to retrieve the SSH-Key of the server you're connected to.

It appears there are some other libraries that appear to have this feature, but the ones I found are commercially licensed. I suppose even a regular SSH connector (no ftp) with that ability would work -just to get the SSH key - if I could find one.

So my question is
How to I retrieve an unknown SSH key from a connected server using WinSCP .NET assembly?
OR
Is there another -freely available- library that enables me to retrieve said SSH key?

Upvotes: 1

Views: 3798

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202360

You can use the Session.ScanFingerprint method to implement an interactive host key verification.

There's C# and PowerShell example for Implementing SSH host key cache (known hosts).
It should not be difficult to re-implement it using VB.NET.

Upvotes: 2

Related Questions