Iptables

使用 IPTables 轉發 FTP

  • May 21, 2014

我正在嘗試使用 IPTables 轉發 FTP 伺服器。FTP 伺服器在 Windows 2008 機器 (Cerberus) 上執行。

圖片

詳細資訊贏箱:

  • IP:192.168.220.51
  • FTP埠:21
  • PASV 埠:11000-13000

FTP 伺服器在 LAN 中執行良好。

路由器在其他客戶端(服務 NAT、DHCP、防火牆…)上執行良好。我需要將 FTP 服務轉發到外界,但我不能使用埠 20-21(已經佔用)。

我試過這個,但沒有奏效:

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 2121 -j DNAT --to 192.168.220.51:21

目前 IPTables 配置:

[root@router ~]# service iptables status
Table: nat
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    MASQUERADE  all  --  0.0.0.0/0            0.0.0.0/0           
2    MASQUERADE  all  --  0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Table: filter
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  127.0.0.1            0.0.0.0/0           
2    DROP       all  --  0.0.0.0/0            0.0.0.0/0           
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:2222 
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:67 
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:68 
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:1194 
7    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 
8    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:443 
9    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:3128 
10   ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:53 
11   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:53 
12   ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:1194 
13   ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
14   DROP       udp  --  0.0.0.0/0            0.0.0.0/0           
15   DROP       tcp  --  0.0.0.0/0            0.0.0.0/0           tcp flags:0x17/0x02 

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

任何幫助都會很棒:-)。

賞金編輯:我無法弄清楚這一點,任何幫助將不勝感激。

編輯2

在執行命令後,我現在可以遠端登錄到我的 FTP 伺服器:

modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 2121 -j DNAT --to 192.168.220.51:21

我開始到達那裡……我想我現在只需要讓我的 PASV-Ports 工作……

EDIT3:額外資訊

[root@router ~]# iptables -L -n -v
Chain INPUT (policy ACCEPT 3251 packets, 154K bytes)
pkts bytes target     prot opt in     out     source               destination         
 540 48534 ACCEPT     all  --  *      *       127.0.0.1            0.0.0.0/0           
4270K 5625M ACCEPT     all  --  *      *       192.168.220.0/24     0.0.0.0/0           
   0     0 DROP       all  --  eth1   *       0.0.0.0/0            0.0.0.0/0           
   0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:2222 
   0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:67 
   0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:68 
   0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:1194 
  65  8487 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:8080 
   0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:443 
   8   404 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:3128 
   0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp dpt:53 
   0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:53 
   0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            0.0.0.0/0           udp dpt:1194 
63870   81M ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED 
 974  224K DROP       udp  --  *      *       0.0.0.0/0            0.0.0.0/0           
 638 34956 DROP       tcp  --  *      *       0.0.0.0/0            0.0.0.0/0           tcp flags:0x17/0x02 

Chain FORWARD (policy ACCEPT 3578K packets, 3355M bytes)
pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 2275K packets, 703M bytes)
pkts bytes target     prot opt in     out     source               destination  



[root@router ~]# iptables -L -n -v -t nat
Chain PREROUTING (policy ACCEPT 33954 packets, 2595K bytes)
pkts bytes target     prot opt in     out     source               destination         
   0     0 DNAT       tcp  --  eth0   *       0.0.0.0/0            0.0.0.0/0           tcp dpt:2121 to:192.168.220.51:21 

Chain POSTROUTING (policy ACCEPT 5925 packets, 699K bytes)
pkts bytes target     prot opt in     out     source               destination         
   0     0 MASQUERADE  all  --  *      eth1    0.0.0.0/0            0.0.0.0/0           
27170 1785K MASQUERADE  all  --  *      eth0    0.0.0.0/0            0.0.0.0/0           

Chain OUTPUT (policy ACCEPT 5777 packets, 457K bytes)
pkts bytes target     prot opt in     out     source               destination  

解決方案

我就是這樣做的,可能不是很漂亮,但它有效。

[root@router ~]# iptables -t nat -I PREROUTING -p tcp --dport 2121 -j DNAT --to 192.168.220.51:2121
[root@router ~]# iptables -I FORWARD -p tcp -d 192.168.220.51 --dport 2121 -j ACCEPT
[root@router ~]# iptables -t nat -I PREROUTING -p tcp --dport 11000:13000 -j DNAT --to 192.168.220.51:11000-13000
[root@router ~]# iptables -I FORWARD -p tcp -d 192.168.220.51 --dport 11000:13000 -j ACCEPT

一些額外說明:FTP 伺服器正在偵聽 bot 21 和 2121,並且 PASV 範圍設置為 11000 到 13000

要獲得被動支持,您需要將被動埠轉發到具有****相同埠號的內部 ftp 伺服器。

簡單的解決方案:一直轉發它們

InternetIP:11000-13000 -> 192.168.220.51:11000-13000

更難的解決方案:

您將需要一些特殊用途的連接跟踪代理來讀取 FTP 協議並及時進行正確的 NAT 映射。看起來 ip_conntrack_ftp 可能會這樣做。

我相信您需要載入 FTP 連接跟踪模組。

http://www.sns.ias.edu/~jns/wp/2006/01/12/iptables-connection-tracking-ftp/

引用自:https://serverfault.com/questions/252425