Detailed introduction to the knowledge points of iptables firewall

Detailed introduction to the knowledge points of iptables firewall

One, iptables overview

Linux system firewall-netfilter/iptables: IP information packet filtering system, it is actually composed of two components netfilter and iptables.
Mainly work at the network layer, aiming at IP data packets. Reflected in the processing of information such as the IP address and port in the packet.

Second, the relationship between netfilter and iptables

(1), netfilter

netfilter: a firewall function system that belongs to the "kernel space" (Kernel Space, also known as kernel space). It is part of the kernel and consists of some packet filtering tables. These tables contain a set of rules used by the kernel to control the processing of packet filtering.

(Two), iptables

iptables: A firewall management system that belongs to the "User Space" (User Space, also known as User Space). It is a command program used to manage the Linux firewall. It makes it easy to insert, modify and delete the rules in the packet filtering table. It is usually located in the /sbin/iptables directory.

Netfilter/iptables is called iptables for short later. iptables is a kernel-based firewall with four built-in rule tables: raw, mangle, nat, and filter. After all the rules in the table are configured, they will take effect immediately without restarting the service.

Three, four watches and five chains

The role of the rule table: to accommodate various rule chains
The role of the rule chain: to accommodate various firewall rules
There are chains in the table and rules in the chain

(1) Four tables

raw表:确定是否对该数据包进行状态跟踪。包含两个规则链,OUTPUT、PREROUTING。
mangle表:修改数据包内容,用来做流量整形的,给数据包设置标记。包含五个规则链,INPUT、OUTPUT、FORWARD、PREROUTING、POSTROUTING。
nat表:负责网络地址转换,用来修改数据包中的源、目标IP地址或端口。包含三个规则链,OUTPUT、PREROUTING、POSTROUTING。
filter表:负责过滤数据包,确定是否放行该数据包(过滤)。包含三个规则链,INPUT、FORWARD、OUTPUT。

#在 iptables 的四个规则表中,mangle 表和 raw 表的应用相对较少。

(2) Five chains

INPUT:处理入站数据包,匹配目标IP为本机的数据包。
OUTPUT:处理出站数据包,一般不在此链上做配置。
FORWARD:处理转发数据包,匹配流经本机的数据包。
PREROUTING链:在进行路由选择前处理数据包,用来修改目的地址,用来做DNAT。相当于把内网中的80端口映射到路由器外网端口上。
POSTROUTING链:在进行路由选择后处理数据包,用来修改源地址,用来做SNAT。相当于内网通过路由器NAT转换功能实现内网主机通过一个公网IP地址上网。

(3) When the data packet arrives at the firewall, the priority order between the rule tables

When packets arrive at the firewall, the order of priority between the rule tables:

raw > mangle > nat > filter

(4) Matching order between rule chains

1. Host-based firewall

入站数据(来自外界的数据包,且目标地址是防火墙本机):PREROUTING --> INPUT --> 本机的应用程序
出站数据(从防火墙本机向外部地址发送的数据包):本机的应用程序 --> OUTPUT --> POSTROUTING

2. Network firewall

转发数据(需要经过防火墙转发的数据包):PREROUTING --> FORWARD --> POSTROUTING

3. The matching order in the rule chain:

Check in order from top to bottom, and stop when a matching rule is found (except for the LOG policy, which means to record related logs).
If no matching rule is found in the chain, it will be processed according to the default policy of the chain (not Under the modified condition, the default policy is allowed)

Fourth, the installation of iptables

CentOS 7默认使用firewalld防火墙,没有安装 iptables,若想使用iptables防火墙。必须先关闭firewalld防火墙,再安装 iptables
systemctl stop firewalld.service
systemctl disable firewalld.service

yum -y install iptables iptables-services
systemctl start iptables.service

(1) The configuration method of iptables firewall

1、使用iptables 命令行。
2、使用system-config-firewall  (桌面环境)

(2) iptables command line configuration method

命令格式:

