iosswiftsshsftpnmssh

Error making connection with Sftp server using NMSSH library swift IOS app. NMSSH: Failure establishing SSH session


I am using NMSSH library in my ios app to open a connection with the server and download a json file and save it to local directory.

I have tried two different sFTP servers to open connection. One is my own server and other one I found a dummy server on internet. Now the issue is I am able to sucessfully open a connection and download the file from Dummy server found on internet.(screenshot attached.)

Logs :

Credentials :

let host = "test.rebex.net" 
  
let port = 22  
 
let user = "demo"

let password = "password"

let fileToDownload = "readme.txt"



2023-07-26 16:33:35.514732+0500 FTP[42664:5996676] NMSSH: libssh2 (v1.8.0) initialized
2023-07-26 16:33:35.514788+0500 FTP[42664:5996676] NMSSH: Start test.rebex.net resolution
2023-07-26 16:33:35.937435+0500 FTP[42664:5996672] NMSSH: Socket connection to 194.108.117.16 on port 22 succesful
2023-07-26 16:33:36.984023+0500 FTP[42664:5996672] NMSSH: Remote host banner is SSH-2.0-RebexSSH_5.0.8466.0
2023-07-26 16:33:36.984372+0500 FTP[42664:5996672] NMSSH: The host's fingerprint is 03:61:C4:98:F1:FF:7D:23:97:51:07:13:88:B8:C5:55
2023-07-26 16:33:36.984533+0500 FTP[42664:5996672] NMSSH: SSH session started
2023-07-26 16:33:37.148409+0500 FTP[42664:5996672] NMSSH: User auth list: password,keyboard-interactive,publickey
2023-07-26 16:33:37.298370+0500 FTP[42664:5996672] NMSSH: Password authentication succeeded.

Optional("Welcome,\r\n\r\nYou are connected to an FTP or SFTP server used for testing purposes by Rebex FTP/SSL or Rebex SFTP sample code.\r\nOnly read access is allowed and the FTP download speed is limited to 16KBps.\r\n\r\nFor information about Rebex FTP/SSL, Rebex SFTP and other Rebex .NET components, please visit our website at https://www.rebex.net/\r\n\r\nFor feedback and support, contact support@rebex.net\r\n\r\nThanks!\r\n")

When I try to open connection with my own server I get the following error :

 2023-07-26 16:36:47.112351+0500 FTP[42687:5998750] NMSSH: libssh2 (v1.8.0) initialized
    
    2023-07-26 16:36:47.112440+0500 FTP[42687:5998750] NMSSH: Start 52.6.162.79 resolution
    
    2023-07-26 16:36:47.372795+0500 FTP[42687:5998755] NMSSH: Socket connection to 52.6.162.79 on port 22 succesful
    
    2023-07-26 16:36:48.017514+0500 FTP[42687:5998755] NMSSH: Failure establishing SSH session
    
    2023-07-26 16:36:48.017645+0500 FTP[42687:5998755] NMSSH: Disconnected
    
    Connection failed.

The code snippet to open a conneciton with server is mentioned below :

func readSFTPFile() {
   

    let session = NMSSHSession(host: host, port: port, andUsername: user)
    session.connect()

    if session.isConnected {
        
        session.authenticate(byPassword: password)

        if session.isAuthorized {
            let sftp = NMSFTP.connect(with: session)

            if sftp != nil {
                let remotePath = fileToDownload
                let localPath = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Downloads/\(fileToDownload)")

                let fileData = sftp.contents(atPath: fileToDownload)
                let StrValue = String(data: fileData!, encoding: String.Encoding.utf8)
                print(StrValue)
                
                sftp.disconnect()
            } else {
                print("Error connecting to SFTP server.")
            }
        } else {
            print("Authentication failed.")
        }
        session.disconnect()
    } else {
        print("Connection failed.")
    }
}

I am very confused, Why the connection with dummy server is working fine and not with my own server. Is there any configuration changes I need to make on server end to allow Iphone to connect with server ?

Some Information about dummy server :

https://test.rebex.net

enter image description here


Solution

  • I ran into this issue when trying to connect to a SFTPGO server. This might be caused by the older libssh2 (1.8.0) used by the NMSSH library, which only supports older key algorithms like ssh-rsa and ssh-dss. Since these algorithms are older, your server might require the use of newer/safer key algorithms (rsa-sha2-512, rsa-sha2-256, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, ecdsa-sha2-nistp521 or ssh-ed25519). The testserver may be allowing use of the older algorithms.

    1. Try updating libssh2 yourself to 1.9.0 (or newer), it looks like https://github.com/Frugghi/iSSH2 can help with this.
    2. Use a fork of NMSSH with libssh 1.9.0 included: https://github.com/speam/NMSSH
    3. Allow older keyalgorithms on your server/software (not preferred for security reasons)

    Having problems myself with #1, #2 fixed it for me.