iptables学习笔记(centos 7.6)

目录

iptables简介

iptables-services的安装

使用iptables

iptables规则书写:

描述规则的基本参数

描述规则的扩展参数

防火墙的备份与还原

iptables与firewalld的区别与联系


iptables简介

参考文章:https://blog.csdn.net/daocaokafei/article/details/115091313
参考文章:https://blog.csdn.net/weixin_44792344/article/details/109674599
参考文章:https://blog.csdn.net/u011537073/article/details/82685586
参考文章:https://blog.csdn.net/IT__learning/article/details/121784633

iptables和netfilter的关系:
这是第一个要说的地方,Iptables和netfilter的关系是一个很容易让人搞不清的问题。很多的知道iptables却不知道 netfilter。其实iptables只是Linux防火墙的管理工具而已,位于/sbin/iptables。真正实现防火墙功能的是 netfilter,它是Linux内核中实现包过滤的内部结构。

规则(rules)其实就是网络管理员预定义的条件,规则一般的定义为“如果数据包头符合这样的条件,就这样处理这个数据包”。规则存储在内核空间的信息 包过滤表中,这些规则分别指定了源地址、目的地址、传输协议(如TCP、UDP、ICMP)和服务类型(如HTTP、FTP和SMTP)等。当数据包与规则匹配时,iptables就根据规则所定义的方法来处理这些数据包,如放行(accept)、拒绝(reject)和丢弃(drop)等。配置防火墙的 主要工作就是添加、修改和删除这些规则。

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

                          

iptables的表与链

iptables具有Filter, NAT, Mangle, Raw四种内建表:

1. Filter表

Filter表示iptables的默认表,因此如果你没有自定义表,那么就默认使用filter表,它具有以下三种内建链:

  • INPUT链 – 入站数据过滤。
  • OUTPUT链 – 出站数据过滤。
  • FORWARD链 – 转发数据过滤。

2. NAT表

NAT表有四种内建链:

  • PREROUTING链 – 处理刚到达本机并在路由转发前的数据包。它会转换数据包中的目标IP地址(destination ip address),通常用于DNAT(destination NAT)。
  • POSTROUTING链 – 处理即将离开本机的数据包。它会转换数据包中的源IP地址(source ip address),通常用于SNAT(source NAT)。
  • INPUT链 – 处理进入本机的数据包。
  • OUTPUT链 – 处理本机产生的数据包。

3. Mangle表

Mangle表用于指定如何处理数据包。它能改变TCP头中的QoS位。Mangle表具有5个内建链:

  • PREROUTING
  • OUTPUT
  • FORWARD
  • INPUT
  • POSTROUTING

4. Raw表

Raw表用于处理异常,它具有2个内建链:

  • PREROUTING chain
  • OUTPUT chain

iptables-services的安装

centos 7.6与centos 7.9 默认没有安装iptables-services,但是有iptables客户端,可以使用iptables命令设置规则以及不影响firewalld的调用。但是无法使用systemctl管理也无法使用/etc/sysconfig/iptables防火墙规则配置文件。
#############################################################################
iptables程序的配置文件:
[root@iZbp16mm3xbwen89azh9ffZ ~]# rpm -qc $(rpm -qf $(which iptables))
/etc/sysconfig/ip6tables-config
/etc/sysconfig/iptables-config
查询iptables-services配置文件  ###没有安装iptables-services之前/etc/sysconfig/目录下是没有iptables文件的
[root@VM-12-16-centos ~]# cat /etc/sysconfig/iptables
cat: /etc/sysconfig/iptables: No such file or directory
#############################################################################
下面开始安装iptables服务:
#停止和禁止开机自启动
systemctl stop firwalld.service&&systemctl disable firewalld.service
#安装iptables客户端和服务端
yum install iptables iptables-services -y
#开启iptables服务和开机自启动
systemctl enable iptables.service&&systemctl start iptables.service
查询iptables服务
# systemctl status iptables.services

[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core)    #阿里云服务器centos 7.6
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# systemctl status iptables
Unit iptables.service could not be found.        #未安装iptables服务
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# whereis iptables
iptables: /usr/sbin/iptables /usr/share/man/man8/iptables.8.gz
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# iptables -V
iptables v1.4.21             #iptables客户端版本iptables v1.4.21
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# yum install iptables-services -y  
#安装iptables-services
Installed:
  iptables-services.x86_64 0:1.4.21-35.el7                                                                     

