【运维知识进阶篇】iptables防火墙详解

这篇文章给大家介绍下iptables防火墙,防火墙大致分三种,分别是硬件、软件和云防火墙。硬件的话部署在企业网络的入口,有三层路由的H3C、华为、Cisco(思科),还有深信服等等;软件的话一般是开源软件,写在网站内部,最常见的有iptables(写入Linux内核)和firewalld(CentOS7有的),云防火墙就是阿里云业务的防火墙安全组等等。


名词解释

在介绍防火墙之前,我们先了解几个名词

容器:可以存放东西

(table):存放的容器

(chain):存放规则的容器

规则(policy):准许或拒绝规则

这几个像俄罗斯套娃那样环环相套!

iptables执行过程

NetFilter框架

最底层是网络接口层,网络接口层上面是网络层,网络层部署了NetFilter(网络过滤框架)

网络层上面部署了TCP、UDP传输层,这一层部署了Filter表、NAT表、Mangle表,表种存放要检查的条件和操作,这些都是内核层,再往上就是用户层,也就是应用层,可以设置iptables命令。

Linux服务器双网卡路由器

从物理层和数据链路层进入先通过PREROUTING链,进入路由判决,如果可以进入会通过INPUT链进入TCP、UDP,再进入应用层,由应用层再返回到TCP、UDP,通过OUTPUT链进入路由判决,再通过POSTROUTING链传输到物理层和数据链路层出来。

如果在路由判决那里不能进入的话,则会通过FORWARD链传输到出口的路由判决,在通过POSTROUTING链进入物理层和数据链路层传输出来。

表与链

iptables是4表5链,4表是filter表、nat表、raw表、mangle表,5链是input、pouput、forward、prerouting、postrouting

filter表有input链、output链、forward链

nat表有prerouting链、output链、postrouting链

mangle表有prerouting链、forward链、input链、output链、postrouting链

1、filter表

负责流量进出,屏蔽或准许端口IP

filter表

主要和主机自身相关,真正负责主机防火墙功能的(过滤流入与流出主机数据包)filter标识iptables默认使用的表,这个表定义了三个链。企业工作场景:主机防火墙

INPUT 负责过滤所有目标地址是本机地址的数据包,通俗说就是过滤进入主机的数据包
FORWARD 负责转发流经主机的数据包,起转发的作用,和NAT关系很大,有专门的LVS NAT模式,net.ipv4.ip_forward=0
OUTPUT 处理所有源地址是本机地址的数据包,通俗说就是从主机发送出去的数据包

2、NAT表

双网卡的,负责接受到后转发,类似于路由器,可以实现NAT功能:共享上网(内网服务器上外网)、端口映射和ip映射

NAT

负责网络地址转换,即来源与目的IP地址和端口的转换。

应用:和主机本身无关,一般用于局域网共享上网或者特殊的端口转换服务

工作场景:

1、用于企业路由(zebra)或网关(iptables),共享上网(postrouting)

2、做内部外部IP地址一对一映射(dmz),硬件防火墙映射IP到内部服务器,ftp服务(prerouting)

3、web,单个端口的映射,直接映射80端口(prerouting),这个表定义了3个链,nat功能相当于网络的acl控制,和网络交换机acl类似。

OUTPUT 和主机放出去的数据包有关,改变主机发出数据包的目的地址
PREROUTING

在数据包到达防火墙时,进行路由判断之前执行的规则,作用是改变数据包的目的地址、目的端口等

就是收信时,根据规则重写收件人的地址

例如:把公网IP映射到局域网的服务器上,如果是web服务,可以把80转换为局域网的服务器9000端口上10.0.0.61:80(目标端口)----nat----10.0.0.7:22

POSTROUTING

在数据包离开防火墙时进行路由判断之后执行的规则,作用是改变数据包的源地址,源端口等

写好发件人的地址,要让家人的回信时能够有地址可以回

例如:默认笔记本和虚拟机都是局域网地址,在出网的时候被路由器将源地址改为了公网地址

生产应用:局域网共享上网

3、Mangle表

存放头部信息

环境准备及命令

准备Ansible(10.0.0.61,172.16.1.61)和Web01(10.0.0.7、172.16.1.7)两台主机

安装iptables

