使用 tshark 在 apache 服务器上捕获传入的 RESTAPI 流量

capturing incoming RESTAPI traffic on apache server using tshark

是否可以将传入的 REST API 请求捕获到 tomcat 服务器,以验证外部客户端是否使用了正确的凭据。产生了 401 响应,但我们需要证明 REST API 不是问题,而是请求。

我成功安装了 wireshark,并根据建议使用 tshark 尝试捕获传入的数据包。

tshark -D
1. usbmon1 (USB bus number 1)
2. eth2
3. any (Pseudo-device that captures on all interfaces)
4. lo

我假设 http 请求是 'tcp'?正确的?那为什么这里不显示呢?我尝试了以下在网上找到的命令:

tshark 'tcp port 80 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)' -R 'http.request.method == "GET" || http.request.method == "HEAD"'

但这会导致错误;

That string isn't a valid capture filter (USB link-layer type filtering 
not implemented).
See the User's Guide for a description of the capture filter syntax.
0 packets captured

我知道具体的传入请求 url 我期待并认为我可以用它进行过滤 https://xxxxxxxxxxxxxxxx/termAPI/list 非常感谢您的帮助。

编辑:

尝试并测试了以下内容:

tshark -i 2 -f 'port 80'

然后 运行 一个示例 API 请求并捕获了以下内容:

Capturing on eth2
0.000000000 192.1xx.xxx -> 192.168.cc.xxxx TCP 66 49330 > http [SYN] 
Seq=0   Win=8192 Len=0 MSS=1400 WS=4 SACK_PERM=1
0.000146849 192.168.cc.xxxx -> 192.1xx.xxx TCP 66 http > 49330 [SYN, ACK] 
Seq=0 Ack=1 Win=14100 Len=0 MSS=1410 SACK_PERM=1 WS=128
0.005808528 192.1xx.xxx -> 192.168.cc.xxxx TCP 54 49330 > http [ACK] 
Seq=1 Ack=1 Win=65800 Len=0
0.031745954 192.1xx.xxx -> 192.168.cc.xxxx HTTP 220 
GET  /termsapi/google/search/main/rules/active HTTP/1.1
0.031845414 192.168.cc.xxxx -> 192.1xx.xxx TCP 54 http > 49330 [ACK] Seq=1 
Ack=167 Win=15232 Len=0
0.063554179 192.168.cc.xxxx -> 192.1xx.xxx TCP 2854 [TCP segment of a 
reassembled  PDU]
0.063568626 192.168.cc.xxxx -> 192.1xx.xxx TCP 2854 [TCP segment of a 
reassembled PDU]
0.063572832 192.168.cc.xxxx -> 192.1xx.xxx HTTP 695 HTTP/1.1 200 
OK     (application/json)
0.064066260 192.168.cc.xxxx -> 192.1xx.xxx TCP 54 http > 49330 [FIN, ACK] 
Seq=6242 Ack=167 Win=15232 Len=0
0.075055934 192.1xx.xxx -> 192.168.cc.xxxx TCP 54 49330 > http [ACK] 
Seq=167 Ack=2801 Win=65800 Len=0
0.075067927 192.1xx.xxx -> 192.168.cc.xxxx TCP 54 49330 > http [ACK] 
Seq=167 Ack=6243 Win=65800 Len=0
0.075095146 192.1xx.xxx -> 192.168.cc.xxxx TCP 54 49330 > http [FIN, ACK] 
Seq=167 Ack=6243 Win=65800 Len=0
0.075098758 192.168.cc.xxxx -> 192.1xx.xxx TCP 54 http > 49330 [ACK] 
Seq=6243 Ack=168 Win=15232 Len=0

但我看不到凭据

根据手册页 tshark -D

Print a list of the interfaces on which TShark can capture, and exit.

然后你必须选择一个。对于您的情况,您可能想听听 eth2。

您可以通过以下方式监听 eth2 上的所有流量:

tshark -ieth2

如果只想捕获 GET 请求,可以使用 capture filter expression, from the documentation :

tshark -ieth2 "port 80 and tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420"

然后您将看到所有 GET 请求都到达您的服务器。

== 编辑

如果您想查看数据包的所有详细信息(凭据,...),您可以通过添加 -T pdml 选项要求 tshark 以数据包详细信息标记语言输出数据包。