1.认识防火墙 —— 通过定义一些有顺序的规则,并管理进入到网络内的主机数据包的一种机制。
1.1)防火墙最重要的任务:
- 切割被信任 (如 子域)与不信任 (如 Internet)的网段
- 划分出可提供 Internet 的服务与必须受保护的服务
- 分析出可以接受与不可接受的数据包状态
1.2)Linux 系统上的防火墙的主要类别
- Netfilter (数据包过滤机制)—— 在 Linux 上面我们使用内核建立了 Netfilter 机制,而 Netfilter 提供了 iptables 及 firewalld 软件来作为 防火墙数据包包过滤的命令。
- TCP Wrappers (程序管理)
- Proxy (代理服务器)
2.iptables
2.1)安装 iptables
- dnf search iptables # 查找 iptables 相关软件
iptables-services.x86_64 : iptables and ip6tables services for iptables
- dnf install iptables-services.x86_64 -y # 安装 iptables-services.x86_64
2.2)启动 iptables.service
- systemctl stop firewalld.service # 关闭 firewalld.service
- systemctl disable firewalld.service # 不允许 开机启动 firewalld.service
- systemctl mask firewalld.service # 锁住 firewalld.service
- systemctl enable --now iptables.service # 立即 启用 ,并允许开机启动 iptables.service
2.3)iptables 的语法
2.3.1)iptables 规则的 查看 、保存与 清除
iptables [ -t tables ] [ -L ] [ -nv]
- -t :后接 table ,例如 filter 或 nat , 若省略此项 ,默认为 filter
- -L:列出 目前的 table 规则
- -n:不进行 IP 与 HOSTNAME 的反查,显示信息的速度更快
- -v:列出更多信息,如 该规则的数据包总位数、相关的网络接口等
2.3.2)iptables-save [ -t table] 列出完整的防火墙规则
在硬件磁盘上备份保存防火墙规则 # 清除规则后,重启服务,数据规则会恢复
- iptables-save > /etc/sysconfig/iptables # 永久保存方法一 ,输出导入到 /etc/sysconfig/iptables 策略记录文件
- service iptables save # 永久保存方法二 ,命令保存
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
iptables [ -t table] [ -FXZ ]
- -F:清除所有已制定的规则
- -X:清除所有用户“自定义”的 chain (tables)
- -Z:将所有的 chain 的计数与流量统计归零
2.3.4)定义默认策略(Policy)
iptables [ -t nat ] -P [ INPUT ,OUTPUT,FORWARD ]
- -P:定义策略(Policy)
- ACCEPT:该数据包可接受
- DROP:该数据包直接丢弃,不会让 Client 端知道为何被丢弃!
2.3.5)数据包的基础对比:IP、网络 及 接口设备
iptables [ -AID 链名 ] [ -io 网络接口 ] [ -p 协议 ] \ > [ -s 来源 IP 网络 ] [ -d 目标 IP 网络 ] -j [ ACCEPT | DRPO |REJECT |LOG ]
[ -AI 链名 ]:针对某条链进行规则的 “插入”或 “累加”
- -A:新增加一条规则,改规则增加到原有规则最后面
- -I:插入一条规则,如果没有指定插入顺序,默认插入变成第一条规则
- D:删除一条规则
- 链:如 INPUT 、OUTPUT 、FORWARD 等
[ -io 网络接口 ]:设置数据包进出的接口规范
- -i:数据包所进入的网络接口,需与 INPUT 链配合
- -o:数据包所传出的网络接口,需与 OUTPUT 链配合
[ -p 协议 ] :设置规则适用于那种数据包格式
- 主要的数据包格式 : tcp 、udp、icmp 及 all。
[ -s 来源 IP 网络 ]:设置此规则的数据包的来源地,可指定单纯的 IP 或 网络 (如 规范为“不许”时 ,则加上“!”)
[ -d 目标 IP 网络 ]:设置此规则的数据包的目标地,可指定单纯的 IP 或 网络
-j :[ ACCEPT 接收 | DRPO 丢弃 |REJECT 拒绝 |LOG 记录 ]
2.3.6)TCP 、UDP 的规则对比:针对端口设置
iptables [ -AI 链名 ] [ -io 网络接口 ] [ -p 协议 tcp、udp ]
- [ -s 来源 IP 网络 ] [ --sport 端口范围 ] -j [ ACCEPT 接受 | DRPO 丢弃 |REJECT 拒绝 ]
- [ -d 目标 IP 网络 ] [ --dport 端口范围 ] -j [ ACCEPT | DRPO |REJECT ]
例: 设置防火墙策略,使主机仅能接受80端口的数据访问,及 自己访问自己(回环接口)
防火墙策略设置(172.25.254.20 主机)
- iptables -A INPUT -i lo -j ACCEPT # 开放 lo 这个本机的接口
- iptables -A INPUT -p tcp --dport 80 -j ACCEPT # 允许 INPUT 接受 80 端口访问
- iptables -A INPUT -p tcp -j REJECT # 禁止 INPUT 接受访问 (排在第三顺序,先读取的规则是优先的,即除了 80 端口,其他的端口都不接受数据访问)
- iptables -nL # 查看设置
测试 (172.25.254.10 主机):
- ssh 172.25.254.20 -l root # ssh 协议 通过 22 端口访问,不允许
ssh: connect to host 172.25.254.20 port 22: Connection refused
- firefox http:\\172.25.254.20 # http 协议通过 80 端口 访问,允许
2.3.7)iptables 外挂模块:mac(网卡硬件地址) 与 state(状态模块)
iptables -A INPUT [ -m state ] [ --state 状态 ]
-m :iptables 的外挂模块 ,常见如下
- state :状态模块
- mac:网卡硬件地址 (hardware address)
--state:数据包状态,主要如下
- INVALID:无效的数据包,如数据破损的数据包状态
- ESTABLISHED:已经连接成功的连接状态
- NEW:新建立连接的数据包状态
- RELATED:表示这个数据包是与主机发送出去的数据包有关
例:设置防火墙数据包状态模块
- iptables -F
- iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
- iptables -A INPUT -i lo -m state --state NEW -j ACCEPT
- iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
- iptables -A INPUT -s 172.25.254.10 -p tcp --dport 22 -m state --state NEW -j ACCEPT
- iptables -A INPUT -m state --state NEW -j REJECT
- iptables -nL
- service iptables save # 保存 防火墙策略设置
2.4 )NAT 服务器的设置
NAT ( 网络地址的转换 ,Network Address Translation )
2.4.1) SNAT( 来源 NAT ,Source NAT ) :修改数据包报头的来源项目
- SNAT数据包传出时,Linux 主机通过 iptables 的 NAT table 内的 POSTROUTING 链 将数据包报头的来源伪装成 Linux 的 public IP ,并将两个不同来源(172.25.254.10 及 192.168.0.100 public IP)的数据包对应写入暂存内存中,然后将此数据包传送出去
- SNAT数据包接收时,NAT table 内的 POSTROUTING 链,会将 目标 IP 修改成为后端主机(172.25.254.10),然后发现目标已不是本机 (192.168.0.100 public IP),通过路由分析数据包流向,传送到 172.25.254.20 (内部接口),然后再传送到最终目标(172.25.254.10)
内网客户端主机设置
- vim /etc/sysconfig/network-scripts/ens160
BOOTPROTO="none"
NAME="ens160"
DEVICE="ens160"
ONBOOT="yes"
IPADDR=172.25.254.10
NETMASK=255.255.255.0
GATWAY=172.25.254.20 # 设置 网关为 172.25.254.20
- nmcli connection reload
- nmcli connection down ens160
- nmcli connection up ens160
外网主机设置
- cat /etc/sysconfig/network-scripts/ens160
BOOTPROTO="none"
NAME="ens160"
DEVICE="ens160"
ONBOOT="yes"
IPADDR=192.168.0.30
NETMASK=255.255.255.0
NAT 主机设置
- iptables -F
- service iptables save
- iptables -t nat -A POSTROUTING -o ens192 -j SNAT --to-source 172.25.254.20
- iptables -t nat -nL
- vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
- sysctl -a | grep ip_forward
net.ipv4.ip_forward = 1
2.4.2) DNAT( 目标 NAT ,Destination NAT ) :修改数据包报头的目标项目
- DNAT,主要用在内部主机架设可以让 internet 访问的服务器
- NAT 服务器 ,会将目标 IP 由 public IP 改成 内部主机172.25.254.10
NAT 主机设置
- iptables -t nat -A PREROUTING -i ens192 -j DNAT --to-dest 172.25.254.10 # 将路由前数据目标 IP 更改为 172.25.254.10
- iptables -t nat -nL # 查看 nat 表
2.5 firewalld
2.5.1)安装 firewalld
- dnf search firewalld # 查找 firewalld 相关软件
firewalld.noarch : A firewall daemon with D-Bus interface providing a dynamic firewall
- dnf install firewalld.noarch -y # 安装 firewalld.noarch
启动 firewalld
- systemctl disable --now iptables.service # 立即 关闭 ,并禁止开机启动 iptables.service
- systemctl mask iptables.service # 锁住 iptables.service
- systemctl unmask firewalld.service # 解锁 firewalld.service
- systemctl enable --now firewalld.service # 立即 启用 ,并允许开机启动 firewalld.service
2.5.2)firewalld 的 域
- trusted:接受所有的网络连接
- home:家庭网络,允许 ssh,mdns,ipp-client,samba-client,dhcp-client
- work:工作网络,允许 ssh,ipp-client,,dhcp-client
- public:公共网络,允许 ssh,dhcp-client
- dmz:军用级网络,允许 ssh
- block:拒绝所有
- drop:丢弃,所有数据丢弃,无任何回复
- internal:内部网络,允许 ssh,mdns,ipp-client,samba-client,dhcp-client
- external:ipv4 网络地址伪装转发 ,允许 sshd
firewalld 的设定原理及 数据存储
- /etc/firewalld # 火墙配置目录
- /lib/firewalld # 火墙模块目录
自定义 服务模块
- cd /lib/firewalld/services/
- cp http.xml westos.xml # 以 http.xml 模块为模板 ,复制生成 westos.xml 自定义服务文件
- vim westos.xml # 编辑 修改端口为 8080
- firewall-cmd --reload
- firewall-cmd --get-services | grep westos
firewalld 的管理命令
- firewall-cmd --state # 查看火墙状态
- firewall-cmd --get-active-zones # 查看当前火墙生效的域
- firewall-cmd --get-default-zone # 查看火墙默认的域
- firewall-cmd --list-all # 查看默认域中的火墙策略
- firewall-cmd --list-all --zone=work # 查看指定域(work)中的火墙策略
- firewall-cmd --set-default-zone=trusted # 设定默认 域
- firewall-cmd --get-servers # 查看所有可以设定的服务
- firewall-cmd --permanent --remove-service=cockpit # 移除 服务 cockpit
- firewall-cmd --reload # 重启 firewalld
firewalld 的高级规则
- firewall-cmd --permanent --get-all-rules # 查看高级规则
- firewall-cmd --direct --add-rule ipv4 filter INPUT 1 -s 172.25.254.0/24 -p tcp --dport 22 -j ACCEPT
firewalld 的 SNAT
- firewall-cmd --permanent --add-masquerade
- firewall-cmd --reload
firewalld 的 DNAT
- firewall-cmd --permanent --add-forward-por=port=22:porto=tcp:toaddr=172.25.254.30
- firewall-cmd --reload