tcpdump 只显示 in 或 out mac 地址,如何同时显示?

tcpdump only display in or out mac address, how to display both?

我的 OS version:CentOS Linux 7.5.1804 版(核心版)

我的 tcpdump 版本:

tcpdump: option requires an argument -- 'V'
tcpdump version 4.9.2
libpcap version 1.5.3
OpenSSL 1.0.2k-fips  
26 Jan 2017

当命令为tcpdump -i any -e -nn时,输出是这样的:

15:55:07.876878  **In 00:50:56:c0:00:08** ethertype IPv4 (0x0800), length 80: 172.16.248.1.61944 > 172.16.248.101.80: Flags [S], seq 448335523, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 386448815 ecr 0,sackOK,eol], length 0
15:55:07.876939 **Out 00:50:56:28:6d:20** ethertype IPv4 (0x0800), length 80: 172.16.248.1.61944 > 172.16.248.101.80: Flags [S], seq 448335523, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 386448815 ecr 0,sackOK,eol], length 0
15:55:15.878019  **In 00:50:56:c0:00:08** ethertype IPv4 (0x0800), length 80: 172.16.248.1.61944 > 172.16.248.101.80: Flags [S], seq 448335523, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 386456815 ecr 0,sackOK,eol], length 0
15:55:15.878083 **Out 00:50:56:28:6d:20** ethertype IPv4 (0x0800), length 80: 172.16.248.1.61944 > 172.16.248.101.80: Flags [S], seq 448335523, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 386456815 ecr 0,sackOK,eol], length 0

我们可以看到输出只包含"in"或"out"mac地址,而不是两者。

但是,在 macOS 中,我的 tcpdump 版本是:

tcpdump: option requires an argument -- V
tcpdump version tcpdump version 4.9.2 -- Apple version 83.200.2
libpcap version 1.8.1 -- Apple version 79.200.4
LibreSSL 2.2.7

输出是这样的:

13:58:58.699758 **00:50:56:c0:00:08 > 00:0c:29:cb:e2:ab**, ethertype IPv4 (0x0800), length 78: 172.16.248.1.61179 > 172.16.248.101.80: Flags [S], seq 1223964422, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 383603998 ecr 0,sackOK,eol], length 0
13:58:59.703124 **00:50:56:c0:00:08 > 00:0c:29:cb:e2:ab**, ethertype IPv4 (0x0800), length 78: 172.16.248.1.61179 > 172.16.248.101.80: Flags [S], seq 1223964422, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 383604999 ecr 0,sackOK,eol], length 0
13:59:00.709878 **00:50:56:c0:00:08 > 00:0c:29:cb:e2:ab**, ethertype IPv4 (0x0800), length 78: 172.16.248.1.61179 > 172.16.248.101.80: Flags [S], seq 1223964422, win 65535, options [mss 1460,nop,wscale 6,nop,nop,TS val 383605999 ecr 0,sackOK,eol], length 0

我们可以看到输出同时包含"in"和"out"mac地址。

所以,我的问题是,如何让 Centos 中的 tcpdump 像我的 macOS 一样显示 mac 地址?即同时显示 "in" 和 "out" mac 地址.


(我认为是因为他没有声望而添加到 OP)

十六进制输出的示例是:

23:26:49.169360 In 00:50:56:c0:00:08 ethertype IPv4 (0x0800), length 68: 172.16.248.1.62593 > 172.16.248.101.2020: Flags [.], ack 14720436, win 1983, options [nop,nop,TS val 568496069 ecr 131615210], length 0

0x0000:  0000 0001 0006 **0050 56c0 0008** 0000 0800
0x0010:  4548 0034 837c 0000 4006 ae77 ac10 f801
0x0020:  ac10 f865 f481 07e4 55d8 41fc 6751 69a6
0x0030:  8010 07bf bdd9 0000 0101 080a 21e2 8fc5
0x0040:  07d8 49ea
23:26:49.171185 Out 00:0c:29:cb:e2:ab ethertype IPv4 (0x0800), length 4200: 172.16.248.101.2020 > 172.16.248.1.62593: Flags [P.], seq 14732048:14736180, ack 10297, win 380, options [nop,nop,TS val 131615212 ecr 568496070], length 4132

0x0000:  0004 0001 0006 **000c 29cb e2ab** 0000 0800
0x0010:  4512 1058 d695 4000 4006 0b70 ac10 f865
0x0020:  ac10 f801 07e4 f481 6751 9702 55d8 41fc
0x0030:  8018 017c 58d3 0000 0101 080a 07d8 49ec
0x0040:  21e2 8fc6 e771 c1f5 87f2 15e9 9b2a c2cd
0x0050:  dfce 5048 7123 1656 3fea eda7 ca39 d65c
0x0060:  3a6c b571 d6c2 5fad d39e 1605 4df6 946f
0x0070:  5657 7ef3 b85b dd66 48a7 bcd7 3567 a54f

您可以尝试使用 -xx-XX 选项。

由于 tcpdump 最初是专门为查看网络层及以上(IP 及以上... TCP、UDP、ICMP)而创建的,因此其默认行为自然是仅解码和显示这些层。

虽然 -e 选项将添加 link 层信息,特别是如果您有一些更多的以太网变体,您可能看不到您要查找的所有内容。如果您使用 -xx-XX 选项,这些选项将从帧头开始显示帧中的原始字节,而不是从网络层头开始显示。

使用这些选项,您可以自己手动解码帧头,不会隐藏任何内容。

您似乎配置为在 LINUX_SLLCooked 模式下使用 tcpdump。这有一个完全不同的帧头,并且只会向您显示目标 MAC 因为源被用来保存:

 2 byte packet type
 2 byte ARP_HRD type
 2 byte address length

您可以阅读有关此格式的更多信息 here

解决此问题并查看两个 MAC 地址的最简单方法是实际 select 使用 -i 的接口而不是依赖 any 选项,这将总是被煮熟。