iptables详解

2017-05-02  本文已影响0人  ckhzw

netfilter 内核中的防火墙框架,承载并生效规则;4表5链;

iptables 规则管理工具;

内核中的TCP/IP协议栈上,本生并没有防护的功能,netfilter在TCP/IP协议栈上设置几道关卡,勾住流经的报文;根据在其上使用iptables命令设置的规则,作出相应的操作;

  1.实现的功能:用于判定将规则添加至哪个表;
  2.报文的流经位置:用户判断将规则添加至哪个链;
  3.报文的流向:判定规则中何为源,何为目标;
  4.匹配条件:用户编写正确的匹配规则;
  5. 专用于某种应用的同类规则,匹配范围小的放在前面;
  6.专用于某些应用的不同规则,匹配到的可能性较多的放在前面;同类别的规则可以使用自定义链单独存放;
  7.用于通用目录的规则放在前面;

iptables命令语法格式:

iptables [-t TABLE] COMMAND CHAIN 基本匹配条件 [-m 扩展匹配条件] -j 处理动作
-t TABLE 默认filter表

  COMMAND :
    链管理:
      -N CHAIN:用户自定义链,被调用后才能使用;
      -X CHAIN 删除用户自定义的且引用计数为0的空链
      -E old-chain new-chain 重命名自定义链,且引用计数为0;
      -P chain target 设置默认策略;
      -F [chain] 清空链上的规则,默认为清空表上的所有链上的规则;
      -S chain 列出指定链的规则;
      -Z chain [rulenum] 将指定链上的计数器至零; 被匹配到的包个数和所有被本规则匹配到的总字节数
    规则显示:-vnL 
          -vnL --line-numbers 列出规则是,显示其在链上的相应的编号;
            -S chain 显示指定链上的所有规则
    规则管理:增删改查
      -A CHAIN 追加一条规则;
      -I CHAIN NUM 在指定的位置插入一条规则;
      -R CHAIN NUM 替换指定的规则
      -D chain num 根据编号删除规则
      -D chain rule 根据规则删除

通用匹配条件:

  -d 目标地址;
  -s 源地址;
  -p tcp|udp|icmp 指定协议
  -i IFACE 报文流入接口;PREROUTING,INPUT,FORWARD
  -o IFACE 报文流出接口:OUTPUT,FORWARD,POSTROUTING 

扩展匹配条件:

规则的保存和还原:

    保存规则:
      iptables-save >iptables
    还原规则:
      iptables-restore <iptables 

示例:

开放ssh给所有主机,开放samba给172.18.20.6 
[root@CentOS7 ~]# iptables -F  
[root@CentOS7 ~]# iptables -A INPUT -d 172.18.20.7 -p tcp --dport 22 -j ACCEPT  
[root@CentOS7 ~]# iptables -A INPUT -d 172.18.20.7 -j REJECT
[root@CentOS7 ~]# iptables -vnL --line-numbers
Chain INPUT (policy ACCEPT 59 packets, 6120 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      286 19032 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:22
2       33  2712 REJECT     all  --  *      *       0.0.0.0/0            172.18.20.7          reject-with icmp-port-unreachable
[root@CentOS7 ~]# iptables -I INPUT 2 -d 172.18.20.7 -p udp --dport 137:138 -j ACCEPT 
[root@CentOS7 ~]# iptables -I INPUT 2 -d 172.18.20.7 -p tcp --dport 139 -j ACCEPT 
[root@CentOS7 ~]# iptables -I INPUT 2 -d 172.18.20.7 -p tcp --dport 445 -j ACCEPT 
[root@CentOS7 ~]#iptables -A OUTPUT -d 172.18.20.7 -j REJECT
[root@CentOS7 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
 1276 95960 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:22
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:445
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:139
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            172.18.20.7          udp dpts:137:138
   39  3052 REJECT     all  --  *      *       0.0.0.0/0            172.18.20.7          reject-with icmp-port-unreachable

#自定义smb_input_rules 链开放ssh给所有客户端,拒绝所有连接,允许172.18.20.7访问samba服务
[root@CentOS7 ~]# iptables -F 
[root@CentOS7 ~]# iptables -A INPUT -d 172.18.20.7 -p tcp --dport 22 -j ACCEPT 
[root@CentOS7 ~]# iptables -N smb_input_rules 
[root@CentOS7 ~]# iptables -A smb_input_rules -d 172.18.20.7 -p udp --dport 137:138 -j ACCEPT 
[root@CentOS7 ~]# iptables -A smb_input_rules -d 172.18.20.7 -p tcp --dport 139 -j ACCEPT 
[root@CentOS7 ~]# iptables -A smb_input_rules -d 172.18.20.7 -p tcp --dport 445 -j ACCEPT 
[root@CentOS7 ~]# iptables -vnL smb_input_rules 
Chain smb_input_rules (0 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 ACCEPT     udp  --  *      *       0.0.0.0/0            172.18.20.7          udp dpts:137:138
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:139
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            172.18.20.7          tcp dpt:445
[root@CentOS7 ~]# iptables -A INPUT -d 172.18.20.7 -j smb_input_rules 
 [root@CentOS7 ~]iptables -A INPUT -d 172.18.20.7 -j REJECT 
[root@CentOS7 ~]# iptables -vnL OUTPUT 
Chain OUTPUT (policy ACCEPT 44 packets, 5080 bytes)
 pkts bytes target     prot opt in     out     source               destination         

#删除自定义链(smb_input_rules)
[root@CentOS7 ~]# iptables -D INPUT 2   #删除引用计数
[root@CentOS7 ~]# iptables -F smb_input_rules  #清空规则
[root@CentOS7 ~]# iptables -X smb_input_rules  #删除自定且引用计数为0的空链
上一篇下一篇

猜你喜欢

热点阅读