iptables [-t 表名] 管理选项 [链名] [匹配条件] [-j 控制类型]
注意事项:
不指定表名时,默认指filter表
不指定链名时,默认指表内的所有链
除非设置链的默认策略,否则必须指定匹配条件
选项、链名、控制类型使用大写字母,其余均为小写
常用的控制类型:
ACCEPT:允许数据包通过。
DROP:直接丢弃数据包,不给出任何回应信息。
REJECT:拒绝数据包通过,会给数据发送端一个响应信息。
SNAT:修改数据包的源地址。
DNAT:修改数据包的目的地址。
MASQUERADE:伪装成一个非固定公网IP地址。
LOG:在/var/log/messages文件中记录日志信息,然后将数据包传递给下一条规则。LOG只是一种辅助动作,并没有真正处理数据包。
常用的管理选项:
-A :在指定链的末尾追加(--append)一条新的规则
-I :在指定链的开头插入(--insert)一条新的规则,未指定序号时默认作为第一条规则
-R :修改、替换(--replace)指定链中的某一条规则,可指定规则序号或具体内容
-P :设置指定链的默认策略(--policy)
-D :删除(--delete)指定链中的某一条规则,可指定规则序号或具体内容
-F :清空(--flush)指定链中的所有规则,若未指定链名,则清空表中的所有链
-L :列出(--list)指定链中所有的规则,若未指定链名,则列出表中的所有链
-n :使用数字形式(--numeric)显示输出结果,如显示 IP 地址而不是主机名
-v :显示详细信息,包括每条规则的匹配包数量和匹配字节数
--line-numbers:查看规则时,显示规则的序号
添加新的规则:
iptables -t filter -A INPUT -p icmp -j REJECT
iptables -I INPUT 2 -p tcp --dport 22 -j ACCEPT
查看规则列表:
iptables [-t 表名] -n -L [链名] [--line-numbers]
或
iptables -[vn]L				#注意:不可以合写为 -Ln

iptables -n -L --line-numbers
设置默认策略:
iptables [-t 表名] -P <链名> <控制类型>

iptables -P INPUT DROP
iptables -P FORWARD DROP 
#一般在生产环境中设置网络型防火墙、主机型防火墙时都要设置默认规则为DROP,并设置白名单
删除规则:
iptables -D INPUT 2
iptables -t filter -D INPUT -p icmp -j REJECT
注意:
1.若规则列表中有多条相同的规则时,按内容匹配只删除的序号最小的一条
2.按号码匹配删除时,确保规则号码小于等于已有规则数,否则报错
3.按内容匹配删数时,确保规则存在,否则报错
清空规则:
iptables [-t 表名] -F [链名] 

iptables -F INPUT
iptables -F
注意:
1.-F 仅仅是清空链中的规则,并不影响 -P 设置的默认规则,默认规则需要手动进行修改
2.-P 设置了DROP后,使用 -F 一定要小心!
#防止把允许远程连接的相关规则清除后导致无法远程连接主机,此情况如果没有保存规则可重启主机解决
3.如果不写表名和链名,默认清空filter表中所有链里的所有规则

(3) Matching of rules

1. General matching

可直接使用,不依赖于其他条件或扩展,包括网络协议、IP地址、网络接口等条件。

协议匹配:-p 协议名
地址匹配:-s 源地址、-d 目的地址	#可以是IP、网段、域名、空(任何地址)
接口匹配:-i 入站网卡、-o 出站网卡

iptables -A FORWARD ! -p icmp -j ACCEPT 
iptables -A INPUT -s 192.168.80.11 -j DROP
iptables -I INPUT -i ens33 -s 192.168.80.0/24 -j DROP

2. Implicit matching

要求以特定的协议匹配作为前提,包括端口、TCP标记、ICMP类型等条件。

端口匹配:--sport 源端口、--dport 目的端口
#可以是个别端口、端口范围
--sport 1000			匹配源端口是1000的数据包
--sport 1000:3000		匹配源端口是1000-3000的数据包
--sport :3000			匹配源端口是3000及以下的数据包
--sport 1000:			匹配源端口是1000及以上的数据包
注意:--sport 和 --dport 必须配合 -p <协议类型> 使用
iptables -A INPUT -p tcp --dport 20:21 -j ACCEPT
iptables -I FORWARD -d 192.168.80.0/24 -p tcp --dport 24500:24600 -j DROP
TCP标记匹配:--tcp-flags TCP标记
iptables -I INPUT -i ens33 -p tcp --tcp-flags SYN,RST,ACK SYN -j ACCEPT
#丢弃SYN请求包,放行其他包
ICMP类型匹配:--icmp-type ICMP类型		
#可以是字符串、数字代码、、目标不可达
“Echo-Request”(代码为 8)表示 请求
“Echo-Reply”(代码为 0)表示 回显
“Destination-Unreachable”(代码为 3)表示 目标不可达
关于其它可用的 ICMP 协议类型,可以执行“iptables -p icmp -h”命令,查看帮助信息
iptables -A INPUT -p icmp --icmp-type 8 -j DROP		#禁止其它主机ping 本机
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT	#允许本机ping 其它主机
iptables -A INPUT -p icmp --icmp-type 3 -j ACCEPT	#当本机ping 不通其它主机时提示目标不可达
#此时其它主机需要配置关于icmp协议的控制类型为 REJECT
iptables -A INPUT -p icmp -j REJECT	

3. Explicit matching

要求以“-m 扩展模块”的形式明确指出类型,包括多端口、MAC地址、IP范围、数据包状态等条件。
多端口匹配:-m multiport --sports 源端口列表
		    -m multiport --dports 目的端口列表