[root@Ansible ~]# yum install -y iptables-services

查看iptables服务的配置文件

[root@Ansible ~]# rpm -ql iptables-services
/etc/sysconfig/ip6tables
/etc/sysconfig/iptables        #防火墙的配置文件
/usr/lib/systemd/system/ip6tables.service
/usr/lib/systemd/system/iptables.service        #防火墙服务配置文件(命令)
/usr/libexec/initscripts/legacy-actions/ip6tables
/usr/libexec/initscripts/legacy-actions/ip6tables/panic
/usr/libexec/initscripts/legacy-actions/ip6tables/save
/usr/libexec/initscripts/legacy-actions/iptables
/usr/libexec/initscripts/legacy-actions/iptables/panic
/usr/libexec/initscripts/legacy-actions/iptables/save
/usr/libexec/iptables
/usr/libexec/iptables/ip6tables.init
/usr/libexec/iptables/iptables.init

 将防火墙相关的模块、加载到内核中

[root@Ansible ~]# modprobe ip_tables
[root@Ansible ~]# modprobe iptable_filter
[root@Ansible ~]# modprobe iptable_nat
[root@Ansible ~]# modprobe ip_conntrack
[root@Ansible ~]# modprobe ip_conntrack_ftp
[root@Ansible ~]# modprobe ip_nat_ftp
[root@Ansible ~]# modprobe ipt_state 

 永久加入,写进开机自启动

cat >>/etc/rc.local<

 列出三表相关的内核模块,lsmod是列出系统当前载入的所有内核模块

[root@Ansible ~]# lsmod |egrep 'filter|nat|ipt'
nf_nat_ftp             12809  0 
nf_conntrack_ftp       18478  1 nf_nat_ftp
iptable_nat            12875  0 
nf_nat_ipv4            14115  1 iptable_nat
nf_nat                 26583  2 nf_nat_ftp,nf_nat_ipv4
nf_conntrack          139264  6 nf_nat_ftp,nf_nat,xt_state,nf_nat_ipv4,nf_conntrack_ftp,nf_conntrack_ipv4
iptable_filter         12810  0 
ip_tables              27126  2 iptable_filter,iptable_nat
libcrc32c              12644  3 xfs,nf_nat,nf_conntrack

关闭firewalld,启用iptables,并设置开机自启动

[root@Ansible ~]# systemctl stop firewalld
[root@Ansible ~]# systemctl disable firewalld
[root@Ansible ~]# systemctl start iptables.service
[root@Ansible ~]# systemctl enable iptables.service
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.

列出iptables所有规则

[root@Ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)        #链默认规则
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED 
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0          #规则 
ACCEPT     all  --  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
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT)        #链默认规则
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT)        #链默认规则
target     prot opt source               destination     

iptables命令参数

参数 含义
-L 显示表中的所有规则
-n 不要把端口或ip反向解析为名字
-t 指定表,不指定默认是filter表
-A append追加,加入准许类规则
-D delete删除,-D INPUT 1
-I insert拒绝类规则放在所有规则的最上面
-p 协议protocal tcp/udp/icmp/all
--dport 目标端口dest destination指定端口加上协议 -p tcp
--sport 源端口,source源
-d --destination 目标IP
-m 指定模块 multiport
-i input输入的时候,从哪个网卡进来
-o output输出的时候,从哪个网卡出去
-j

满足条件后的工作:DROP/ACCEPT/REJECT

DROP REJECT拒绝

DROP把数据丢掉,不会返回信息给用户

REJECT拒绝,返回拒绝信息

-F flush 清楚所有规则,不会处理默认的规则
-X 删除用户自定义的链
-Z 链的计数器清零(数据包计数器和数据包字节计数器)

配置filter表规则

正式配置之前,先备份,清空规则

[root@Ansible ~]# iptables -F
[root@Ansible ~]# iptables -X
[root@Ansible ~]# iptables -Z

