iptables实战案例
目录
一、实验拓扑
二、网络规划
三、实验要求
四、环境准备
1.firewall
(1)配置防火墙各大网卡IP并禁用 firewall和selinux
(2)打开firewall路由转发
2.PC1(内网)
(1)配置防火墙各大网卡IP并禁用 firewall和selinux
3.PC2外网
(1)配置防火墙各大网卡IP并禁用 firewall和selinux
4.服务器
(1)配置防火墙各大网卡IP并禁用 firewall和selinux
(3)下载nginx,并开通服务
5.环境检测
(1)内网连接
(2)外网连接
五、防火墙配置及测试
一、实验拓扑
二、网络规划
内部PC1位于内网区域,地址段为: 192.168.1.0/24,pc1地址为:192.168.1.1/24,网关地址为:192.168.1.254/24
服务器S1位于服务器区域,地址段为: 192.168.2.0/24,s1地址为:192.168.2.1/24,网关地址为:192.168.2.254/24
PC2位于互联网区域,模拟外部互联网,地址段为:10.0.0.0/8,pc2地址为:10.0.0.1/8
Linux防火墙的三块网卡为别连接不同的网络区域,地址分别为 :ens33 192.168.1.254/24;ens34 10.0.0.100/8;ens35 192.168.2.254/24
三、实验要求
内部网络中的pc1采用SNAT访问外部互联网,但是无法ping到内部网关。
内部网络服务器s1通过DNAT发布服务到互联网。
互联网主机pc2能够访问DMZ区域的服务器,但是不能够进行ping和ssh连接。
四、环境准备
1.firewall
(1)配置防火墙各大网卡IP并禁用 firewall和selinux
#配置网卡[root@SERVER1 ~]# cd /etc/sysconfig/network-scripts/[root@SERVER1 network-scripts]# vim ifcfg-ens160TYPE=EthernetBOOTPROTO=staticNAME=ens160UUID=e9cd890f-3b47-4859-8c19-3040664051f7DEVICE=ens160ONBOOT=yesIPADDR=192.168.1.254PREFIX=24[root@SERVER1 network-scripts]# vim ifcfg-ens192TYPE=EthernetBOOTPROTO=staticNAME=ens192DEVICE=ens192ONBOOT=yesIPADDR=10.0.0.100PREFIX=8[root@SERVER1 network-scripts]# vim ifcfg-ens224TYPE=EthernetBOOTPROTO=staticNAME=ens224DEVICE=ens224ONBOOT=yesIPADDR=192.168.2.254PREFIX=24 #关闭防火墙 设置selinux[root@SERVER1 network-scripts]# systemctl stop firewalld.service [root@SERVER1 network-scripts]# setenforce 0
(2)打开firewall路由转发
[root@SERVER1 network-scripts]# vim /etc/sysctl.conf[root@SERVER1 network-scripts]# sysctl -pnet.ipv4.ip_forward = 1
2.PC1(内网)
(1)配置防火墙各大网卡IP并禁用 firewall和selinux
#关闭防火墙 设置selinux[root@PC1 network-scripts]# systemctl stop firewalld.service [root@PC1 network-scripts]# setenforce 0#设置网卡[root@PC1 ~]# cd /etc/sysconfig/network-scripts/[root@PC1 network-scripts]# vim ifcfg-ens160TYPE=EthernetBOOTPROTO=staticNAME=ens160DEVICE=ens160ONBOOT=yesIPADDR=192.168.1.1PREFIX=24GATEWAY=192.168.1.254DNS1=192.168.1.254
3.PC2外网
(1)配置防火墙各大网卡IP并禁用 firewall和selinux
#关闭防火墙 设置selinux[root@PC2 network-scripts]# systemctl stop firewalld.service [root@PC2 network-scripts]# setenforce 0#设置网卡 网关[root@PC2 ~]# cd /etc/sysconfig/network-scripts/[root@PC2 network-scripts]# vim ifcfg-ens160TYPE=EthernetBOOTPROTO=staticNAME=ens160DEVICE=ens160IPADDR=10.0.0.1PREFIX=8GATEWAY=10.0.0.254DNS1=10.0.0.254ONBOOT=yes
4.服务器
(1)配置防火墙各大网卡IP并禁用 firewall和selinux
#关闭防火墙 设置selinux[root@localhost network-scripts]# systemctl stop firewalld.service [root@localhost network-scripts]# setenforce 0#设置网卡 网关[root@localhost ~]# cd /etc/sysconfig/network-scripts/[root@localhost network-scripts]# vim ifcfg-ens160TYPE=EthernetBOOTPROTO=staticNAME=ens160DEVICE=ens160ONBOOT=yesIPADDR=192.168.2.1PREFIX=24GATEWAY=192.168.2.254DNS1=192.168.2.254
(3)下载nginx,并开通服务
#配置本地源[root@localhost ~]# cd /etc/yum.repos.d/[root@localhost yum.repos.d]# vim local.repo [local]name=localbaseurl=file:///mnt/AppStreamenabled=1gpgcheck=0root@localhost yum.repos.d]# mount /dev/sr0 /mnt/mount: /mnt: WARNING: device write-protected, mounted read-only.mount: (hint) your fstab has been modified, but systemd still usesthe old version; use 'systemctl daemon-reload' to reload.[root@localhost yum.repos.d]# yum install -y nginx
5.环境检测
(1)内网连接
[root@PC1 ~]# ping 192.168.2.1PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.64 bytes from 192.168.2.1: icmp_seq=1 ttl=63 time=0.685 ms[root@PC1 ~]# ping 10.0.0.100PING 10.0.0.100 (10.0.0.100) 56(84) bytes of data.64 bytes from 10.0.0.100: icmp_seq=1 ttl=64 time=0.639 ms64 bytes from 10.0.0.100: icmp_seq=2 ttl=64 time=0.694 ms[root@localhost ~]# ping 192.168.1.1PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.64 bytes from 192.168.1.1: icmp_seq=1 ttl=63 time=0.505 ms[root@localhost ~]# ping 10.0.0.100PING 10.0.0.100 (10.0.0.100) 56(84) bytes of data.64 bytes from 10.0.0.100: icmp_seq=1 ttl=64 time=0.412 ms64 bytes from 10.0.0.100: icmp_seq=2 ttl=64 time=0.404 ms
(2)外网连接
[root@localhost network-scripts]# ping 10.0.0.100PING 10.0.0.100 (10.0.0.100) 56(84) bytes of data.64 bytes from 10.0.0.100: icmp_seq=1 ttl=64 time=0.811 ms64 bytes from 10.0.0.100: icmp_seq=2 ttl=64 time=0.395 ms
五、防火墙配置及测试
内部网络中的pc1采用SNAT访问外部互联网,但是无法ping到内部网关。
#修改iptables 内部网络通过SNAT,访问外部网络[root@SERVER1 ~]# iptables -t filter -A INPUT -p icmp --icmp-type 8 -j DROP#访问外网测试[root@SERVER1 ~]# ping 10.0.0.1PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.64 bytes from 10.0.0.1: icmp_seq=1 ttl=64 time=0.469 ms64 bytes from 10.0.0.1: icmp_seq=2 ttl=64 time=0.408 ms^C--- 10.0.0.1 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 1001msrtt min/avg/max/mdev = 0.408/0.438/0.469/0.037 ms#修改iptables 使内网不能访问网关[root@SERVER1 ~]# iptables -t filter -A INPUT -s 192.168.1.1 -d 192.168.1.254 -p icmp --icmp-type 8 -j DROP[root@PC1 ~]# ping 192.168.1.254PING 192.168.1.254 (192.168.1.254) 56(84) bytes of data.#服务器访问内网[root@localhost ~]# ping 192.168.1.1PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.64 bytes from 192.168.1.1: icmp_seq=1 ttl=63 time=0.851 ms64 bytes from 192.168.1.1: icmp_seq=2 ttl=63 time=0.822 ms
内部网络服务器s1通过DNAT发布服务到互联网。
#配置nginx[root@localhost yum.repos.d]# systemctl start nginx[root@localhost yum.repos.d]# cd /usr/share/nginx/html/[root@localhost html]# ls404.html 50x.html index.html nginx-logo.png poweredby.png[root@localhost html]# echo c2505 > index.html [root@localhost html]# curl 192.168.2.1c2505#配置iptables[root@SERVER1 ~]# iptables -t nat -A PREROUTING -i ens192 -d 10.0.0.100 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.1[root@SERVER1 ~]# iptables -nL -t natChain PREROUTING (policy ACCEPT)target prot opt source destination DNAT tcp -- 0.0.0.0/0 10.0.0.100 tcp dpt:80 to:192.168.2.1#测试[root@localhost ~]# curl 10.0.0.100c2505
互联网主机pc2能够
访问DMZ区域的服务器,但是不能够进行ping和ssh连接。
#配置iptables[root@SERVER1 ~]# iptables -t nat -A PREROUTING -i ens192 -d 10.0.0.100 -p tcp --dport 80 -j DNAT --to-destination 192.168.2.1[root@SERVER1 ~]# iptables -t filter -A INPUT -p icmp --icmp-type 8 -j DROP[root@SERVER1 ~]# iptables -t filter -A INPUT -p tcp -s 10.0.0.1 --dport 22 -j DROP[root@SERVER1 ~]# iptables -nL Chain INPUT (policy ACCEPT)target prot opt source destination DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 8DROP tcp -- 10.0.0.1 0.0.0.0/0 tcp dpt:22#测试[root@localhost ~]# curl 10.0.0.100c2505[root@localhost ~]# ssh root@192.168.2.1ssh: connect to host 192.168.2.1 port 22: No route to host[root@localhost ~]# ping 192.168.2.1PING 192.168.2.1 (192.168.2.1) 56(84) bytes of data.