iptstate介绍

iptstate简介

iptstate 是一个在Ubuntu等Linux系统上运行的开源工具,全称是 IP Tables State。它设计用于实时显示Linux内核中Netfilter框架的连接跟踪表(state table),以类似于 top 命令的界面呈现。Netfilter是Linux内核中用于处理网络数据包的框架,而iptstate的主要作用是帮助用户监控和管理通过iptables(Netfilter的用户空间工具)建立的网络连接状态。

该工具最初由Phil Dibowitz开发,旨在为iptables实现类似于IP Filter中的“state top”功能。它可以展示当前网络连接的状态(如ESTABLISHED、NEW等)、源IP、目标IP、端口、服务类型等信息,并支持动态排序、过滤和颜色编码等功能。iptstate特别适合网络管理员或安全研究人员,用于实时观察网络流量或排查问题。


主要特点

  1. 实时监控:以动态更新的方式显示连接状态,类似于 top 的界面。
  2. 可排序输出:支持按任意字段(如源IP、目标IP、端口等)排序,且排序方向可逆。
  3. 过滤功能:可以根据源IP、目标IP或端口过滤显示内容。
  4. 服务解析:支持将IP地址解析为主机名(需启用DNS查找)。
  5. 颜色编码:通过颜色区分不同的协议或状态,便于快速识别。
  6. 单次输出模式:除了实时监控,还可以选择仅输出一次状态表后退出。
  7. 自定义刷新率:用户可以调整界面刷新频率。

在Ubuntu上安装iptstate

在Ubuntu系统中,可以通过以下步骤安装iptstate:

1
2
sudo apt update
sudo apt install iptstate
  • 依赖:安装前需确保内核启用了Netfilter的CONNTRACK模块(大多数现代Ubuntu内核默认启用)。
  • 权限:运行iptstate通常需要root权限,因为它需要访问内核的连接跟踪表。
root@vgt-ubuntu-10:~# iptstate  --help
loop: 104
IPTables State Top Version 2.2.6
Usage: iptstate [<options>]

  -c, --no-color
        Toggle color-code by protocol

  -C, --counters
        Toggle display of bytes/packets counters

  -d, --dst-filter <IP>
        Only show states with a destination of <IP>
        Note, that this must be an IP, hostname matching is not yet supported.

  -D --dstpt-filter <port>
        Only show states with a destination port of <port>

  -h, --help
        This help message

  -l, --lookup
        Show hostnames instead of IP addresses. Enabling this will also enable
        -L to prevent an ever-growing number of DNS requests.

  -m, --mark-truncated
        Mark truncated hostnames with a '+'

  -o, --no-dynamic
        Toggle dynamic formatting

  -L, --no-dns
        Skip outgoing DNS lookup states

  -f, --no-loopback
        Filter states on loopback

  -p, --no-scroll
        No scrolling (don't use a "pad")

  -r, --reverse
        Reverse sort order

  -R, --rate <seconds>
        Refresh rate, followed by rate in seconds
        Note: For statetop, not applicable for -s

  -1, --single
        Single run (no curses)

  -b, --sort <column>
        This determines what column to sort by. Options:
          d: Destination IP (or Name)
          p: Protocol
          s: State
          t: TTL
          b: Bytes
          P: Packets
        To sort by Source IP (or Name), don't use -b.
        Note that bytes/packets are only available when supported in the kernel,
        and enabled with -C

  -s, --src-filter <IP>
        Only show states with a source of <IP>
        Note, that this must be an IP, hostname matching is not yet supported.

  -S, --srcpt-filter <port>
        Only show states with a source port of <port>

  -t, --totals
        Toggle display of totals

See man iptstate(8) or the interactive help for more information.

使用示例

1. 基本实时监控

1
sudo iptstate
  • 说明:启动iptstate,进入实时监控模式,默认按源IP排序。
  • 界面说明
    • 显示源IP、目标IP、协议、端口、状态等。
    • 使用 h 查看帮助,q 退出。

2. 单次输出状态表

1
sudo iptstate -1
  • 说明:仅输出当前连接状态表一次,然后退出。
  • 输出示例
    Source Destination Proto State TTL
    192.168.1.100:22 10.0.0.1:54321 tcp ESTABLISHED 43199
    192.168.1.101:80 172.16.0.2:12345 tcp TIME_WAIT 119

3. 按目标IP过滤

1
sudo iptstate -d 192.168.1.100
  • 说明:只显示目标IP为192.168.1.100的连接。

4. 启用主机名解析

1
sudo iptstate -l
  • 说明:将IP地址解析为主机名显示(可能增加DNS查询开销)。

5. 调整刷新率

1
sudo iptstate -R 2
  • 说明:设置刷新率为每2秒更新一次。

交互式操作

在实时监控模式下,iptstate支持以下快捷键:

  • b:切换排序字段。
  • s:设置源IP过滤。
  • d:设置目标IP过滤。
  • t:切换显示总数。
  • q:退出程序。
  • 空格:立即刷新。

注意事项

  • 权限要求:需要root或CAP_NET_ADMIN权限才能访问连接跟踪表。
  • 内核支持:确保内核编译时启用了CONFIG_NF_CONNTRACK(Ubuntu默认支持)。
  • 局限性:不支持基于主机名的过滤,仅支持IP地址;IPv6支持可能不够完善。

适用场景

  • 网络调试:快速查看哪些连接正在通过防火墙。
  • 安全审计:监控可疑的网络活动。
  • 性能优化:分析连接状态以优化iptables规则。