欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

理解Linux安全防护:netfilter防火墙架构与iptables详解,以及CentOS的firewalld与Ubuntu的ufw对比

最编程 2024-02-11 13:53:11
...

Linux的防火墙: netfilter防火墙框架, iptables , firewalld, ufw

netfilter 是Linux的防火墙框架 iptables 是Linux的防火墙的管理工具,基于netfilter firewalld 是Redhat,CentOS等对 ptables 的包装 ufw 是 Debian,Ubuntu等对 iptables 的包装

graph TB
A("  netfilter( Linux内核的防火墙框架 )  " )

B("iptables(Linux) "  )--操作-->A

C("firewalld(CentOS) ")--操作-->B
 D("ufw(Ubuntu)  ")--操作-->B

@[TOC]

netfilter

netfilter官网

netfilter是Linux 2.4内核引入的包过滤引擎。它作为一个通用的、抽象的框架,提供一整套的hook函数的管理机制,使得诸如数据包过滤、网络地址转换(NAT)和基于协议类型的连接跟踪成为了可能。 netfilter的架构就是在整个网络流程的若干位置放置了一些检测点(HOOK),而在每个检测点上登记了一些处理函数进行处理。

现今许多市面上许多的IP分享器或无线网络路由器(Wireless router),多是嵌入式Linux平台,并利用Netfilter的数据包处理能力,提供NAT以及防火墙的功能。

在这里插入图片描述

Netfilter IP层的五个HOOK点 (五钩)

  1. NF_IP_PRE_ROUTING:刚刚进入网络层的数据包通过此点(刚刚进行完版本号,校验 和等检测), 目的地址转换在此点进行;
  2. NF_IP_LOCAL_IN:经路由查找后,送往本机的通过此检查点,INPUT包过滤在此点进行;
  3. NF_IP_FORWARD:要转发的包通过此检测点,FORWARD包过滤在此点进行;
  4. NF_IP_POST_ROUTING:所有马上便要通过网络设备出去的包通过此检测点,内置的源地址转换功能(包括地址伪装)在此点进行;
  5. NF_IP_LOCAL_OUT:本机进程发出的包通过此检测点,OUTPUT包过滤在此点进行。

@[TOC]

iptables

netfilter官网 netfilter官网 iptables 项目 Iptables 指南 --- Iptables Tutorial 1.2.2

iptables的四表五链

表(tables):提供特定的功能,iptables内置了4个表,即filter表、nat表、mangle表和raw表,分别用于实现包过滤,网络地址转换、包重构(修改)和数据跟踪处理。 链(chains):是数据包传播的路径,每一条链其实就是众多规则中的一个检查清单,每一条链中可以有一 条或数条规则。当一个数据包到达一个链时,iptables就会从链中第一条规则开始检查,看该数据包是否满足规则所定义的条件。如果满足,系统就会根据 该条规则所定义的方法处理该数据包;否则iptables将继续检查下一条规则,如果该数据包不符合链中任一条规则,iptables就会根据该链预先定 义的默认策略来处理数据包。

表链关系图 在这里插入图片描述

四表

filter表:过滤规则表,根据预定义的规则过滤符合条件的数据包 nat表:network address translation 地址转换规则表 mangle表:修改数据标记位规则表 raw表:关闭启用的连接跟踪机制,加快封包穿越防火墙速度

表的执行顺序 : mangle->nat->filter

五链

  1. PREROUTING : 路由之前的规则链
  2. INPUT : 数据包入口规则链
  3. FORWARD : 转发规则链
  4. OUTPUT : 数据包出口规则链
  5. POSTROUTING : 路由之前的规则链

五链对五钩

iptables的五链(chain)
  1. PREROUTING : 路由之前的规则链 , 可用于目标网络地址转换(DNAT)。
  2. INPUT : 数据包入口规则链
  3. FORWARD : 转发规则链
  4. OUTPUT : 数据包出口规则链
  5. POSTROUTING : 路由之后的规则链, 可用于源网络地址转换(SNAT)。
iptables的五链(chain)对应netfilter的五钩(hook)
  1. PREROUTING 对应 NF_INET_PRE_ROUTING
  2. INPUT 对应 NF_INET_LOCAL_IN
  3. FORWARD 对应 NF_INET_FORWARD
  4. OUTPUT 对应 NF_INET_LOCAL_OUT
  5. POSTROUTING 对应 NF_INET_POST_ROUTING
以用iptables开放3306端口的例子说明
##      在INPUT链插入一条规则,允许tcp协议的3306目标端口的包通行
iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
### 或  在INPUT链插入一条规则,允许tcp协议的3306目标端口的包通行
iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
## -I INPUT 和 -A INPUT 是 在INPUT链插入或追加规则
## -p 协议 , -p tcp tcp协议
## --dport 3306 (dest port)目标端口
## -j ACCEPT 解释 -j(jump target跳转目标,先当于链匹配后干什么,可接 ACCEPT,DROP,REJECT等) , ACCEPT:接包
  • -I INPUT-A INPUT 是 在INPUT链插入或追加规则 , -I等效--insert , -A等效--append
  • -p 接协议 , -p tcp tcp协议 , -p等效--protocol
  • --dport 3306 (dest port)目标端口
  • -j ACCEPT -j等效--jump (jump target)跳转目标,先当于链匹配后干什么,可接 ACCEPT,DROP,REJECT等) ACCEPT:接包回应 , DROP丢包不回应 , REJECT回应拒绝 , LOG在/var/log/messages文件中记录 日志信息,然后将数据包传递给下一条规则

iptables 语句的语法规则

iptables [-t table] command [match] [target/jump]

iptables [-t table] command [match] [target/jump]

除了 command 部分是必须的, 其它部分是可选的 在这里插入图片描述 对上图parameter部分更详细的说明 在这里插入图片描述! 感叹号是取反排除的意思, 可选

