看懂iptables的规则,增删查改

2020-05-30  本文已影响0人  clam314

详细讲解系列http://www.zsythink.net/archives/1199

关注点:规则 = 匹配条件 + 动作

一、target:匹配到规则后的动作

在iptables规则中的target是数据包匹配到规则后需要进行的处理或者动作,可以分为基本和扩展。
一些常用的target:

二、查看规则 iptables -L

#简单查询filter表
iptables -t filter -L
#显示更多信息
 iptables -t filter -vL
#不对IP地址进行名称反解,直接显示IP地址
 iptables -t filter -nvL
#显示该表中指定链
 iptables -nvL INPUT
#显示规则的编号
iptables --line-number  -nvL INPUT
[clam@shell-host ~]$ sudo iptables -t filter -vL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
   37  2812 ACCEPT     all  --  any    any     anywhere             anywhere             state RELATED,ESTABLISHED
    0     0 ACCEPT     icmp --  any    any     anywhere             anywhere            
    0     0 ACCEPT     all  --  lo     any     anywhere             anywhere            
    0     0 ACCEPT     tcp  --  any    any     anywhere             anywhere             state NEW tcp dpt:ssh
   20  2639 REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 48 packets, 3718 bytes)
 pkts bytes target     prot opt in     out     source               destination         

[root@shell-host clam]# iptables --line-number  -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           
2       93  8789 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
6       83 10883 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
  1. 每条规则的字段的具体含义:
  1. 每条链括号里的字段含义:

当把链设置为接受(ACCEPT),应该是黑名单机制,但是上面显示的规则大部分是ACCEPT,并不是想象中的DROP或者REJECT,这因为IPTABLES的工作机制导致的,上例其实是利用了这些"机制",完成了所谓的"白名单"机制,并不是我们所描述的"黑名单"机制

三、增加规则 iptables -I

#插入规则
iptables -t filter -I INPUT -s 192.168.50.90 -j DROP
#指定位置插入规则
iptables -t filter -I INPUT  3 -s 192.168.50.90 -j DROP
#追加规则
iptables -t filter -A INPUT -s 192.168.50.90 -j ACCEPT
#插入规则
[root@shell-host clam]# iptables -t filter -I INPUT -s 192.168.50.90 -j DROP
[root@shell-host clam]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           
   80  7849 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    2   120 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
   63  8148 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
#追加规则
[root@shell-host clam]# iptables -t filter -A INPUT -s 192.168.50.90 -j ACCEPT
[root@shell-host clam]# iptables -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           
   93  8789 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
   87 11359 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
    0     0 ACCEPT     all  --  *      *       192.168.50.90        0.0.0.0/0           

四、删除规则 iptables -D |清空规则iptables -F

#根据规则的编号去删除规则
iptables -D INPUT 2
#根据具体的匹配条件与动作删除规则
iptables -D INPUT -s 192.168.50.90 -j DROP
#清空指定表的指定链中的所有规则
iptables -t filter -F INPUT
[root@shell-host clam]# iptables --line-number -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           
2       95  8941 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
5        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
6       91 11915 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
7        0     0 ACCEPT     all  --  *      *       192.168.50.90        0.0.0.0/0           
[root@shell-host clam]# iptables -D INPUT 2
[root@shell-host clam]# iptables --line-number -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           
2        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
5       91 11915 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
6        0     0 ACCEPT     all  --  *      *       192.168.50.90        0.0.0.0/0    
[root@shell-host clam]# iptables -D INPUT -s 192.168.50.90 -j DROP
[root@shell-host clam]# iptables --line-number -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4       97 12543 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 ACCEPT     all  --  *      *       192.168.50.90        0.0.0.0/0           

五、修改规则 iptables -R | 修改链 iptables -P

#修改规则
iptables -R INPUT 5 -s 192.168.50.90 -j DROP
#修改链的动作
iptables -P FORWARD DROP

命令没有使用-s指定对应规则中原本的源地址,那么在修改完成后,修改的规则中的源地址会自动变为0.0.0.0/0(此IP表示匹配所有网段的IP地址),而此时万一-j对应的动作又为REJECT,那么所有IP的请求都被拒绝了(因为没有指定原本的源地址,当前规则的源地址自动变为0.0.0.0/0),如果正在使用ssh远程到服务器上进行iptables设置,那么ssh请求也将会被阻断!

[root@shell-host clam]# iptables --line-number -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4       97 12543 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 ACCEPT     all  --  *      *       192.168.50.90        0.0.0.0/0           
[root@shell-host clam]# iptables -R INPUT 5 -s 192.168.50.90 -j DROP
[root@shell-host clam]# iptables --line-number -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4       97 12543 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           
[root@shell-host clam]# iptables --line-number -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4      105 13503 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 4 packets, 360 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
[root@shell-host clam]# iptables -P FORWARD DROP
[root@shell-host clam]# iptables --line-number -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        3   180 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
4      106 13581 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited
5        0     0 DROP       all  --  *      *       192.168.50.90        0.0.0.0/0           

Chain FORWARD (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination   

六、保存规则 service iptables save

  1. iptables的修改会立即生效,但是是临时的,iptables restart后会失效
  2. 对规则进行了修改以后,如果想要修改永久生效,必须使用"service iptables save"命令保存,规则默认保存在/etc/sysconfig/iptables文件中
  3. 不执行service iptables save,可以使用service iptables restart恢复到之前的状态,即:在restart前不save,之前的修改将会全部丢失,在重启iptables以后,规则会再次回到上次保存/etc/sysconfig/iptables文件时的模样
[root@shell-host clam]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  确定  ]
[root@shell-host clam]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Sat May 30 01:30:25 2020
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Sat May 30 01:30:25 2020
上一篇 下一篇

猜你喜欢

热点阅读