Iptables

  • 1)iptables
  • 2)主机之间的通讯过程
  • 3) iptables使用原则
  • 4) iptables容器
  • 5) iptables的表和链
  • 6)防火墙工作原理说明
  • 7)防火墙服务部署过程


1)iptables

概念:

开源的基于数据包过滤的防火墙工具,实现数据的流入和流出控制,主要工作在OSI七层的二(链路层),三(网络层),四(传输层),如果重新编译内核,也可以支持七层控制(squid代理+iptables)开源的基于数据包过滤的防火墙工具,实现数据的流入与流出控制
Iptables主要工作在OSI七层的二(链路层-很少关注)、三(网络层)、四(传输层)层,如果重新编译内核,Iptables也可以支持7层控制(squid代理+iptables)。

2)主机之间的通讯过程

主机与主机之间通讯过程,数据包传递方式
主机A        --->
数据信息
传输层头部信息	(TCP UDP  源端口 :目标端口)+ 数据信息
网络层头部		(IP 源IP:目标IP)				传输层头部+数据信息              
链路层头部		(源mac:目标mac) 				网络层头部+传输层头部+数据信息
物理层           将所有数据信息转换为二进制信息 01010101011101
网卡(网线)     将0信号变为低电压传递 1信号高电压

主机B        <---
网卡(网线)     将电信号转为二进制信息
物理层           将二进制信息转换成数据包
链路层           源mac:目标mac 是不是自己本地主机上的mac地址
网络层           源IP:目标IP    是不是自己本地主机上的IP地址
传输层           源端口:目标端口
查看到数据信息 处理响应
 
IP地址标识身份信息           ==>护照
mac地址信息                 ==>中华人民共和国身份证

3) iptables使用原则

1)用户并发访问量不大时 使用软件防火墙即可
2)用户并发访问量较高时 使用硬件防火墙

4) iptables容器

容器概念:装东西器皿  
iptables程序容器:表概念-- 防护墙中的表拥有不同的功能策略  
表容器:          链概念-- 控制数据包处理方式	           
链容器:          策略概念 -- 不同配置信息

5) iptables的表和链

表和链概念:四表五链
Filter:这是默认表, 实现数据过滤处理
        INPUT: 流量进入时,进行处理方式
        OUTPUT: 流量出去时,进行处理方式
		FORWARD:留经网卡流量
NAT:当遇到新创建的数据包连接时将参考这个表, 会将数据进行映射转换
     源IP地址:10.0.0.1       局域网识别有效  --- 互联网 172.16.0.1 
     目标IP地址:172.16.0.1   局域网识别有效  --- 局域网 10.0.0.1 
	 端口映射转换: 9000 --> 80
	 地址转换原因:IP地址是有限的 0.0.0.0 ~ 255.255.255.255  40+亿 
	               IP地址重复使用 只有少部分可以重复使用 私网地址/公网地址
	 OUTPUT:  自身产生的流量在出去时,做映射转换
     PREROUTING: 经过数据流量,在进入时做映射转换
	 POSTROUTING: 经过数据流量,在出去时做映射转换			   
Managle:可以将数据包信息进行修改调整
raw:    将数据包某些标记信息进行拆解

6)防火墙工作原理说明

1.	防火墙是层层过滤的,实际是按照配置规则的顺序从上到下,从前到后进行过滤的。
2.	如果匹配上规则,即明确表示是阻止还是通过,数据包就不再向下匹配新的规则。
3.	如果规则中没有明确表明是阻止还是通过的,也就是没有匹配规则,向下进行匹配,直到匹配默认规则得到明确的阻止还是通过。
4.	防火墙的默认规则是所有规则执行完才执行的。

7)防火墙服务部署过程

