-1

The organization has a integration service layer built on .NET 4.5.2. It has several WCF services running. One service uses SFTP to connect to SFTP server in the same machine to read and move some files. It uses simple username and password authentication. All works well in the production environments.

I'm trying to debug the solution in Visual Studio 2019 (16.8.6) and having trouble connecting to the SFTP server. It uses Renci SSH.NET v2016.0.0. Upgrading is not an option. I just want to debug it on the development environment.

The development environment (Windows Server 2012 R2) already had OpenSSH installed (OpenSSH_for_Windows_8.9p1, LibreSSL 3.4.3). And I'm running Visual Studio as administrator.

I can connect to the SFTP using the specific user and password in CMD, and in FileZilla. I also created a simple console application and it is also able to connect, navigate to the relevant folder and read the files.

I then created a simple WCF service and a service host and tried to connect, but this fails. It gives the error, "An existing connection was forcibly closed by the remote host", when it tries to connect.

Sample of console app connect code, This one works.

SftpClient _client = new SftpClient(host, u1, password1);
_client.Connect();

This is the code sample from the WCF service.

public string GetSFTPConnection(ConnectionDetails connectionDetails)
        {
            try
            {
                SftpClient _client = new SftpClient(connectionDetails.Host, connectionDetails.UserName, connectionDetails.Password);
                _client.Connect();

                if (_client.IsConnected)
                {
                    return "SFTP Connected Successfully!";
                }

                return "SFTP Connection Failed!";
            }
            catch (Exception e)
            {

                return $"SFT Connection Error@ {e}";
            }
        }

Error:

InnerException = An existing connection was forcibly closed by the remote host
StackTrace = at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
             at Renci.SshNet.Session.Connect()
             at Renci.SshNet.BaseClient.Connect()
             at SFTPServiceNew.Service1.GetSFTPConnection(ConnectionDetails connectionDetails) in C...
   
   
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
 <s:Header />
  <s:Body>
    <GetSFTPConnectionResponse xmlns="http://tempuri.org/">
      <GetSFTPConnectionResult>SFT Connection Error@ Renci.SshNet.Common.SshConnectionException: An existing connection was forcibly closed by the remote host ---&gt; System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at Renci.SshNet.Abstractions.SocketAbstraction.Read(Socket socket, Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
   at Renci.SshNet.Session.SocketRead(Int32 length, Byte[] buffer)
   at Renci.SshNet.Session.ReceiveMessage()
   at Renci.SshNet.Session.MessageListener()
   --- End of inner exception stack trace ---
   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.Connect()
   at SFTPServiceNew.Service1.GetSFTPConnection(ConnectionDetails connectionDetails) in C:...</GetSFTPConnectionResult>
    </GetSFTPConnectionResponse>
  </s:Body>
</s:Envelope>

It also returns different errors depending on the host.

localhost:

An existing connection was forcibly closed by the remote host

127.0.0.1:

Server response does not contain SSH protocol identification.

Environment IP address (10.xx.xx.xx):

Permission denied (password).

Computer name:

An existing connection was forcibly closed by the remote host

I have a feeling that this could be something to do with how a WCF service and it's host is run in the debug mode. And the user configuration in SSH. But I'm stumped as to how to figure it out. I don't have a lot of experience with WCF services or OpenSSH.

I've also tried to add my user account into OpenSSH using the blow command. But it fails.

mkpasswd -l -u [my_user_account] >> etc\passwd 

The system cannot find the path specified.

And there doesn't seem to be any etc\passwd file in the OpenSSH folder either.

Sample WCF service application structure

Sample WCF service when debugging

OpenSSH folder in server

Very grateful if someone can point me in the right direction.

MichaelO
  • 1
  • 2
  • Can you open a plain `Socket` connection and read the SSH identification string from it? – Martin Prikryl Jun 07 '22 at 06:49
  • Hi Martin, thank you for your suggestion. I tried a sample with a socket and found out that there is a another SFTP server running. The machine had both OpenSSH and Cerberus running. I turned off OpenSSH and was able to connect with the service. – MichaelO Jun 14 '22 at 05:55

1 Answers1

0

I found out recently that this machine had both OpenSSH and Cerberus running and configured for SFTP. Cerberus had a different set of users defined. I turned off OpenSSH and tried connecting to the Cerberus SFTP in the WCF service and it was successful.

I'm guessing this was due to some kind of conflict between OpenSSH and Cerberus SFTP.

MichaelO
  • 1
  • 2