Contents
  1. 1. IPtable
    1. 1.1. iptables传输数据包的过程:
    2. 1.2. 4个表: 优先顺序Raw——mangle——nat——filter
    3. 1.3. 5个链:链(chains)是数据包传播的路径,每一条链是众多规则中的一个检查清单,每一条链中可以有一条或数条规则:
    4. 1.4. 原则
    5. 1.5. 命令
      1. 1.5.1. rule-specification
      2. 1.5.2. -m常用模块
      3. 1.5.3. 备份还原
      4. 1.5.4. 限制访问ip, 按顺序先允许个别,后拒绝所有
    6. 1.6. 数据传输:BDOC-邮箱-北方-萝岗
    7. 1.7. (optional-部分机器)
  2. 2. 端口转发
  3. 3. Firewalld
    1. 3.0.1. firewalld区域概念
    2. 3.0.2. 配置

目前Linux系统的防火墙类型主要有两种:分别是iptables和firewalld,他们不是真正的防火墙,是指用来定义防火墙规则功能的”防火墙管理工具/程序”,将定义好的规则交由内核中的netfilter即网络过滤器来读取,从而真正实现防火墙功能。

在配置防火墙时,不建议两种配置方法结合使用(建议只使用其中的一种)

  • iptables-静态防火墙
    • 早期的Linux系统中默认使用的是iptables防火墙,配置文件在/etc/sysconfig/iptables,主要工作在网络层
    • 该防火墙使用链式规则,只可以过滤互联网的数据包,无法过滤从内网到内网的数据包
    • iptables只可以通过命令行进行配置
    • iptables默认是允许所有,需要通过拒绝去做限制
    • iptables在修改了规则之后必须得全部刷新才可以生效,还会丢失连接(无法守护进程)
  • firewalld-动态防火墙
    • 取代了之前的iptables防火墙,配置文件在/usr/lib/firewalld和/etc/fiewalld中,主要工作在网络层
    • 新增区域概念,不仅可以过滤互联网的数据包,也可以过滤内网的数据包
    • firewalld不仅可以通过命令行进行配置,也可以通过图形化界面配置
    • firewalld默认是拒绝所有,需要通过允许去放行
    • firewalld可以动态修改单条规则,动态管理规则集(允许更新规则而不破环现有会话和连接,可以守护进程)

IPtable

iptables传输数据包的过程:

  1. 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去。
  2. 如果数据包就是进入本机的,它就会沿着图向下移动,到达INPUT链。数据包到了INPUT链后,任何进程都会收到它。本机上运行的程序可以发送数据包,这些数据包会经过OUTPUT链,然后到达POSTROUTING链输出。
  3. 如果数据包是要转发出去的,且内核允许转发,数据包就会如图所示向右移动,经过FORWARD链,然后到达POSTROUTING链输出。

4个表: 优先顺序Raw——mangle——nat——filter

  • filter表:包过滤:INPUT、FORWARD、OUTPUT
  • nat表:网络地址转换(IP、端口):PREROUTING、POSTROUTING、OUTPUT
  • mangle表:包重构(修改数据包的服务类型、TTL、并且可以配置路由实现QOS内核模块):PREROUTING、POSTROUTING、INPUT、OUTPUT、FORWARD
  • raw表:决定数据包是否被状态跟踪机制处理:OUTPUT、PREROUTING

5个链:链(chains)是数据包传播的路径,每一条链是众多规则中的一个检查清单,每一条链中可以有一条或数条规则:

  • PREROUTING:nat,mangle,raw
  • FORWARD:mangle,filter
  • INPUT:mangle,filter
  • OUTPUT:nat,mangle,raw,filter
  • POSTROUTING:nat,mangle,raw

原则

INPUT严防,OUTPUT预防;
细粒度在前,粗粒度在后;

命令

iptables
https://zhuanlan.zhihu.com/p/501380475
https://ipset.netfilter.org/iptables.man.html

