TCPflow是一个免费、开源、功能强大的基于命令行的工具,用于分析 Linux 等类 Unix 系统上的网络流量。它捕获通过 TCP 连接接收或传输的数据,并将其存储在文件中以供以后分析,并采用允许协议分析和调试的有用格式。
另请阅读:16 个用于分析 Linux 网络使用情况的最佳带宽监控工具
它实际上是一个类似 tcpdump 的工具,因为它处理来自线路或存储文件的数据包。它支持与其对应产品相同的强大过滤表达式。唯一的区别是 tcpflow 将所有 TCP 数据包按顺序排列,并将每个流组装在一个单独的文件中(每个流方向一个文件)以供以后分析。
其功能集包括一个高级插件系统,用于解压缩压缩的 HTTP 连接、撤消 MIME 编码或调用第三方程序进行后处理等。
tcpflow 有许多用例,包括了解网络数据包流,还支持执行网络取证和泄露 HTTP 会话的内容。
如何在 Linux 系统中安装 TCPflow
TCPflow在主流 GNU/Linux 发行版的官方存储库中可用,您可以使用包管理器安装它,如图所示。
$ sudo apt install tcpflow #Debian/Ubuntu $ sudo yum install tcpflow #CentOS/RHEL $ sudo dnf install tcpflow #Fedora 22+
安装tcpflow后,可以使用超级用户权限运行它,否则使用sudo命令。请注意,它侦听活动网络接口(例如enp0s3)。
$ sudo tcpflow tcpflow:监听 enp0s3
默认情况下 tcpflow 将所有捕获的数据存储在具有表单名称的文件中(如果您使用某些选项,例如timestamp ,这可能会有所不同)。
sourceip.sourceport-destip.destport 192.168.043.031.52920-216.058.210.034.00443
现在让我们列出目录以查看是否在任何文件中捕获了 tcp 流。
$ ls -1 总计 20 -rw-r--r--。1根根808 9月19日12:49 192.168.043.031.52920-216.058.210.034.00443 -rw-r--r--。1 根 59 9 月 19 日 12:49 216.058.210.034.00443-192.168.043.031.52920
正如我们之前提到的,每个 TCP 流都存储在自己的文件中。从上面的输出中,您可以看到有三个转录文件,它们指示两个相反方向的 tcpflow,其中第一个文件中的源 IP 和第二个文件中的目标 IP,反之亦然。
The first file 192.168.043.031.52920-216.058.210.034.00443 contains data transfered from host 192.168.043.031 (the localhost on which tcpflow was run) via port 52920, to host 216.058.210.034 (the remote host) via port 443.
And the second file 216.058.210.034.00443-192.168.043.031.52920 contains data sent from host 216.058.210.034 (the remote host) via port 443 to host 192.168.043.031 (the localhost on which tcpflow was run) via port 52920.
There is also an XML report generated, which contains information about the program such as how it was compiled, and the computer it was run on and a record of every tcp connection.
As you may have noticed, tcpflow stores the transcript files in the current directory by default. The -o
option can help you specify the output directory where the transcript files will be written.
$ sudo tcpflow -o tcpflow_files $ sudo ls -l tcpflow_files total 32 -rw-r--r--. 1 root root 1665 Sep 19 12:56 157.240.016.035.00443-192.168.000.103.45986 -rw-r--r--. 1 root root 45 Sep 19 12:56 169.044.082.101.00443-192.168.000.103.55496 -rw-r--r--. 1 root root 2738 Sep 19 12:56 172.217.166.046.00443-192.168.000.103.39954 -rw-r--r--. 1 root root 68 Sep 19 12:56 192.168.000.102.00022-192.168.000.103.42436 -rw-r--r--. 1 root root 573 Sep 19 12:56 192.168.000.103.39954-172.217.166.046.00443 -rw-r--r--. 1 root root 4067 Sep 19 12:56 192.168.000.103.45986-157.240.016.035.00443 -rw-r--r--. 1 root root 38 Sep 19 12:56 192.168.000.103.55496-169.044.082.101.00443 -rw-r--r--. 1 root root 3159 Sep 19 12:56 report.xml
You can also print the contents of packets to stdout as they are received, without storing any captured data to files, using the -c
flag as follows.
To test this effectively, open a second terminal and run a ping, or browse the internet. You should be able to see the ping details or your browsing details being captured by tcpflow.
$ sudo tcpflow -c
It is possible to capture all traffic on a particular port, for example port 80 (HTTP). In the case of HTTP traffic, you will be able to see the HTTP Headers followed by the content all on the stdout or in one file if the -c
switch is removed.
$ sudo tcpflow port 80
To capture packets from a specific network interface, use the -i
flag to specify the interface name.
$ sudo tcpflow -i eth0 port 80
You can also specify a target host (accepted values are IP address, hostname or domains), as shown.
$ sudo tcpflow -c host 192.68.43.1 OR $ sudo tcpflow -c host www.google.com
您可以使用带有该-a
标志的所有扫描仪启用所有处理,这相当于-e
all 开关。
$ sudo tcpflow -a 或者 $ sudo tcpflow -e 全部
还可以激活特定的扫描仪;可用的扫描仪包括 md5、http、netviz、tcpdemux 和 wifiviz(运行tcpflow -H查看有关每个扫描仪的详细信息)。
$ sudo tcpflow -e http 或者 $ sudo tcpflow -e md5 或者 $ sudo tcpflow -e netviz 或者 $ sudo tcpflow -e tcpdemux 或者 $ sudo tcpflow -e wifiviz
以下示例显示如何启用除 tcpdemux 之外的所有扫描仪。
$ sudo tcpflow -a -x tcpdemux
TCPflow 通常会在捕获数据包之前尝试将网络接口置于混杂模式。您可以使用-p
如图所示的标志来防止这种情况。
$ sudo tcpflow -p -i eth0
要从 tcpdump pcap 文件读取数据包,请使用该-r
标志。
$ sudo tcpflow -f 文件.pcap
您可以使用-v
或-d 10
选项启用详细模式。
$ sudo tcpflow -v 或者 $ sudo tcpflow -d 10
重要提示: tcpflow的一个限制是,目前它不理解 IP 片段,因此作为包含 IP 片段的 TCP 连接的一部分传输的数据将无法正确捕获。
有关更多信息和使用选项,请参阅tcpflow手册页。
$ 人 TCP 流
TCPflow Github 存储库:https://github.com/simsong/tcpflow
目前为止就这样了!TCPflow是一个功能强大的 TCP 流记录器,对于理解网络数据包流和执行网络取证等非常有用。尝试一下并在评论中与我们分享您的想法。