[root@localhost ~]# iptables -I INPUT -s 192.168.56.106 -p icmp -j REJECT
[root@localhost ~]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 980 packets, 121K bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT icmp -- * * 192.168.56.106 0.0.0.0/0 reject-with icmp-port-unreachable
938 115K LIBVIRT_INP all -- * * 0.0.0.0/0 0.0.0.0/0
[root@localhost ~]# iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 1168 packets, 133K bytes)
num pkts bytes target prot opt in out source destination
1 3 252 REJECT icmp -- * * 192.168.56.106 0.0.0.0/0 reject-with icmp-port-unreachable
2 1126 127K LIBVIRT_INP all -- * * 0.0.0.0/0 0.0.0.0/0
[root@localhost ~]# iptables -D INPUT 1
[root@localhost ~]# iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 1223 packets, 137K bytes)
num pkts bytes target prot opt in out source destination
1 1181 130K LIBVIRT_INP all -- * * 0.0.0.0/0 0.0.0.0/0
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -L
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@localhost ~]# iptables -P INPUT DROP
[root@localhost ~]# iptables -I INPUT -p icmp -j ACCEPT
[root@localhost ~]# ping -c 4 192.168.10.10
PING 192.168.10.10 (192.168.10.10) 56(84) bytes of data.
64 bytes from 192.168.10.10: icmp_seq=1 ttl=64 time=0.156 ms
64 bytes from 192.168.10.10: icmp_seq=2 ttl=64 time=0.117 ms
64 bytes from 192.168.10.10: icmp_seq=3 ttl=64 time=0.099 ms
64 bytes from 192.168.10.10: icmp_seq=4 ttl=64 time=0.090 ms
--- 192.168.10.10 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.090/0.115/0.156/0.027 ms
[root@linuxprobe ~]# iptables -D INPUT 1
[root@linuxprobe ~]# iptables -P INPUT ACCEPT
[root@linuxprobe ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
[root@localhost ~]# iptables -I INPUT -p tcp -s 192.168.56.0/24 --dport 22 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -p tcp --dport 22 -j DROP
[root@localhost ~]# iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 6306 packets, 6997K bytes)
num pkts bytes target prot opt in out source destination
1 216 14861 ACCEPT tcp -- * * 192.168.56.0/24 0.0.0.0/0 tcp dpt:22
2 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
注意DROP规则是用的A,append,即在ACCEPT之后,添加一条规则。如果DROP添加在前面的话,一开始就拦截报文,后面的ACCEPT就不生效了
[root@localhost ~]# iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 6324 packets, 6999K bytes)
num pkts bytes target prot opt in out source destination
[root@localhost ~]# iptables -I INPUT -p tcp --dport 12345 -j REJECT
[root@localhost ~]# iptables -I INPUT -p udp --dport 12345 -j REJECT
[root@localhost ~]# iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 6718 packets, 7023K bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:12345 reject-with icmp-port-unreachable
2 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:12345 reject-with icmp-port-unreachable
[root@localhost ~]# iptables -I INPUT -s 192.168.56.103 -p tcp --dport 80 -j REJECT
[root@localhost ~]# iptables -I INPUT -p tcp --dport 1000:1024 -j REJECT
[root@localhost ~]# iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 7335 packets, 7061K bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpts:1000:1024 reject-with icmp-port-unreachable
假设,我想要放行ssh远程连接相关的报文,也想要放行web服务相关的报文,那么,我们在INPUT链中添加如下规则
[root@localhost ~]# iptables -I INPUT -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# iptables -P INPUT DROP
[root@localhost ~]# iptables -nvL INPUT --line
Chain INPUT (policy DROP 8511 packets, 9003K bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 255 14844 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
如果此时,我不小心执行了”iptables -F”操作,在当前ssh远程工具中执行”iptables -F”命令后,由于INPUT链中已经不存在任何规则,所以,所有报文都被拒绝了,包括当前的ssh远程连接
这就是默认策略设置为DROP的缺点,在对应的链中没有设置任何规则时,这样使用默认策略为DROP是非常不明智的,因为管理员也会把自己拒之门外,即使对应的链中存在放行规则,当我们不小心使用”iptables -F”清空规则时,放行规则被删除,则所有数据包都无法进入,这个时候就相当于给管理员挖了个坑,所以,我们如果想要使用”白名单”的机制,最好将链的默认策略保持为”ACCEPT”,然后将”拒绝所有请求”这条规则放在链的尾部,将”放行规则”放在前面,这样做,既能实现”白名单”机制,又能保证在规则被清空时,管理员还有机会连接到主机。下面的白名单机制就比较合适。
[root@localhost ~]# iptables -P INPUT ACCEPT
[root@localhost ~]# iptables -I INPUT -p tcp --dport 22 -j ACCEPT
[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -j REJECT
[root@localhost ~]# iptables -nvL INPUT --line
Chain INPUT (policy ACCEPT 8889 packets, 9045K bytes)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 182 11316 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
3 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
既将INPUT链的默认策略设置为了ACCEPT,同时又使用了白名单机制,因为如果报文符合放行条件,则会被前面的放行规则匹配到,如果报文不符合放行条件,则会被最后一条拒绝规则匹配到,此刻,即使我们误操作,执行了”iptables -F”操作,也能保证管理员能够远程到主机上进行维护,因为默认策略仍然是ACCEPT