Each table contains built-in chains and user-defined chains. INPUT FORWARD OUTPUT
Each chain is a list of rules
Each rule specifies what to do with a packet that matches. ACCEPT, DROP or RETURN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
--append  -A chain		Append to chain
--check -C chain Check for the existence of a rule
--delete -D chain Delete matching rule from chain
--delete -D chain rulenum Delete rule rulenum (1 = first) from chain
--insert -I chain [rulenum] Insert in chain as rulenum (default 1=first)
--replace -R chain rulenum Replace rule rulenum (1 = first) in chain
--list -L [chain [rulenum]] List the rules in a chain or all chains
--list-rules -S [chain [rulenum]] Print the rules in a chain or all chains
--flush -F [chain] Delete all rules in chain or all chains
--zero -Z [chain [rulenum]] Zero counters in chain or all chains
--new -N chain Create a new user-defined chain
--delete-chain -X [chain] Delete a user-defined chain
--policy -P chain target Change policy on chain to target(built-in only)
--rename-chain -E old-chain new-chain Change chain name, (moving any references)

rule-specification

-p, –protocol protocol: tcp, udp, udplite, icmp, icmpv6,esp, ah, sctp, mh or the special keyword “all”
-s, –source address[/mask][,…] 写多个地址最后会拆分多条规则
-d, –destination address[/mask][,…]
-m, –match match
-j, –jump target: ACCEPT DROP REJECT

其中REJECT支持
–reject-with [type] : icmp-net-unreachable, icmp-host-unreachable, icmp-port-unreachable, icmp-proto-unreachable, icmp-net-prohibited, icmp-host-prohibited, or icmp-admin-prohibited, tcp-reset

https://blog.csdn.net/sbdx/article/details/117002045
对外用-j DROP,延长攻击时长
对内用-j REJECT –reject-with tcp-reset好,节约时间,节约带宽。

https://ipset.netfilter.org/iptables-extensions.man.html

-m常用模块