Dependency Updated:
  iptables.x86_64 0:1.4.21-35.el7                                                                              

Complete
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# systemctl stop firewalld && systemctl disable firewalld
###关闭firewalld防火墙以及关闭firewalld的开机自启动
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# systemctl start iptables
###开启iptables-services
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# systemctl status iptables
● iptables.service - IPv4 firewall with iptables
   Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
   Active: active (exited) since Fri 2022-07-29 10:41:17 CST; 24s ago
  Process: 12336 ExecStart=/usr/libexec/iptables/iptables.init start (code=exited, status=0/SUCCESS)
 Main PID: 12336 (code=exited, status=0/SUCCESS)

Jul 29 10:41:17 iZbp16mm3xbwen89azh9ffZ systemd[1]: Starting IPv4 firewall with iptables...
Jul 29 10:41:17 iZbp16mm3xbwen89azh9ffZ iptables.init[12336]: iptables: Applying firewall rules: [  OK  ]
Jul 29 10:41:17 iZbp16mm3xbwen89azh9ffZ systemd[1]: Started IPv4 firewall with iptables.

使用iptables

首先介绍iptables的结构:iptables -> Tables -> Chains -> Rules. 简单地讲,tables由chains组成,而chains又由rules组成。
表的优先级raw(跟踪数据表规则表)>mangle(修改数据标记位规则表)>nat(地址转换规则表)>filter(过滤规则表)
filter 表:控制数据包是否允许进出及转发,可以控制的链路有 INPUT、FORWARD 和 OUTPUT。
nat 表:控制数据包中地址转换,可以控制的链路有 PREROUTING、INPUT、OUTPUT 和 POSTROUTING。
mangle:修改数据包中的原数据,可以控制的链路有 PREROUTING、INPUT、OUTPUT、FORWARD 和 POSTROUTING。
raw:控制 nat 表中连接追踪机制的启用状况,可以控制的链路有 PREROUTING、OUTPUT。

Linux 防火墙的过滤框架

如果是外部主机发送数据包给防火墙本机,数据将会经过 PREROUTING 链与 INPUT 链;如果是防火墙本机发送数据包到外部主机,数据将会经过 OUTPUT 链与 POSTROUTING 链;如果防火墙作为路由负责转发数据,则数据将经过 PREROUTING 链、FORWARD 链以及POSTROUTING 链。

iptables规则书写:

基本语法:iptables [-t 表] [操作命令] [链] [规则匹配器] [-j 目标动作]

iptables命令选项输入顺序:

iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作

