1

We want to upload files to a Ubuntu FTP server. We are able to connect and upload the file to the server using the FILE Zilla. The FTp server is SSL enabled and has TLS1.2. While uploading or connecting the certificate is shown after accepting it only are we able to connect or upload the file.

Need to have a automated system which would upload the files to the FTP as and when the files are created. For this we are developing a .net C# application which woud upload the file. we are able to connect to the FTP and also display the files present in it with C# FtpWebRequest. But when uploading a file to the ubuntu FTP (vsFTP) ( using C# FTPWebRequest ) the file gets partially uploaded and then the connection terminates. An exception occurs with this error "GetResponse - The remote server returned an error: (426) Connection closed; transfer aborted..".

Have checked the Firewall to confirm if that is causing the issue, so ran the application outside the firewall but still get the same exception. Added the code for TLS1.2 and also ServicePointManager.ServerCertificateValidationCallback but still not able to get the file uploaded with success response.

found a similar question [related to 426- Abort connection][1] implemented the suggestion for adding a config setting, but even this dint work for me.

Any body who has been able to upload a file to linux FTP which is TLS enbable would you please share the logic or let me know what exactly is need to be done in my case. Any FTP config setting that needs to enabled or disabled would help me fix this

Just to confirm when disabled the SSL and kept it as plain FTp we are able to upload the File without any issue.

Below is the Code Block used to upload the file

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://siteURl//home/Files/" + "Myfile.xml");
            request.Credentials = new NetworkCredential("UserName", "Password");
            request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.CacheIfAvailable);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            //request.KeepAlive = false;
            request.Timeout = 10000;
            request.UseBinary = true;
            request.UsePassive = true;
            request.KeepAlive = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            request.EnableSsl = true;
            
            ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
            
            StreamReader sourceStream = new StreamReader(filePath);
            byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;
            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();
          FtpWebResponse response = (FtpWebResponse)request.GetResponse();

The line FtpWebResponse response = (FtpWebResponse)request.GetResponse(); is where the connection aborts and we get the error the get response gives the 426 error [1]: powershell ftps upload causing error "DATA connection terminated without ssl shutdown" on stream close

Below is the log we get when the ftp fails.

   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5147134Z
System.Net Information: 0 : [7256] FtpControlStream#62468121 - Received response [426 Failure reading network stream.]
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5157116Z
System.Net.Sockets Verbose: 0 : [7256] Entering Socket#49652976::Dispose()
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5177039Z
System.Net Information: 0 : [7256] FtpWebRequest#66824994::(Releasing FTP connection#62468121.)
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5177039Z
System.Net Verbose: 0 : [7256] Entering FtpWebRequest#66824994::GetResponse()
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5187110Z
System.Net Information: 0 : [7256] FtpWebRequest#66824994::GetResponse(Method=STOR.)
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5187110Z
System.Net Error: 0 : [7256] Exception in FtpWebRequest#66824994::GetResponse - The remote server returned an error: (426) Connection closed; transfer aborted..
  at System.Net.FtpWebRequest.GetResponse()
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5286972Z
System.Net Verbose: 0 : [7256] Exiting FtpWebRequest#66824994::GetResponse() 
   ProcessId=4236
   DateTime=2020-10-09T10:46:50.5286972Z
System.Net Information: 0 : [5524] ServicePoint#60068066 - Closed as idle.
   ProcessId=4236
   DateTime=2020-10-09T10:48:17.3365536Z
System.Net Information: 0 : [5524] ServicePoint#57712780 - Closed as idle.```


  

  • FTP has a Text mode and a Binary Mode. One reason for the error is you are trying to send binary data in text mode. You said the uploads starts so if you are terminating in the middle then try changing to binary mode. – jdweng Oct 13 '20 at 12:58
  • Thanks jdweng for your response. I have added the code to usebinary for uploading the file. – B_d_Developer Oct 13 '20 at 13:32
  • I have just updated my initial post with the code block used for uploading the file. – B_d_Developer Oct 13 '20 at 13:38
  • If you are using SSL/TLS then the you are secure and should use a URL with FTPS (not FTP). – jdweng Oct 13 '20 at 13:51
  • okk.. In fileZilla I am using the protocol as **FTP** and the encryption as ** Use Explicit Ftp over TLS is available* so I was using the same in code. and most of the code that I found with TLS were using it as FTP. Let me check how to use FTPS – B_d_Developer Oct 13 '20 at 14:23
  • Just change the URL from FTP to FTPS. I believe windows dll will actually handle the transfer. – jdweng Oct 13 '20 at 14:28
  • by making it ftps://url/home/file it is giving error **The URI prefix is not recognized.** – B_d_Developer Oct 13 '20 at 14:45
  • I think you need to add : WebRequest.RegisterPrefix("ftps", new FtpsWebRequestCreator()); See : https://stackoverflow.com/questions/4331665/ftps-ftp-over-ssl-in-c-sharp – jdweng Oct 13 '20 at 14:56
  • Try SFTP instead of FTPS – jdweng Oct 13 '20 at 15:49
  • thanks jdweng for the suggestion. have used a nuget packge which provide the SFTP. was able to upload the file – B_d_Developer Oct 14 '20 at 14:17

1 Answers1

1

The same problem, but for a power shell script is here:

powershell ftps upload causing error "DATA connection terminated without ssl shutdown" on stream close

... and a workaround is offered, at least for the vsftpd server, to add, to the server options:

strict_ssl_read_eof=NO

It will keep complaining (internally, in the log, the server), but you'll get no exception. There are security risks associated, though (check the link)

jaime
  • 31
  • 2