A-A+

利用路由器静态PAT访问FTP,测试主动和被动模式的方法

2015年11月20日 站长资讯 暂无评论

这是一个通过路由器的静态PAT来访问FTP服务器(Server)的测试,把最常见的两种FTP模式(主动模式和被动模式)都要做一遍。可以深入理解日常ftp操作的方法,对处理组网中的传输故障也很有帮助。

1.本方法是在cisco模拟器上完成的,测试的拓扑图很简单就不画出来了,路由器R2(作为ftp客户端)连接路由器R1,再连接一个FTP服务器,其ip地址和端口配置如下:

R2 (FTP Client 202.100.1.2/24)-------------------(202.100.1.2/24) R1 (10.1.1.1/24)---------------------(10.1.1.18/24)FTP Server

参考链接:http://www.zhangdaqian.net/blog/atpat 中对于ftp的处理,以及pat后的非标准21-ftp端口设置.htm

2.测试目标:

通过测试,理解FTP的两种模式:被动模式,主动模式

A.被动模式:

command :Client (源端口>tcp 1024) ------>FTP Server (目标端口tcp 21)

data:Client (目标端口>tcp 1024) ------ > FTP Server (源端口>tcp 1024)

B.主动模式:

command:Client (源端口>tcp 1024) ------>FTP Server (目标端口tcp 21)

data:Client (目标端口>tcp 1024) <------ FTP Server (源端口tcp 20) 3.基本配置:

  1. R1:  
  2. config t  
  3. interface Ethernet0/0  
  4.  ip address 202.100.1.1 255.255.255.0  
  5.  no shutdown  
  6. interface Ethernet0/1  
  7.  ip address 10.1.1.1 255.255.255.0  
  8.  no shutdown  
  9.   
  10. R2:  
  11. config t  
  12. interface Ethernet0/0  
  13.  ip address 202.100.1.2 255.255.255.0  
  14.  no shutdown  

FTP服务器:

IP:10.1.1.18/25

GW:10.1.1.1

4.R1路由器PAT配置:

A.先只配置静态PAT

  1. config t  
  2. interface Ethernet0/0  
  3.  ip nat outside  
  4. interface Ethernet0/1  
  5.  ip nat inside  
  6. ip nat inside source static tcp 10.1.1.18 21 interface e0/0 21  

B.查看静态PAT设置

  1. R1#show ip nat translations    
  2. Pro Inside global      Inside local       Outside local      Outside global  
  3. tcp 202.100.1.1:21     10.1.1.18:21       ---                ---  

5.FTP访问测试:

①FTP被动模式测试

A.R2作为FTP客户端配置FTP客户端为被动模式:

config t

ip ftp passive

----备注:思科路由器默认采用的是被动模式,可以不用敲

B.R2作为FTP客户端配置FTP用户名密码:

config t

ip ftp username xll

ip ftp password 1234qwer

B.R2作为客户端可以成功拷贝文件:

  1. R2#copy ftp://202.100.1.1 flash:  
  2. Address or name of remote host [202.100.1.1]?   
  3. Source filename [watch.sh]?   
  4. Destination filename [watch.sh]?   
  5. %Warning:There is a file already existing with this name   
  6. Do you want to over write? [confirm]  
  7. Accessing ftp://202.100.1.1/watch.sh...  
  8. Erase flash: before copying? [confirm]  
  9. Erasing the flash filesystem will remove all files! Continue? [confirm]  
  10. Erasing device... eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ...erased  
  11. Erase of flash: complete  
  12. Loading watch.sh !  
  13. [OK - 986/4096 bytes]  
  14.   
  15. Verifying checksum...  OK (0xE972)  
  16. 986 bytes copied in 7.492 secs (132 bytes/sec)  

C.在文件拷贝过程中,在R1查看NAT转换:

  1. R1#show ip nat translations   
  2. Pro Inside global      Inside local       Outside local      Outside global  
  3. tcp 202.100.1.1:21     10.1.1.18:21       202.100.1.2:30662  202.100.1.2:30662  
  4. tcp 202.100.1.1:21     10.1.1.18:21       202.100.1.2:35984  202.100.1.2:35984  
  5. tcp 202.100.1.1:21     10.1.1.18:21       202.100.1.2:46087  202.100.1.2:46087  
  6. tcp 202.100.1.1:21     10.1.1.18:21       202.100.1.2:48529  202.100.1.2:48529  
  7. tcp 202.100.1.1:21     10.1.1.18:21       ---                ---  
  8. tcp 202.100.1.1:1576   10.1.1.18:1576     202.100.1.2:28656  202.100.1.2:28656   
  9. tcp 202.100.1.1:1577   10.1.1.18:1577     202.100.1.2:30584  202.100.1.2:30584  
  10. tcp 202.100.1.1:1578   10.1.1.18:1578     202.100.1.2:48461  202.100.1.2:48461  

-----可出看出:在对外映射的时侯采用了21端口,IOS会自动识别这个端口是FTP控制口,

从而去检查里面FTP数据控制数据发现服务器告诉客户端的这个用于传送数据的端口,然后自动的添加映射。

②FTP主动模式测试

A.R2作为FTP客户端配置FTP客户端为主动模式:

config t

no ip ftp passive

B.R2作为FTP客户端配置FTP用户名密码:

--同上步