说明:表名、链名用于指定 iptables命令所操作的表和链,命令选项用于指定管理iptables规则的方式(比如:插入、增加、删除、查看等;规则匹配用于指定对符合什么样条件的数据包进行处理;目标动作或跳转用于指定数据包的处理方式(比如允许通过、拒绝、丢弃、跳转(Jump)给其它链处理。

说明支持的链
raw一般是为了不再让iptables对数据包进行跟踪,提高性能PREROUTING、OUTPUT
mangle对数据包进行修改,用于实现服务质量五个链都可以
nat进行地址转换,用于网关路由器PREROUTING、OUTPUT、INPUT、POSTROUTING
filter(默认)对包进行过滤,用于防火墙规则INPUT、FORWARD、OUTPUT
常用操作命令说明
-A在指定链尾部添加规则
-D删除匹配的规则
-R替换匹配的规则
-I在指定位置插入规则(例:iptables -I INPUT 1 --dport 80 -j ACCEPT(将规则插入到filter表INPUT链中的第一位上)
-L/S列出指定链或所有链的规则
-F删除指定链或所有链的规则
-N创建用户自定义链[例:iptables -N allowed]
-X删除指定的用户自定义链
-P为指定链设置默认规则策略,对自定义链不起作用
-Z将指定链或所有链的计数器清零
-E更改自定义链的名称[例:iptables -E allowed disallowed]
-nip地址和端口号以数字方式显示[例:iptables -nL]
常用规则匹配器说明
-p tcp/udp/icmp/all匹配协议,all会匹配所有协议
-s addr[/mask]匹配源地址
-d addr[/mask]匹配目标地址
--sport port1[:port2]匹配源端口(可指定连续的端口)
--dport port1[:port2]匹配目的端口(可指定连续的端口)
-o interface匹配出口网卡,只适用FORWARD、POSTROUTING、OUTPUT(例:iptables -A FORWARD -o eth0)
-i interface匹配入口网卡,只使用PREROUTING、INPUT、FORWARD。
--icmp-type匹配icmp类型(使用iptables -p icmp -h可查看可用的ICMP类型)
--tcp-flags mask comp匹配TCP标记,mask表示检查范围,comp表示匹配mask中的哪些标记。(例:iptables -A FORWARD -p tcp --tcp-flags ALL SYN,ACK -j ACCEPT 表示匹配SYN和ACK标记的数据包)
目标动作说明
ACCEPT允许数据包通过
DROP丢弃数据包
REJECT丢弃数据包,并且将拒绝信息发送给发送方
SNAT源地址转换(在nat表上)例:iptables -t nat -A POSTROUTING -d 192.168.0.102 -j SNAT --to 192.168.0.1
DNAT目标地址转换(在nat表上)例:iptables -t nat -A PREROUTING -d 202.202.202.2 -j DNAT --to-destination 192.168.0.102
REDIRECT目标端口转换(在nat表上)例:iptables -t nat -D PREROUTING -p tcp --dport 8080 -i eth2.2 -j REDIRECT --to 80
MARK将数据包打上标记;例:iptables -t mangle -A PREROUTING -s 192.168.1.3 -j MARK --set-mark 60

描述规则的基本参数

以下这些规则参数用于描述数据包的协议、源地址、目的地址、允许经过的网络接口,以及如何处理这些数据包。这些描述是对规则的基本描述。

-p 协议(protocol)
指定规则的协议,如tcp, udp, icmp等,可以使用all来指定所有协议。
如果不指定-p参数,则默认是all值。这并不明智,请总是明确指定协议名称。
可以使用协议名(如tcp),或者是协议值(比如6代表tcp)来指定协议。映射关系请查看/etc/protocols,还可以使用–protocol参数代替-p参数

-s 源地址(source)
指定数据包的源地址
参数可以使IP地址、网络地址、主机名
例如:-s 192.168.1.101指定IP地址
例如:-s 192.168.1.10/24指定网络地址
如果不指定-s参数,就代表所有地址,还可以使用–src或者–source

-d 目的地址(destination)
指定目的地址,参数和-s相同,还可以使用–dst或者–destination

-i 输入接口(input interface)
-i代表输入接口(input interface)
-i指定了要处理来自哪个接口的数据包
这些数据包即将进入INPUT, FORWARD, PREROUTE链
例如:-i eth0指定了要处理经由eth0进入的数据包
如果不指定-i参数,那么将处理进入所有接口的数据包
如果出现! -i eth0,那么将处理所有经由eth0以外的接口进入的数据包
如果出现-i eth+,那么将处理所有经由eth开头的接口进入的数据包
还可以使用-in-interface参数

-o 输出(out interface)
-o代表”output interface”
-o指定了数据包由哪个接口输出
这些数据包即将进入FORWARD, OUTPUT, POSTROUTING链
如果不指定-o选项,那么系统上的所有接口都可以作为输出接口
如果出现! -o eth0,那么将从eth0以外的接口输出
如果出现-i eth+,那么将仅从eth开头的接口输出
还可以使用-out-interface参数

描述规则的扩展参数

对规则有了一个基本描述之后,有时候我们还希望指定端口、TCP标志、ICMP类型等内容。

--sport 源端口(source port)针对 -p tcp 或者 -p udp
缺省情况下,将匹配所有端口
可以指定端口号或者端口名称,例如"--sport 22"与"--sport ssh"。
/etc/services文件描述了上述映射关系。
从性能上讲,使用端口号更好
使用冒号可以匹配端口范围,如"--sport 22:100"
还可以使用"--source-port"

--dport 目的端口(destination port)针对-p tcp 或者 -p udp
参数和--sport类似,还可以使用"--destination-port"

--tcp-flags TCP标志 针对-p tcp
可以指定由逗号分隔的多个参数,有效值可以是:SYN, ACK, FIN, RST, URG, PSH,可以使用ALL或者NONE

--icmp-type ICMP类型 针对-p icmp
--icmp-type 0 表示Echo Reply
--icmp-type 8 表示Echo

 说明:iptables -F 无法清除链默认规则(即iptables -P 设置的规则)

查询iptables相关规则
# iptables -nvL
#查看iptables现有规则
iptables -L -n
#先允许所有,不然有可能会杯具
iptables -P INPUT ACCEPT
#清空表的所有链规则(链的默认规则不受此命令影响)
iptables -F
#清空所有自定义规则
iptables -X
#所有计数器归0
iptables -Z
#拒绝进入防火墙的所有ICMP协议数据包
iptables -I INPUT -p icmp -j REJECT
#只允许管理员从202.13.0.0/16网段使用SSH远程登录防火墙主机
iptables -A INPUT -p tcp --dport 22 -s 202.13.0.0/16 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP
说明:这个用法比较适合对设备进行远程管理时使用,比如位于分公司中的SQL服务器需要被总公司的管理员管理时。
#允许本机开放从TCP端口20-1024提供的应用服务
iptables -A INPUT -p tcp --dport 20:1024 -j ACCEPT
iptables -A OUTPUT -p tcp --sport 20:1024 -j ACCEPT
#允许转发来自192.168.0.0/24局域网段的DNS解析请求数据包
iptables -A FORWARD -s 192.168.0.0/24 -p udp --dport 53 -j ACCEPT
iptables -A FORWARD -d 192.168.0.0/24 -p udp --sport 53 -j ACCEPT
#禁止其他主机ping防火墙主机,但是允许从防火墙上ping其他主机
iptables -I INPUT -p icmp --icmp-type Echo-Request -j DROP
iptables -I INPUT -p icmp --icmp-type Echo-Reply -j ACCEPT
iptables -I INPUT -p icmp --icmp-type destination-Unreachable -j ACCEPT
#禁止转发来自MAC地址为00:0C:29:27:55:3F的和主机的数据包
iptables -A FORWARD -m mac --mac-source 00:0c:29:27:55:3F -j DROP
说明:iptables中使用“-m 模块关键字”的形式调用显示匹配。咱们这里用“-m mac –mac-source”来表示数据包的源MAC地址。
#允许防火墙本机对外开放TCP端口20、21、25、110以及被动模式FTP端口1250-1280
iptables -A INPUT -p tcp -m multiport --dport 20,21,25,110,1250:1280 -j ACCEPT
说明:这里用“-m multiport –dport”来指定目的端口及范围
#禁止转发源IP地址为192.168.1.20-192.168.1.99的TCP数据包
iptables -A FORWARD -p tcp -m iprange --src-range 192.168.1.20-192.168.1.99 -j DROP
说明:此处用“-m –iprange –src-range”指定IP范围。
#禁止转发与正常TCP连接无关的非—syn请求数据包
iptables -A FORWARD -m state --state NEW -p tcp ! --syn -j DROP
说明:“-m state”表示数据包的连接状态,“NEW”表示与任何连接无关的,新的嘛!
#拒绝访问防火墙的新数据包,但允许响应连接或与已有连接相关的数据包
iptables -A INPUT -p tcp -m state --state NEW -j DROP
iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
说明:“ESTABLISHED”表示已经响应请求或者已经建立连接的数据包,“RELATED”表示与已建立的连接有相关性的,比如FTP数据连接等。
#只开放本机的web服务(80)、FTP(20、21、20450-20480),放行外部主机发住服务器其它端口的应答数据包,将其他入站数据包均予以丢弃处理
iptables -I INPUT -p tcp -m multiport --dport 20,21,80 -j ACCEPT
iptables -I INPUT -p tcp --dport 20450:20480 -j ACCEPT
iptables -I INPUT -p tcp -m state --state ESTABLISHED -j ACCEPT
iptables -P INPUT DROP
#允许来自于lo接口的数据包(本地访问)
iptables -A INPUT -i lo -j ACCEPT
#开放22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#开放21端口(FTP)
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#开放80端口(HTTP)
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#开放443端口(HTTPS)
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
iptables -A INPUT -m state --state  RELATED,ESTABLISHED -j ACCEPT
#其他入站一律丢弃
iptables -P INPUT DROP
#所有出站一律绿灯
iptables -P OUTPUT ACCEPT
#所有转发一律丢弃
iptables -P FORWARD DROP
#封堵网段(10.20.30.0/24),两小时后解封
[root@dongm ~]# iptables -I INPUT -s 10.20.30.0/24 -j DROP
[root@dongm ~]# iptables -I FORWARD -s 10.20.30.0/24 -j DROP
[root@dongm at]# at now+2hours
at> iptables -D INPUT 1
at> iptables -D FORWARD 1<EOT>     #利用Ctrl+D保存
job 9 at Fri Nov 11 13:04:00 2022
[root@dongm at]# tail -5 /var/spool/at/a0000901a83b10
}
${SHELL:-/bin/sh} << 'marcinDELIMITER4e66ee6f'
iptables -D INPUT 1
iptables -D FORWARD 1
marcinDELIMITER4e66ee6f

[root@localhost ~]# iptables -S   ##查看规则,-t可以选择表
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -s 127.0.0.1/32 -d 127.0.0.1/32 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 21 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 20 -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-port-unreachable
-A OUTPUT -j ACCEPT
[root@localhost ~]# iptables -nL --line-numbers  ##查看规则并添加序号
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  127.0.0.1            127.0.0.1           
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:21
6    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:20

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0     
[root@localhost ~]# iptables -R INPUT 1 -s 127.0.0.1 -j REJECT  
##根据序号修改规则
[root@localhost ~]# iptables -D INPUT 3     ###根据序号删除规则
[root@localhost ~]# iptables -nL --line-numbers
Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  127.0.0.1            0.0.0.0/0            reject-with icmp-port-unreachable
2    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:21
5    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:20

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-port-unreachable

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
[root@localhost ~]# iptables -F  ###删除所有定义的规则,无法改变已设置的默认规则


C:\Users\dongmin1>ping 192.168.220.128   #####192.168.220.128主机无规则下

正在 Ping 192.168.220.128 具有 32 字节的数据:
来自 192.168.220.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.220.128 的回复: 字节=32 时间<1ms TTL=64
来自 192.168.220.128 的回复: 字节=32 时间=1ms TTL=64
来自 192.168.220.128 的回复: 字节=32 时间<1ms TTL=64

192.168.220.128 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 0ms,最长 = 1ms,平均 = 0ms

####192.168.220.128主机iptables -A INPUT -p icmp -j REJECT规则下
C:\Users\dongmin1>ping 192.168.220.128

正在 Ping 192.168.220.128 具有 32 字节的数据:
来自 192.168.220.128 的回复: 无法连到端口。
来自 192.168.220.128 的回复: 无法连到端口。
来自 192.168.220.128 的回复: 无法连到端口。
来自 192.168.220.128 的回复: 无法连到端口。

192.168.220.128 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),

####192.168.220.128主机iptables -I INPUT -p icmp -j DROP规则下
#本机INPUT链的Echo-Request表示是否允许其他主机ping本防火墙主机,Echo-Reply表示是否允许其他主机对本机防火墙发出的ping包的应答数据包通过
#本机OUTPUT链的Echo-Request表示是否允许本机ping其他的主机,Echo-Reply表示是否允许本机对其他主机发出的ping所答复的数据包通过
C:\Users\dongmin1>ping 192.168.220.128

正在 Ping 192.168.220.128 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

192.168.220.128 的 Ping 统计信息:
    数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),


