AWS VPC私有子网后NAT实例的FTP访问

6
我在AWS上创建了一个包含公有子网和私有子网的VPC。所有应用服务器都位于私有子网中,所有出站请求必须通过面向互联网的NAT实例进行。
目前,我们的项目需要应用服务器访问服务提供商提供的ftp服务器。
我已经尝试了几种方法来解决这个问题,但都没有成功。我所做的是在NAT和应用程序安全组上打开一个端口范围,比如(40000-60000)以及标准ftp端口20-21。
用户认证可以通过,但我无法从应用服务器列出内容。
我能够从NAT访问ftp服务器,完全没有问题。
那么我该怎么做才能使其正常工作呢?

这个问题在 Digital Ocean 上也出现了吗? - Ihtisham Tanveer
2个回答

4

@JohnRotenstein 绝对正确,如果可以的话,你应该使用被动 FTP。但是如果像我一样,你被客户卡住,他们坚持要求你使用主动式FTP,因为他们想要你连接到的FTP站点自1990年以来一直在运行,现在更改它是完全不合理的,那么请继续阅读。

AWS的NAT服务器不支持处于私有子网中的机器使用主动式FTP进行连接。结束语。如果你问我,这是个漏洞,但如果你问AWS支持团队,他们会说这是一个不支持的功能。

我们最终想出的解决方案(并且有效)是:

  • Add an Elastic Network Interface (ENI) in a public subnet on to your EC2 instance in the private subnet
    • So now your EC2 instance has 2 network adapters, 2 internal IPs, etc.
    • Let's call this new ENI your "public ENI"
  • Attach a dedicated elastic IP to your new public ENI
    • Let's assume you get 54.54.54.54 and the new public ENI's internal IP address is 10.1.1.10
  • Add a route in your operating system's networking configuration to only use the new public ENI

    • In windows, the command will look like this, assuming the evil active ftp server you're trying to connect to is at 8.1.1.1:

      route add 8.1.1.1 mask 255.255.255.254 10.1.1.1 metric 2
      
    • This adds a route for all traffic to the FTP server at 8.1.1.1 using subnet mask 255.255.255.254 (ie. this IP and only this IP) should go to the internet gateway 10.1.1.1 using ethernet adapter 2 (your second NIC)

  • Fed up yet? Yeah, me too, but now comes the hard part. The OS doesn't know it's public IP address for the public EIN. So you need to teach your FTP client to send the PORT command with the public IP. For example if using CURL, use the --ftp-port command like so:

    curl -v --ftp-port 54.54.54.54 ftp://8.1.1.1 --user myusername:mypass
    

完成!现在您可以从几乎完全位于私有子网中的EC2机器连接到恶梦般的活动FTP站点。


我能够在没有路由的情况下完成curl部分。只需告诉--ftp-port AWS分配给实例的公共IP即可。请注意,自7.19.5版本以来,您可以在IP之后指定范围。因此,现在用户不必打开所有端口,而只需在AWS防火墙中打开一个范围即可。 - jgomo3

3
尝试在FTP上使用被动模式(PASV)。
从Slacksite获得信息:主动FTP与被动FTP,明确的解释
在主动FTP中,客户端从一个随机的未特权端口(N > 1023)连接到FTP服务器的命令端口,即端口21。然后,客户端开始侦听端口N+1,并向FTP服务器发送FTP命令PORT N+1。然后,服务器将从其本地数据端口(端口20)回连到客户端指定的数据端口。
因此,流量正在尝试在未通过NAT的附加端口上进行通信。相反,被动模式创建了一个出站连接,然后将被允许通过NAT。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接