美文网首页
看懂iptables的规则,增删查改

看懂iptables的规则,增删查改

作者: clam314 | 来源:发表于2020-05-30 01:44 被阅读0次

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

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

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

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

    • ACCEPT:允许数据包通过
    • DROP:直接丢弃数据包,不给任何回应信息,这时候客户端会感觉自己的请求泥牛入海了,过了超时时间才会有反应
    • REJECT:拒绝数据包通过,必要时会给数据发送端一个响应的信息,客户端刚请求就会收到拒绝的信息
    • SNAT:源地址转换,解决内网用户用同一个公网地址上网的问题
    • MASQUERADE:是SNAT的一种特殊形式,适用于动态的、临时会变的ip上
    • DNAT:目标地址转换
    • REDIRECT:在本机做端口映射
    • LOG:在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则,也就是说除了记录以外不对数据包做任何其他操作,仍然让下一条规则去匹配

    二、查看规则 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
    
    • -t:指定要操作的表,此例为filter表,可以省略-t filter,当没有使用-t选项指定表时,默认为操作filter表
    • -v:展示更多信息
    • -L:列出所有规则
    • -n:不对IP地址进行名称反解,直接显示IP地址
    • --line-number:显示规则的编号
    [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. 每条规则的字段的具体含义:
    • pkts: 对应规则匹配到的报文的个数
    • bytes: 对应匹配到的报文包的大小总和
    • target: 规则对应的target,往往表示规则对应的"动作",即规则匹配成功后需要采取的措施
    • prot: 表示规则对应的协议,是否只针对某些协议应用此规则
    • opt: 表示规则对应的选项
    • in: 表示数据包由哪个接口(网卡)流入,我们可以设置通过哪块网卡流入的报文需要匹配当前规则
    • out: 表示数据包由哪个接口(网卡)流出,我们可以设置通过哪块网卡流出的报文需要匹配当前规则
    • source: 表示规则对应的源头地址,可以是一个IP,也可以是一个网段
    • destination: 表示规则对应的目标地址。可以是一个IP,也可以是一个网段
    1. 每条链括号里的字段含义:
    • policy :当前链的默认策略,policy ACCEPT表示该链的默认动作为ACCEPT,默认接受通过该节点(链)的所有请求,所以在配置该链的具体规则时,应该将需要拒绝的请求配置到规则中,说白了就是"黑名单"机制,默认所有人都能通过,只有指定的人不能通过

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

    • packets:表示当前链默认策略匹配到的包的数量,0 packets表示默认策略匹配到0个包
    • bytes:表示当前链默认策略匹配到的所有包的大小总和

    三、增加规则 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
    
    • -I:表示insert,插入,-I INPUT表示在INPUT链的首部插入规则
    • -s:表示source,指明"匹配条件"中的"源地址",即如果报文的源地址属于-s对应的地址,那么报文则满足匹配条件
    • -j:指明当"匹配条件"被满足时,所对应的动作,此例中指定的动作为DROP
    • 3:表示插入编号为3的规则,其余规则标号后移一位
    • A:表示append,追加,-A INPUT表示在INPUT链的尾部追加规则
    #插入规则
    [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
    
    • -D:表示删除指定链中的某条规则
    • -F:表示清空对应链中的规则,执行时需三思
    [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
    
    • -R:表示修改指定的链,虽然指定了编号,但-s选项以及对应的源地址不可省略,必须指定规则对应的原本的匹配条件(如果有多个匹配条件,都需要指定)

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

    • -P:表示修改指定的链
    [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
    

    相关文章

      网友评论

          本文标题:看懂iptables的规则,增删查改

          本文链接:https://www.haomeiwen.com/subject/sxjqzhtx.html