--icmp-type 8 表示Echo-Request
#iptables  -A OUTPUT -p icmp  --icmp-type Echo-Request -j DROP   #不允许从防火墙上ping其他主机
[root@master ~]# ping 180.101.50.242
ping: sendmsg: Operation not permitted
--icmp-type 0 表示Echo-Reply
# iptables -A INPUT -p icmp --icmp-type Echo-Reply -j DROP   #不允许其他主机答复本机的ping数据包通过
[root@master ~]# ping 180.101.50.242
PING 180.101.50.242 (180.101.50.242) 56(84) bytes of data.

#iptables -I INPUT -p icmp --icmp-type destination-Unreachable -j ACCEPT    

##############-p icmp总结##################
--icmp-type ICMP类型 针对-p icmp
--icmp-type 0 表示Echo Reply
--icmp-type 8 表示Echo
--icmp-type说明
在192.168.1.10主机配置
# iptables -A OUTPUT -p icmp --icmp-type 0 -j DROP
表示其他主机ping 192.168.1.10时,不允许192.168.1.10主机icmp协议的回应数据包通过
# iptables -A OUTPUT -p icmp --icmp-type 8 -j DROP
表示不允许192.168.1.10主机ping其他主机icmp协议的发数据包通过
# iptables -A INPUT -p icmp --icmp-type 8 -j DROP
表示不允许其他主机ping 192.168.1.10主机icmp协议的发数据包通过
# iptables -A INPUT -p icmp --icmp-type 0 -j DROP
表示192.168.1.10主机ping其他主机时,不允许其他主机icmp协议的回应数据包通过
############################################