iptables -A INPUT -p tcp -m multiport --dport 80,22,21,20,53 -j ACCEPT			
iptables -A INPUT -p udp -m multiport --dport 53 -j ACCEPT	
IP范围匹配:-m iprange --src-range IP范围
iptables -A FORWARD -p udp -m iprange --src-range 192.168.80.100-192.168.80.200 -j DROP			
#禁止转发源地址位于192.168.80.100-192.168.80.200的udp数据包
MAC地址匹配:-m mac --mac-source MAC地址
iptables -A FORWARD -m mac --mac-source xx:xx:xx:xx:xx:xx -j DROP
#禁止来自某MAC 地址的数据包通过本机转发
状态匹配:-m state --state 连接状态
常见的连接状态:
NEW :与任何连接无关的,还没开始连接
ESTABLISHED :响应请求或者已建立连接的,连接态
RELATED :与已有连接有相关性的(如FTP 主被动模式的数据连接),衍生态,一般与ESTABLISHED 配合使用
INVALID :不能被识别属于哪个连接或没有任何状态
iptables -A FORWARD -m state --state NEW -p tcp ! --syn -j DROP
#禁止转发与正常 TCP 连接无关的非--syn 请求数据包(如伪造的网络攻击数据包)
iptables -I INPUT -p tcp -m multiport --dport 80,22,21,20,53 -j ACCEPT
iptables -A INPUT -p udp -m multiport --dport 53 -j ACCEPT			
iptables -I INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -P INPUT DROP 

Five, SNAT principle and application

SNAT application environment: LAN hosts share a single public IP address to access the Internet (private cannot be routed normally in the Internet)

(1) Principle of SNAT

Modify the source address of the data packet.

(2) Prerequisites for SNAT conversion

1.局域网各主机已正确设置IP地址、子网掩码、默认网关地址
2.Linux网关开启IP路由转发
临时打开:
echo 1 > /proc/sys/net/ipv4/ip_forward
或
sysctl -w net.ipv4.ip_forward=1

永久打开:
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 		#将此行写入配置文件

sysctl -p 						#读取修改后的配置
iptables防火墙的知识点详细介绍:

iptables -t nat -A POSTROUTING -s 192.168.80.0/24 -o ens33 -j SNAT --to 12.0.0.1
或
iptables -t nat -A POSTROUTING -s 192.168.80.0/24 -o ens33 -j SNAT --to-source 12.0.0.1-12.0.0.10
									内网IP	     出站 外网网卡                 外网IP或地址池
SNAT转换2:非固定的公网IP地址(共享动态IP地址):

iptables -t nat -A POSTROUTING -s 192.168.80.0/24 -o ens33 -j MASQUERADE

小知识扩展:
一个IP地址做SNAT转换,一般可以让内网 100到200 台主机实现上网。

Six, DNAT principle and application

DNAT application environment: publish the server located in the local area network in the Internet

(1) Principle of DNAT

DNAT principle: modify the destination address of the data packet.

(2) Prerequisites for DNAT conversion

1.局域网的服务器能够访问Internet
2.网关的外网地址有正确的DNS解析记录
3.Linux网关开启IP路由转发
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1 	

sysctl -p 
DNAT转换1:发布内网的Web服务

#把从ens33进来的要访问web服务的数据包目的地址转换为 192.168.80.10
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to 192.168.80.10
或
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp --dport 80 -j DNAT --to-destination 192.168.80.10
                             入站 外网网卡  外网IP											   内网服务器IP

iptables -t nat -A PREROUTING -i ens33 -p tcp --dport 80 -j DNAT --to 192.168.80.10-192.168.80.20
DNAT转换2:发布时修改目标端口

#发布局域网内部的OpenSSH服务器,外网主机需使用250端口进行连接
iptables -t nat -A PREROUTING -i ens33 -d 12.0.0.1 -p tcp --dport 250 -j DNAT --to 192.168.80.10:22

在外网环境中使用SSH测试

ssh -p 250 [email protected]

yum -y install net-tools 		#若没有 ifconfig 命令可提前使用 yum 进行安装
ifconfig ens33

Note: When using DNAT, it is also used in conjunction with SNAT to achieve the correct return of response data packets

Small knowledge expansion:
Host-based firewalls mainly use INPUT and OUTPUT chains, and generally specify the ports in detail when setting rules.
Network-based firewalls mainly use FORWARD chains. When setting rules, they rarely specify ports, but generally specify IP addresses or Internet. Section

Seven, backup and restore of firewall rules

导出(备份)所有表的规则
iptables-save > /opt/ipt.txt
导入(还原)规则
iptables-restore < /opt/ipt.txt
将iptables规则文件保存在 /etc/sysconfig/iptables 中,iptables服务启动时会自动还原规则
iptables-save > /etc/sysconfig/iptables
systemctl stop iptables						#停止iptables服务会清空掉所有表的规则
systemctl start iptables					#启动iptables服务会自动还原/etc/sysconfig/iptables 中的规则

Guess you like

Origin blog.csdn.net/weixin_51573771/article/details/112094775