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 链表结构关系图

4. iptables匹配流程

4.1 规则表之间的顺序:

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

4.2 规则链之间的顺序:

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

4.3 规则链内的匹配顺序

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

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

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

5.5.2 REJECT

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

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

小结:由此可以看出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: 修改替换规则

6.6 设置默认规则

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

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 常用管理选项汇总表

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

8.1 协议匹配: -p 协议名


上图为:除了icmp协议,其他都丢弃

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

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

9. 项目实战

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

9.1 部署环境

主机名 主机IP(1) 主机IP(2) 网卡模式(1) 网卡模式(2) 主机网关
iptables01 192.168.200.99 NET8 192.168.200.100
iptables02 192.168.200.100 192.168.100.100 NET8 NET1(仅主机)
iptables03 192.168.100.110 NET8 192.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

转载于:https://www.cnblogs.com/ywb123/p/11219633.html

iptables 防火墙(上)相关推荐

  1. Linux上iptables防火墙的基本应用教程

    iptables是Linux上常用的防火墙软件,下面vps侦探给大家说一下iptables的安装.清除iptables规则.iptables只开放指定端口.iptables屏蔽指定ip.ip段及解封. ...

  2. (转载)Linux上iptables防火墙的基本应用教程

    (转载)http://www.vpser.net/security/linux-iptables.html iptables是Linux上常用的防火墙软件,下面vps侦探给大家说一下iptables的 ...

  3. 安全的Web主机iptables防火墙脚本

    下面以自己的Web服务器举例说明之,系统的默认策略是INPUT为DROP,OUTPUT.FORWARD链为ACCEPT,DROP设置得比较宽松,因为我们知道出去的数据包比较安全:为了验证脚本的通用性, ...

  4. 快速入门linux系统的iptables防火墙 1 本机与外界的基本通信管理

    概述 iptables是一种运行在linux下的防火墙组件,下面的介绍可以快速的学习iptables的入门使用. 特点(重要) 它的工作逻辑分为 链.表.规则三层结构. 数据包通过的时候,在对应表中, ...

  5. Iptables防火墙详细介绍与实战增强服务器安全

    Iptables防火墙详细介绍与实战增强服务器安全 一:Iptables的概述及应用 iptables概述: netfilter/iptables : IP信息包过滤系统,它实际上由两个组件netfi ...

  6. centos6.5下系统编译定制iptables防火墙扩展layer7应用层访问控制功能及应用限制QQ2016上网...

    iptables防火墙扩展之layer7应用层访问控制 概述: iptables防火墙是工作在网络层,针对TCP/IP数据包实施过滤和限制,属于典型的包过滤防火墙.以基于网络层的数据包过滤机制为主,同 ...

  7. Iptables防火墙配置详解

    iptables防火墙配置详解 iptables简介 iptables是基于内核的防火墙,功能非常强大,iptables内置了filter,nat和mangle三张表. (1)filter表负责过滤数 ...

  8. iptables 防火墙设置

    1.安装iptables防火墙 怎么知道系统是否安装了iptables?执行iptables -V,如果显示如:  iptables v1.3.5  说明已经安装了iptables.  如果没有安装i ...

  9. Linux iptables防火墙设置与NAT服务配置

    Linux iptables防火墙设置与NAT服务配置 - 摘要: linux教程,NAT服务器,iptables防火墙设置与NAT服务配置, 防火墙是指设置在不同网络或网络安全域之间的一系列部件的组 ...

最新文章

  1. i++ 和++i的区别
  2. .exp文件_mini_httpd 任意文件读取漏洞(附EXP脚本)
  3. Computer:现代计算机操作系统的四大基本特性(并发/共享/虚拟/异步)
  4. POJ-1655 Balancing Act 树的重心
  5. 编写第一个HADOOP应用程序
  6. 如何免费申请用于开发目的的Hybris Commerce license
  7. impdp导入表结构和表数据_ORACLE数据库如何用datapump工具导出表结构然后导入到其它Schema下面...
  8. ubuntu下安装jdk
  9. 深入理解傅里叶变换的性质:实函数、卷积、相关、功率谱、频响函数
  10. vue 中 Excel 的导入导出
  11. Kubernetes Dashboard on Ubuntu 16.04安装记录
  12. Netty工作笔记0008---NIO的Buffer的机制及子类
  13. 山东省大学计算机科学与技术,我校计算机科学与技术专业获山东省大学最佳专业排行榜第一名...
  14. MongoDB副本集学习(三):性能和优化相关
  15. 均匀权重向量集合的生成
  16. 玩转树莓派——游戏主机模拟器
  17. 惠普T620瘦客户机安装Win7时AMD显卡驱动造成Aero透明效果不能使用
  18. 用SET工具包制作钓鱼网站
  19. 网上流行护眼色的RGB值和颜色代码汇总
  20. 16位深度图像转8位灰度

热门文章

  1. python全栈开发要学些什么_如何迅速学习Python 全栈开发?
  2. 每天一道LeetCode-----实现LRU置换算法
  3. 学习笔记-----C++模板类中友元函数重载输出运算符时提示无法解析的外部符号解决方案
  4. VC如何在单文档里显示对话框
  5. MFC改变static text颜色
  6. HDU Problem - 5113 Black And White(搜索剪枝)
  7. ubuntu 18.04安装jdk8和eclipse
  8. Leetcode题库 744.寻找比目标字母大的最小字母(C实现)
  9. [BUUCTF-pwn]——test_your_nc
  10. 手把手教你安装VMtools