Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file to implicit FTPS server in C# with TLS session reuse

Tags:

c#

.net

ssl

ftp

ftps

I'm trying to upload file to FileZilla server through ftps by protocol TLS. On the server port 20 and 21 is closed. The only way how I managed to connect to server is by using FluentFTP but I couldn't upload file because of some FileZilla server bug.

https://github.com/robinrodricks/FluentFTP/issues/335
https://forum.filezilla-project.org/viewtopic.php?t=51601

public static void UploadTest(
    string pathUploadFile, string addressIP, int port, string location,
    string userName, string password)
{
    FtpClient ftp;

    Console.WriteLine("Configuring FTP to Connect to {0}", addressIP);
    ftp = new FtpClient(addressIP, port, new NetworkCredential(userName, password));
    ftp.ConnectTimeout = 600000;
    ftp.ReadTimeout = 60000;
    ftp.EncryptionMode = FtpEncryptionMode.Implicit;
    ftp.SslProtocols = SslProtocols.Default | SslProtocols.Tls11 | SslProtocols.Tls12;
    ftp.ValidateCertificate += new FtpSslValidation(OnValidateCertificate);
    ftp.Connect();
    // upload a file
    ftp.UploadFile(pathUploadFile, location);

    Console.WriteLine("Connected to {0}", addressIP);
    ftp.Disconnect();

    void OnValidateCertificate(FtpClient control, FtpSslValidationEventArgs e)
    {
        // add logic to test if certificate is valid here
        e.Accept = true;
    }
}

enter image description here

Is there any way around without a violating security level? If not is there any other free library which support uploading files with TLS/SSL? I also tried this but it didn't work.
https://learn.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest.enablessl

Thanks.

like image 368
Rafal Rafal Avatar asked Jan 26 '23 08:01

Rafal Rafal


1 Answers

You can use WinSCP .NET assembly.

It supports implicit TLS (port 990). And uses OpenSSL TLS implementation (not .NET Framework), so it should not have the problem that FluentFTP has. It definitely works for me against FileZilla FTP server, even with session resumption requirement turned on.

SessionOptions sessionOptions = new SessionOptions
{
    Protocol = Protocol.Ftp,
    HostName = "ftp.example.com",
    UserName = "username",
    Password = "password",
    FtpSecure = FtpSecure.Implicit,
    TlsHostCertificateFingerprint = "xx:xx:xx:...",
};

using (Session session = new Session())
{
    session.Open(sessionOptions);

    session.PutFiles(localPath, remotePath).Check();
}

(I'm the author of WinSCP)

For more references about the problem, see also Can connect to FTP using FileZilla or WinSCP, but not with FtpWebRequest or FluentFTP.

like image 104
Martin Prikryl Avatar answered Jan 28 '23 14:01

Martin Prikryl