day49-Firewalld防火墙

2019-10-08  本文已影响0人  江枍_a99e

1.安全

(1)硬件环境:

①硬件层面:

(2)云环境:

①系统层面:

(3)云架构
(4)HTTPS+WAF+负载均衡

https://help.aliyun.com/document_detail/61993.html?spm=a2c4g.11186623.6.573.c85f5414Ajl83H

1570502634323.png
(5)HTTPS+高仿IP+WAF+负载均衡

https://help.aliyun.com/document_detail/35163.html?spm=a2c4g.11186623.6.792.2fc36251vRqVB1
ps:①高防需要配置HTTPS
② WAF也需要配置HTTPS
③ 源站 http
④考虑安全 性能差
⑤考虑性能 安全弱

1570499089835.png

2.firewalld防火墙

(1)firewalld与iptables的区别
(2)规则
(3)firewalld查看处于哪个区域
[root@manager ~]# firewall-cmd --get-active-zones 
public

[root@manager ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1
  sources: 
  services: ssh dhcpv6-client
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
(4)使用firewalld各个区域规则结合配置,调整默认public区域拒绝所有流量,但如果来源IP是10.0.0.0/24网段则允许。 复规则实现(只需要一个区域)
[root@manager ~]# firewall-cmd --remove-service={ssh,dhcpv6-client}
[root@manager ~]# firewall-cmd --add-source="10.0.0.0/24" --zone=trusted
[root@manager ~]# firewall-cmd --get-active-zones 
public
  interfaces: eth0 eth1
trusted
  sources: 10.0.0.0/24
  

#清空配置
[root@manager ~]# firewall-cmd --reload
(5)firewalld放行端口
#添加放行端口
[root@manager ~]# firewall-cmd --add-port=80/tcp
[root@manager ~]# firewall-cmd --add-port={8081/tcp,8082/tcp}

#移除放行端口
[root@manager ~]# firewall-cmd --remove-port=80/tcp 
[root@manager ~]# firewall-cmd --remove-port={8081/tcp,8082/tcp}

[root@manager ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1
  sources: 
  services: ssh dhcpv6-client
  ports: 80/tcp 8081/tcp 8082/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 

(6)放行服务--->对应的还是端口-->比端口看起来更清晰
[root@manager ~]# firewall-cmd --add-service=http
[root@manager ~]# firewall-cmd --add-service=https
[root@manager ~]# firewall-cmd --add-service={zabbix-agent,zabbix-server}
[root@manager ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1
  sources: 
  services: ssh dhcpv6-client http https zabbix-agent zabbix-server
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
  
  
 移除
[root@manager ~]# firewall-cmd --remove-service={zabbix-agent,zabbix-server}
(7)自定义服务名称--->服务对应的端口 8080 8081 8082 -->api业务
[root@manager services]# cd /usr/lib/firewalld/services
[root@manager services]# cp http.xml api.xml
[root@manager services]# cat api.xml 
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>API (HTTP)</short>
  <port protocol="tcp" port="8081"/>
  <port protocol="tcp" port="8082"/>
  <port protocol="tcp" port="8083"/>
</service>

[root@manager services]# firewall-cmd --reload
success
[root@manager services]# firewall-cmd --add-service=api
success

3.firewalld实现端口转发

1570518954254.png
firewall-cmd --permanent --zone=<区域> --add-forward-port=port=<源端口号>:proto=<协议>:toport=<目标端口号>:toaddr=<目标IP地址>

[root@manager ~]# firewall-cmd --add-forward-port=port=5555:proto=tcp:toport=22:toaddr=172.16.1.31

# 地址伪装
[root@manager ~]# firewall-cmd --add-masquerade
#------------------------------------------------------->
#管理上抓包
[root@manager ~]# tcpdump port 5555 -nn

#后端主机的抓包
[root@nfs ~]# tcpdump -i eth1 port 22 -nn

4.Firewalld富规则

[root@Firewalld ~]# man firewalld.richlanguage  # 获取富规则手册
    rule
        [source]
        [destination]
        service|port|protocol|icmp-block|masquerade|forward-port
        [log]
        [audit]
        [accept|reject|drop]

rule [family="ipv4|ipv6"]
source address="address[/mask]" [invert="True"]
service name="service name"
port port="port value" protocol="tcp|udp"
forward-port port="port value" protocol="tcp|udp" to-port="port value" to-addr="address"
accept | reject [type="reject type"] | drop

例题:
1.允许10.0.0.1主机能够访问 http服务,允许172.16.1.0/24能访问8080端口

10.0.0.1   --->    80         http     除此以外都不可以访问
172.16.1.x --->    8080       api      除此以外都不可以访问

[root@manager ~]# firewall-cmd --reload

[root@manager ~]# firewall-cmd --add-rich-rule='rule family=ipv4  source address=10.0.0.1/32 service name="http" accept'

[root@manager ~]# firewall-cmd --add-rich-rule='rule family=ipv4  source address=172.16.1.0/24 service name="api" accept'

2.默认public区域对外开放所有人能通过ssh服务连接,但拒绝172.16.1.0/24网段通过ssh连接服务器*

[root@manager ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=172.16.1.0/24 service name="ssh" drop'

3.使用firewalld,允许所有人能访问http,https服务,但只有10.0.0.1主机可以访问ssh服务

[root@manager ~]# firewall-cmd --reload
[root@manager ~]# firewall-cmd --add-service={http,https}
[root@manager ~]# firewall-cmd --remove-service=ssh

[root@manager ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=10.0.0.1/32 service name="ssh" accept'


####使用端口方式  2222
[root@manager ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address=10.0.0.1/32 port port="2222" protocol=tcp accept'

4.当用户来源IP地址是10.0.0.1主机,则将用户请求的5555端口转发至后端172.16.1.31的22端口

任何人访问10.0.0.61 5555端口都给转发
10.0.0.1   --->    10.0.0.61 5555   ---> 172.16.1.31 22

[root@manager ~]# firewall-cmd --add-rich-rule='rule family=ipv4 source address="10.0.0.1/32" forward-port port="5555" protocol="tcp" to-port="22" to-addr="172.16.1.31"'
[root@manager ~]#  firewall-cmd --add-masquerade 

5.firewalld实现共享上网

在指定的带有公网IP的实例上启动Firewalld防火墙的NAT地址转换,以此达到内部主机上网。


15442375496785.jpg

详细流程


15517748885644.jpg
(1)firewalld防火墙开启masquerade, 实现地址转换
[root@Firewalld ~]# firewall-cmd --add-masquerade --permanent
[root@Firewalld ~]# firewall-cmd --reload

(2)客户端将网关指向firewalld服务器,将所有网络请求交给firewalld

[root@web03 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
GATEWAY=172.16.1.61

(3)客户端还需配置dns服务器

[root@web03 ~]# cat /etc/resolv.conf
nameserver 223.5.5.5

(4)重启网络,使其配置生效

[root@web03 ~]# ifdown eth1 && ifup eth1

(5)测试后端web的网络是否正常

[root@web03 ~]# ping baidu.com
PING baidu.com (123.125.115.110) 56(84) bytes of data.
64 bytes from 123.125.115.110 (123.125.115.110): icmp_seq=1 ttl=127 time=9.08 ms

firewalld共享上网方式不是特别推荐 (阿里云上如何实现---> 提交工单)

推荐:

​ 物理环境: 使用路由器来实现上网

​ 云环境: 推荐使用NAT网关设备
WAF+负载均衡配置

上一篇下一篇

猜你喜欢

热点阅读