1
2
3
4
5
6
7
8
9
10
11
12
-m comment 注释
--comment "my local LAN"
-m multiport 适用于tcp, udp, udplite, dccp and sctp. Up to 15 ports can be specified. A port range (port:port) counts as two ports.
--ports port[,port|,port:port]...
--source-ports,--sports port[,port|,port:port]...
--destination-ports,--dports port[,port|,port:port]...
-m iprange
--src-range from[-to]
--dst-range from[-to]
-m tcp can be used if `--protocol tcp' is specified
--source-port,--sport port[:port]
--destination-port,--dport port[:port]

备份还原

1
2
3
4
5
iptables -nvL INPUT --line-number
iptables-save >/data/tmp/iptables.bak
iptables-restore </data/tmp/iptables.bak

可以导出后编辑规则,导入UTF8编码的文本
  • 带行号显示 iptables -nL INPUT –line-numbers -v
  • 配置命令 iptables -S OUTPUT
  • 删除 iptables -D INPUT 8 按序号删除风险高,容易错位,必须从下至上操作
    • iptables -A INPUT -s 192.168.1.5 -j DROP
    • iptables -D INPUT -s 192.168.1.5 -j DROP
  • 插入到指定位置前 iptables -I INPUT 125 -s <address> -j ACCEPT
  • 0流量规则 iptables -S INPUT -v |grep -E "c\ 0\ 0"
    • 删除 iptables -S INPUT -v |grep -E "c\ 0\ 0" |sed 's/-c\ 0\ 0//' | cut -d " " -f 2- | xargs -rL1 iptables -D
  • 排除连接的ip端口 tcpdump -c 100 -i bond1 -nntq -P in '( (not src net 10.27.48) and (not src net 10.17.14) and (!src port 9092) and (dst portrange 30000-49999 ) and (tcp) )'
    • 简化结果 head test|awk -F'[ :]' '{print $2, $4}'|sort -u
    • 获取端口|awk -F'.' '{print $NF}'|xargs -I{} lsof -i:{}
    • 获取入访IP|awk -F'.' '{print $1"."$2"."$3"."$4}' |sort -u

批量删除规则:

  1. 检查确认 iptables -S OUTPUT -v | grep 192 | cut -d " " -f 2- | xargs -rL1 echo iptables -D
  2. 执行 iptables -S OUTPUT | grep 192 | cut -d " " -f 2- | xargs -rL1 iptables -D

限制访问ip, 按顺序先允许个别,后拒绝所有

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 清除规则
iptables -F

# 本地回路(若只对需保护的端口 放通+drop 则不需要配置此项)
iptables -I INPUT -i lo -j ACCEPT
iptables -I OUTPUT -o lo -j ACCEPT

# 服务器初始开放端口(注意ssh)
iptables -I INPUT -p icmp -j ACCEPT -m comment --comment "允许ping"
iptables -I INPUT -p tcp --dport 22 -j ACCEPT
iptables -I INPUT -p tcp --dport 10050 -j ACCEPT -m comment --comment "zabbix"

# 跳板机/网页界面放通
iptables -R INPUT 6 -p tcp -s 10.17.14.0/24 -m multiport --dport 9870,8088,18080,19888,12345,8090,9090,3000 -j ACCEPT -m comment --comment "web"

# 集群网段放通1 (任意端口)
iptables -A INPUT -m iprange --src-range 10.27.48.1-10.27.48.43 -j ACCEPT

# 集群网段放通2 自定义链(需要限定部分端口)
## 创建
iptables -t filter -N CLUSTER # filter表类型, 自定义链名称
iptables -I CLUSTER -s 10.17.41.127 -j ACCEPT
iptables -I CLUSTER -s 10.17.41.129 -j ACCEPT
iptables -I CLUSTER -s 10.3.4.191 -j ACCEPT
iptables -I CLUSTER -s 10.3.4.192 -j ACCEPT
iptables -I CLUSTER -s 10.3.4.194 -j ACCEPT
iptables -I CLUSTER -s 10.3.4.195 -j ACCEPT
iptables -I CLUSTER -s 10.17.41.20 -j ACCEPT
iptables -I CLUSTER -s 10.17.41.21 -j ACCEPT
iptables -I CLUSTER -s 10.17.41.24 -j ACCEPT
iptables -I CLUSTER -s 10.17.41.25 -j ACCEPT

## 指定哪些连接进入自定义链
## 替换规则
iptables -R INPUT ? -p tcp -m multiport --dports 9000,50010,50020,8080 -j CLUSTER
iptables -I INPUT ? -p tcp -m multiport --dports 9000,50010,50020,8080 -j DROP
## 改名
iptables -E CLUSTER HDFS
## 删除
iptables -X HDFS


# 默认拒绝(危险,必须确保必要连接已允许)
iptables -P INPUT DROP

# 限制输出(注意ssh),可选
iptables -A OUTPUT -p all -d 10.0.0.0/8 -j ACCEPT
iptables -P OUTPUT DROP

# 查看流量
iptables -nvL --line-number

数据传输:BDOC-邮箱-北方-萝岗

iptables -N hdfs
iptables -A INPUT -p tcp -m multiport –dport 9000,50010 -j hdfs
iptables -A hdfs -p tcp -m iprange –src-range 10.3.16.118-10.3.16.126 -j ACCEPT
iptables -A hdfs -p tcp -m iprange –src-range 10.3.60.121-10.3.60.130 -j ACCEPT
iptables -A hdfs -p tcp -m iprange –src-range 10.33.21.190-10.33.21.194 -j ACCEPT
iptables -A hdfs -p tcp -m iprange –src-range 10.17.41.127-10.17.41.133 -j ACCEPT
iptables -A hdfs -p tcp -m iprange –src-range 10.17.41.18-10.17.41.26 -j ACCEPT
iptables -A hdfs -p tcp -m iprange –src-range 10.3.4.141-10.3.4.145 -j ACCEPT
iptables -A hdfs -p tcp -m iprange –src-range 10.3.4.191-10.3.4.195 -j ACCEPT
iptables -A hdfs -p tcp -s 10.3.4.48 -j ACCEPT

(optional-部分机器)

iptables -A INPUT -p tcp -m tcp –dport 10003 -s 10.3.4.192,10.3.4.194,10.27.48.1,10.27.48.2 -j ACCEPT -m comment –comment “user-center”

端口转发

1
2
3
4
5
6
7
8
9
10
11
sysctl -w net.ipv4.ip_forward=1 # less  /etc/sysctl.conf

sudo iptables -t nat -I PREROUTING -p tcp --dport 8848 -j DNAT --to-destination 36.xxxx:32222

sudo iptables -t nat -I POSTROUTING -p tcp -d 36.xxxx --dport 32222 -j MASQUERADE

sudo iptables -I FORWARD -p tcp -d 36.xxxx --dport 32222 -j ACCEPT

iptables -t nat -nL PREROUTING --line-number
iptables -t nat -nL POSTROUTING --line-number
iptables -t filter -nL FORWARD

Firewalld

frewalld是服务名称,firewall-cmd和firewall-config是配置工具名称

firewall-cmd  基于命令行配置

firewall-config基于图形化界面配置(这两个配置方式实时同步)

runtime运行时: 修改规则马上生效,但是是临时生效 [不建议]
permanent持久配置: 修改后需要reload重载才会生效 [强烈推荐]

1
2
3
4
5
6
7
#禁用旧版防火墙服务
[root@firewalld ~]# systemctl mask iptables
[root@firewalld ~]# systemctl mask ip6tables

#启动firewalld防火墙, 并加入开机自启动服务
[root@firewalld ~]# systemctl start firewalld
[root@firewalld ~]# systemctl enable firewalld

firewalld区域概念

数据包到达防火墙匹配规则:
绑定源地址的区域规则>网卡绑定的区域规则>默认区域的规则

默认所有网卡都是public区域,可以根据需要将网卡设置为不同的区域

zone 区域 规则
Trust 信任区域 允许所有的数据包流入与流出
Public 公共区域 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、dhcpv6-client服务相关,则允许流量
External 外部区域 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量
Home 家庭区域 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh、mdns、ipp-client、samba-client与dhcpv6-client服务相关,则允许流量
Internal 内部区域 同home区域
Work 工作区域 拒绝流入的流量,除非与流出的流量数相关;而如果流量与ssh、ipp-client与dhcpv6-client服务相关,则允许流量
Dmz 隔离区域 拒绝流入的流量,除非与流出的流量相关;而如果流量与ssh服务相关,则允许流量
Block 限制区域 拒绝流入的流量,除非与流出的流量相关(有回应)
Drop 丢弃区域 拒绝流入的流量,除非与流出的流量相关(无回应)
zone配置文件:block.xml、dmz.xml、drop.xml、external.xml、 home.xml、internal.xml、public.xml、trusted.xml、work.xml,他们都保存在“/usr/lib/firewalld/zones/”目录下。

cat /etc/services 保存的协议类型和端口号

配置

默认临时生效,–permanent 表示此配置加入到永久生效

或者在配置结束后执行此命令 firewall-cmd --runtime-to-permanent将临时更改为永久

永久配置完成后需要立即同步 firewall-cmd --reload 立即同步永久配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#### 查看默认区域并进行更改

firewall-cmd --get-zones 查询可用的区域
firewall-cmd --get-default-zone 查询默认区域的名称
firewall-cmd --get-active-zone 显示当前正在使用的区域与网卡名称
firewall-cmd --set-default-zone=trusted 设置默认区域为trusted区域

#### 将网卡/子网与区域绑定(允许/拒绝此子网通过)

firewall-cmd --zone=drop --add-source=192.168.20.0/24 将此子网与drop区域绑定(拒绝从此子网发来的流量)
firewall-cmd --zone=trusted --add-interface=ens160 将此网卡与trusted区域绑定(允许从此网卡发来的流量)

--remove-source 删除子网与区域的绑定
--change-source 更改子网与区域的绑定

#### 配置区域允许/拒绝的协议/端口号

firewall-cmd --list-all 显示当前区域的端口号、网卡、服务等信息
--list-all-zones 显示所有区域的
firewall-cmd --get-services 列举出来当前所有被允许的协议
firewall-cmd --zone=public --add-service http 配置public区域允许通过http协议
--remove-service ssh 拒绝通过ssh协议
--add-port=123/tcp 允许通过tcp的123端口
--remove-port=123/tcp 拒绝通过tcp的123端口


#### 配置协议端口转换(端口映射)

firewall-cmd --permanent --zone=public --add-forward-port=port=888:proto=tcp:toport=22:toaddr=192.168.10.1

将192.168.10.1主机的tcp 22端口号转为888端口号(public区域接收ssh)

--remove-forward-port 删除此端口映射

#### 其它配置
--panic-on  紧急模式,切断一切的网络连接(特殊情况去使用)
--panic-off  恢复一切的网络连接

#### 配置富规则rich(更复杂、更详细的防火墙策略配置)
优先级最高(高于默认规则,两个并不冲突),能够根据源目地址、端口号来限制用户

firewall-cmd --zone=public --list-rich-rule 显示public区域已经配置的富规则

firewall-cmd --permanent --zone=public --add-rich-rule="rule family="ipv4" source address="192.168.100.1/24" service name="ssh" accept" 允许来自192.168.100.1的主机访问22端口

--add-rich-rule 添加一个富规则
--remove-rich-rule 删除一个富规则
accept/reject 允许/拒绝访问

常用

1
2
3
4
5
6
7
sudo firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.17.14.0/24" accept"
sudo firewall-cmd --reload
sudo firewall-cmd --zone=trusted --list-rich-rule

sudo firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="10.x.x.x" accept"