B.R2作为客户端可以成功拷贝文件,

  1. R2#debug ip ftp   
  2. *Mar  1 01:00:05.831: %SYS-5-CONFIG_I: Configured from console by console  
  3. R2#copy ftp://202.100.1.1 flash:  
  4. Address or name of remote host [202.100.1.1]?   
  5. Source filename [watch.sh]?   
  6. Destination filename [watch.sh]?   
  7. %Warning:There is a file already existing with this name   
  8. Do you want to over write? [confirm]  
  9. Accessing ftp://202.100.1.1/watch.sh...  
  10. *Mar  1 01:00:14.627: FTP: 220 3Com 3CDaemon FTP Server Version 2.0  
  11. *Mar  1 01:00:14.627: FTP: ---> USER xll  
  12. *Mar  1 01:00:15.211: FTP: 331 User name ok, need password  
  13. *Mar  1 01:00:15.215: FTP: ---> PASS 1234qwer  
  14. *Mar  1 01:00:15.759: FTP: 230 User logged in  
  15. *Mar  1 01:00:15.759: FTP: ---> TYPE I   
  16. *Mar  1 01:00:16.211: FTP: 200 Type set to I.  
  17. *Mar  1 01:00:16.211: FTP: ---> PORT 202,100,1,2,68,45  
  18. *Mar  1 01:00:16.759: FTP: 200 PORT command successful.  
  19. *Mar  1 01:00:16.763: FTP: ---> RETR watch.sh  
  20. *Mar  1 01:00:17.183: FTP: 150 File status OK ; about to open data connection  

----可以看出停在了数据连接状态,如果这时配合用wireshark抓包的话,可以看到源地址为10.1.1.18,源端口为tcp 20的包到达了R2,

因为R2无法连接10.1.1.18(没有路由),所有数据通讯端口无法建立

---下面是在R2上提前开启debug的抓包情况

  1. R2#debug ip tcp packet port 20  
  2. R2#  
  3. *Mar  1 00:25:18.959: %SYS-5-CONFIG_I: Configured from console by vty1 (202.100.1.1)  
  4. R2#  
  5. *Mar  1 00:25:39.103: tcp0: I LISTEN 10.1.1.18:20 202.100.1.2:25901 seq 1802999574  
  6.         OPTS 24 SYN  WIN 65535  
  7. R2#  
  8. *Mar  1 00:25:42.131: tcp0: I LISTEN 10.1.1.18:20 202.100.1.2:25901 seq 1802999574   
  9.         OPTS 24 SYN  WIN 65535  
  10. R2#  
  11. *Mar  1 00:25:48.151: tcp0: I LISTEN 10.1.1.18:20 202.100.1.2:25901 seq 1802999574  
  12.         OPTS 24 SYN  WIN 65535  

---这时console口已经死掉,只能重启路由器

C.R1配置动态PAT

configure t

access-list 10 permit 10.1.1.0 0.0.0.255

ip nat inside source list 10 interface ethernet 0/0 overload

D.再次拷贝文件,可以成功

  1. R2#copy ftp://202.100.1.1 flash:  
  2. Source filename []? watch.sh  
  3. Destination filename [watch.sh]?   
  4. %Warning:There is a file already existing with this name   
  5. Do you want to over write? [confirm]  
  6. Accessing ftp://202.100.1.1/watch.sh...  
  7. Erase flash: before copying? [confirm]  
  8. Erasing the flash filesystem will remove all files! Continue? [confirm]  
  9. Erasing device... eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ...erased  
  10. Erase of flash: complete  
  11. Loading watch.sh !  
  12. [OK - 986/4096 bytes]  
  13.   
  14. Verifying checksum...  OK (0xE972)  
  15. 986 bytes copied in 7.704 secs (128 bytes/sec)  
  16. R2#  

----从R2上tcp 20的抓包来看,tcp三次握手是由FTP服务器发起的:

  1. R2#debug ip tcp packet port 20  
  2. TCP Packet debugging is on for port number 20  
  3. *Mar  1 00:57:00.967: tcp0: I LISTEN 202.100.1.1:20 202.100.1.2:21993 seq 84304373  
  4.         OPTS 24 SYN  WIN 65535  
  5. *Mar  1 00:57:00.971: tcp0: O SYNRCVD 202.100.1.1:20 202.100.1.2:21993 seq 2180803741  
  6.         OPTS 4 ACK 84304374 SYN  WIN 8192  
  7. *Mar  1 00:57:01.035: tcp0: I SYNRCVD 202.100.1.1:20 202.100.1.2:21993 seq 84304374  
  8.         ACK 2180803742  WIN 65535  
  9. *Mar  1 00:57:01.039: tcp0: I ESTAB 202.100.1.1:20 202.100.1.2:21993 seq 84304374  
  10.         DATA 986 ACK 2180803742 PSH  WIN 65535  
  11. *Mar  1 00:57:01.043: tcp0: I ESTAB 202.100.1.1:20 202.100.1.2:21993 seq 84305360  
  12.         ACK 2180803742 FIN  WIN 65535  

---在路由器上可以通debug ip ftp查看完整的FTP过程,可以看到客户端是否采用主动模式取决于客户端是否会发出PASV命令

6.修改FTP端口:

如果修改FTP服务器的端口为2121,在路由器配置静态NAT如下:

ip nat inside source static tcp 10.1.1.18 2121 interface Ethernet0/0 21

此时如果FTP客户端采用FTP主动模式与FTP服务器连接是没有问题的,但是如果采用FTP被动模式,因为R1作为NAT设备没有临时设置静态PAT,导致客户端连接服务器的数据端口失败,因此需要在R1上配置告诉R1路由器FTP服务器的端口。

access-list 20 permit 10.1.1.18

ip nat service list 20 ftp tcp port 2121

标签:

给我留言