[root@Ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

1、禁止访问22端口

[root@Ansible ~]# iptables -t filter -I INPUT -p tcp   --dport 22  -j DROP
[root@Ansible ~]# 
Connection closed by foreign host.

Disconnected from remote host(Linux86-10.0.0.61-Ansible) at 22:43:23.

Type `help' to learn how to use Xshell prompt.
[c:\~]$ 

【运维知识进阶篇】iptables防火墙详解_第1张图片

 删除规则

Connecting to 10.0.0.61:22...
Connection established.
To escape to local shell, press Ctrl+Alt+].

Last login: Sat May  6 22:44:26 2023
[root@Ansible ~]# 

2、封ip,屏蔽某个ip

[root@Ansible ~]# iptables -I INPUT  -s 10.0.0.7   -j DROP 
[root@Ansible ~]# iptables -I INPUT  -s 172.16.1.7   -j DROP 
[root@Ansible ~]#  iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       all  --  172.16.1.7           0.0.0.0/0           
DROP       all  --  10.0.0.7             0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

屏蔽之后用10.0.0.7ssh连接会卡住 

[root@Web01 ~]# ssh 10.0.0.61

3、禁止网段连入(禁止10.0.0.0/24网段访问 8888端口)

#指定10.0.0.0网段的屏蔽了22端口
[root@Ansible ~]# iptables -F
[root@Ansible ~]# iptables -I INPUT  -s 10.0.0.0/24  -p tcp  --dport 22 -j DROP 

[root@Web01 ~]# ssh 10.0.0.61 hostname
^C
[root@Web01 ~]# ssh 172.16.1.61 hostname
[email protected]'s password: 
Ansible

#指定10.0.0.0网段的屏蔽8888端口
[root@Ansible ~]# iptables -F
[root@Ansible ~]# iptables -I INPUT  -s 10.0.0.0/24  -p tcp  --dport 8888 -j DROP 

这个可以用nc和telnet测试

4、只允许指定网段连入(允许10.0.0.0网段)

类似于阿里云白名单功能,开放指定端口和网段,其他均拒绝

1、利用!排除,只准许10.0.0.0/24访问

[root@Ansible ~]# iptables -I INPUT ! -s 10.0.0.0/24  -j DROP


[root@Web01 ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.466 ms
64 bytes from 10.0.0.61: icmp_seq=2 ttl=64 time=0.658 ms
^C
--- 10.0.0.61 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.466/0.562/0.658/0.096 ms
[root@Web01 ~]# ping 172.16.1.61
PING 172.16.1.61 (172.16.1.61) 56(84) bytes of data.
^C
--- 172.16.1.61 ping statistics ---
4 packets transmitted, 0 received, 100% packet loss, time 2999ms

2、修改链默认规则,修改为拒绝,添加准许

[root@Ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination  
[root@Ansible ~]# iptables -P INPUT DROP 
[root@Ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination                

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
[root@Ansible ~]# iptables -A  INPUT   -s 10.0.0.0/24   -j  ACCEPT 
[root@Ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     all  --  10.0.0.0/24          0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 
 

【运维知识进阶篇】iptables防火墙详解_第2张图片

5、指定多个端口

[root@Ansible ~]# ​​iptables -I INPUT -p tcp --dport 8888 -j DROP 
[root@Ansible ~]# iptables -I INPUT -p tcp --dport 9999 -j DROP 
[root@Ansible ~]# ​iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:9999
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8888

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

[root@Ansible ~]# iptables -I INPUT  -p tcp -m multiport ! --dport 80,443  -j DROP

#如果是禁用连续端口可以不加-m multiport 1:1024
[root@Ansible ~]# iptables -I INPUT  -p tcp  --dport 1024:65535  -j DROP 
[root@Ansible ~]#  iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpts:1024:65535

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

6、匹配ICMP类型

ICMP(Internet Control Message Protocol)Internet控制报文协议,ping,是整个网站的核心

通过防火墙规则,控制是否可以ping

[root@Ansible ~]# iptables -I INPUT  -p icmp --icmp-type 8  -j DROP 
[root@Ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
DROP       icmp --  0.0.0.0/0            0.0.0.0/0            icmptype 8

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

[root@Web01 ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
^C
--- 10.0.0.61 ping statistics ---
6 packets transmitted, 0 received, 100% packet loss, time 5002ms

通过内核参数,控制禁止被ping 

[root@Ansible ~]# cat /etc/sysctl.conf     
net.ipv4.icmp_echo_ignore_all = 0    #0为允许,1为禁止
[root@Ansible ~]# sysctl -p 
net.ipv4.icmp_echo_ignore_all = 0

7、匹配网络状态(TCP/IP连接状态)

-m state --state

NEW:已经或将启动新的连接

ESTABLISHED:已建立的连接

RELATED:正在启动的新连接

INVALID:非法或无法识别的

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
​
iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

8、限制并发及速率

-m limit 限制模块

-m limit --limit 10/minute     #每分钟只能有10个数据包 每6秒生成

 -m limit --limit n/{second/minute/hour}:指定时间内的请求速率”n”为速率,后面为时间分别为:秒 分 时

-m limit --limit 10/minute   --limit-burst 5  每6秒释放工牌 给别人使用 
​
#10个数据包   
前5个  1个1个工牌   从第6个开始 每6秒 才能释放1个工牌 

--limit-burst [n]:在同一时间内允许通过的请求“n”为数字,不指定默认为5

#ping  icmp 协议  进行测试
[root@Ansible ~]# iptables -F 
[root@Ansible ~]# iptables -I INPUT  -p icmp  -m limit --limit 10/minute    --limit-burst 5   -j ACCEPT 
[root@Ansible ~]# iptables -A INPUT  -p tcp  --dport 22 -j  ACCEPT 
[root@Ansible ~]# iptables -P INPUT DROP 
[root@Ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            limit: avg 10/min burst 5
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination   

 测试

[root@Web01 ~]# ping 10.0.0.61
PING 10.0.0.61 (10.0.0.61) 56(84) bytes of data.
64 bytes from 10.0.0.61: icmp_seq=1 ttl=64 time=0.298 ms
64 bytes from 10.0.0.61: icmp_seq=2 ttl=64 time=1.33 ms
64 bytes from 10.0.0.61: icmp_seq=3 ttl=64 time=0.435 ms
64 bytes from 10.0.0.61: icmp_seq=4 ttl=64 time=0.460 ms
64 bytes from 10.0.0.61: icmp_seq=5 ttl=64 time=0.758 ms
64 bytes from 10.0.0.61: icmp_seq=7 ttl=64 time=2.28 ms    #7和1之间间隔6秒
64 bytes from 10.0.0.61: icmp_seq=13 ttl=64 time=0.793 ms  #13和7之间间隔6秒
64 bytes from 10.0.0.61: icmp_seq=19 ttl=64 time=0.734 ms
64 bytes from 10.0.0.61: icmp_seq=25 ttl=64 time=0.803 ms
^C
--- 10.0.0.61 ping statistics ---
26 packets transmitted, 9 received, 65% packet loss, time 25013ms
rtt min/avg/max/mdev = 0.298/0.877/2.286/0.572 ms

9、防火墙规则的保存与恢复

iptables-save 保存,默认输出到屏幕

iptables-restore 恢复,加上文件

写入/etc/sysconfig/iptables

[root@Ansible ~]# iptables-save   >/etc/sysconfig/iptables
[root@Ansible ~]#  cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Sun May  7 16:11:34 2023
*nat
:PREROUTING ACCEPT [306:36403]
:INPUT ACCEPT [182:29467]
:OUTPUT ACCEPT [145:11135]
:POSTROUTING ACCEPT [145:11135]
COMMIT
# Completed on Sun May  7 16:11:34 2023
# Generated by iptables-save v1.4.21 on Sun May  7 16:11:34 2023
*filter
:INPUT DROP [17:1428]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [49:5580]
-A INPUT -p icmp -m limit --limit 10/min -j ACCEPT
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
COMMIT
# Completed on Sun May  7 16:11:34 2023
[root@Ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            limit: avg 10/min burst 5
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@Ansible ~]# iptables -D INPUT 1 
[root@Ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@Ansible ~]# iptables-restore < /etc/sysconfig/iptables
[root@Ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0            limit: avg 10/min burst 5
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 

此外,systemctl restart iptables 会读取/etc/sysconfig/iptables内容

10、filter表简单总结

1、封ip,端口,网段

2、禁止ping

3、限制速度和并发

4、iptables filter表功能可以在云服务器使用

企业中用法

iptables配置方式:1、逛公园模式,默认规则是ACCEPT;2、看电影模式,默认规则是DROP

默认是拒绝

1、ssh可以连接

[root@Ansible ~]# iptables -F
[root@Ansible ~]# iptables -X
[root@Ansible ~]# iptables -Z
[root@Ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@Ansible ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
[root@Ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination 



[root@Web01 ~]# ssh 10.0.0.61 hostname
[email protected]'s password: 
Ansible

2、设置允许本机lo通讯规则

允许本机回环lo(本地操作)接口数据流量流出和流入

[root@Ansible ~]# iptables -A INPUT -i lo -j ACCEPT
[root@Ansible ~]# iptables -A OUTPUT -o lo -j ACCEPT

3、配置默认规则及放行80,443端口

[root@Ansible ~]# iptables -P INPUT DROP 
[root@Ansible ~]# iptables -P FORWARD DROP 
[root@Ansible ~]# iptables -P OUTPUT ACCEPT 
[root@Ansible ~]# 
[root@Ansible ~]# iptables -A INPUT  -m multiport -p tcp  --dport 443,80   -j ACCEPT
[root@Ansible ~]# iptables -nL 
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 443,80

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
[root@Ansible ~]# iptables -A INPUT  -s 10.0.0.0/24  -j ACCEPT 
[root@Ansible ~]# iptables -A INPUT  -s 172.16.1.0/24  -j ACCEPT
#此处还可以添加 网段 比如说 10.7.1.0/24

[root@Ansible ~]# iptables -nL
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 443,80
ACCEPT     all  --  10.0.0.0/24          0.0.0.0/0           
ACCEPT     all  --  172.16.1.0/24        0.0.0.0/0           

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
[root@Ansible ~]# iptables-save 
# Generated by iptables-save v1.4.21 on Sun May  7 16:24:29 2023
*nat
:PREROUTING ACCEPT [9:711]
:INPUT ACCEPT [2:112]
:OUTPUT ACCEPT [3:312]
:POSTROUTING ACCEPT [3:312]
COMMIT
# Completed on Sun May  7 16:24:29 2023
# Generated by iptables-save v1.4.21 on Sun May  7 16:24:29 2023
*filter
:INPUT DROP [0:0]
:FORWARD DROP [0:0]
:OUTPUT ACCEPT [7:1480]
-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m multiport --dports 443,80 -j ACCEPT
-A INPUT -s 10.0.0.0/24 -j ACCEPT
-A INPUT -s 172.16.1.0/24 -j ACCEPT
-A OUTPUT -o lo -j ACCEPT
COMMIT
# Completed on Sun May  7 16:24:29 2023

NAT表

恢复链

[root@Ansible ~]# iptables -P INPUT ACCEPT
[root@Ansible ~]# iptables -P FORWARD ACCEPT 
[root@Ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            multiport dports 443,80
ACCEPT     all  --  10.0.0.0/24          0.0.0.0/0           
ACCEPT     all  --  172.16.1.0/24        0.0.0.0/0           

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           
[root@Ansible ~]# iptables -F
[root@Ansible ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

1、实现共享上网

1、防火墙配置

[root@Ansible ~]#  iptables -t nat -A POSTROUTING  -s 172.16.1.0/24   -j SNAT  --to-source 10.0.0.61 
[root@Ansible ~]# echo 'net.ipv4.ip_forward = 1'  >> /etc/sysctl.conf 
[root@Ansible ~]# sysctl -p
net.ipv4.icmp_echo_ignore_all = 0
net.ipv4.ip_forward = 1

注意:公网IP不固定
iptables -t nat -A POSTROUTING  -s 172.16.1.0/24   -j  MASQUERADE

2、web配置

[root@Web01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=no            #修改网卡,禁止开机自启动
IPADDR=10.0.0.7
PREFIX=24
GATEWAY=10.0.0.2
DNS1=223.5.5.5
[root@Web01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=10.0.0.7
PREFIX=24
GATEWAY=172.16.1.61
DNS1=1.2.4.8
[root@Web01 ~]# systemctl restart network
[root@Ansible ~]# ssh 172.16.1.7
Last login: Sun May  7 21:16:43 2023 from 10.0.0.1
[root@Web01 ~]# ip a
1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0:  mtu 1500 qdisc pfifo_fast state DOWN group default qlen 1000
    link/ether 00:0c:29:91:01:20 brd ff:ff:ff:ff:ff:ff
3: eth1:  mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:91:01:2a brd ff:ff:ff:ff:ff:ff
    inet 172.16.1.7/24 brd 172.16.1.255 scope global eth1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe91:12a/64 scope link 
       valid_lft forever preferred_lft forever

 3、在web01测试结果

[root@Web01 ~]# ip r
default via 172.16.1.61 dev eth1 
169.254.0.0/16 dev eth1 scope link metric 1003 
172.16.1.0/24 dev eth1 proto kernel scope link src 172.16.1.7 
[root@Web01 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.16.1.61     0.0.0.0         UG    0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
172.16.1.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1
[root@Web01 ~]# ping baidu.com
PING baidu.com (39.156.66.10) 56(84) bytes of data.
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=1 ttl=127 time=10.3 ms
64 bytes from 39.156.66.10 (39.156.66.10): icmp_seq=2 ttl=127 time=12.3 ms
^C
--- baidu.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 10.306/11.346/12.386/1.040 ms
[root@Web01 ~]# ping 1.2.4.8
PING 1.2.4.8 (1.2.4.8) 56(84) bytes of data.
64 bytes from 1.2.4.8: icmp_seq=1 ttl=127 time=12.5 ms
64 bytes from 1.2.4.8: icmp_seq=2 ttl=127 time=11.3 ms
^C
--- 1.2.4.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 11.337/11.945/12.554/0.618 ms

2、端口转发(端口映射)

【运维知识进阶篇】iptables防火墙详解_第3张图片

[root@Ansible ~]# iptables -t nat -A PREROUTING    -d 10.0.0.61 -p tcp --dport 9000  -j DNAT --to-destination 172.16.1.7:22
[root@Ansible ~]# iptables -nL -t nat
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            10.0.0.61            tcp dpt:9000 to:172.16.1.7:22

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  172.16.1.0/24        0.0.0.0/0            to:10.0.0.61

 本地shell测试

c:\~]$ ssh [email protected] 9000


Connecting to 10.0.0.61:9000...
Connection established.
To escape to local shell, press Ctrl+Alt+].

Last login: Sun May  7 21:34:55 2023 from 10.0.0.61

3、IP映射

[root@Ansible ~]#  iptables -t nat -A PREROUTING  -d 10.0.0.62  -j DNAT  --to-destination 172.16.1.7 
[root@Ansible ~]# iptables -nL -t nat 
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         
DNAT       tcp  --  0.0.0.0/0            10.0.0.61            tcp dpt:9000 to:172.16.1.7:22
DNAT       all  --  0.0.0.0/0            10.0.0.62            to:172.16.1.7

Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         
SNAT       all  --  172.16.1.0/24        0.0.0.0/0            to:10.0.0.61

4、NAT表简单总结

1、可以实现共享上网

2、可以实现端口转发

3、NAT功能在云服务器无法使用,但有替代品叫NAT网关

iptables简单练习

1、查看iptables当前所有规则的命令

iptables-save 

iptables -nL

iptables -nL -t nat

2、禁止来自10.0.0.188IP地址访问80端口的请求

iptables -I   INPUT   -s 10.0.0.188 -p tcp   --dport 80      -j  DROP 

3、在命令行执行的iptables规则永久生效

iptables-save > /etc/sysconfig/iptables

 4、实现把访问10.0.0.3:80请求转到172.16.1.17:80

iptables -t nat  -A PREROUTING  -d 10.0.0.3  -p tcp --dport 80    -j   DNAT  --to-destination  172.16.1.17:80

5、实现172.16.1.0/24段所有主机通过124.32.54.26外网IP共享上网

iptables   -t nat  -A POSTROUTING    -s 172.16.1.0/24    -j  SNAT  --to-source   123.32.54.26
​
iptables   -t nat  -A POSTROUTING    -s 172.16.1.0/24    -j  SNAT  MASQUERADE

6、iptables实现防止syn ddos和ping攻击

-A FORWARD -p tcp --syn -m limit --limit 1/s --limit-burst 5 -j ACCEPT
-A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
-A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
说明:第一行:每秒中最多允许5个新连接。第二行:防止各种端口扫描。第三行:Ping洪水攻击(Ping of Death),可以根据需要调整或关闭

我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

你可能感兴趣的:(进阶运维知识,运维知识分享,运维,网络,ssh,网络协议,安全)