防火墙( FireWall ):具有隔离功能,工作在网络或主机边缘,对进出网络或主机的数据包基于一定的规则检查,并在匹配某规则时由规则定义的行为进行处理的一组功能的组件,基本上的实现都是默认情况下关闭所有的通过型访问,只开放允许访问的策略。根据分类可以分为主机防火墙和网络防火墙。接下来通过简单的小实验,了解一下防火墙的一些规则定义。
1、主机防火墙:服务范围为当前主
例:其他主机只能访问本机的22和80端口
[root@centos7 ~]# iptables -A INPUT -s 192.168.239.1 -j ACCEPT //这里是使用xshell连接的虚拟机,所以这里先运行Windows主机连接
[root@centos7 ~]# iptables -A OUTPUT -d 192.168.239.1 -j ACCEPT
[root@centos7 ~]#iptables -A INPUT -j REJECT
[root@centos7 ~]#iptables -A OUTPUT -j REJECT
[root@centos7 ~]#iptables -I INPUT -p tcp -m multiport --dports 22,80 -j ACCEPT
[root@centos7 ~]#iptables -I OUTPUT -p tcp -m multiport --sports 22,80 -j ACCEPT
[root@centos7 ~]#iptables -vnL
在其他主机上使用telnet命令测试
测试80端口
[root@centos7 ~]# telnet 192.168.239.132 80
Trying 192.168.239.132...
Connected to 192.168.239.132.
Escape character is '^]'.
测试22端口
[root@centos7 ~]# telnet 192.168.239.132 22
Trying 192.168.239.132...
Connected to 192.168.239.132.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
此时ping192.168.239.132是不通的
[root@centos7 ~]# ping 192.168.239.132
PING 192.168.239.132 (192.168.239.132) 56(84) bytes of data.
例:开放本机的ssh,samba,http服务
[root@centos7 ~]#iptables -A -s 192.168.239.1 -j ACCEPT
[root@centos7 ~]#iptables -A INPUT -j REJECT
[root@centos7 ~]#iptables -I INPUT -p tcp -m multiport --dport 22,80,139,445 -j ACCEPT
[root@centos7 ~]#iptables -I INPUT 2 -p udp -m mutliport --dport 137:138 -j ACCEPT
2、网络防火墙:服务范围为防火墙一侧的局域网
iptables/netfilter网络防火墙:
(1) 充当网关
(2) 使用filter表的FORWARD链
注意的问题:
(1) 请求-响应报文均会经由FORWARD链,要注意规则的方向性
(2) 如果要启用conntrack机制,建议将双方向的状态为ESTABLISHED的报文直接放行
接下来我们实现网络防火墙的实验,以下是网络环境的网络拓扑图。
比如,在WEB服务器上有两个网站,客户端可以访问www.youku.com,但是访问不了www.google.com。我们得在网络防火墙上设置一些规则才能实现这个功能。
在客户端A主机上,把网关指向192.168.239.130
[root@centos7 network-scripts]# route add default gw 192.168.239.130 dev ens33
[root@centos7 network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.239.130 0.0.0.0 UG 0 0 0 ens33
0.0.0.0 192.168.43.1 0.0.0.0 UG 0 0 0 ens37
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 ens37
192.168.43.0 0.0.0.0 255.255.255.0 U 0 0 0 ens37
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
192.168.239.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33
在WEB服务器上,因为响应报文要经过防火墙,所以网关指向192.168.30.2
配置IP地址及网关
[root@centos7 network-scripts]# ifconfig
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.30.3 netmask 255.255.255.0 broadcast 192.168.30.255
ether 00:0c:29:bf:a8:6d txqueuelen 1000 (Ethernet)
RX packets 7778 bytes 601233 (587.1 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 550 bytes 47609 (46.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos7 network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.30.2 0.0.0.0 UG 100 0 0 ens37
192.168.30.0 0.0.0.0 255.255.255.0 U 100 0 0 ens37
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
配置web服务,采用虚拟主机的方式实现两个不同的站点。
[root@centos7 conf.d]# echo www.youku.com >/var/www/html/index.html
[root@centos7 conf.d]# echo www.google.com >/var/www/html2/index.html
[root@centos7 conf.d]# cat vhosts.conf
<virtualhost *:80>
servername www.youku.com
documentroot /var/www/html
</virtualhost>
<virtualhost *:80>
servername www.google.com
documentroot /var/www/html2
</virtualhost>
[root@centos7 ~]# systemctl start httpd
配置网络防火墙,这里使用一台linux主机充当
[root@centos7 network-scripts]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.239.130 netmask 255.255.255.0 broadcast 192.168.239.255
inet6 fe80::20c:29ff:fe65:a88f prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:65:a8:8f txqueuelen 1000 (Ethernet)
RX packets 7002 bytes 598227 (584.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4551 bytes 565206 (551.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.30.2 netmask 255.255.255.0 broadcast 192.168.30.255
ether 00:0c:29:65:a8:99 txqueuelen 1000 (Ethernet)
RX packets 7007 bytes 562236 (549.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 556 bytes 53620 (52.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
开启路由转发功能
[root@centos7 network-scripts]# echo net.ipv4.ip_forward=1 >> /etc/sysctl.conf
[root@centos7 network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
在未配置防火墙规则之前,先测试一下,我们发现这两个网站都是可以访问的。
[root@centos7 ~]# curl www.youku.com
www.youku.com
[root@centos7 ~]# curl www.google.com
www.google.com
配置防火墙规则,这里我们允许访问www.youku.com,不允许访问www.google.com。
[root@centos7 network-scripts]# iptables -F //先清空原来的防火墙策略
[root@centos7 network-scripts]# iptables -I FORWARD -m string --algo bm --string "google" -j REJECT
[root@centos7 network-scripts]# iptables -I FORWARD -m string --algo bm --string "youku" -j ACCEPT
在客户端测试
[root@centos7 ~]# curl www.google.com
^C
[root@centos7 ~]# curl www.youku.com
www.youku.com
实现内网的安全,192.1268.239.0/24可以访问192.1268.30.0/24这个网段,但是反过来就不允许访问。
[root@centos7 ~]# iptables -F //先清空原来的防火墙策略
[root@centos7 ~]# iptables -A FORWARD -j REJECT
[root@centos7 ~]# iptables -I FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@centos7 ~]# iptables -I FORWARD 2 -s 192.168.239.0/24 -d 192.168.30.0/24 -m state --state NEW-j ACCEPT
内网的某台机器用作web服务,外网可以访问
[root@centos7 ~]# iptables -I FORWARD 2 -d 192.168.239.128 -p tcp --dport 80 -j ACCEPT