{"id":197,"date":"2020-06-13T19:49:10","date_gmt":"2020-06-13T18:49:10","guid":{"rendered":"https:\/\/blog.thomarite.uk\/?p=197"},"modified":"2020-06-13T19:49:10","modified_gmt":"2020-06-13T18:49:10","slug":"ftp-passive","status":"publish","type":"post","link":"https:\/\/blog.thomarite.uk\/index.php\/2020\/06\/13\/ftp-passive\/","title":{"rendered":"FTP Passive"},"content":{"rendered":"\n<p>I have a supplier at my employer that requires to use a FTP server to send big files when you open a support ticket. For a long time (a couple of years) whenever I had to upload big files, I had to use my personal VM because my ftp connections failed from the office. I always blamed the super-smart firewall.<\/p>\n\n\n\n<p>One day, I decided to fix the issue and allow the connection in our corporate firewall. I failed. Still couldnt upload files from the office. So keep using my personal VM.<\/p>\n\n\n\n<p>This week I had to upload again a big file. This time I am working from home, so  pretty much it is going to work the upload. Wrong! It fails. Ok, I checked a bit and got to the conclusion that it is my ISP or modem at home that is blocking FTP. Most ISP use <a href=\"https:\/\/en.wikipedia.org\/wiki\/Carrier-grade_NAT\">CGN<\/a> to stretch as much as possible the limited IPv4. I have IPv6 at home and my VM has IPv6 too&#8230; but the ftp server doesnt.<\/p>\n\n\n\n<p>I checked the internet if there was any know issue with my ISP and FTP connections. No luck. I connected to my modem, nothing obvious messing around with FTP.<\/p>\n\n\n\n<p>I decided to give it a proper go to this issue. I knew that it worked from my VM and it didnt from home. I noticed that I was running the same ftp client version in the VM and at home. So let&#8217;s debug the ftp client and take a packet capture in both locations.<\/p>\n\n\n\n<p>CLI from the VM:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ftp -vd b.b.b.b\nftp: setsockopt: Bad file descriptor\nName: ftp\n---> USER ftp\n331 Please specify the password.\nPassword:\n---> PASS XXXX\n230 Login successful.\n---> SYST\n215 UNIX Type: L8\nRemote system type is UNIX.\nUsing binary mode to transfer files.\nftp> cd support\n---> CWD support\n250 Directory successfully changed.\nftp> cd 211211\n---> CWD 211211\n250 Directory successfully changed.\nftp> put TEST.txt\nlocal: TEST.txt remote: TEST.txt\n---> TYPE I\n200 Switching to Binary mode.\nftp: setsockopt (ignored): Permission denied\n---> PORT a,a,a,a,162,57\n200 PORT command successful. Consider using PASV.\n---> STOR TEST.txt\n150 Ok to send data.\n226 Transfer complete.\n28 bytes sent in 0.00 secs (854.4922 kB\/s)\nftp> quit\n---> QUIT<\/pre>\n\n\n\n<p>And this is the packet capture:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"387\" src=\"https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/vps-ftp-ok-1024x387.png\" alt=\"\" class=\"wp-image-198\" srcset=\"https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/vps-ftp-ok-1024x387.png 1024w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/vps-ftp-ok-300x113.png 300w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/vps-ftp-ok-768x290.png 768w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/vps-ftp-ok-1200x453.png 1200w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/vps-ftp-ok.png 1526w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>After typing &#8220;put&#8221; in packet 33, I see a &#8220;PASV&#8221; message from the server and a new connection (initiated by the server!) is established for the data transfer. All good.<\/p>\n\n\n\n<p>So now, make the same from home and compare.<\/p>\n\n\n\n<p>CLI from home without debug:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ftp b.b.b.b\nConnected to b.b.b.b.\nName: ftp\n331 Please specify the password.\nPassword:\n230 Login successful.\nRemote system type is UNIX.\nUsing binary mode to transfer files.\nftp> cd support\n250 Directory successfully changed.\nftp> cd 211211\n250 Directory successfully changed.\nftp> put TEST.txt\nlocal: TEST.txt remote: TEST.txt\n500 Illegal PORT command.\nftp: bind: Address already in use\nftp> quit\n221 Goodbye.<\/pre>\n\n\n\n<p>CLI from home with debug:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ftp -vd b.b.b.b\nftp: setsockopt: Bad file descriptor\nName: ftp\n---> USER ftp\n331 Please specify the password.\nPassword:\n---> PASS XXXX\n230 Login successful.\n---> SYST\n215 UNIX Type: L8\nRemote system type is UNIX.\nUsing binary mode to transfer files.\nftp> cd support\n---> CWD support\n250 Directory successfully changed.\nftp> cd 211211\n---> CWD 211211\n250 Directory successfully changed.\nftp> put TEST.txt\nlocal: TEST.txt remote: TEST.txt\n---> TYPE I\n200 Switching to Binary mode.\nftp: setsockopt (ignored): Permission denied\n---> PORT 192,168,1,158,202,145\n500 Illegal PORT command.\nftp: bind: Address already in use\nftp> quit\n---> QUIT\n221 Goodbye.<\/pre>\n\n\n\n<p>So with and without debug I keep seeing &#8220;ftp: bind: Address already in use&#8221;&#8230;..<\/p>\n\n\n\n<p>And this is the packet capture from home:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"283\" src=\"https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-nok-1024x283.png\" alt=\"\" class=\"wp-image-199\" srcset=\"https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-nok-1024x283.png 1024w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-nok-300x83.png 300w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-nok-768x212.png 768w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-nok-1200x331.png 1200w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-nok.png 1236w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>So after I type &#8220;put&#8221; in packet 32, the answer from the server is a &#8220;500&#8221;.<\/p>\n\n\n\n<p>I wasnt clearly paying attention to the clues. I was still banging my head why the server was sending a &#8220;500 Ilegal PORT command&#8221;.<\/p>\n\n\n\n<p>I was comparing both captures and both debug outputs&#8230; but still didnt it.<\/p>\n\n\n\n<p>I thought I understood FTP. I knew that you use port TCP 21 to establish the control session and the data session \/ transfer is via new TCP session using a random port. That&#8217;s one of the reasons that using NAT or CGN can screw up your FTP sessions.<\/p>\n\n\n\n<p>So I assumed that the issues wasnt my ISP. So it had to be my side (or me).<\/p>\n\n\n\n<p>So finally, I decided to search for &#8220;ftp: bind: Address already in use&#8221; as it was the message that came up with and without debugging.<\/p>\n\n\n\n<p>Oh boy, first entry in the face!<\/p>\n\n\n\n<p><a href=\"https:\/\/www.linuxquestions.org\/questions\/linux-distributions-5\/problems-with-ftp-server-bind-address-allready-in-use-213509\/\">https:\/\/www.linuxquestions.org\/questions\/linux-distributions-5\/problems-with-ftp-server-bind-address-allready-in-use-213509\/<\/a><\/p>\n\n\n\n<p>An entry from 2004&#8230;. it can&#8217;t fix my problem for sure&#8230;. keep reading and update from 2020&#8230; it says it works&#8230;. oh boy II<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">try using a passive connection with \"ftp -p\" instead, see if it helps...<\/pre>\n\n\n\n<p>There we go:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ ftp -vdp b.b.b.b\nftp: setsockopt: Bad file descriptor\nName: ftp\n---> USER ftp\n331 Please specify the password.\nPassword:\n---> PASS XXXX\n230 Login successful.\n---> SYST\n215 UNIX Type: L8\nRemote system type is UNIX.\nUsing binary mode to transfer files.\nftp> cd support\n---> CWD support\n250 Directory successfully changed.\nftp> cd 211211\n---> CWD 211211\n250 Directory successfully changed.\nftp> put TEST.txt\nlocal: TEST.txt remote: TEST.txt\n---> TYPE I\n200 Switching to Binary mode.\nftp: setsockopt (ignored): Permission denied\n---> PASV\n227 Entering Passive Mode (b,b,b,b,46,248).\n---> STOR TEST.txt\n150 Ok to send data.\n226 Transfer complete.\n26 bytes sent in 0.00 secs (12.5386 kB\/s)\nftp> quit\n---> QUIT\n221 Goodbye.<\/pre>\n\n\n\n<p>it worked !!!<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"383\" src=\"https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-pas-ok-1024x383.png\" alt=\"\" class=\"wp-image-200\" srcset=\"https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-pas-ok-1024x383.png 1024w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-pas-ok-300x112.png 300w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-pas-ok-768x287.png 768w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-pas-ok-1200x449.png 1200w, https:\/\/blog.thomarite.uk\/wp-content\/uploads\/2020\/06\/home-ftp-pas-ok.png 1491w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/figure>\n\n\n\n<p>I felt embarrassed.  Time to search for FTP passive vs active&#8230;<\/p>\n\n\n\n<figure class=\"wp-block-embed-wordpress wp-block-embed is-type-wp-embed is-provider-titan-ftp-server\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"sumOntx8M8\"><a href=\"https:\/\/titanftp.com\/2018\/08\/23\/what-is-the-difference-between-active-and-passive-ftp\/\">What is the Difference Between Active and Passive FTP?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;What is the Difference Between Active and Passive FTP?&#8221; &#8212; Titan FTP Server\" src=\"https:\/\/titanftp.com\/2018\/08\/23\/what-is-the-difference-between-active-and-passive-ftp\/embed\/#?secret=sumOntx8M8\" data-secret=\"sumOntx8M8\" width=\"600\" height=\"338\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Really good explanation. I hope I will never forget it.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>FTP <strong>Active<\/strong>: The client issues a PORT command to the server signalling that it will \u201cactively\u201d provide an IP and port number so <strong>the server opens<\/strong> the Data Connection back to the client.<\/li><\/ul>\n\n\n\n<ul class=\"wp-block-list\"><li>FTP <strong>Passive<\/strong>: The client issues a PASV command to indicate that it will wait \u201cpassively\u201d for the server to supply an IP and port number, after which <strong>the client opens<\/strong> a Data Connection to the server.<\/li><\/ul>\n\n\n\n<p>So it worked in my VM because somehow the ftp server sent a PASV command (maybe because it detects there is no NAT as I have a public IP???). <\/p>\n\n\n\n<p>From home, it failed because, by default, the connection is ftp active, so when the server tried to open the new data connection to me(something I couldnt see in the packet capture&#8230;) it failed as my ADSL modem wouldnt allow inbound connections.<\/p>\n\n\n\n<p>Once I enabled &#8220;-p&#8221; in my connection to the server, all worked because it was me who started the new data connection and my firewall allows everything outbound.<\/p>\n\n\n\n<p>Happy to solve the problem after a couple of years, and after a couple of hours of &#8220;serious&#8221; troubleshooting. It was shocking how blind I was. I had the ftp error message and the PASV from the trace.<\/p>\n\n\n\n<p>Anyway, I learned something new.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a supplier at my employer that requires to use a FTP server to send big files when you open a support ticket. For a long time (a couple of years) whenever I had to upload big files, I had to use my personal VM because my ftp connections failed from the office. I &hellip; <a href=\"https:\/\/blog.thomarite.uk\/index.php\/2020\/06\/13\/ftp-passive\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;FTP Passive&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2],"tags":[],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-unix","category-networks"],"_links":{"self":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":1,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":201,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/posts\/197\/revisions\/201"}],"wp:attachment":[{"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.thomarite.uk\/index.php\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}