{"id":840,"date":"2016-04-11T20:46:32","date_gmt":"2016-04-11T20:46:32","guid":{"rendered":"http:\/\/blog.paranoidprofessor.com\/?p=840"},"modified":"2016-04-12T06:51:23","modified_gmt":"2016-04-12T06:51:23","slug":"command-line-fun-transferring-data","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/04\/11\/command-line-fun-transferring-data\/","title":{"rendered":"command line fun \u2013 transferring data with sftp"},"content":{"rendered":"<p>I must admit that I misunderstood the task when I first saw it. \u00a0The vendor had changed how they provide the data. \u00a0We will no longer be able to retrieve the data using ftp &#8211; the data should be picked up with sftp.<\/p>\n<p>I immediately thought that this would be quite the trick as as far as I was aware you cannot script sftp the same way you can ftp. \u00a0I was discussing this with my boss to get yet another dose of information. \u00a0My boss said that this seemed an odd choice of technology, that what would be a better choice would be to use ftps.<\/p>\n<p>It was about this time that I realized that I made the first mistake of thinking about the programs not the protocols. \u00a0Indeed there are three different protocols, FTP, SFTP and FTPS and there are two programs with the same name as the protocol. \u00a0The second mistake was that I was not familiar with the FTPS protocol. \u00a0First a bit about the protocols.<\/p>\n<h2>File Transfer Protocol (FTP)<\/h2>\n<p>The FTP protocol is to connect to the server with two connections. \u00a0The first is the command connection which controls the transfers, and the second connection is the data connection. \u00a0The process is controlled over one while the data is transferred over the second\u00a0connection.<\/p>\n<p>Although the setup of the ftp server can be anything the typical setup uses ports 20 and 21.<\/p>\n<p>The ftp client\u00a0connects from the client computer on port &#8220;X&#8221; to the server using TCP on port 21. \u00a0Once the connection has been made a second connection will be used to for transferring the data. \u00a0The way this second connection is made depends on the type of mode that is used &#8211; active or passive.<\/p>\n<h3>Active Mode<\/h3>\n<p>The second connection is actually made by the server from port 20 to port X + 1 on the client.<\/p>\n<p>It is really simple, but this won&#8217;t usually work in practice at most serious companies\u00a0as usually there are firewalls protecting the server from the internet. \u00a0The firewalls only job is to prevent unknown computers to connect to the machine and thus would prevent the active ftp from\u00a0working.<\/p>\n<p>This problem was foreseen and thus the passive mode was also created.<\/p>\n<h3>Passive Mode<\/h3>\n<p>Passive mode is exactly the same as the active mode except that the client opens the second connection\u00a0instead of the server. \u00a0It is because the\u00a0client opens the second connection that usually\u00a0eliminates the problem of the firewall interfering with the creation of the connection.<\/p>\n<p>The coordination of how to keep the connections connected is pretty simple. \u00a0When the ftp client initiates the passive mode with the command PASV, it receives the number of the port to connect to for the data connection.<\/p>\n<p><strong>Advantages<\/strong><\/p>\n<ul>\n<li>No size limitation on file transfers<\/li>\n<li>Some clients can be scripted<\/li>\n<\/ul>\n<p><strong>Disadvantages<\/strong><\/p>\n<ul>\n<li>Usernames, passwords and files are sent in clear text<\/li>\n<li>Filtering active FTP connections is difficult on your local machine (passive is preferred)<\/li>\n<li>Servers can be spoofed to send data to a random port on an unintended computer<\/li>\n<\/ul>\n<p>The big disadvantage to the ftp protocol is that the user and password is communicated as clear text. \u00a0It is possible for anyone sniffing packets to get this information.<\/p>\n<p>&nbsp;<\/p>\n<h2>FTPS (or FTPES or FTP-SSL)<\/h2>\n<p>The ftp program allows the user to transfer files and change directories on the remote computer. \u00a0It is really useful. \u00a0When the only real weakness of the file transfer protocol is that the users credentials are passed over in clear text, it seems small enough to correct.<\/p>\n<p>Indeed that is exactly what was attempted with FTPS extensions to the file transfer protocol. \u00a0The change was to simply add encryption to plug this particular weakness. \u00a0So the change that was done was adding <a href=\"https:\/\/en.wikipedia.org\/wiki\/Transport_Layer_Security\" target=\"_blank\">Transport Layer Security<\/a> and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Secure_Sockets_Layer\" target=\"_blank\">Secure Sockets Layer<\/a> encryption protocols.<\/p>\n<p><strong>Advantages<\/strong><\/p>\n<ul>\n<li>Provides services for server-to-server file transfer<\/li>\n<li>SSL\/TLS uses X.509 certificate to\u00a0authenticate<\/li>\n<\/ul>\n<p><strong>Disadvantages<\/strong><\/p>\n<ul>\n<li>Requires a DATA channel, which can make\u00a0it hard to use behind the firewalls<\/li>\n<li>Doesn\u2019t define a standard for file name character sets<\/li>\n<li>Not all FTP servers support SSL\/TLS<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Secure File Transfer Protocol (SFTP)<\/h2>\n<p>The SFTP protocol is also sometimes referred to as SSH File Transfer Protocol. \u00a0The SFTP protocol is a network protocol that provides file access, file transfer and file management over a network connection.<\/p>\n<p>All data that is transferred between the client and server, including login credentials, are encrypted. This is usually done through the user of public and private keys but can be done in addition to a user and password.<\/p>\n<p>The file transfer protocol uses only a single connection over port 22 on the server. \u00a0Both the commands and data transfer take place over this single connection.<\/p>\n<p><strong>Advantages<\/strong><\/p>\n<ul>\n<li>Only one connection is needed (no special DATA connection)<\/li>\n<li>The connection is always secured<\/li>\n<li>The protocol includes operations for permission and attribute manipulation, file locking and more functionality<\/li>\n<\/ul>\n<p><strong>Disadvantages<\/strong><\/p>\n<ul>\n<li>SSH keys are harder to manage and validate<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<h2>Putting it all to good use<\/h2>\n<p>The file transfer protocol actually is not the most secure protocol due to the fact that the user credentials are sent over as clear text. \u00a0This is actually important if you actually think that someone may be sniffing your packets (not so likely) but depending on the service this might not be so important.<\/p>\n<p>If the data that is transferred to the FTP server is encrypted then it may not be as important if the username and password is captured. \u00a0If on the destination server the data is processed and then removed this may not be a problem or if the data is really well encrypted this might not be a problem. \u00a0Perhaps the information is public information and if the data escapes it is not important (ie. which days are public holidays for a specific trading calendar, what is the trading price of a stock or what is a company&#8217;s PE ratio)<\/p>\n<p>A simple scripted solution using ftp<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\nUSER=richard\r\nPASS=secretpasscode\r\nDATA=\/var\/tmp\/datafile.tar\r\n\r\nftp -i -n ftp.somedomain.co.uk &lt;&lt;MARK\r\nuser $USER $PASS\r\npwd\r\ncd data\r\nbin\r\nhash\r\nput $DATA\r\nls -ltr\r\nMARK\r\n\r\necho file transferred<\/code><\/pre>\n<\/div>\n<p>This is a small script, which despite not being very secure, makes a small connection and puts a data file. \u00a0It is only possible because the input to the ftp command can be piped in. \u00a0This is done in this clever little script be redirecting the data from the script itself.<\/p>\n<p>It is not possible to do this same trick using the sftp program, but it is possible to create a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Secure_copy\" target=\"_blank\">script using secure copy<\/a>.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\nUSER=richard\r\nMACHINE=192.168.178.57\r\nDATA=\/var\/tmp\/datafile.tar\r\n\r\nscp $DATA $USER@$MACHINE:\/tmp \r\n<\/code><\/pre>\n<\/div>\n<p>This script is even smaller than the ftp script and is more clear to the casual reader.<\/p>\n<p>The scp command\u00a0actually still requires a password and is not &#8220;scriptable&#8221; in the same way that the ftp client was. \u00a0However, it is possible to setup the user setup so the public\/private key is used for authentication and password isn&#8217;t necessary.<\/p>\n<p>&nbsp;<\/p>\n<h2>ftp Scripting &#8211; Extra credit edition<\/h2>\n<p>For the really paranoid who must continue to use ftp, you might not want to connect directly to a server over the internet but instead connect via a proxy server. \u00a0It is possible to do that using ftp. \u00a0Simply pass all the information necessary for the proxy server.<\/p>\n<div class=\"sbody-code\">\n<pre><code>#!\/bin\/bash\r\n\r\n#our destination machine\r\nUSER=dick\r\nPASS=secretpasscode\r\nDEST=ftp.somedomain.co.uk\r\n\r\n#our proxy server\r\nPROXYUSER=bob\r\nPROXYPASS=secret\r\nPROXYMACH=myproxy.mydomain.com\r\nDATA=\/var\/tmp\/datafile.tar\r\n\r\nftp -i -n $PROXYMACH &lt;&lt;MARK\r\nuser $USER@PROXYUSER@DEST $PASS@$PROXYPASS\r\ncd data\r\nbin\r\nhash\r\nput $DATA\r\nls -ltr $DATA\r\nMARK\r\n\r\necho file transferred<\/code><\/pre>\n<\/div>\n<p>Although it is possible to add this level of complexity to buffer your server from the ills of the world, it really wouldn&#8217;t be more secure than to use secure copy (scp) for transferring the data files. \u00a0Secure copy would have encrypted credentials and the key used as a &#8220;password&#8221; would most likely be considerably longer and more secure than any normal password.<\/p>\n<p>Even if you picked a small 256bit key, it would on average still be better than some 16 or 20 character \u00a0login. \u00a0Yet, a more reasonable choice of a 1024 or 2048 bit key would be massively more secure than any password selected.<\/p>\n<p><b>Nice explanation of active \/ passive ftp<\/b><br \/>\n<a href=\"http:\/\/www.slacksite.com\/other\/ftp.html\" target=\"_blank\">http:\/\/www.slacksite.com\/other\/ftp.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I must admit that I misunderstood the task when I first saw it. \u00a0The vendor had changed how they provide the data. \u00a0We will no longer be able to retrieve the data using ftp &#8211; the data should be picked &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2016\/04\/11\/command-line-fun-transferring-data\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,20],"tags":[57,17,39],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/840"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=840"}],"version-history":[{"count":18,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/840\/revisions"}],"predecessor-version":[{"id":859,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/840\/revisions\/859"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=840"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=840"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=840"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}