语法中 COMMAND 部分的选项

语法 " iptables [-t table] command [match] [target/jump] " 中, 除了 command 部分是必须的

Command Example Explanation
-A

--append
iptables -A INPUT ... This command appends the rule to the end of the chain. The rule will in other words always be put last in the rule-set and hence be checked last, unless you append more rules later on.
-D

--delete
iptables -D INPUT --dport 80 -j DROP, iptables -D INPUT 1 This command deletes a rule in a chain. This could be done in two ways; either by entering the whole rule to match (as in the first example), or by specifying the rule number that you want to match. If you use the first method, your entry must match the entry in the chain exactly. If you use the second method, you must match the number of the rule you want to delete. The rules are numbered from the top of each chain, starting with number 1.
-R

--replace
iptables -R INPUT 1 -s 192.168.0.1 -j DROP This command replaces the old entry at the specified line. It works in the same way as the --delete command, but instead of totally deleting the entry, it will replace it with a new entry. The main use for this might be while you're experimenting with iptables.
-I

--insert
iptables -I INPUT 1 --dport 80 -j ACCEPT Insert a rule somewhere in a chain. The rule is inserted as the actual number that we specify. In other words, the above example would be inserted as rule 1 in the INPUT chain, and hence from now on it would be the very first rule in the chain.
-L

--list
iptables -L INPUT This command lists all the entries in the specified chain. In the above case, we would list all the entries in the INPUT chain. It's also legal to not specify any chain at all. In the last case, the command would list all the chains in the specified table (To specify a table, see the Tables section). The exact output is affected by other options sent to the parser, for example the -n and -v options, etc.
-F

--flush
iptables -F INPUT This command flushes all rules from the specified chain and is equivalent to deleting each rule one by one, but is quite a bit faster. The command can be used without options, and will then delete all rules in all chains within the specified table.
-Z

--zero
iptables -Z INPUT This command tells the program to zero all counters in a specific chain, or in all chains. If you have used the -v option with the -L command, you have probably seen the packet counter at the beginning of each field. To zero this packet counter, use the -Z option. This option works the same as -L, except that -Z won't list the rules. If -L and -Z is used together (which is legal), the chains will first be listed, and then the packet counters are zeroed.
-N

--new-chain
iptables -N allowed This command tells the kernel to create a new chain of the specified name in the specified table. In the above example we create a chain called allowed. Note that there must not already be a chain or target of the same name.
-X

--delete-chain
iptables -X allowed This command deletes the specified chain from the table. For this command to work, there must be no rules that refer to the chain that is to be deleted. In other words, you would have to replace or delete all rules referring to the chain before actually deleting the chain. If this command is used without any options, all chains but those built in to the specified table will be deleted.
-P

--policy
iptables -P INPUT DROP This command tells the kernel to set a specified default target, or policy, on a chain. All packets that don't match any rule will then be forced to use the policy of the chain. Legal targets are DROP and ACCEPT (There might be more, mail me if so).
-E