####配置应用程序规则(重点领悟)
如何在默认链策略为DROP的情况下,进行防火墙设置。在这里,我们将引进一种新的 参数-m state,并检查数据包的状态字段。

1.SSH

# 1.允许接收远程主机的SSH请求 
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

# 2.允许发送本地主机的SSH响应
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

-m state: 启用状态匹配模块(state matching module)
--state: 状态匹配模块的参数。当SSH客户端第一个数据包到达服务器时,状态字段为NEW;建立连接后数据包的状态字段都是ESTABLISHED
--sport 22: sshd监听22端口,同时也通过该端口和客户端建立连接、传送数据。因此对于SSH服务器而言,源端口就是22
--dport 22: ssh客户端程序可以从本机的随机端口与SSH服务器的22端口建立连接。因此对于SSH客户端而言,目的端口就是22
如果服务器也需要使用SSH连接其他远程主机,则还需要增加以下配置:

# 1.送出的数据包目的端口为22 
iptables -A OUTPUT -o eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT

# 2.接收的数据包源端口为22
iptables -A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

2.HTTP
如何访问192.168.220.108主机的80端口
iptables -P INPUT DROP
iptables -p OUTPUT DROP
[root@master ~]# iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
[root@master ~]# iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

3.完整的配置
# 1.删除现有规则 
iptables -F

