iptables 防火墙(上)

iptables 防火墙(上)

1. 防火墙概述

1.1 概念与作用

网络中的防火墙是一种将内部网络和外部网络分开的方法,是一种隔离技术。防火墙在内网与外网通信时进行访问控制,依据所设置的规则对数据包作出判断,最大限度地阻止网络中的黑客破坏企业网络,从而加强企业网络安全。

1.2 防火墙分类

1.2.1 硬件防火墙

如思科的ASA防火墙,H3C的Sepath防火墙等。

1.2.2 软件防火墙

如iptables等。

按架设的位置,可以分为主机防火墙,网关防火墙

1.3 iptables防火墙

Linux操作系统中默认内置一个软件防火墙, 即iptables防火墙

1.3.1 netfilter

位于Linux内核中的包过滤功能体系,称为Linux防火墙的“内核态”

1.3.2 iptables

位于/sbin/iptables,用来管理防火墙规则的工具,称为Linux防火墙的“用户态”

1.4 包过滤的工作层次

主要是网络层,针对IP数据包,体现在对包内的IP地址,端口等信息的处理上。

2. iptables规则链

2.1 规则链

  • 规则的作用:对数据包进行过滤或处理
  • 链的作用:容纳各种防火墙规则
  • 链的分类依据:处理数据包的不同时机

2.2 默认包括5种规则链

  • INPUT:处理入站数据包
  • OUTPUT:处理出站数据包
  • FORWARD:处理转发数据包
  • POSTROUTING:在进行路由选择后处理数据包
  • PREROUTING:在进行路由选择前处理数据包

3. iptables规则表

3.1 规则表

  • 表的作用: 容纳各种规则链
  • 表的划分依据:防火墙规则的动作相似

3.2 默认包括4个规则表

  • raw表:确定是否对该数据包进行状态跟踪
  • mangle表:为数据包设置标记
  • nat表:修改数据包中的源、目标IP地址或端口
  • filter表:确定是否被放行该数据包(过滤)

3.3 链表结构关系图

image_1cn8i89ef1vq7sci1vnmdr0kb59.png-380kB

4. iptables匹配流程

4.1 规则表之间的顺序:

raw--->mangle--->nat--->filter

4.2 规则链之间的顺序:

  • 入站: PREROUTING--->INPUT
  • 出站: OUTPUT--->POSTROUTING
  • 转发: PREROUTING--->FORWARD--->POSTROUTING

4.3 规则链内的匹配顺序

  • 按顺序依次检查,匹配即停止(LOG 策略例外)
  • 若找不到相匹配规则,按该链的默认策略处理

image_1cn8ikktp1b87113a4961pm1e0jm.png-300.1kB

5. iptables命令

5.1 语法构成

iptables[-t表名]选项[链名] [条件] [j控制类型]


注意事项:

  • 不指定表名时,默认指filter 表
  • 不指定链名时,默认指表内的所有链
  • 除非设置链的默认策略,否则必须指定匹配条件
  • 选项、链名、控制类型使用大写字母,其余均为小写

5.2 数据包的常见控制类型

  • ACCEPT: 允许通过
  • DROP: 直接丢弃,不给出任何回应
  • REJECT: 拒绝通过,必要时会给出提示
  • LOG: 记录日志信息,然后传给下一条规则继续匹配

5.3 命令简介

5.3.1 查看规则
[root@iptables01 /]# iptables -L -nv      #查看规则(默认查看filter表)
Chain INPUT (policy ACCEPT 97 packets, 7567 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 59 packets, 6728 bytes)
 pkts bytes target     prot opt in     out     source               destination
[root@iptables01 /]# iptables -t nat -L   #指定查看nat表
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
5.3.2 清空规则
[root@iptables01 /]# iptables -F   #清空规则
[root@iptables01 /]# service iptables stop   #清空的更彻底

5.4 项目小实战(一)

项目要求:查出xshell的连接链