--rename-chain
iptables -E allowed disallowed The -E command tells iptables to change the first name of a chain, to the second name. In the example above we would, in other words, change the name of the chain from allowed to disallowed. Note that this will not affect the actual way the table will work. It is, in other words, just a cosmetic change to the table.
命令 说明
-A
等效
--append
将规则rule到指定chain的最后
iptables -A INPUT …
-D
等效
--drop
将rule从执行chain中删除。两种使用方式
1. 指定完整的规则
2. 指定规则的序号:序号chain中从上到下依次递增,1开始
iptables -D INPUT --dport 80 -j DROP, iptables -D INPUT 1
-R
等效
--replace
替换指定位置的rule
iptables -R INPUT 1 -s 192.168.0.1 -j DROP
-I
等效
--insert
向chain中的指定位置插入rule
iptables -I INPUT 1 --dport` 80 -j ACCEPT
-L
等效
--list
列出指定chain中的所有rule
iptables -L INPUT
-F
等效
--flush
清空指定chain上的所有rule,和一条一条删除效果一样,只不过更快
iptables -F INPUT
-Z
等效
--zero
清空指定chain上的计数器。这些计数器用于计量包数和字节数
iptables -Z INPUT
-N
等效
--new-chain
创建一个新的chain
iptables -N allowed
-X
等效
--delete-chain
删除置顶chain,只有已经被清空的chain,即没有任何rule的chain才能被删除
内建的chain如INPUT等是无法被删除的
iptables -X allowed
-P
等效
--policy
为chain设置默认的target或policy,在该chain上,任何没有被rule匹配到的包,都会应用该默认规则。
只有两个合法的target:DROP 和 ACCEPT
iptables -P INPUT DROP
-E
等效
--rename-chain
重命名chain
iptables -E allowed disallowed

语法中 parameter/match 参数/匹配 部分的选项

www.frozentux.net/iptables-tu…

匹配项 说明
-p --protocol 指定协议 例如 -p tcp
- 协议只能是 /etc/protocols 文件中存在的,否则会报错
- 可以正向也可以反向。
iptables -A INPUT -p ! tcp 表示非tcp
-s --src --source 来源IP,可以有多种形式。也可以用 ! 运算符反向指定
- 单个IP形式 192.168.0.0
- CIDR形式 192.168.0.0/24
- 子网掩码形式 192.168.0.0/255.255.255.0
-d --dst --destination 同上,反向而已
-i --in-interface 指定包的来源接口,如en0。
- 只能在INPUT FORWARD PREROUTING 三个chain使用
- 允许通配,+表示所有接口,en+表示en开头的所有接口
-o --out-interface 同上,反向而已
-f --fragment 匹配分段数据包的第二、第三。。。段
如果不指定,就只会匹配未分段的数据包或者分段数据包的第一段
TCP相关的匹配

有关 TCP 的匹配 , 加上-p tcp

匹配项 说明 例子
--sport, --source-port 匹配来源端口 iptables -A INPUT -p tcp --sport 22
--dport, --destination-port 匹配目标端口 iptables -A INPUT -p tcp --dport 22
--tcp-flags 匹配tcp标记,如SYN/RST/ACK/FIN等,也可用ALL、NONE iptables -p tcp --tcp-flags SYN,FIN,ACK SYN
--syn 遗留语法,和 --tcp-flags SYN,FIN,ACK SYN一个效果 iptables -p tcp --syn
--tcp-option 根据tcp选项匹配 iptables -p tcp --tcp-option 16
Match --sport, --source-port
Kernel 2.3, 2.4, 2.5 and 2.6
Example iptables -A INPUT -p tcp --sport 22
Explanation The --source-port match is used to match packets based on their source port. Without it, we imply all source ports. This match can either take a service name or a port number. If you specify a service name, the service name must be in the /etc/services file, since iptables uses this file in which to find. If you specify the port by its number, the rule will load slightly faster, since iptables don't have to check up the service name. However, the match might be a little bit harder to read than if you use the service name. If you are writing a rule-set consisting of a 200 rules or more, you should definitely use port numbers, since the difference is really noticeable. (On a slow box, this could make as much as 10 seconds' difference, if you have configured a large rule-set containing 1000 rules or so). You can also use the --source-port match to match any range of ports, --source-port 22:80 for example. This example would match all source ports between 22 and 80. If you omit specifying the first port, port 0 is assumed (is implicit). --source-port :80 would then match port 0 through 80. And if the last port specification is omitted, port 65535 is assumed. If you were to write --source-port 22:, you would have specified a match for all ports from port 22 through port 65535. If you invert the port range, iptables automatically reverses your inversion. If you write --source-port 80:22, it is simply interpreted as --source-port 22:80. You can also invert a match by adding a ! sign. For example, --source-port ! 22 means that you want to match all ports but port 22. The inversion could also be used together with a port range and would then look like --source-port ! 22:80, which in turn would mean that you want to match all ports but ports 22 through 80. Note that this match does not handle multiple separated ports and port ranges. For more information about those, look at the multiport match extension.
--source-port 匹配用于根据数据包的源端口匹配数据包。没有它,我们暗示所有源端口。此匹配可以采用服务名称或端口号。如果指定服务名,则服务名必须在 /etc/services 文件中,因为 iptables 使用该文件在其中查找。如果您通过端口号指定端口,则规则加载速度会稍快一些,因为 iptables 不必检查服务名称。但是,与使用服务名称相比,匹配项可能更难阅读。如果您正在编写包含 200 条或更多规则的规则集,则绝对应该使用端口号,因为差异非常明显。 (如果您配置了一个包含 1000 条左右规则的大型规则集,那么在慢速机器上,这可能会产生多达 10 秒的差异)。您还可以使用 --source-port 匹配来匹配任何范围的端口,例如 --source-port 22:80。此示例将匹配 22 到 80 之间的所有源端口。如果您省略指定第一个端口,则假定端口 0(隐含)。 --source-port :80 然后将匹配端口 0 到 80。如果省略最后一个端口规范,则假定端口 65535。如果你写--source-port 22:,你会为从端口22到端口65535的所有端口指定一个匹配。如果你反转端口范围,iptables会自动反转你的反转。如果你写--source-port 80:22,它会简单地解释为--source-port 22:80。您还可以通过添加一个来反转匹配!符号。例如, --source-port ! 22 表示您要匹配除端口 22 之外的所有端口。反转也可以与端口范围一起使用,然后看起来像 --source-port ! 22:80,这反过来意味着您要匹配除端口 22 到 80 之外的所有端口。请注意,此匹配不处理多个单独的端口和端口范围。有关这些的更多信息,请查看多端口匹配扩展。
Match --dport, --destination-port
Kernel 2.3, 2.4, 2.5 and 2.6
Example iptables -A INPUT -p tcp --dport 22
Explanation This match is used to match TCP packets, according to their destination port. It uses exactly the same syntax as the --source-port match. It understands port and port range specifications, as well as inversions. It also reverses high and low ports in port range specifications, as above. The match will also assume values of 0 and 65535 if the high or low port is left out in a port range specification. In other words, exactly the same as the --source-port syntax. Note that this match does not handle multiple separated ports and port ranges. For more information about those, look at the multiport match extension.
此匹配用于根据目标端口匹配 TCP 数据包。 它使用与 --source-port 匹配完全相同的语法。 它了解端口和端口范围规范以及倒置。 如上所述,它还反转端口范围规范中的高端口和低端口。 如果端口范围规范中忽略了高端口或低端口,则匹配还将采用 0 和 65535 的值。 换句话说,与 --source-port 语法完全相同。 请注意,此匹配不处理多个单独的端口和端口范围。 有关这些的更多信息,请查看多端口匹配扩展。
Match --tcp-flags
Kernel 2.3, 2.4, 2.5 and 2.6
Example iptables -p tcp --tcp-flags SYN,FIN,ACK SYN
Explanation This match is used to match on the TCP flags in a packet. First of all, the match takes a list of flags to compare (a mask) and secondly it takes list of flags that should be set to 1, or turned on. Both lists should be comma-delimited. The match knows about the SYN, ACK, FIN, RST, URG, PSH flags, and it also recognizes the words ALL and NONE. ALL and NONE is pretty much self describing: ALL means to use all flags and NONE means to use no flags for the option. --tcp-flags ALL NONE would in other words mean to check all of the TCP flags and match if none of the flags are set. This option can also be inverted with the ! sign. For example, if we specify ! SYN,FIN,ACK SYN, we would get a match that would match packets that had the ACK and FIN bits set, but not the SYN bit. Also note that the comma delimitation should not include spaces. You can see the correct syntax in the example above.
Match --syn
Kernel 2.3, 2.4, 2.5 and 2.6
Example iptables -p tcp --syn
Explanation The --syn match is more or less an old relic from the ipchains days and is still there for backward compatibility and for and to make transition one to the other easier. It is used to match packets if they have the SYN bit set and the ACK and RST bits unset. This command would in other words be exactly the same as the --tcp-flags SYN,RST,ACK SYN match. Such packets are mainly used to request new TCP connections from a server. If you block these packets, you should have effectively blocked all incoming connection attempts. However, you will not have blocked the outgoing connections, which a lot of exploits today use (for example, hacking a legitimate service and then installing a program or suchlike that enables initiating an existing connection to your host, instead of opening up a new port on it). This match can also be inverted with the ! sign in this, ! --syn, way. This would match all packets with the RST or the ACK bits set, in other words packets in an already established connection.
Match --tcp-option
Kernel 2.3, 2.4, 2.5 and 2.6
Example iptables -p tcp --tcp-option 16
Explanation This match is used to match packets depending on their TCP options. A TCP Option is a specific part of the header. This part consists of 3 different fields. The first one is 8 bits long and tells us which Options are used in this stream, the second one is also 8 bits long and tells us how long the options field is. The reason for this length field is that TCP options are, well, optional. To be compliant with the standards, we do not need to implement all options, but instead we can just look at what kind of option it is, and if we do not support it, we just look at the length field and can then jump over this data. This match is used to match different TCP options depending on their decimal values. It may also be inverted with the ! flag, so that the match matches all TCP options but the option given to the match. For a complete list of all options, take a closer look at the Internet Engineering Task Force who maintains a list of all the standard numbers used on the Internet.

语法中的 -j 部分

www.frozentux.net/iptables-tu…

-j 等效 --jump (jump target) 目标跳转

指定规则的目标;也就是说,如果包匹配应当做什么。目标可以是用户自定义链(不是这条规则所在的),某个会立即决定包的命运的专用内建目标,或者一个扩展(参见下面的EXTENSIONS)。如果规则的这个选项被忽略,那么匹配的过程不会对包产生影响,不过规则的计数器会增加。

-j后接的target
jump target
说明
ACCEPT 允许数据包通过。
DROP 直接丢弃数据包,不给任何回应信息,客户端只能干等,直到超时。
REJECT 明确拒绝数据包,会给发送端回复一个响应,明确告知数据包被拒绝。
REDIRECT 重定向,在本机做端口映射。
LOG 在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则,
也就是说除了记录以外不对数据包做任何其他操作,仍然让下一条规则去匹配。
SNAT 源地址转换,解决内网用户用同一个公网地址上网的问题。
MASQUERADE 是SNAT的一种特殊形式,适用于动态的、临时会变的ip上。
DNAT 目标地址转换。

ACCEPT , CLASSIFY , CLUSTERIP , CONNMARK , CONNSECMARK , DNAT , DROP , DSCP , ECN , LOG , MARK , MASQUERADE , MIRROR , NETMAP , NFQUEUE , NOTRACK , QUEUE , REDIRECT , REJECT , RETURN , SAME , SECMARK , SNAT , TCPMSS , TOS , TTL , ULOG

iptables 命令行用例

列出iptables的链规则 -L 或 --list , (查看开放的端口)

列出 iptables 的所有链的规则
### 列出 iptables 的所有链的规则, 用数字显示地址和端口
iptables -L -n 
#####  n和L写在一起的话, n必须在L前面
iptables -nL
###  列出 iptables 的所有链的规则
iptables -L
iptables --list
###  更详细地列出 iptables 的所有链的规则
iptables -L -v
###  更更详细地列出 iptables 的所有链的规则
iptables -L -vv
###  更更更详细地列出 iptables 的所有链的规则
iptables --list -vvv
列出 iptables 的 INPUT 链 的规则
###  列出 iptables 的 INPUT 链的规则
iptables -L INPUT
### 列出 iptables 的 INPUT 链的规则, 用数字显示地址和端口
iptables -nL INPUT
iptables -n -L INPUT
#####  -L必须接链, -n可以写在-L之前或链后, 不能写成 iptables -Ln INPUT 或 iptables -L -n INPUT , 可以写成 iptables -L INPUT -n
iptables -L INPUT -n
###  更详细列出 iptables 的 INPUT 链的规则
iptables -L INPUT -v
iptables -L INPUT -v -n
iptables -L INPUT -nv
iptables -L INPUT -vn
###  更更详细列出 iptables 的 INPUT 链的规则
iptables -L INPUT -vvv
iptables -L INPUT -vvv -n
iptables -L INPUT -v -n -v -v
iptables -L INPUT -nvvvvvvv
iptables -L INPUT -vvnvv

开放关闭端口

开放关闭端口, 添加允许所有ip的 22 , 80 , 443 , 1433 , 3306 , 8080 端口的数据包通行的规则

向INPUT链插入端口规则 , 开放 22 , 80 , 443 , 1433 , 3306 , 8080 端口 , 用-I,还可以指定从第几行插入

### 向INPUT链插入端口规则 , 开放 22 , 80 , 443  , 1433 , 3306 , 8080 端口
sudo iptables -I INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 1433 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
### 向INPUT链第五行前插入端口规则 , 开放 22 , 80 , 443  , 1433 , 3306 , 8080 端口 
sudo iptables -I INPUT 5 -p tcp --dport 22 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 443 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 1433 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 3306 -j ACCEPT
sudo iptables -I INPUT 5 -p tcp --dport 8080 -j ACCEPT

向INPUT链追加端口规则 , 开放 22 , 80 , 443 , 1433 , 3306 , 8080 端口 , 用 -A

### 向INPUT链追加端口规则 , 开放 22 , 80 , 443  , 1433 , 3306 , 8080 端口
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 1433 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 3306 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

删除INPUT链的规则---删除 "22 , 80 , 443 , 1433 , 3306 , 8080 端口通行" 这些条规则 上面用-I-A两次添加了相同的规则,分别插入在INPUT链开头,和追加在INPUT链末尾 , 下面的删除代码也要执行两次才会删除这些规则

###  删除INPUT链的规则---删除 "22 , 80 , 443  , 1433 , 3306 , 8080 端口通行"  这些条规则
sudo iptables -D INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 1433 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 3306 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 8080 -j ACCEPT
开放关闭不连续端口 , 使用-m multiport使得--dpart--spart后的端口可以用逗号,分隔
sudo iptables -I INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
sudo iptables -A INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
sudo iptables -D INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
开放关闭连续端口, 用冒号:分隔 , 开始端口最小1:末尾端口最大65535

插入1到65535端口, 追加1到65535端口, 删除两遍

sudo iptables -I INPUT -p tcp --dport 1:65535 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 1:65535 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 1:65535 -j ACCEPT
sudo iptables -D INPUT -p tcp --dport 1:65535 -j ACCEPT
开放关闭连续和不连续的端口

开放22端口,80端口,443端口,666到888端口,1433端口,3306到3399端口,8000到8999端口

### 开放 22端口,80端口,443端口,666到888端口,1433端口,3306到3399端口,8000到8999端口
sudo iptables -I INPUT -p tcp -m multiport --dport 22,80,443,666:888,1433,3306:3399,8000:8999 -j ACCEPT
上面的方法一起用

在INPUT链的第五行前插入一条规则如下 允许来自192.168.0.100的22端口,80端口,443端口,666到888端口,1433端口,3306到3399端口,8000到8999端口 的数据包通行 然后删除这条规则

###  查看INPUT链的规则
iptables -nL INPUT
###  在INPUT链的第五行前插入一条规则如下
###  允许来自192.168.0.100的22端口,80端口,443端口,666到888端口,1433端口,3306到3399端口,8000到8999端口 的数据包通行
sudo iptables -I INPUT -p tcp -s 192.168.0.100 -m multiport --dport 22,80,443,666:888,1433,3306:3399,8000:8999 -j ACCEPT
###  查看INPUT链的规则
iptables -nL INPUT
###  删除上面插入的规则
sudo iptables -D INPUT -p tcp -s 192.168.0.100 -m multiport --dport 22,80,443,666:888,1433,3306:3399,8000:8999 -j ACCEPT

开放关闭源IP的端口

在上面的命令中加 -s ip地址 就能指定ip

对指定IP开放关闭 3306 端口 , 用-s--source接IP地址

对192.168.0.100 开放关闭 3306 端口 , 用-s--source接IP地址

sudo iptables -I INPUT -p tcp -s 192.168.0.100 --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp -s 192.168.0.100 --dport 22 -j ACCEPT
sudo iptables -D INPUT -p tcp -s 192.168.0.100 --dport 22 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -s 192.168.0.100 --dport 22 -j ACCEPT
iptables -nL INPUT

如果重复执行添加(插入或追加),就会出现重复的规则,删除也要删除相同次数才能删完, 示例
sudo iptables -I INPUT -p tcp --dport 888 -j ACCEPT
iptables -nL INPUT
sudo iptables -A INPUT -p tcp --dport 888 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp --dport 888 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp --dport 888 -j ACCEPT
iptables -nL INPUT
sudo iptables -I INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
iptables -nL INPUT
sudo iptables -A INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -m multiport --dport 22,80,443,1433,3306,8080 -j ACCEPT
iptables -nL INPUT
sudo iptables -I INPUT -p tcp --dport 1:65535 -j ACCEPT
iptables -nL INPUT
sudo iptables -A INPUT -p tcp --dport 1:65535 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp --dport 1:65535 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp --dport 1:65535 -j ACCEPT
iptables -nL INPUT
sudo iptables -I INPUT -p tcp -s 192.168.0.100 --dport 88 -j ACCEPT
iptables -nL INPUT
sudo iptables -A INPUT -p tcp -s 192.168.0.100 --dport 88 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -s 192.168.0.100 --dport 88 -j ACCEPT
iptables -nL INPUT
sudo iptables -D INPUT -p tcp -s 192.168.0.100 --dport 88 -j ACCEPT
iptables -nL INPUT

清除所有规则 -F--flush

删除所有链所有规则iptables -F
###  查看所有链所有规则
iptables -L
###  清除所有链所有规则
iptables -F
iptables -flush
###  查看所有链所有规则
iptables -L
删除 INPUT 链所有规则iptables --flush INPUT
###  查看 INPUT 链所有规则
iptables -L INPUT
###  清除 INPUT 链所有规则
iptables --F INPUT
iptables --flush INPUT
###  查看 INPUT 链所有规则
iptables -L INPUT







filewalld (Redhat,CentOS,Fedora等)

filewalld官网

  • firewalld是对iptables的封装,使防火墙管理起来更容易更清晰, 本质上是 firewald操作iptables操作netfilter, 底层始终是netfilter
  • firewalld使用zone(区域)概念,相当于Windows的专用网络公用网络等, 其实质是一套套规则, 方便切换
  • firewalld的每个zone指定了target, 就是iptables的target
  • firewalld的zone指定了端口port,和服务service 可以通过指定端口放行数据包,也可以使用service指定的端口放行数据包 zone可以直接管理端口, 也可以通过service间接管理端口
graph LR
A[<h3>zone</h3>firewalld的区域<br/>zone] -- zone直接包含IP协议端口--> D
A --> C(<h3>service</h3><br/>zone包含service<br/>service包含IP协议端口)
 D{管理IP协议端口}
C --> D

firewalld对比iptables

  • iptables使用chains链管理规则 , firewalld使用zones区域和services服务管理规则, zone区域类似Windows的专用网络公用网络等
  • iptables默认都放行,需添加限制规则 ; firewalld默认都限制,需要添加放行规则
  • iptables的规则时静态的,每修改一条都要全部清除,重新读取配置表,全部刷新,已有连接会断开 firewalld的规则是动态的, 修改后firewall-cmd --reload不会断开已有连接
  • iptables 没有守护进程,并不能算是真正意义上的服务, 而 firewalld 有守护进程
  • iptables 通过控制端口来控制服务,而 firewalld 则是通过控制协议来控制端口

启用禁用启动停止重启firewalld

CentOS7的firewalld默认是启用的 使用systemctl命令时, firewalld等效firewalld.service

启用firewalld服务, 开机启动firewalld服务

###  启用firewalld服务, 开机启动firewalld服务
sudo systemctl enable firewalld
sudo systemctl enable firewalld.service
###  查看是否已启用firewalld服务,是enable启用还是disable禁用? 有可能启用但未启动,也可能禁用但当前启动
systemctl is-enabled firewalld.service

禁用firewalld服务, 系统启动时不开启firewalld服务

###  禁用firewalld服务, 系统启动时不开启firewalld服务
sudo systemctl disable firewalld
sudo systemctl disable firewalld.service
###  查看firewalld服务是enable启用还是disable禁用? 有可能启用但未启动,也可能禁用但当前启动
systemctl is-enabled firewalld.service

启动firewalld服务

###  启动firewalld服务
sudo systemctl start firewalld
sudo systemctl start firewalld.service

停止firewalld服务

###   停止firewalld服务
sudo systemctl stop firewalld
sudo systemctl stop firewalld.service

重启firewalld服务, 重新加载firewalld, 使修改立即生效

##  使配置或修改立即生效
###   重启firewalld服务, 会断开已有连接, 可以在服务停止状态执行
sudo systemctl restart firewalld
sudo systemctl restart firewalld.service
###   重新加载firewalld, 不会断开已有连接, 不能在firewalld服务处于停止状态的时候执行
sudo firewall-cmd --reload
firewall-cmd --reload 对比 systemctl restart firewalld
  • firewall-cmd --reload 是使修改的配置生效, 不会断开连接
  • systemctl restart firewalld 是重启firewalld服务, 新配置会生效, 但会断开已有连接

比如: 远程控制台已建立连接, 此时本地关闭22端口,--delete-service ssh服务,

  • 执行firewall-cmd --reload并不会断开连接, 但远程退出后就无法登录了
  • 执行 systemctl restart firewalld 则远程连接立即断开

查看firewalld服务状态, 服务是否运行

sudo systemctl status firewalld
sudo systemctl status firewalld.service
sudo firewall-cmd --state

systemctl扩展知识

查看已启用的服务列表:systemctl list-unit-files|grep enabled 查看启动失败的服务:systemctl --failed




firewall的两个配置文件夹,预定义和自定义的

  • 预定义文件夹 : /usr/lib/firewalld/
    cd /usr/lib/firewalld/
    ls /usr/lib/firewalld/
    
  • 自定义文件夹 : /etc/firewalld/
    cd /etc/firewalld
    ls /etc/firewalld
    



在这里插入图片描述

在控制台操作 firewalld ,
firewall-cmd 或者用 firewall-offline-cmd

控制台操作 firewalld , 用 firewall-cmdfirewall-offline-cmd命令, 而不是 firewalld offline是脱机的,离线的意思.

  • firewall-cmd只能在firewalld服务运行时使用,
  • firewall-offline-cmd 在firewalld运行和停止时都能够使用
  • 如果本地机没有图形界面, 又没开放ssh端口, 可以先在本地机关闭firewalld服务, 远程机连上后用firewall-offline-cmd操作,方便复制粘贴配置,配置好后再开启firewalld服务

两种开头大部分用法相同, 但也有一些区别,比如

  • 运行时可以使用firewall-cmd --reload 命令, 但没有 firewall-offline-cmd --reload 命令
  • firewall-offline-cmd 可以在非运行状态执行, 很多命令本身就是永久生效的, 不能再加--permanent
  • firewall-offline-cmd 不能使用 --get-active-zone获取活动zone, 但能查看默认zone, --get-default-zone

man firewall-cmdman firewall-offline-cmd可查看说明手册 用firewall-cmd --helpfirewall-offline-cmd --help 可以查看帮助

firewall-cmd --help | grep port 查看port相关的帮助内容 firewall-cmd --help | grep service 查看service相关的帮助内容 firewall-cmd --help | grep zone 查看zone相关的帮助内容 firewall-cmd --help | grep interface 查看interface相关的帮助内容


permanent

--permanent 选项, 使设置永久生效

firewall-cmd命令语句中的一些设置, 加--permanent 才永久生效, 不加--permanent的话

  • 立即生效的命令,不加--permanent的话,firewall-cmd --reload后会失效
  • firewall-cmd --reload后才生效的命令, 不加--permanent的话,在重启系统 或systemctl restart firewalld 后就失效

有些设置本身就永久生效, 不用加,也不能加--permanent firewall-offline-cmd开头的命令,一般都不能加--permanent , 本身就是永久的


firewalld的区域(zone)概念

firewalld官方文档---Zone

firewalld的zones类似Windonws的专用网络公用网络等, 相当于规则模板, 有利于管理的方便

firewalld自带的zones , 预定义的zones

来自官方文档 https://firewalld.org/documentation/zone/predefined-zones.html

Predefined Zones 预定义区域 These are the zones provided by firewalld sorted according to the default trust level of the zones from untrusted to trusted: 下面这些是 firewalld 提供的预定义区域,根据其默认的信任级别从不信任到信任排序, 按信任度从低到高排列

  • drop 信任度最低

Any incoming network packets are dropped, there is no reply. Only outgoing network connections are possible. 任何传入的网络数据包都被丢弃,没有回复。 只有传出网络连接是可能的。 只能发送,不能接收

  • block

Any incoming network connections are rejected with an icmp-host-prohibited message for IPv4 and icmp6-adm-prohibited for IPv6. Only network connections initiated within this system are possible. 任何传入的网络连接都会被拒绝,并使用 IPv4 的 icmp-host-prohibited 消息和 IPv6 的 icmp6-adm-prohibited 消息。 只有在该系统内发起的网络连接是可能的。 被拒绝的连接会收到一个icmp,返回目标主机不可达。

  • public

For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted. 用于公共区域。 您不相信网络上的其他计算机不会损害您的计算机。 仅接受选定的传入连接。 这也是firewalld默认的区域,默认不开放22以外的任何端口,需手动添加

  • external

For use on external networks with masquerading enabled especially for routers. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted. 用于启用伪装的外部网络,尤其适用于路由器。 您不相信网络上的其他计算机不会损害您的计算机。 仅接受选定的传入连接。

  • dmz

For computers in your demilitarized zone that are publicly-accessible with limited access to your internal network. Only selected incoming connections are accepted. 对于您的非军事区内的计算机可以公开访问,但对您的内部网络的访问权限有限。 仅接受选定的传入连接。

  • work

For use in work areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.

  • home

For use in home areas. You mostly trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted. 用于工作区域。 您主要相信网络上的其他计算机不会损害您的计算机。 仅接受选定的传入连接。

  • internal

For use on internal networks. You mostly trust the other computers on the networks to not harm your computer. Only selected incoming connections are accepted. 用于内部网络。 您主要相信网络上的其他计算机不会损害您的计算机。 仅接受选定的传入连接。

  • trusted 信任度最高

All network connections are accepted. 接受所有网络连接。相当于关闭防火墙

  • firewalld使用zone(区域)概念,相当于Windows的专用网络公用网络等, 其实质是一套套规则, 方便切换
  • firewalld的每个zone指定了target, 就是iptables的target
  • firewalld的zone指定了端口port,和服务service 可以通过指定端口放行数据包,也可以使用service指定的端口放行数据包 zone可以直接管理端口, 也可以通过service间接管理端口
graph LR
A[<big><big><b>zone</b></big></big><br/>firewalld的区域<br/>zone] -- zone直接包含IP协议端口--> D
A --> C(<big>service</big><br/>zone包含service<br/>service包含IP协议端口)
 D{管理IP协议端口}
C --> D

firewalld可以指定一个default zone, 还可以给每个网卡接口interface指定一个zone, 当网卡没有指定zone时,默认zone对该网卡生效 可以 用firewall-cmd --get-default-zone查看默认zone , 用firewall-offline-cmd --get-default-zone查看默认zone , 用firewall-cmd --get-active-zone查看网卡接口zone 不能用firewall-offline-cmd --get-active-zone

  • 刚安装的CentOS7 的默认的zone和主网卡的zone 都是public 在这里插入图片描述
  • 刚安装的Fedora 36 WorkStation版 的默认的zone和主网卡的zone 都是FedoraWorkstation 说明zone也并不局限于上面那几个 在这里插入图片描述
  • ens33是Vmware的默认主网卡名称, enp0s3是VirtualBox默认的主网卡名称
查看 active活动zone 和 default默认zone
查看当前活动zone

查看当前活动zone只能用firewall-cmd开头 , 不能firewall-offline-cmd开头

sudo firewall-cmd --get-active-zone   ###  查看当前活动zone
查看默认zone

查看默认zone既可以用firewall-cmd开头 , 也能firewall-offline-cmd开头

sudo firewall-cmd --get-default-zone   ###  查看默认zone
sudo firewall-offline-cmd --get-default-zone   ###  查看默认zone
查看有哪些zone可选

firewalld用xml文件保存zone模板,

  • 可以到/usr/lib/firewalld/zones目录查看预定义的zone模板 或自定义的zone模板目录 /etc/firewalld/zones查看自定义的模板,自定义模板的目录一开始是空文件夹,如果对区域进行了修改,就会在自定义文件夹 /etc/firewalld/zones出现同名的xml,预定义文件夹/usr/lib/firewalld/zones的xml不会变化. 新增的自定义名称的zone也会出现在 /etc/firewalld/zones
cd /usr/lib/firewalld/zones     ### 预定义的zone模板的目录
ls /usr/lib/firewalld/zones 
cd /etc/firewalld/zones       ### 自定义的zone模板的目录 , 该目录模板优先级高于预定义目录的模板
ls /usr/lib/firewalld/zones 

cat /usr/lib/firewalld/zones/public.xml
cat /etc/firewalld/zones/public.xml   ###  修改public才会出现

  • 也可以用命令查看有哪些zone --get-zones 列出预定义和自定义的所有zone的名称 --list-all-zones 列出预定义和自定义的所有zone的详情
列出已定义的zone的名称 --get-zones , 只显示名称

列出已定义的zone --get-zones , 包括预定义和自定义的zone , 只显示名称

sudo firewall-cmd --get-zones   ###  列出所有区域的名称
sudo firewall-offline-cmd --get-zones   ###  列出所有区域的名称

例如

  • CentOS 7 最小安装版
    sudo firewall-cmd --get-zones
    block dmz drop external home internal public trusted work
    
    CentOS 7 默认使用了firewalld预定义的zone中的public
  • Fedora 36 WorkStation版
    sudo firewall-cmd --get-zones
    FedoraServer FedoraWorkstation block dmz drop external home internal libvirt nm-shared public trusted work
    
    Fedora 36 WorkStation版 默认并没有用firewalld预定义的zone , 而是自定义了一个新zone 名为 FedoraWorkstation
列出所有zone的配置详情(添加的和启用的规则) --list-all-zones
sudo firewall-cmd --list-all-zones   ###  列出所有区域的配置详情
sudo firewall-offline-cmd --list-all-zones   ###  列出所有区域的配置详情
列出默认区域 default zone 的配置详情(添加的和启用的规则) --list-all
sudo firewall-cmd --list-all   ###   列出默认区域的配置详情
sudo firewall-offline-cmd --list-all   ###   列出默认区域的配置详情
查看指定区域的配置详情

查看指定区域详情可以用--list-all --zone= 或者 --info-zone= 查看public区域的配置,添加的和启用的规则

sudo firewall-cmd --list-all --zone=public   ###  查看public区域的配置详情
sudo firewall-offline-cmd --list-all --zone=public   ###  查看public区域的配置详情
###   查看各预定义区域的配置,添加的和启用的规则
sudo firewall-cmd --zone=drop --list-all
sudo firewall-cmd --info-zone=drop
sudo firewall-offline-cmd --zone=drop --list-all
sudo firewall-offline-cmd --info-zone=drop

sudo firewall-cmd --zone=block --list-all
sudo firewall-cmd --info-zone=block
sudo firewall-offline-cmd --zone=block --list-all
sudo firewall-offline-cmd --info-zone=block

sudo firewall-cmd --zone=public --list-all
sudo firewall-cmd --info-zone=public
sudo firewall-offline-cmd --zone=public --list-all
sudo firewall-offline-cmd --info-zone=public

sudo firewall-cmd --zone=external --list-all
sudo firewall-cmd --info-zone=external
sudo firewall-offline-cmd --zone=external --list-all
sudo firewall-offline-cmd --info-zone=external

sudo firewall-cmd --zone=dmz  --list-all
sudo firewall-cmd --info-zone=dmz
sudo firewall-offline-cmd --zone=dmz  --list-all
sudo firewall-offline-cmd --info-zone=dmz

sudo firewall-cmd --zone=work --list-all
sudo firewall-cmd --info-zone=work
sudo firewall-offline-cmd --zone=work --list-all
sudo firewall-offline-cmd --info-zone=work

sudo firewall-cmd --zone=home --list-all
sudo firewall-cmd --info-zone=home
sudo firewall-offline-cmd --zone=home --list-all
sudo firewall-offline-cmd --info-zone=home

sudo firewall-cmd --zone=internal --list-all
sudo firewall-cmd --info-zone=internal
sudo firewall-offline-cmd --zone=internal --list-all
sudo firewall-offline-cmd --info-zone=internal

sudo firewall-cmd --zone=trusted --list-all
sudo firewall-cmd --info-zone=trusted
sudo firewall-offline-cmd --zone=trusted --list-all
sudo firewall-offline-cmd --info-zone=trusted

###   如果没有 --zone 参数, 就显示默认zone 的规则
sudo firewall-cmd --list-all   ###   列出默认区域的配置详情
sudo firewall-offline-cmd --list-all   ###   列出默认区域的配置详情

firewalld的默认zone, 默认区域的概念

当一个网卡接口(例如lo)没有指定zone的时候, 就会使用默认zone的规则 可以用--remove-interface=选项来移除zone的interfaces对应的网卡, 使默认zone生效

查看默认zone是哪个区域
sudo firewall-cmd --get-default-zone   ### 查看默认zone是哪个区域
设置默认区域 --set-default-zone=

设置默认区域,带 --set-default-zone=的语句不用加--permanent就能永久生效, 也不能加--permanent

##  设置默认区域, 默认区域在没有区域时起作用
##  设置默认区域,带 `--set-default-zone=`的语句不用加`--permanent`就能永久生效, 也不能加`--permanent`
###  设置默认区域为drop 
sudo firewall-cmd --set-default-zone=drop
sudo firewall-offline-cmd --set-default-zone=drop
###  设置默认区域为block
sudo firewall-cmd --set-default-zone=block
sudo firewall-offline-cmd --set-default-zone=block
###  设置默认区域为public
sudo firewall-cmd --set-default-zone=public
sudo firewall-offline-cmd --set-default-zone=public
###  设置默认区域为external
sudo firewall-cmd --set-default-zone=external
sudo firewall-offline-cmd --set-default-zone=external
###  设置默认区域为dmz
sudo firewall-cmd --set-default-zone=dmz
sudo firewall-offline-cmd --set-default-zone=dmz
###  设置默认区域为work
sudo firewall-cmd --set-default-zone=work
sudo firewall-offline-cmd --set-default-zone=work
###  设置默认区域为home
sudo firewall-cmd --set-default-zone=home
sudo firewall-offline-cmd --set-default-zone=home
###  设置默认区域为internal
sudo firewall-cmd --set-default-zone=internal
sudo firewall-offline-cmd --set-default-zone=internal
###  设置默认区域为trusted
sudo firewall-cmd --set-default-zone=trusted
sudo firewall-offline-cmd --set-default-zone=trusted
查看默认区域的配置,添加的和启用的规则
###   查看默认区域的配置,添加的和启用的规则
sudo firewall-cmd --list-all
sudo firewall-offline-cmd --list-all
查看默认zone放行了哪些端口
###  查看默认zone启用了哪些服务
sudo firewall-cmd --list-ports
sudo firewall-offline-cmd --list-ports
查看默认zone启用了哪些服务
###  查看默认zone启用了哪些服务
sudo firewall-cmd --list-services
sudo firewall-offline-cmd --list-services

查看当前活动的zone区域,以及对应的网卡
###   查看当前活动的zone区域,以及对应的网卡
sudo firewall-cmd --get-active-zones
查看指定网卡的zone
###  查看lo网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=lo
sudo firewall-offline-cmd --get-zone-of-interface=lo
###  查看ens33网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=ens33
sudo firewall-offline-cmd --get-zone-of-interface=ens33
###  查看enp0s3网卡接口的zone
sudo firewall-cmd --get-zone-of-interface=enp0s3
sudo firewall-offline-cmd --g