# 2.配置默认链策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

# 3.允许远程主机进行SSH连接
iptables -A INPUT -i eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

# 4.允许本地主机进行SSH连接
iptables -A OUTPUT -o eth0 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT

# 5.允许HTTP请求
iptables -A INPUT -i eth0 -p tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -o eth0 -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT

前提基础:
1、当主机收到一个数据包后,数据包先在内核空间中处理,若发现目的地址是自身,则传到用户空间中交给对应的应用程序处理,若发现目的不是自身,则会将包丢弃或进行转发。

2、iptables实现防火墙功能的原理是:在数据包经过内核的过程中有五处关键地方,分别是PREROUTING、INPUT、OUTPUT、FORWARD、POSTROUTING,称为钩子函数,iptables这款用户空间的软件可以在这5处地方写规则,对经过的数据包进行处理,规则一般的定义为“如果数据包头符合这样的条件,就这样处理数据包”。

3、iptables中定义有5条链,说白了就是上面说的5个钩子函数,因为每个钩子函数中可以定义多条规则,每当数据包到达一个钩子函数时,iptables就会从钩子函数中第一条规则开始检查,看该数据包是否满足规则所定义的条件。如果满足,系统就会根据该条规则所定义的方法处理该数据包;否则iptables将继续检查下一条规则,如果该数据包不符合钩子函数中任一条规则,iptables就会根据该函数预先定义的默认策略来处理数据包

4、iptables中定义有表,分别表示提供的功能,有filter表(实现包过滤)、nat表(实现网络地址转换)、mangle表(实现包修改)、raw表(实现数据跟踪),这些表具有一定的优先级:raw-->mangle-->nat-->filter

一条链上可定义不同功能的规则,检查数据包时将根据上面的优先级顺序检查

linux iptables详解--个人笔记
1、目的地址是本地,则发送到INPUT,让INPUT决定是否接收下来送到用户空间,流程为①--->②;

2、若满足PREROUTING的nat表上的转发规则,则发送给FORWARD,然后再经过POSTROUTING发送出去,流程为: ①--->③--->④--->⑥

主机发送数据包时,流程则是⑤--->⑥

防火墙的备份与还原

# rpm -qa|grep iptables
iptables-services-1.4.21-35.el7.x86_64
iptables-1.4.21-35.el7.x86_64
# rpm -ql iptables-services-1.4.21-35.el7.x86_64
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables                 ##iptables-services配置文件
......................
# rpm -ql iptables-1.4.21-35.el7.x86_64
/etc/sysconfig/ip6tables-config
/etc/sysconfig/iptables-config
/usr/sbin/ip6tables
/usr/sbin/ip6tables-restore
/usr/sbin/ip6tables-save
/usr/sbin/iptables
/usr/sbin/iptables-restore             ##可对iptables-save重定向的文件进行iptables规则还原
/usr/sbin/iptables-save                ##输出当前iptables规则
/usr/sbin/xtables-multi
.......................

保存iptables规则的2种方式

①系统没有安装iptables-services软件包的情况下

默认的 iptables 命令配置的防火墙规则会立刻生效,但如果不保存,当计算机重启后所有的规则都会丢失,所以对防火墙规则进行及时保存的操作是非常必要的。可以使用iptables软件包自带的iptables-save命令,iptables-save命令只输出当前规则。可以将iptables-save的输出重定向到文件的方式保存当前iptables规则
# iptables-save|tee -a filename
但是电脑重启后规则消失,使用命令
# iptables-restore < filename
加载保存的规则到重启后的系统。

