Linux 安全 | 使用 iptables 测量流量

iptables 的作用及与 firewalld 的区别

1. iptables 是什么?有什么用?

iptables 是 Linux 系统上经典的防火墙工具,工作在网络层,主要功能包括:

  • 数据包过滤

    根据规则允许或阻止特定的网络流量(如 IP 地址、端口、协议等)

  • 流量转发

    控制数据包在不同网络接口间的转发

  • 网络地址转换(NAT)

    实现端口映射、IP 地址转换等功能

  • 流量监控与统计

    记录通过防火墙的数据包数量和字节数,这在网络调试、渗透测试等场景中非常有用

为什么使用 iptables 而不是 firewalld?

firewalld 是 RedHat 系 Linux 中较新的防火墙管理工具,它本质上是 iptables 的前端封装,提供了更友好的接口和动态更新机制。但在以下场景中,iptables 更具优势:

  • 精细化流量统计

    iptables 可以直接通过规则链记录特定 IP / 端口的流量数据,操作简单直接

  • 脚本化自动化

    iptables 命令更适合写入脚本,实现复杂的防火墙策略自动化配置

  • 兼容性

    所有 Linux 发行版都支持 iptables,而 firewalld 主要用于 RedHat 系

  • 学习曲线

    对于简单的过滤和监控需求,直接使用 iptables 命令比学习 firewalld 的区域(zone)概念更直观

了解更多查看:

【红帽Linux】RHEL 8 和 9 安全加固指南

如何使用 iptables 测量流量

实验步骤

首先查看一下当前 iptables 的状态,使用 -L 选项:

复制代码
[root@localhost ~]# 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

可以看到目前系统中的3个规则链,这里假设系统中已经有一些规则了,为了不破坏原有规则,我们需要自己创建一个专门用于测量流量的规则链,名字随意取,这里就叫"traffic"

使用命令 iptables -N 来创建一个新的规则链

复制代码
[root@localhost ~]# iptables -N traffic
[root@localhost ~]# 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
Chain traffic (0 references)
target       prot opt source      destination

接着在 traffic 这个链上添加2个规则,用于测量本机到 192.168.88.100 这台服务器之间的流量;

复制代码
[root@localhost ~]# iptables -A traffic -s 192.168.88.100
[root@localhost ~]# iptables -A traffic -d 192.168.88.100
[root@localhost ~]# 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
Chain traffic (0 references)
target     prot opt source      destination
           all -- 192.168.88.100 anywhere
           all -- anywhere 192.168.88.100
[root@localhost ~]#

接着,要将 traffic 链,接入(附加)到系统原先的"INPUT" 和 "OUTPUT" 规则链里,这样才能监控到流量

复制代码
[root@localhost ~]# iptables -A INPUT -j traffic
[root@localhost ~]# iptables -A OUTPUT -j traffic[root@localhost ~]# iptables -LChain INPUT (policy ACCEPT)target      prot opt source       destinationtraffic     all -- anywhere       anywhereChain FORWARD (policy ACCEPT)target      prot opt source       destinationChain OUTPUT (policy ACCEPT)target      prot opt source       destinationtraffic     all -- anywhere       anywhereChain traffic (2 references)target      prot opt source       destination            all -- 192.168.88.100 anywhere            all -- anywhere 192.168.88.100[root@localhost ~]#

使用 iptables -Z 将流量统计信息清零,一切就绪,就可以触发流量进行观察了

复制代码
[root@localhost ~]# iptables -Z

使用 iptables -L 规则链 -nvx 命令来查看与目标服务器之间传输了多少数据包以及字节; 建议配合 watch 命令进行持续监控

复制代码
[root@localhost ~]# watch -d -n1 iptables -L traffic -nvx
Every 1.0s: iptables -L traffic -nvx                                               Wed May 11 14:01:57 2022
Chain traffic (2 references)
    pkts     bytes target    prot opt in     out     source            destination
      17     708             all -- *        *       192.168.88.100    0.0.0.0/0
     364     15940           all -- *        *       0.0.0.0/0         192.168.88.100

可以看到,iptables 会记录你发送出去了多少包,以及流量大小; 也能记录对方发给你的流量

监控结束后,还原 iptables

1.首先清空系统原先的"INPUT" 和 "OUTPUT" 规则链里面的 "traffic"; 但首先要查看下该规则在链下的编号(也就是第几行)

删除规则的命令格式: iptables -D INPUT 规则编号

复制代码
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target      prot opt source              destination
traffic     all -- anywhere              anywhere # <-- 这就是第一条规则,在第1行,所以编号是 1
Chain FORWARD (policy ACCEPT)
target      prot opt source              destination
Chain OUTPUT (policy ACCEPT)
target      prot opt source              destination
traffic     all -- anywhere              anywhere # <-- 该链下的第一条规则,编号1
Chain traffic (2 references)
target      prot opt source              destination
            all -- 192.168.88.100        anywhere
            all -- anywhere              192.168.88.100

删除 INPUT OUTPUT 的"traffic"规则

复制代码
[root@localhost ~]# iptables -D INPUT 1
[root@localhost ~]# iptables -D OUTPUT 1
# 再次验证,发现规则链下的规则被删除了
[root@localhost ~]# 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
Chain traffic (0 references)
target      prot opt source         destination
            all -- 192.168.88.100 anywhere
            all -- anywhere 192.168.88.100

2.清理 traffic 链里面的规则,使用 iptables -F 选项

复制代码
[root@localhost ~]# iptables -F traffic
[root@localhost ~]# 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
Chain traffic (0 references)
target        prot opt source         destination
[root@localhost ~]#

3.删除 traffic 链,只剩下系统原先的3个规则链

复制代码
[root@localhost ~]# iptables -X traffic
[root@localhost ~]# 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
[root@localhost ~]#
相关推荐
通信瓦工2 小时前
IEC 60598-1-2020灯具通用安全要求标准介绍
安全·灯具·标准下载
浅拾光º2 小时前
mysql字符串截取,如何在MySQL备份文件中安全截取敏感字符串?
数据库·mysql·安全
挺6的还2 小时前
42.传输层协议TCP(一)
linux
踏过山河,踏过海2 小时前
在SSL证书是有效的前提下,依旧显示“资源不安全
网络协议·安全·ssl
阿方索3 小时前
DHCP 服务器
linux·运维
春夜喜雨3 小时前
linux下如何检查与设置程序与服务崩溃时生成coredump文件--包括systemctl启动的服务
linux
小狗爱吃黄桃罐头3 小时前
正点原子【第四期】Linux之驱动开发学习笔记-6.1 pinctrl和gpio子系统
linux·驱动开发·学习
小心草里有鬼3 小时前
Linux 数据库 Mysql8 主从复制
linux·运维·数据库·sql·mysql
czhc11400756633 小时前
Linux925 shell 变量:本地、环境变量、全局变量;数组:普通数组、关联数组;交互定义、basename、dirname
linux·交互