防火墙
常见防火墙选用
- 公司网站入口使用的硬件防火墙,三层路由带有防火墙功能
- Iptables访问量小 C5 C6 默认,CentOS 7 Firewalld(关闭 安装iptables)
- selinux关闭
名词(关系)与单词
名词 | 含义 | 对比 |
---|---|---|
容器 | 存放内容/存放东西 | |
Netfilter/iptables | Netfilter/iptables是表的容器 | 国家 |
表 | (table) 表是用来存放链的容器 | 省 |
链 (chain) | 链存放 规则容器 | 市 |
规则 ( policy ) | 准许/拒绝访问 | 区县具体地点 |
防火墙执行过程
- 防火墙是层层过滤的,实际是按照配置规则的顺序从上到下,从前到后进行过滤的。
- 如果匹配上规则,即明确表示是阻止(DROP)还是通过(ACCEPT)数据包就不再向下匹配新的规则。 3. 如果规则中没有明确表明是阻止还是通过的,也就是没有匹配规则,向下进行匹配,直到匹配默认 规则得到明确的阻止还是通过。
-
防火墙的默认规则是所有规则执行完才执行的。
四表五链
整体说明 4表及作用
表 | 功能 |
---|---|
Filter | 过滤,默认的表,防火墙功能 |
NAT | 实现NAT转化:1.共享上网 2.端口转发 |
mangle | |
raw |
参考查询帮助:man iptables
4表中的5链
![](https://img.haomeiwen.com/i9469008/5eaeebd764bce01c.png)
![](https://img.haomeiwen.com/i9469008/3413625dfd4f5c16.png)
filter表
filter表 | 企业工作场景:主机防火墙 |
---|---|
INPUT | 就是过滤进入主机的数据包 |
FORWARD | 负责转发流经主机的数据包。 |
OUTPUT | 就是处理从主机发出去的数据包 |
nat表
nat表 | |
---|---|
PREROUTING | 处理用户请求中的目的地址 目的端口 端口转发 ip映射 |
POSTROUTING | 处理离开服务器的请求 源端口 源ip :共享上网 |
OUTPUT | 和主机放出去的数据包有关,改变主机发出数据包的目的地址。 |
Mangle表
主要负责修改数据包中特殊的路由标记,如TTL,TOS,MARK等,这个表定义了5个链 (chains):
iptables命令及参数
iptables | |
---|---|
-t | 指定表 filter(默认) nat |
-A | append 把规则追加到末尾 |
-I (大写字母I) | insert 把规则插入到规则的第1条 (添加拒绝类规则的时候) |
-p | protocal 指定协议:tcp /udp/icmp |
--dport | destination port 目标端口 |
--sport | source port 源端口 |
-d | dest ip address 目标ip地址 |
-s | source ip address 源ip地址 |
-i | input 数据进来的时候通过的网卡 |
-o | output 数据进来的时候通过的网卡 |
-j | jump 方法 DROP(拒绝) ACCEPT(准许) REJECT(拒绝) |
iptables查看 删除 | |
---|---|
-F | 清除链中所有的规则 |
-X | 清空自定义链的规则 |
-Z | 清空计数器 |
-n | 不要把端口解析服务名字 |
-L | 显示表中的规则 |
--line-number | 给每个链中的规则加上行号 |
-D | 删除规则 根据规则的号码进行删除 -D INPUT 2 |
![](https://img.haomeiwen.com/i9469008/9b925668e6eefb65.png)
防火墙实践
防火墙之filter表
环境准备
m01 iptables
db01
安装防火墙
yum install iptables iptables-services
[root@m01 ~]# rpm -qa iptables-services iptables-services-1.4.21-28.el7.x86_64 [root@m01 ~]# rpm -ql iptables-services /etc/sysconfig/ip6tables /etc/sysconfig/iptables #iptables 配置文件 /usr/lib/systemd/system/ip6tables.service /usr/lib/systemd/system/iptables.service #iptables服务管理配置
启动防火墙
systemctl start iptables.service
systemctl enable iptables.service
检查防火墙内核模块是否加载成功:
[root@m01 ~]# lsmod |egrep 'nat|ipt|filter'
nf_nat_ftp 12770 0
nf_conntrack_ftp 18638 1 nf_nat_ftp
iptable_nat 12875 0
nf_nat_ipv4 14115 1 iptable_nat
nf_nat 26787 2 nf_nat_ftp,nf_nat_ipv4
ipt_REJECT 12541 0
nf_reject_ipv4 13373 1 ipt_REJECT
nf_conntrack 133095 7 nf_nat_ftp,nf_nat,xt_state,nf_nat_ipv4,xt_conntrack,nf_conntrack_ftp,nf_conntrack_ipv4
iptable_filter 12810 1
ip_tables 27126 2 iptable_filter,iptable_nat
libcrc32c 12644 3 xfs,nf_nat,nf_conntrack
手动加载内核模块
modprobe ip_tables modprobe iptable_filter modprobe iptable_nat modprobe ip_conntrack modprobe ip_conntrack_ftp modprobe ip_nat_ftp modprobe ipt_state
配置规则-禁止访问22端口
[root@m01 ~]# iptables -F
[root@m01 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@m01 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
如果你是用xshell工具并且使用22端口连接的,执行这条命令后默认把我们自己也挡住外面了,可以直接去虚拟机清除规则。
配置防火墙规则注意事项:
- 去机房重启系统或者登陆服务器删除刚才的禁止规则。
- 让机房人员重启服务器或者让机房人员拿用户密码登录进去 3. 通过服务器的远程管理卡管理(推荐)
- 先写一个定时任务,每5分钟就停止防火墙 5. 测试环境测试好,写成脚本,批量执行
filter表其他规则配置
只让10.0.0.0/24网段进行访问
只要是10.0.0.0/24 局域网的用户 访问m01 都ACCEPT 此例子主要限制:网段或ip地址
[root@m01 ~]# iptables -t filter -I INPUT -p tcp ! -s 10.0.0.0/24 -j DROP
[root@m01 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- !10.0.0.0/24 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
准许或禁止端口
多个端口:表示范围1-1024范围
[root@m01 ~]# iptables -A INPUT -p tcp --dport 1:1024 -j ACCEPT
[root@m01 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:1:1024
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
nc用法:
nc -l 指定监听端口
nc/telenet连接nc服务端
[root@m01 ~]# nc -l 99 >/tmp/new.txt
[root@m01 ~]# cat /tmp/new.txt
db01
nc客户端
[root@db01 ~]# cat /etc/hostname | nc 10.0.0.61 99
多个端口 不连续 80,443,52113,22
[root@m01 ~]# iptables -t filter -I INPUT -p tcp -m multiport ! --dport 80,443,22 -j DROP
[root@m01 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 multiport dports !80,443,22
DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpts:!1:1024
DROP tcp -- !10.0.0.0/24 0.0.0.0/0
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
准许或禁止ping
[root@m01 ~]# ping 10.0.0.61 -c1
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.072 ms
--- 10.0.0.61 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.072/0.072/0.072/0.000 ms
[root@m01 ~]# ping 172.16.1.61 -c1
PING 172.16.1.61 (172.16.1.61) 56(84) bytes of data.
64 bytes from 172.16.1.61: icmp_seq=1 ttl=64 time=0.044 ms
--- 172.16.1.61 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.044/0.044/0.044/0.000 ms
[root@m01 ~]# iptables -I INPUT -p icmp --icmp-type any -j DROP
[root@m01 ~]# ping 172.16.1.61 -c1
PING 172.16.1.61 (172.16.1.61) 56(84) bytes of data.
^C
--- 172.16.1.61 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
[root@m01 ~]# ping 10.0.0.61 -c1
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
^C
--- 10.0.0.61 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 0ms
[root@m01 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 255
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
禁止后检查是否通畅:
可以使用
- telnet
- nc
- nmap
- 进行检查
连接状态
NEW: 已经或将启动新的连接
ESTABLISHED:已建立的连接
RELATED:正在启动的新连接
INVALID: 非法或无法识别的
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
匹配网络限制策略(限制并发 访问的频率)
-m limit
-m limit --limit n/{second/minute/hour}:
解释:指定时间内的请求速率”n”为速率,后面为时间分别为:秒 分 时
--limit-burst [n]
解释:在同一时间内允许通过的请求”n”为数字,不指定默认为5
iptables -I INPUT -s 10.0.0.0/24 -p icmp --icmp-type 8 -m limit --limit 6/min --limit-burst 5 -j ACCEPT
在INPUT链中添加规则 只需要 10.0.0.0/24这个网段ping主机,并且一分钟内最多ping 5次。
如果需要在虚拟机测试这个规则,请先添加允许连接你22端口的规则,在把默认规则改为拒绝。如下;
[root@m01 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@m01 ~]# iptables -P INPUT DROP
[root@m01 ~]# iptables -P FORWARD DROP
[root@m01 ~]# iptables -P OUTPUT ACCEPT
[root@m01 ~]# iptables -nL
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- 10.0.0.0/24 0.0.0.0/0 icmptype 8 limit: avg 6/min burst 5
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
保存规则
[root@m01 ~]# iptables-save ##保存规则
# Generated by iptables-save v1.4.21 on Wed Jul 3 15:35:45 2019
*nat
:PREROUTING ACCEPT [89:6905]
:INPUT ACCEPT [54:4206]
:OUTPUT ACCEPT [147:11109]
:POSTROUTING ACCEPT [145:10957]
-A POSTROUTING -s 172.16.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.61
COMMIT
# Completed on Wed Jul 3 15:35:45 2019
# Generated by iptables-save v1.4.21 on Wed Jul 3 15:35:45 2019
*filter
:INPUT DROP [28:2286]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [77:11333]
-A INPUT -s 10.0.0.0/24 -p icmp -m icmp --icmp-type 8 -m limit --limit 6/min -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
COMMIT
# Completed on Wed Jul 3 15:35:45 2019
root@m01 ~]# iptables-save >/etc/sysconfig/iptables ##将规则定向到配置文件,写到配置文件可以永久生效
root@m01 ~]# iptables -F ##清除所有规则
[root@m01 ~]# iptables -nL ##查看规则
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@m01 ~]# iptables-restore < /etc/sysconfig/iptables ##将配置文件里的规则恢复
[root@m01 ~]# iptables -nL ##查看规则
Chain INPUT (policy DROP)
target prot opt source destination
ACCEPT icmp -- 10.0.0.0/24 0.0.0.0/0 icmptype 8 limit: avg 6/min burst 5
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@m01 ~]# #systemctl restart iptables.service ##重启iptables 自动恢复规则,前提已经写入到配置文件
- 注意事项: iptables-save >/etc/sysconfig/iptables
- iptables 是关闭状态 stop/disable
- 不要使用iptables -nL 查看状态,如果使用防火墙自动打开
- 查看防火墙状态: systemctl is-active iptables
生产环境防火墙配置
1.逛公园:防火墙默认的规则 默认规则都是准许ACCEPT
2.电影院:默认规则都是 拒绝DROP 凭票进入
配置允许SSH登录 端口进入
[root@m01 ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT1
允许本机回环lo接口数据流量流出于流入
[root@m01 ~]# iptables -A INPUT -i lo -j ACCEPT
[root@m01 ~]# iptables -A OUTPUT -o lo -j ACCEPT
-i input 与 INPUT链一起使用
-o output 与 OUTPUT 链一起使用
准许icmp协议通过
[root@m01 ~]# iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
准许用户使用的端口通过 80,443
[root@m01 ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
[root@m01 ~]# iptables -A INPUT -p tcp --dport 443 -j ACCEPT
允许用户与服务器建立连接
[root@m01 ~]# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
[root@m01 ~]# iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
开启信任的IP网段
[root@m01 ~]# iptables -A INPUT -s 10.0.0.0/24 -p all -j ACCEPT
[root@m01 ~]# iptables -A INPUT -s 172.16.1.0/24 -p all -j ACCEPT
修改默认规则
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
NAT表
PREROUTING | 处理请求中的目标ip和端口 :: 端口转发(映射) |
POSTROUTING | 共享上网 |
OUTPUT |
PREROUTING
防火墙配置PREROUTING规则
开启内核转发
检查iptable nat模块是否加载 lsmod
端口转发
[root@m01 ~]# iptables -t nat -A PREROUTING -d 10.0.0.61 -p tcp --dport 9000 -j DNAT --to-destination 10.0.0.41:22
[root@m01 ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 10.0.0.61 tcp dpt:9000 to:10.0.0.41:22
会导致 数据从 .61转发给 .41的时候 无法再从 .41 返回给 .61
所以需要添加一个回去的路
把内网网关指向172.16.1.61
[root@db01 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 172.16.1.61 0.0.0.0 UG 0 0 0 eth1
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
172.16.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
[root@db01 ~]# ip r
default via 172.16.1.61 dev eth1
10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.51
169.254.0.0/16 dev eth0 scope link metric 1002
169.254.0.0/16 dev eth1 scope link metric 1003
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.51
IP转发
添加临时虚拟ip
配置转发规则
[root@m01 ~]# ip addr add 10.0.0.66 dev eth0 label eth0:0
[root@m01 ~]# iptables -t nat -A PREROUTING -p tcp -d 10.0.0.66 -j DNAT --to-destination 172.16.1.51
[root@m01 ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- 0.0.0.0/0 10.0.0.61 tcp dpt:8080 to:172.16.1.51:22
DNAT tcp -- 0.0.0.0/0 10.0.0.66 to:172.16.1.51
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 172.16.1.0/24 0.0.0.0/0 to:10.0.0.61
POSTROUTING
防火墙配置POSTROUTING规则
开启内核转发
检查iptable nat模块是否加载 lsmod
[root@m01 ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.61
[root@m01 ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
SNAT all -- 172.16.1.0/24 0.0.0.0/0 to:10.0.0.61
[root@m01 ~]# tail /etc/sysctl.conf
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_forward=1
[root@m01 ~]# sysctl -p
net.ipv4.ip_forward = 1
[root@m01 ~]# cat /proc/sys/net/ipv4/ip_forward
1
[root@m01 ~]# lsmod |egrep 'ipt|nat|filter'
xt_nat 12681 1
iptable_nat 12875 1
nf_nat_ipv4 14115 1 iptable_nat
nf_nat 26787 2 nf_nat_ipv4,xt_nat
nf_conntrack 133095 4 nf_nat,nf_nat_ipv4,xt_conntrack,nf_conntrack_ipv4
iptable_filter 12810 1
ip_tables 27126 2 iptable_filter,iptable_nat
ipt_REJECT 12541 0
nf_reject_ipv4 13373 1 ipt_REJECT
libcrc32c 12644 3 xfs,nf_nat,nf_conntrack
[root@m01 ~]#
关闭eth0网卡
在eth0网卡中加入网关 指向xxx.61
[root@db01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0 DEVICE=eth0
ONBOOT=no
IPADDR=10.0.0.51
PREFIX=24
GATEWAY=10.0.0.254
DNS1=10.0.0.254
[root@db01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=static
IPADDR=172.16.1.51
PREFIX=24 NAME=eth1
DEVICE=eth1 ONBOOT=yes
GATEWAY=172.16.1.61
重启网卡,通过内网连接进来 并进行测试
[root@db01 ~]# ping db01 -c1
PING db01 (172.16.1.51) 56(84) bytes of data.
64 bytes from db01 (172.16.1.51): icmp_seq=1 ttl=64 time=0.029 ms
--- db01 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.029/0.029/0.029/0.000 ms
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -o eth0 -j SNAT --to-source 10.0.0.61 #公 网ip固定
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -o eth0 -j SNAT --to-source MASQUERADE #伪装
MAS QUE RADE 【mæskəˈreɪd】
iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -j MASQUERADE
如果是 172的网段则
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE 即可。
防火墙小结
防火墙4表5链
filter表 INPUT链 实现防火墙功能
nat 表 PREROUTING实现 端口转发
nat 表 POSTROUTING链实现 共享上网
网友评论