②系统安装iptables-services软件包的情况下

# iptables -nvL --line-numbers     ##查看当前规则
# service iptables save           ##将当前系统的规则保存到/etc/sysconfig/iptables文件
# systemctl enable iptables  ##使iptables服务开机启动,自动加载/etc/sysconfig/iptables的规则

举例说明:

iptables 软件包提供了两个非常有用的工具,我们可以使用这两个工具处理大量的防火墙规则。这两个工具分别是 iptables-save 和 iptables-restore。

CentOS 7 系统中安装iptables-services软件包后,使用service iptables save 命令将规则保存至 默认文件/etc/sysconfig/iptables中,可以实现保存iptables规则的作用,计算机重启后会自动加载该文件中的规则(需要systemctl enable iptables)。如果使用 iptables-save 将规则保存至其他位置,也可以实现备份iptables规则的作用。当iptables规则需要做还原操作时,可以使用 iptables-restore 将备份文件直接导入当前防火墙规则。

###iptables服务端和客户端的iptables软件版本(centos 7.6)
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# rpm -qa|grep iptables
iptables-1.4.21-35.el7.x86_64
iptables-services-1.4.21-35.el7.x86_64  
[root@master ~]# rpm -qc iptables-1.4.21-35.el7.x86_64  #命令软件包
/etc/sysconfig/ip6tables-config
/etc/sysconfig/iptables-config
[root@master ~]# rpm -qc iptables-services-1.4.21-35.el7.x86_64   #服务软件包
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables

######执行iptables-save 命令:显示出当前启用的所有规则,按照 
raw、mangle、nat、filter 表的顺序依次列出,如下所示: 
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# iptables-save|tee 1.txt  
##iptables-save可以显示所有表和规则设置,并可以重定向到文件后利用iptables-restore恢复####
# Generated by iptables-save v1.4.21 on Fri Jul 29 14:52:20 2022
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [125:19850]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Fri Jul 29 14:52:20 2022
# Generated by iptables-save v1.4.21 on Fri Jul 29 14:52:20 2022
*nat
:PREROUTING ACCEPT [1:40]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [17:1292]
:POSTROUTING ACCEPT [17:1292]
COMMIT
# Completed on Fri Jul 29 14:52:20 2022
#########################################################################
其中:
“#”号开头的表示注释;
“*filter”表示所在的表;
“:链名默认策略”表示相应的链及默认策略,具体的规则部分省略了命令名“iptables”;
在末尾处“COMMIT”表示提交前面的规则设置。
##########################################################################

[root@iZbp16mm3xbwen89azh9ffZ ~]# cat /etc/sysconfig/1.txt ####iptables-save保存的文件
# Generated by iptables-save v1.4.21 on Fri Jul 29 14:53:41 2022
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [230:41650]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Fri Jul 29 14:53:41 2022
# Generated by iptables-save v1.4.21 on Fri Jul 29 14:53:41 2022
*nat
:PREROUTING ACCEPT [1:40]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [25:1900]
:POSTROUTING ACCEPT [25:1900]
COMMIT
# Completed on Fri Jul 29 14:53:41 2022
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# service iptables save 
####此命令可以将规则配置保存到/etc/sysconfig/iptables文件,以便iptables服务开机自动加载######
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
[root@iZbp16mm3xbwen89azh9ffZ sysconfig]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Fri Jul 29 14:57:12 2022
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [339:61642]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
# Completed on Fri Jul 29 14:57:12 2022
# Generated by iptables-save v1.4.21 on Fri Jul 29 14:57:12 2022
*nat
:PREROUTING ACCEPT [2:80]
:INPUT ACCEPT [1:40]
:OUTPUT ACCEPT [54:4104]
:POSTROUTING ACCEPT [54:4104]
COMMIT
# Completed on Fri Jul 29 14:57:12 2022
[root@iZbp16mm3xbwen89azh9ffZ ~]# systemctl enable iptables 
###设置iptables服务开机自启动,加载/etc/sysconfig/iptables配置#########


