防火墙的富规则

1:防火墙的介绍

 在生产环境中,防火墙是不能关闭的

2:直接规则

就是简单的设置,

下面的操作,都是永久生效的,都带有permanent参数,所以,设置完后,要使用reload参数生效

 1:关于服务

放行服务

[root@localhost ~]# firewall-cmd --permanent --add-service=ftp
success
[root@localhost ~]# firewall-cmd --reload 
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client ftp
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

  

删除服务

[root@localhost ~]# firewall-cmd --permanent --remove-service=ftp
success
[root@localhost ~]# firewall-cmd --reload 
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	
[root@localhost ~]# 

  

 

2:关于端口

放行端口

[root@localhost ~]# netstat -pant|grep 21
tcp6       0      0 :::21                   :::*                    LISTEN      1796/vsftpd         
[root@localhost ~]# firewall-cmd --permanent --add-port=21/tcp
success
[root@localhost ~]# firewall-cmd --reload 
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client
  ports: 21/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

  

删除端口

[root@localhost ~]# firewall-cmd --permanent --remove-port=21/tcp
success
[root@localhost ~]# firewall-cmd --reload 
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
	

  

关于防火墙的直接规则,频繁使用都就只有这些,后续学到别的话,会及时补充的

 

3:富规则

就是设置一些清楚规则

下面的三种方式的富规则,不能同时的存在,在这个规则里面只能出现一种

语法的格式

[root@localhost ~]# firewall-cmd --help | grep rich 
  --list-rich-rules    List rich language rules added for a zone [P] [Z]
  --add-rich-rule=<rule>
                       Add rich language rule 'rule' for a zone [P] [Z] [T]
  --remove-rich-rule=<rule>
                       Remove rich language rule 'rule' from a zone [P] [Z]
  --query-rich-rule=<rule>
                       Return whether a rich language rule 'rule' has been
[root@localhost ~]# 

 

1:添加ftp服务的富规则

允许10网段的去访问,不允许20网段的访问

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.20.0/24 service name=ftp reject'

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.10.0/24 service name=ftp accept'

  

测试:

[root@localhost /]# curl ftp://192.168.20.101
curl: (7) Failed connect to 192.168.20.101:21; Connection refused
[root@localhost /]# curl ftp://192.168.10.101
-rw-r--r--    1 0        0               0 Dec 14 07:03 flag
[root@localhost /]# 

 

证明成功了,只有

有个疑问了,我把客户端的10网段断开了,但是我20网段还是能访问,服务器上面的文件,也就是说禁止20网段没有成功(问老师吧)

这个nat模式能ping自己,也能ping仅主机的网卡的地址,

 

2:放行端口

语法:

 

firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.20.0/24 port port=80 protocol=tcp accept'

 

port port=端口号 protocol=协议名 动作  

 

 

3:端口转发

需要注意点就是端口转发和放行服务不能同时存在

就是将80端口转发到8088端口上面去,用户在访问80的时候,会自动的跳转到8088的端口的页面上去,从而之前的80端口的页面就被隐藏了,但是我直接去访问8088了,也能呈现出跟80一样的页面。

(悟了)

 

 

firewal-cmd --permanent --add-rich-rule='rule family=ipv4 source address=0.0.0.0/0 forward-port 
 port=80 prtotocol=tcp to-port=22'

 

forward-port port=端口号,protocol=协议名 to-port=转发到的端口号 

就是隐藏了了以前的端口号 

 

转发端口的案例:

适用于http服务,

 

 

  

 

posted @ 2023-12-14 16:39  q_7  阅读(65)  评论(0编辑  收藏  举报