规则优化最佳实践
- 安全放行所有入站和出站的状态为ESTABLISHED状态连接,建议放在第一条,效率更高
- 谨慎放行入站的新请求
- 有特殊目的限制访问功能,要在放行规则之前加以拒绝
- 同类规则(访问同一应用,比如:http ),匹配范围小的放在前面,用于特殊处理
- 不同类的规则(访问不同应用,一个是http,另一个是mysql ),匹配范围大的放在前面,效率更 高
-s 10.0.0.6 -p tcp --dport 3306 -j REJECT
-s 172.16.0.0/16 -p tcp --dport 80 -j REJECT
- 应该将那些可由一条规则能够描述的多个规则合并为一条,减少规则数量,提高检查效率
- 设置默认策略,建议白名单(只放行特定连接)
iptables -P,不建议,容易出现“自杀现象”
规则的后定义规则做为默认策略,推荐使用,放在后一条
注意:不要把自己远程禁用了 越上面优先级越高 把10.0.0.1设置为ACCEPT就不会被禁
iptables -A INPUT -J REJECT 没有明确允许的全部拒绝
iptables -P INPUT DROP(ACCEPT) 默认就是拒绝 (用iptables -F 清除了 建议不用)
iptables -A INPUT -s 是进
iptables -A OUTPUT -d 是出
iptables -DINPUT 1 删除第一条规则
iptables -R INPUT 2 -s 10.0.0.6 -j REJECT 替换把第二天替换为10.0.0.6
iptables -A INPUT -s 10.0.0.6 -j tcp REJECT 禁止10.0.0.6访问TCP协议(也可以写端口号)
iptables -A INPUT -i eth0 -j REJECT 禁止远端访问eth0
iptables -A INPUT -m state ESTABLISHED -j ACCEPT
iptables -A INPUT -m state --state NEW -j REJECT 我可以ping通你,你不能ping通我
iptables -A INPUT -p icmp --icmp-type 8 -j REJECT 别人ping不同我,我可以ping通别人
iptables -A INPUT -s 10.0.0.6 -p tcp --dport 80 -j REJECT 拒绝端口80(调用模块是用-p -m)
iptables -I INPUT 3 10.0.0.6 -p TCP -m mnltiport --dports 80,442 -j ACCEPT 允许80,443端口访问
iptables -A OUTPUT -P -m string --algo bm --from 62 --string “goole” -j REJECT 拒绝访问有googe的(过滤敏感报文)
iptables -I INPUT 2 -s 10.0.0.6 -j REJECT 默认插入第一条
这个2加入就是插入第二条
iptables -t filter -A INPUT -s 10.0.0.7 -j DROP (拒绝 不会应)(A附加 s原地址)
ACCEPT(接收)
REJECT(拒绝 回应)
iptables -F 清除所有规则
iptables -F OUTPUT 清除OUTPUT规则
iptables -vnL --line-numbers 显示更加详细的内容(序号)
规则谁在前谁在后是业务决定的,访问量多的放在前面
查看类:
-L:list, 列出指定鏈上的所有规则,本选项须置后
-n:numberic,以数字格式显示地址和端口号
-v:verbose,详细信息
-vv 更详细
-x:exactly,显示计数器结果的精确值,而非单位转换后的易读值
–line-numbers:显示规则的序号
-S selected,以iptables-save 命令格式显示链上规则
规则管理类:
-A:append,追加
-I:insert, 插入,要指明插入至的规则编号,默认为第一条
-D:delete,删除
(1) 指明规则序号
(2) 指明规则本身
-R:replace,替换指定链上的指定规则编号
-F:flush,清空指定的规则链
-Z:zero,置零
iptables的每条规则都有两个计数器
(1) 匹配到的报文的个数
(2) 匹配到的所有报文的大小之和
环境准备
关闭firewalld防火墙规则(centos7,8)(关闭的意思是关闭系统默认的规则,用自己制定的规则)
systemctl disable firewalld
或者
systemctl enable --now firewalld
centos6
service iptables stop
chkconfig iptables off
查看防火墙规则
iptables -vnL (也可以只加-L 列出) filter表只支持3个表
[root@root ~]# iptables -vnL
Chain INPUT (policy ACCEPT 12 packets, 884 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 9 packets, 1068 bytes)
pkts bytes target prot opt in out source destination
案例1
在centos8上安装httpd ,centos7进行访问 ,加防火墙规则禁止访问
80端口就是httpd centos7现在是可以访问的
[root@CentOS8 ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@centos7 ~]# curl 10.0.0.8
hello panda`
在centos8添加规则拒绝centos7访问 (不明确拒绝就一直尝试登录)
[root@CentOS8 ~]# iptables -A INPUT -s 10.0.0.204 -j DROP
[root@CentOS8 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 10.0.0.204 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
在centos7上访问 (一直进不去)
[root@centos7 ~]# ping 10.0.0.8
PING 10.0.0.8 (10.0.0.8) 56(84) bytes of data.
在centos8上清除规则,加入明确拒绝的规则
[root@CentOS8 ~]# iptables -F
[root@CentOS8 ~]# iptables -A INPUT -s 10.0.0.204 -j REJECT
[root@CentOS8 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 10.0.0.204 0.0.0.0/0 reject-with icmp-port-unreacha
centos7明确拒绝
[root@centos7 ~]# ping 10.0.0.8
PING 10.0.0.8 (10.0.0.8) 56(84) bytes of data.
From 10.0.0.8 icmp_seq=1 Destination Port Unreachable
From 10.0.0.8 icmp_seq=2 Destination Port Unreachable
实例2
删除规则
在centos8中加入几个规则利用
iptables -iptables -vnL --line-numbers显示序号
[root@CentOS8 ~]# iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 35 2940 REJECT all -- * * 10.0.0.204 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 REJECT all -- * * 10.0.0.6 0.0.0.0/0 reject-with icmp-port-unreachable
3 0 0 REJECT all -- * * 10.0.0.9 0.0.0.0/0 reject-with icmp-port-unreachable
4 0 0 REJECT all -- * * 10.0.0.10 0.0.0.0/0 reject-with icmp-port-unreachable
删除序号1 (后面的规则自动补位)(连续删除两个1就是把序号1,2删除)
[root@CentOS8 ~]# iptables -DINPUT 1
[root@CentOS8 ~]# iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 10.0.0.6 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 REJECT all -- * * 10.0.0.9 0.0.0.0/0 reject-with icmp-port-unreachable
3 0 0 REJECT all -- * * 10.0.0.10 0.0.0.0/0 reject-with icmp-port-unreachable
插入规则
使用 iptables -I INPUT 3 -s 10.0.0.100 -j REJECT 原本的3就变成了4
[root@CentOS8 ~]# iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 10.0.0.6 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 REJECT all -- * * 10.0.0.9 0.0.0.0/0 reject-with icmp-port-unreachable
3 0 0 REJECT all -- * * 10.0.0.10 0.0.0.0/0 reject-with icmp-port-unreachable
[root@CentOS8 ~]# iptables -vnL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 10.0.0.6 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 REJECT all -- * * 10.0.0.9 0.0.0.0/0 reject-with icmp-port-unreachable
3 0 0 REJECT all -- * * 10.0.0.100 0.0.0.0/0 reject-with icmp-port-unreachable
4 0 0 REJECT all -- * * 10.0.0.10 0.0.0.0/0 reject-with icmp-port-unreachable
替换
使用 iptables -R INPUT 2 -s 10.0.0.200 -j REJECT 把第二条规则换为了200
[root@CentOS8 ~]# iptables -vnL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 10.0.0.6 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 REJECT all -- * * 10.0.0.9 0.0.0.0/0 reject-with icmp-port-unreachable
3 0 0 REJECT all -- * * 10.0.0.100 0.0.0.0/0 reject-with icmp-port-unreachable
4 0 0 REJECT all -- * * 10.0.0.10 0.0.0.0/0 reject-with icmp-port-unreachable
[root@CentOS8 ~]# iptables -vnL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 10.0.0.6 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 REJECT all -- * * 10.0.0.200 0.0.0.0/0 reject-with icmp-port-unreachable
3 0 0 REJECT all -- * * 10.0.0.100 0.0.0.0/0 reject-with icmp-port-unreachable
4 0 0 REJECT all -- * * 10.0.0.10 0.0.0.0/0 reject-with icmp-port-unreachable
计数器清0
iptables -Z
[root@CentOS8 ~]# iptables -vnL --line-number
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 10.0.0.200 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 REJECT all -- * * 10.0.0.100 0.0.0.0/0 reject-with icmp-port-unreachable
3 0 0 REJECT all -- * * 10.0.0.10 0.0.0.0/0 reject-with icmp-port-unreachable
4 71 5964 REJECT all -- * * 10.0.0.204 0.0.0.0/0 reject-with icmp-port-unreachable
204 有5000个数据报文
清除
[root@CentOS8 ~]# iptables -Z
[root@CentOS8 ~]# iptables -vnL --line-number
Chain INPUT (policy ACCEPT 6 packets, 364 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 10.0.0.200 0.0.0.0/0 reject-with icmp-port-unreachable
2 0 0 REJECT all -- * * 10.0.0.100 0.0.0.0/0 reject-with icmp-port-unreachable
3 0 0 REJECT all -- * * 10.0.0.10 0.0.0.0/0 reject-with icmp-port-unreachable
4 0 0 REJECT all -- * * 10.0.0.204 0.0.0.0/0 reject-with icmp-port-unreachable
不许出(10.0.0.6不能访问为,本机也不能访问6,只要是出去6的都不能)
[root@CentOS8 ~]# iptables -AOUTPUT -d 10.0.0.6 -j REJECT
[root@CentOS8 ~]# ping 10.0.0.6
PING 10.0.0.6 (10.0.0.6) 56(84) bytes of data.
From 10.0.0.8 icmp_seq=1 Destination Port Unreachable
ping: sendmsg: Operation not permitted
From 10.0.0.8 icmp_seq=2 Destination Port Unreachable
ping: sendmsg: Operation not permitted
[root@centos6 ~]# ping 10.0.0.8
PING 10.0.0.8 (10.0.0.8) 56(84) bytes of data.
清除OUTPUT规则
iptables -F OUTPUT
state模块(可以跟踪状态)(重要)
调整连接追踪功能所能够容纳最大连接数量
cat /proc/sys/net/netfilter/nf_conntrack_max
30720 #数值不要太小,不然会抛弃数据报文
老用户和正在连接的用户可以连接,新用户不行 (我可以ping通你,你不能ping通我)
iptables -A INPUT -m state ESTABLISHED -j ACCEPT
iptables -A INPUT -m state --state NEW -j REJECT
[root@centos7 ~]# ping 10.0.0.8
PING 10.0.0.8 (10.0.0.8) 56(84) bytes of data.
64 bytes from 10.0.0.8: icmp_seq=1 ttl=64 time=0.865 ms
64 bytes from 10.0.0.8: icmp_seq=2 ttl=64 time=0.398 ms
64 bytes from 10.0.0.8: icmp_seq=3 ttl=64 time=0.365 ms
[root@centos6 ~]# ping 10.0.0.8
PING 10.0.0.8 (10.0.0.8) 56(84) bytes of data.
From 10.0.0.8 icmp_seq=1 Destination Port Unreachable
From 10.0.0.8 icmp_seq=2 Destination Port Unreachable
查看连接功能
[root@CentOS8 ~]# cat /proc//net/nf_conntrack
ipv4 2 tcp 6 299 ESTABLISHED src=10.0.0.8 dst=10.0.0.1 sport=22 dport=57671 src=10.0.0.1 dst=10.0.0.8 sport=57671 dport=22 [ASSURED] mark=0 zone=0 use=2
iptables 开机权限是自动清除的,所以要加开机加载的权限(需要执行权限)
记得source一下文件
vi /etc/rc.local
第二种方法 适用于centos7.8
iptables-service >/etc/sysconfig/iptables
systemctl enable iptables.service
centos6
iptables-service >/etc/sysconfig/iptables
chkconfig --list iptables #开机自启