FTP

Dealing with FTP is something needed in many cases, Let’s see how easy is that in Ruby with AIO example.

FTP Client

  1. require 'net/ftp'
  2. ftp = Net::FTP.new('rubyfu.net', 'admin', 'P@ssw0rd') # Create New FTP connection
  3. ftp.welcome # The server's welcome message
  4. ftp.system # Get system information
  5. ftp.chdir 'go/to/another/path' # Change directory
  6. file.pwd # Get the correct directory
  7. ftp.list('*') # or ftp.ls, List all files and folders
  8. ftp.mkdir 'rubyfu_backup' # Create directory
  9. ftp.size 'src.png' # Get file size
  10. ftp.get 'src.png', 'dst.png', 1024 # Download file
  11. ftp.put 'file1.pdf', 'file1.pdf' # Upload file
  12. ftp.rename 'file1.pdf', 'file2.pdf' # Rename file
  13. ftp.delete 'file3.pdf' # Delete file
  14. ftp.quit # Exit the FTP session
  15. ftp.closed? # Is the connection closed?
  16. ftp.close # Close the connection

Yep, it’s simple as that, easy and familiar.

TIP: You can do it all above way using pure socket library, it’s really easy. You may try to do it.

FTP Server

  • Install ftpd gem
    1. gem install ftpd
  1. #
  2. # Pure Ruby FTP server
  3. # KING SABRI | @KINGSABRI
  4. #
  5. require 'ftpd'
  6. class Driver
  7. attr_accessor :path, :user, :pass
  8. def initialize(path)
  9. @path = path
  10. end
  11. def authenticate(user, password)
  12. true
  13. end
  14. def file_system(user)
  15. Ftpd::DiskFileSystem.new(@path)
  16. end
  17. end
  18. class FTPevil
  19. def initialize(path=".")
  20. @driver = Driver.new(File.expand_path(path))
  21. @server = Ftpd::FtpServer.new(@driver)
  22. configure_server
  23. print_connection_info
  24. end
  25. def configure_server
  26. @server.server_name = "Rubyfu FTP Server"
  27. @server.interface = "0.0.0.0"
  28. @server.port = 21
  29. end
  30. def print_connection_info
  31. puts "[+] Servername: #{@server.server_name}"
  32. puts "[+] Interface: #{@server.interface}"
  33. puts "[+] Port: #{@server.port}"
  34. puts "[+] Directory: #{@driver.path}"
  35. puts "[+] User: #{@driver.user}"
  36. puts "[+] Pass: #{@driver.pass}"
  37. puts "[+] PID: #{$$}"
  38. end
  39. def start
  40. @server.start
  41. puts "[+] FTP server started. (Press CRL+C to stop it)"
  42. $stdout.flush
  43. begin
  44. loop{}
  45. rescue Interrupt
  46. puts "\n[+] Closing FTP server."
  47. end
  48. end
  49. end
  50. if ARGV.size >= 1
  51. path = ARGV[0]
  52. else
  53. puts "[!] ruby #{__FILE__} <PATH>"
  54. exit
  55. end
  56. FTPevil.new(path).start

Run it

  1. ruby ftpd.rb .
  2. Interface: 0.0.0.0
  3. Port: 21
  4. Directory: /tmp/ftp-share
  5. User:
  6. Pass:
  7. PID: 2366
  8. [+] FTP server started. (Press CRL+C to stop it)