【问题标题】:Connect to a FTPS server with mismatched server certificate using Net::FTPTLS使用 Net::FTPTLS 连接到服务器证书不匹配的 FTPS 服务器
【发布时间】:2013-01-04 23:42:10
【问题描述】:

我正在尝试通过 Net::FTPTLS 连接到基于 Microsoft 的文件服务器 (IIS),该文件服务器配置为在端口 22 上使用 FTP,并且需要 SSL。

我通过以下方式连接:

require 'net/ftptls'
ftp = Net::FTPTLS.new()
ftp.connect('host.com', port_number)
ftp.login('Username', 'Password')
ftp.puttextfile('somefile.txt', 'where/to/save/somefile.txt')
ftp.close

问题是,我收到以下错误:

hostname does not match the server certificate

看来我必须禁用 openssl 对等验证:OpenSSL::SSL::VERIFY_PEER 应该变成 OpenSSL::SSL::VERIFY_NONE。

关于如何对 Net::FTPTLS 类进行猴子补丁有什么想法吗?有没有人成功做到这一点?

【问题讨论】:

    标签: ruby openssl ftps


    【解决方案1】:

    我所做的不是猴子修补 ruby​​ 本身,而是将它的副本带入我的项目的 /lib 中。

    module Net
    
      class FTPTLS < FTP
        def connect(host, port=FTP_PORT)
          @hostname = host
          super
        end
    
        def login(user = "anonymous", params = {:password => nil, :acct => nil, :ignore_cert => false})
          store = OpenSSL::X509::Store.new
          store.set_default_paths
          ctx = OpenSSL::SSL::SSLContext.new('SSLv23')
          ctx.cert_store = store
          ctx.verify_mode = params[:ignore_cert] ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER
          ctx.key = nil
          ctx.cert = nil
          voidcmd("AUTH TLS")
          @sock = OpenSSL::SSL::SSLSocket.new(@sock, ctx)
          @sock.connect
          @sock.post_connection_check(@hostname) unless params[:ignore_cert]
          super(user, params[:password], params[:acct])
          voidcmd("PBSZ 0")
        end
      end
    end
    

    我还清理了传递的参数。你可以这样使用:

      require 'ftptls'  # Use my local version, not net/ftptls
      @ftp_connection = Net::FTPTLS.new()
      @ftp_connection.passive = true
      @ftp_connection.connect(host, 21)
      @ftp_connection.login('user', :password => 'pass', :ignore_cert => true)
    

    HTH

    【讨论】:

      【解决方案2】:

      使用 Ruby 2.4+ 代替 Net::FTPTLS,代码如下:

      require 'net/ftp'
      ftp = Net::FTP.new(nil, ssl: {:verify_mode => OpenSSL::SSL::VERIFY_NONE})
      ftp.connect('host.com', port_number)
      ftp.login('Username', 'Password')
      ftp.puttextfile('somefile.txt', 'where/to/save/somefile.txt')
      ftp.close
      

      【讨论】:

      • 我建议在这个答案中添加更多信息。
      【解决方案3】:

      这对我来说很好。 #ROR

      ftp = Net::FTP.new("ftps.host.com", ftp_options)
      
      open("where/is/your/file/somefile.txt") do |file_data|
        ftp.putbinaryfile(file_data, 'where/to/save/somefile.txt')
      end
      
      ftp.puttextfile('somefile.txt', 'where/to/save/somefile.txt')
      
      def ftp_options
        {
          port: FTP_PORT,
          username: 'ftp_user',
          password: 'password',
          passive:  true,
          ssl: { verify_mode: 0 }
        }
      end
      

      请记住,您必须提供 ftps.hostname.com

      【讨论】:

        猜你喜欢
        • 2011-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-17
        • 2015-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多