yum install iptables-services
systemctl start iptables.service
[root@test-201 ~]# yum install iptables-services
[root@test-201 ~]# systemctl start iptables.service 
[root@test-201 ~]# iptables -nL
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0            state REL
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
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-wi

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  0.0.0.0/0            0.0.0.0/0            reject-wi

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
iptables命令基本用法:
查看信息用法:
iptables -n -L               --- L列表显示相应表策略信息 n 尽量信息以数值方式显示
iptables -n -L -t 表名       --- 具体查看每个表配置信息
iptables -nL -v              --- 显示详细配置规则信息
iptables -nL --line-number   --- 查看规则序号信息

初始化清除操作:
默认配置信息,旧的配置信息
[root@A ~]# iptables -F         # 清空规则,但不会删除默认规则
-F 		#<==清除一个链或所有链上的规则
[root@A ~]# iptables -Z         # 清空防火墙计数器  可以用于排错
-Z 		#<==链的记数器清零
[root@A ~]# iptables -X         # 清空自定义规则    
-X 		#<==删除用户自定义的链

演示:

[root@test-201 ~]# iptables -nL -v
Chain INPUT (policy ACCEPT 27 packets, 1956 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 18 packets, 2472 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@test-201 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP
[root@test-201 ~]# 
Connection closed by foreign host.

Disconnected from remote host(架构10.0.0.201) at 15:13:47.

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

本地连接:

[c:\~]$ telnet 10.0.0.201 22


Connecting to 10.0.0.201:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
SSH-2.0-OpenSSH_7.4

Protocol mismatch.

Connection closed by foreign host.

Disconnected from remote host(10.0.0.201:22) at 15:12:15.

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


Connecting to 10.0.0.201:22...
Could not connect to '10.0.0.201' (port 22): Connection failed.

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

*需要在虚拟机里取消规则,然后重连

iptables排除网段的某个ip_数据


*单一规则可以直接删除(规则少的情况):

[root@test-201 ~]# iptables -F

如果是某一具体规则删除,怎么创怎么删(规则多的情况):

[root@test-201 ~]# iptables -t filter -A INPUT -p tcp --dport 22 -j DROP     #创建规则
[root@test-201 ~]# iptables -t filter -D INPUT -p tcp --dport 22 -j DROP     #删除规则
方法一:利用防火墙实现数据包过滤
eg01:禁止外网用户访问服务22端口  基于四层进行过滤处理
iptables -t filter -A INPUT -p tcp --dport 22 -j DROP   添加规则 
iptables -t filter -D INPUT 序号   删除规则 
iptables -t filter -A OUTPUT -p tcp --sport 22 -j DROP   添加规则 

eg02:只禁止10.0.0.1访问22端口    基于三层和四层进行过滤
iptables -t filter -A INPUT -s 10.0.0.1 -p tcp --dport -j DROP 
iptables -t filter -A OUTPUT -d 10.0.0.1 -p tcp --sport -j DROP
iptables -t filter -A INPUT -s 10.0.0.0/24 -d 172.16.0.0/24  DROP

eg03:实现禁止ping(本机10.0.0.1,虚拟机10.0.0.201)
双方向禁ping
iptables -t filter -A INPUT -p icmp -j DROP   彻底实现了禁止ping过程
[root@test-201 ~]# iptables -t filter -A INPUT -p icmp -j DROP
[root@test-201 ~]# ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
^C
--- 10.0.0.1 ping statistics ---
16 packets transmitted, 0 received, 100% packet loss, time 15009ms
[root@test-201 ~]#

本机:

iptables排除网段的某个ip_iptables排除网段的某个ip_02


单反向禁ping
iptables -A INPUT -p icmp --icmp-type 8 -j DROP  禁止windows ping linux
[root@test-201 ~]# iptables -A INPUT -p icmp --icmp-type 8 -j DROP 
[root@test-201 ~]#

iptables排除网段的某个ip_数据_03

iptables -A OUTPUT -p icmp --icmp-type 8 -j DROP 禁止linux ping windows
icmp协议说明:
8类型是请求icmp包
0类型是响应icmp包
禁止windows --- linux    linux上配置禁止icmp8类型包进来

[root@test-201 ~]# iptables -A OUTPUT -p icmp --icmp-type 8 -j DROP
[root@test-201 ~]# ping 10.0.0.1
PING 10.0.0.1 (10.0.0.1) 56(84) bytes of data.
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
ping: sendmsg: Operation not permitted
^C
--- 10.0.0.1 ping statistics ---
3 packets transmitted, 0 received, 100% packet loss, time 1999ms

[root@test-201 ~]#
eg04:禁止多个端口不能访问
多端口进行规则配置:
方式一:连续多端口进行阻止
iptables -A INPUT -p tcp --dport 23:80 -j DROP	

方式二:间隔多端口进行配置
iptables -A INPUT -p tcp --dport 23,25,80 -j DROP
iptables -A INPUT -m multiport -p tcp --dport 23,25,80 -j DROP
eg05:如何调整规则顺序
iptables -I INPUT -p tcp --dport 22 -j DROP  --- 将指定插入到某个规则之上 或直接插入到第一条规则

-t -A -D -I -s -d -p --dport --sport -j -m 

eg05:默认规则修改
默认规则修改为: ACCEPT  链规则采用黑名单
默认规则修改为: DROP    链规则采用白名单  最安全
iptables -P INPUT DROP

方法二:利用防火墙实现数据包映射
1)将拥有内网IP地址主机 实现访问互联网
第一个历程:配置内网地址网卡信息
网卡地址:172.16.1.10/24 
网关地址:172.16.1.201
DNS地址: 223.5.5.5

