网络防火墙之自定义chain

网络防火墙

自定义链
链管理:
  -N:new, 自定义一条新的规则链
  -X:delete,删除自定义的空的规则链
  -P:Policy,设置默认策略;对filter表中的链而言,其默认策略有:
    ACCEPT:接受
    DROP:丢弃
  -E:重命名自定义链;引用计数不为0的自定义链不能够被重命名,也不能被删除
 
iptables/netfilter网络防火墙:
  (1) 充当网关
  (2) 使用filter表的FORWARD链
注意的问题:
  (1) 请求-响应报文均会经由FORWARD链,要注意规则的方向性
  (2) 如果要启用conntrack机制,建议将双方向的状态为ESTABLISHED的报文直接放行
 
准备:
firewall:开启ip_forward功能
  [root@firewall ~]#vim /etc/sysctl.conf
  net.ipv4.ip_forward = 1
  [root@firewall ~]#sysctl -p
  [root@firewall ~]#sysctl -a
firewall开启转发功能后,10.0.0.108能互相ping通192.168.37.122
 
 
示例:
 

 

 场景:把对外访问的策略配置在自定义链中:

1、新建链,专用添加访问外网策略

1 [root@firewall-121 ~]# iptables -N TOINTERNET
2 [root@firewall ~]# iptables -vnL --line-numbers
3 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
4 num   pkts bytes target     prot opt in     out     source               destination         
5 1        6   394 ACCEPT     tcp  --  *      *       0.0.0.0/0            10.0.0.108           tcp dpt:80
6 2       22  2089 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
7 3      105  6996 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
8 
9 Chain TOINTERNET (0 references)
View Code

2、在新链中添加策略

1 [root@firewall-121 ~]# iptables -A TOINTERNET -s 10.0.0.108 -p tcp --dport 80 -j ACCEPT
2 [root@firewall-121 ~]# iptables -A TOINTERNET -s 10.0.0.108 -p icmp  --icmp-type 8  -j ACCEPT
View Code

3、把新链关联到FORWARD链中

 1 [root@firewall-121 ~]# iptables -I FORWARD 1 -j TOINTERNET
 2 [root@firewall-121 ~]# iptables -vnL --line-numbers
 3 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 4 num   pkts bytes target     prot opt in     out     source               destination         
 5 1       26  2246 TOINTERNET  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
 6 2        6   394 ACCEPT     tcp  --  *      *       0.0.0.0/0            10.0.0.108           tcp dpt:80
 7 3       33  3287 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
 8 4      106  7056 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable
 9 
10 Chain TOINTERNET (1 references)
11 num   pkts bytes target     prot opt in     out     source               destination         
12 1       12   796 ACCEPT     tcp  --  *      *       10.0.0.108           0.0.0.0/0            tcp dpt:80
13 2        3   252 ACCEPT     icmp --  *      *       10.0.0.108           0.0.0.0/0            icmptype 8
View Code

4、验证新chain策略有效性

1 [root@CentOS7-108 ~]# curl 192.168.37.122
2 internet server
3 [root@CentOS7-108 ~]# ping 192.168.37.122
4 64 bytes from 192.168.37.122: icmp_seq=1 ttl=63 time=0.551 ms
View Code

posted @ 2024-08-29 08:38  起点·漫步前行  阅读(21)  评论(0)    收藏  举报