当前位置: 代码迷 >> 综合 >> thrift协议抓包解析(tcpdump+wireshark or thrift-tool)
  详细解决方案

thrift协议抓包解析(tcpdump+wireshark or thrift-tool)

热度:77   发布时间:2024-03-08 16:06:40.0

目前thrift使用较多,所以我们可能会遇到线上查case或者想拉取一些具体请求的场景,这种需求下如果没有提前打日志就只能通过抓包来分析了。

一、tcpdump+wireshark

抓包第一反应就是通过tcpdump命令来抓取,其中比较常用的命令就是

sudo tcpdump -i any -Xvv dst port 11311 and tcp

这种情况下的包通常都是二进制的格式,通过-Xvv也就能展现成这个样子:
在这里插入图片描述
当然我们还可以通过-w 命令变成一个dump包,这时候可以下载下来用wireshark打开文件进行分析:

sudo tcpdump -i any -Xvv dst port 11311 and tcp -w dump.pcap

在这里插入图片描述
其中wireshark自己就识别了thrift协议并进行了接口的解析以及一定的展示,但是效果还是不够好。

二、开源工具thrift-tools。

(1)下载地址:thrift-tools
(2)安装方法:

  • 直接运行命令:sudo pip install thrift-tools
  • 下载源码进行编译
$ git clone ...
$ cd thrift-tools
$ sudo FROM_SOURCE=1 bin/thrift-tool --iface=eth0 --port 9091 dump --show-all --pretty
...
$ FROM_SOURCE=1 bin/thrift-tool --port 9090 \--pcap-file thrift_tools/tests/resources/calc-service-binary.pcap \dump --show-all --pretty --color \--idl-file=thrift_tools/tests/resources/tutorial.thrift

源码的方式总是报缺少依赖,我是通过pip直接安装的,但是现在看依赖应该是下面这些:

ansicolors==1.1.8
dpkt==1.9.2
meld3==0.6.8
ply==3.11
ptsd==0.2.0
scapy==2.4.2
six==1.15.0
supervisor==4.0.4
tabulate==0.8.7
thrift==0.11.0
thrift-tools==0.0.6

(3)安装好之后执行命令

thrift-tool --iface eth0 --port 11311 dump --show-all --pretty

但是发现报错了,错误信息如下所示:

Error: tcpdump is not available. Cannot use filter !: Traceback (most recent call last):File "/usr/local/lib/python2.7/site-packages/thrift_tools/sniffer.py", line 215, in runsniff(**kwargs)File "/usr/local/lib/python2.7/site-packages/scapy/sendrecv.py", line 836, in sniff*arg, **karg)] = ifaceFile "/usr/local/lib/python2.7/site-packages/scapy/arch/linux.py", line 454, in __init__attach_filter(self.ins, filter, iface)File "/usr/local/lib/python2.7/site-packages/scapy/arch/linux.py", line 140, in attach_filterbp = compile_filter(bpf_filter, iface)File "/usr/local/lib/python2.7/site-packages/scapy/arch/common.py", line 126, in compile_filterraise Scapy_Exception("tcpdump is not available. Cannot use filter !")
Scapy_Exception: tcpdump is not available. Cannot use filter !(device: any)
Exception IOError: (19, 'No such device') in <bound method L2ListenSocket.__del__ of <scapy.arch.linux.L2ListenSocket object at 0x7f61ecf2fa50>> ignored

看这个错误加上网上找了一下解决方案,发现就是tcpdump有问题,比如使用tcpdump --version时,会报出
tcpdump: unrecognized option '--version'
正常的应该是这样的:
在这里插入图片描述
所以解决方法就是重新安装一下tcpdump即可:

yum install tcpdump

成功之后,运行上面的监听命令可以得到如下的结果:
在这里插入图片描述

三、tcpdump+thrift-tools

  相关解决方案