[root@iZbp16mm3xbwen89azh9ffZ ~]# iptables -F   ###如果规则清空
[root@iZbp16mm3xbwen89azh9ffZ ~]# iptables-restore</etc/sysconfig/1.txt 
#######使用iptables-save重定向的规则配置文件恢复规则###############
[root@iZbp16mm3xbwen89azh9ffZ ~]# iptables -nvL      ###查看是否还原
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  299  147K ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED
    6   216 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
    0     0 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            state NEW tcp dpt:22
    8   440 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 327 packets, 52732 bytes)
 pkts bytes target     prot opt in     out     source               destination         

iptables与firewalld的区别与联系

在需要配置防火墙的情况下,二者配置其中一个即可

①使用iptables配置
# systemctl stop firewalld
在firewalld服务关闭的情况下使用iptables命令配置规则即可

②使用firewalld配置
参考资料:https://www.zhihu.com/question/352221004/answer/2479567005
Firewalld自身并不具备防火墙的功能,而是和iptables一样需要通过内核的netfilter来实现,也就是说firewalld和iptables一样,它们的作用都是用于维护规则,而真正使用规则干活的是内核的netfilter,只不过firewalld和iptables的结构以及使用方法不一样罢了。
firewalld任何规则的变更都不需要对整个防火墙规则列表进行重新加载,只需要将变更部分保存并更新到运行中的 iptables即可。
我的理解是:在firewalld防火墙关闭的情况下,iptables设置了规则后再启动firewalld防火墙,那么iptables的规则先清空,再将firewalld防火墙的规则保存并更新到iptables。
netfilter使用定义的规则决定数据包的走向

# iptables -P FORWARD DROP
# iptables -nvL --line-numbers
Chain INPUT (policy ACCEPT 151 packets, 9846 bytes)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy DROP 0 packets, 0 bytes)            ##FORWARD链默认策略为DROP
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 97 packets, 7780 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
# systemctl start firewalld
# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
    
# iptables -nvL --line-numbers            ##ptables的规则先清空,再将firewalld防火墙的规则保存并更新到iptables
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      253 15703 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2       16   960 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        1    92 INPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
4        1    92 INPUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
5        1    92 INPUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
6        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
7        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate RELATED,ESTABLISHED
2        0     0 ACCEPT     all  --  lo     *       0.0.0.0/0            0.0.0.0/0           
3        0     0 FORWARD_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 FORWARD_IN_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
5        0     0 FORWARD_IN_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
6        0     0 FORWARD_OUT_ZONES_SOURCE  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
7        0     0 FORWARD_OUT_ZONES  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
8        0     0 DROP       all  --  *      *       0.0.0.0/0            0.0.0.0/0            ctstate INVALID
9        0     0 REJECT     all  --  *      *       0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 58 packets, 5608 bytes)
num   pkts bytes target     prot opt in     out     source               destination         
1      160  9495 ACCEPT     all  --  *      lo      0.0.0.0/0            0.0.0.0/0           
2       58  5608 OUTPUT_direct  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FORWARD_IN_ZONES (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 FWDI_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
2        0     0 FWDI_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_IN_ZONES_SOURCE (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD_OUT_ZONES (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 FWDO_public  all  --  *      ens33   0.0.0.0/0            0.0.0.0/0           [goto] 
2        0     0 FWDO_public  all  --  *      +       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain FORWARD_OUT_ZONES_SOURCE (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD_direct (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public (2 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 FWDI_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 FWDI_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 FWDI_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FWDI_public_allow (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public_deny (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FWDI_public_log (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public (2 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        0     0 FWDO_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
2        0     0 FWDO_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
3        0     0 FWDO_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain FWDO_public_allow (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public_deny (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain FWDO_public_log (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_ZONES (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        1    92 IN_public  all  --  ens33  *       0.0.0.0/0            0.0.0.0/0           [goto] 
2        0     0 IN_public  all  --  +      *       0.0.0.0/0            0.0.0.0/0           [goto] 

Chain INPUT_ZONES_SOURCE (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain INPUT_direct (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain IN_public (2 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        1    92 IN_public_log  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
2        1    92 IN_public_deny  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
3        1    92 IN_public_allow  all  --  *      *       0.0.0.0/0            0.0.0.0/0           
4        0     0 ACCEPT     icmp --  *      *       0.0.0.0/0            0.0.0.0/0           

Chain IN_public_allow (1 references)
num   pkts bytes target     prot opt in     out     source               destination         
1        1    92 ACCEPT     tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:22 ctstate NEW,UNTRACKED

Chain IN_public_deny (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain IN_public_log (1 references)
num   pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT_direct (1 references)
num   pkts bytes target     prot opt in     out     source               destination     

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值