设为首页收藏本站language 语言切换
查看: 2999|回复: 7
收起左侧

[原创] 【红帽RHCSA/RHCE/RHCA--Linux专题】课外-2.使用iptables 测量流量

[复制链接]
发表于 2022-9-29 17:04:00 | 显示全部楼层 |阅读模式
今天给大家分享一个红帽Linux专题——使用iptables 测量流量
文末有原文链接,需要的可以下载哈!



iptables有什么用?以及为什么使用 iptables 而不是 firewalld
  • iptables 是红帽系 Linux 自带的软件,无需安装; *注: 其实 firewalld 也是基于 iptables
  • 使用场景是,用户需要了解2台主机/端口之间的流量有多少,比如渗透测试中,可以监控"某种操作"产生了多少流量,是否会引起对方察觉
  • 为什么使用 iptables,因为我还不太知道 firewalld 如何实现监控流量



实验步骤


先查看一下当前 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 -L
Chain INPUT (policy ACCEPT)
target      prot opt source       destination
traffic     all -- anywhere       anywhere
Chain FORWARD (policy ACCEPT)
target      prot opt source       destination
Chain OUTPUT (policy ACCEPT)
target      prot opt source       destination
traffic     all -- anywhere       anywhere
Chain 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 ~]#
游客,如果您要查看本帖隐藏内容请回复

发表于 2022-9-29 21:25:43 | 显示全部楼层
【红帽RHCSA/RHCE/RHCA--Linux专题】课外-2.使用iptables 测量流量
沙发 2022-9-29 21:25:43 回复 收起回复
回复 支持 反对

使用道具 举报

发表于 2022-9-30 00:54:28 | 显示全部楼层
谢谢分享            
板凳 2022-9-30 00:54:28 回复 收起回复
回复 支持 反对

使用道具 举报

发表于 2022-10-13 08:58:16 | 显示全部楼层
&#128077;
7# 2022-10-13 08:58:16 回复 收起回复
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-2-3 06:11 , Processed in 0.097128 second(s), 16 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表