iptables排除网段的某个ip_iptables排除网段的某个ip_04

第二个历程:开启防火墙服务器转发功能
vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p  --- 加载系统内核配置

iptables排除网段的某个ip_数据_05

iptables排除网段的某个ip_IP_06

第三个历程:配置防火墙NAT功能实现内网访问外网
iptables -t nat -A POSTROUTING -s 172.16.1.10 -o eth0 -j SNAT --to-source 10.0.0.201
[root@test-201 ~]# iptables -t nat -A POSTROUTING -s 172.16.1.10 -o eth0 -j SNAT --to-source 10.0.0.201

[root@test-201 ~]# ping 223.5.5.5
PING 223.5.5.5 (223.5.5.5) 56(84) bytes of data.
64 bytes from 223.5.5.5: icmp_seq=1 ttl=128 time=5.88 ms
64 bytes from 223.5.5.5: icmp_seq=2 ttl=128 time=4.95 ms
^C
--- 223.5.5.5 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 4.953/5.417/5.882/0.470 ms
[root@test-201 ~]#  iptables -t nat -nL -v
Chain PREROUTING (policy ACCEPT 1 packets, 60 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    6   348 SNAT       all  --  *      eth0    172.16.1.10          0.0.0.0/0            to:10.0.0.201

iptables排除网段的某个ip_数据_07

2)实现外网主机可以访问内网服务
iptables -t nat -A PREROUTING  -d 10.0.0.201 -p tcp --dport 9000 -j DNAT --to-destination 172.16.1.10:3389
10.0.0.200 9000 === 172.16.1.10 3389

先开启远程端口(可用netstat命令查看端口)

本来不通

iptables排除网段的某个ip_IP_08

iptables排除网段的某个ip_安全_09

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

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.10          0.0.0.0/0            to:10.0.0.201
[root@test-201 ~]#  iptables -t nat -nL -v
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DNAT       tcp  --  *      *       0.0.0.0/0            10.0.0.201           tcp dpt:9000 to:172.16.1.10:3389

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

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

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  120  6799 SNAT       all  --  *      eth0    172.16.1.10          0.0.0.0/0            to:10.0.0.201

配置完成后:

iptables排除网段的某个ip_iptables排除网段的某个ip_10


iptables排除网段的某个ip_IP_11

Linux端远程登录也可以:

iptables排除网段的某个ip_linux_12


总结:将iptables命令参数进行熟悉

PS:防火墙配置完毕后,一定要保存配置
iptables-save >/etc/sysconfig/iptables

iptables排除网段的某个ip_IP_13