iptables证明数据包(80,443,21)访问都是通过防火墙

实验环境

物理机上跑两个虚拟机A和B。
主机A,配置双网卡,172.25.254.2 和 1.1.1.1 (网关172.254.25.23)
iptables证明数据包(80,443,21)访问都是通过防火墙
主机B,ip 1.1.1.100 (网关1.1.1.1)
iptables证明数据包(80,443,21)访问都是通过防火墙
主机A 可以上网,AB之间互通,主机B不能上网,主机B可以通过A(路由转发)上网

  • 主机A:
    iptables证明数据包(80,443,21)访问都是通过防火墙
    iptables证明数据包(80,443,21)访问都是通过防火墙
  • 主机B
    iptables证明数据包(80,443,21)访问都是通过防火墙
    iptables证明数据包(80,443,21)访问都是通过防火墙

一、外网访问内网数据包都是经过防火墙的

主机A上:

  • 开启路由功能
    iptables证明数据包(80,443,21)访问都是通过防火墙

  • 查看防火墙及nat表规则(设置为无规则iptables -Fiptables -nL
    iptables证明数据包(80,443,21)访问都是通过防火墙
    iptables证明数据包(80,443,21)访问都是通过防火墙

  • 设置路由规则,将数据包路由到eth0网卡,将原ip封装成172.25.254.2
    iptables -t nat -A POSTROUTING -o eth0 -j SNAT --to-source 172.25.254.2
    text -t nat 表示nat表 -A 添加 POSTROUTING 路由后 -o eth0 数据包出去的时候走eth0 网卡 -j SNAT 动作为原地址转换 --to-source 设置原地址为172.25.254.2
    iptables证明数据包(80,443,21)访问都是通过防火墙

  1. 此时B主机就可以访问百度了,因为可以经过主机A的转发
    iptables证明数据包(80,443,21)访问都是通过防火墙
  2. 也可利用ssh查看一下具体的封装ip
    在B主机上ssh连接物理机
    iptables证明数据包(80,443,21)访问都是通过防火墙 在物理机上可以看到原ip并不是B(1.1.1.100)的,而是A主机的ip(经过了A的封装后的ip 172.25.254.2)
    iptables证明数据包(80,443,21)访问都是通过防火墙

二、内网访问http/https都是经过防火墙的

  • 没有任何规则的时候,http/https都可以访问到。
    iptables证明数据包(80,443,21)访问都是通过防火墙

  • 现在配置规则,关闭80、443端口
    先关闭80
    iptables -A OUTPUT -p tcp --dport 80 -j REJECT
    -A OUTPUT 添加出栈 -p tcp 协议类型 --dport 80 端口 -j REJECT 动作为拒绝
    iptables证明数据包(80,443,21)访问都是通过防火墙
    关闭80后,http访问不通,https依旧可以访问
    iptables证明数据包(80,443,21)访问都是通过防火墙

  • 现在打开80端口,关闭443端口
    iptables证明数据包(80,443,21)访问都是通过防火墙
    发现https不能访问,但是http可以访问了。
    iptables证明数据包(80,443,21)访问都是通过防火墙
    现在关闭80和443端口
    iptables证明数据包(80,443,21)访问都是通过防火墙
    发现现在http和http都无法访问
    iptables证明数据包(80,443,21)访问都是通过防火墙
    现在我们删除所有规则
    iptables证明数据包(80,443,21)访问都是通过防火墙
    现在http和https都可以访问了
    iptables证明数据包(80,443,21)访问都是通过防火墙

三、内网访问ftp都是经过防火墙的

  • 先连接一个万维网ftp服务器ftp://ftp.scene.org
    iptables证明数据包(80,443,21)访问都是通过防火墙
  • 现在配置规则禁止21端口(用于连接)、20端口(用于传输数据)
    iptables证明数据包(80,443,21)访问都是通过防火墙
    访问页面Unable to connect
    iptables证明数据包(80,443,21)访问都是通过防火墙
    现在关闭规则iptables -F,然后刷新页面,可以得到服务器响应。
    iptables证明数据包(80,443,21)访问都是通过防火墙


谢谢!