5.4.1 已知现表规则
[root@iptables01 ~]# iptables -L
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
5.4.2 禁掉转发链FORWARD
[root@iptables01 /]# iptables -P FORWARD DROP
[root@iptables01 /]# 
[root@iptables01 /]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

xshell依据存活,说明它的连接跟FORWARD没关系

5.4.3 禁掉进站链INPUT(或出站链OUTPUT)
[root@iptables01 /]# iptables -P INPUT DROP
[root@iptables01 /]# 
Socket error Event: 32 Error: 10053.
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(iptables01) at 15:26:29.

Type `help' to learn how to use Xshell prompt.
[C:\~]$ 
Reconnecting in 3 seconds. Press any key to exit local shell.
...

xshell掉了,说明它的连接直接与进站链(出站链)有关

  • 重启iptables即可恢复连接

5.5 命令演练

5.5.1 DROP
[root@iptables01 ~]# iptables -I INPUT -p icmp -j DROP   #禁掉ping的本机IP

image_1cn8vpnsi13gp1tl21cp0gg81g5n9.png-12.2kB

[root@iptables01 ~]# iptables -F    #清空链规则
#清空规则后,本机IP又可以ping的通了

image_1cn91j8s6hmi1pm0398kq1ui856.png-8.9kB

5.5.2 REJECT
[root@iptables01 ~]# iptables -I INPUT -p icmp -j REJECT   #禁掉ping的本机IP(但是有提示)

image_1cn91rtl72av7oc1q19hkm162i5j.png-11.4kB

[root@iptables01 ~]# iptables -F    #清空链规则
#清空规则后,本机IP又可以ping的通了

image_1cn91shi517d6npjitn1b6i2bu60.png-8.6kB

小结:由此可以看出DROP与REJECT的区别,DROP无回复,REJECT拒绝,但有回复

6. 常用选项

6.1 增加新的规则

  • A:在链的末尾追加一条规则
  • I:在链的开头(或指定序号)插入一条规则

6.2 实战演练

[root@iptables01 ~]# iptables -A INPUT -p icmp -j ACCEPT
[root@iptables01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     icmp --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@iptables01 ~]# iptables -I INPUT -p tcp -j ACCEPT
[root@iptables01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere     #在这里   
ACCEPT     icmp --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@iptables01 ~]# iptables -I INPUT 2 -p udp -j ACCEPT
[root@iptables01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     udp  --  anywhere             anywhere     #在这里      
ACCEPT     icmp --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

6.3 查看规则列表

  • L: 列出所有的规则条目
  • n: 以数字形式显示地址、端口等信息
  • V: 以更详细的方式显示规则信息
  • line-numbers: 查看规则时,显示规则的序号。-line 与之同效

6.4 删除、清空规则

  • D: 删除链内指定序号(或内容)的一条规则
  • F: 清空所有的规则
[root@iptables01 ~]# iptables -D INPUT 3
[root@iptables01 ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            
ACCEPT     udp  --  anywhere             anywhere            

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

6.5 修改、替换规则

R: 修改替换规则

image_1cn93oh3756dfu87d91haahl90.png-27.2kB

6.6 设置默认规则

P:为指定的链设置默认规则

image_1cn94294nfa21nsq11rjaar4mc9q.png-23.2kB

image_1cn94ct76um66i5ibcogg15ojan.png-14.7kB

6.7 项目小实战(二)

项目要求:在三条链都DROP的情况下,如可保证xshell的正常连接

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

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
[root@iptables01 ~]# iptables -I INPUT -p tcp --dport 22 -j ACCEPT
#设置tcp协议22端口
[root@iptables01 ~]# iptables -P INPUT DROP
[root@iptables01 ~]# 
[root@iptables01 ~]#    #xshell正常工作
[root@iptables01 ~]#
[root@iptables01 ~]# iptables -L
Chain INPUT (policy DROP)
target     prot opt source               destination         
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ssh 

Chain FORWARD (policy DROP)
target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

需要注意的是:若要设置filter 表中INPUT链或者OUTPUT链的默认规则为DROP时,要先设置tcp协议22端口(ssh 远程连接)为ACCEPT,否则通过远程操控的主机将断开连接,若在真实生产环境中,需要到服务器所在机房重新设置才可以,造成不必要的麻烦。

7. 规则的匹配类型

7.1 通用匹配

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

7.2 隐含匹配

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

7.3 显式匹配

  • 要求以“-m扩展模块”的形式明确指出类型
  • 包括多端口、MAC地址、IP 范围、数据包状态等条件

7.4 常用管理选项汇总表

image_1cn9543dr1hrg1ng01oab1ktqmild4.png-85.9kB

8. 常见的通用匹配条件:

8.1 协议匹配: -p 协议名

image_1cn95f4ooouk4du1boq193qvo6dh.png-23.3kB
上图为:除了icmp协议,其他都丢弃

8.2 地址匹配:-s 源地址,-d 目的地址

image_1cn95q5rulmdvphqqs1cnj1ej1ee.png-13.7kB

image_1cn9605j51k6c1vv11u4c187q4hrk2.png-15.1kB

8.3 接口匹配: -i入站网卡、-0出站网卡

image_1cn967fkucsv1nrj1iga41j7s0m2.png-18.1kB

image_1cn96ad7vpav15op98fc041gvamv.png-19.8kB

9. 项目实战

项目要求:三台主机,要求其中两台主机可以在不同网段下互ping,其中一台模拟网关转换

9.1 部署环境

主机名主机IP(1)主机IP(2)网卡模式(1)网卡模式(2)主机网关
iptables01192.168.200.99NET8192.168.200.100
iptables02192.168.200.100192.168.100.100NET8NET1(仅主机)
iptables03192.168.100.110NET8192.168.100.100

9.2 部署网卡配置文件

9.2.1 iptables01的网卡配置文件
[root@iptables01 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@iptables01 network-scripts]# cat ifcfg-eth0 
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.200.99     #本主机的IP
NETMASK=255.255.255.0
GATEWAY=192.168.200.100   #本主机的网关,iptables02第一个网卡的IP
9.2.2 iptables02的网卡配置文件
[root@iptables02 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@iptables02 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.200.100   #本主机的IP,iptables01的网关
NETMASK=255.255.255.0


[root@iptables02 network-scripts]# cat ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.100.100   #本主机的IP,iptables03的网关
NETMASK=255.255.255.0
9.2.3 iptables03的网卡配置文件
[root@iptables03 network-scripts]# pwd
/etc/sysconfig/network-scripts
[root@iptables03 network-scripts]# cat ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.100.110   #本主机的IP
NETMASK=255.255.255.0
GATEWAY=192.168.100.100  #本主机的网关,iptables02第二个网卡的IP

9.3 修改iptables02转发的配置

[root@iptables02 /]# sed -n '7p' /etc/sysctl.conf 
net.ipv4.ip_forward = 1  #修改成1

9.4 实验如下

[root@iptables01 /]# ping 192.168.100.110
PING 192.168.100.110 (192.168.100.110) 56(84) bytes of data.
64 bytes from 192.168.100.110: icmp_seq=1 ttl=63 time=62.5 ms
64 bytes from 192.168.100.110: icmp_seq=2 ttl=63 time=0.851 ms
64 bytes from 192.168.100.110: icmp_seq=3 ttl=63 time=0.935 ms
^C
--- 192.168.100.110 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2585ms
rtt min/avg/max/mdev = 0.851/21.430/62.504/29.043 ms
[root@iptables03 /]# ping 192.168.200.99
PING 192.168.200.99 (192.168.200.99) 56(84) bytes of data.
64 bytes from 192.168.200.99: icmp_seq=1 ttl=63 time=0.473 ms
64 bytes from 192.168.200.99: icmp_seq=2 ttl=63 time=2.37 ms
64 bytes from 192.168.200.99: icmp_seq=3 ttl=63 time=0.880 ms
^C
--- 192.168.200.99 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2316ms
rtt min/avg/max/mdev = 0.473/1.242/2.373/0.816 ms
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值