[转帖]tcpdump: An Incomplete Guide

tcpdump,an,incomplete,guide · 浏览次数 : 0

小编点评

Sure, here's a summary of the content you provided: **Packet Size Reduction:** * The extracted result can be truncated to significantly reduce the file size. * This can be done by truncating the following headers: * IP header * ICMP header * TCP header * UDP header **Specific Headers to Match:** * IP header: * Version * IHL * Type of Service * Total Length * Identification * Time to Live * Protocol * Header Checksum * ICMP header: * Match fields * TCP header: * Source Port * Destination Port * UDP header: * Match fields **Additional Notes:** * The content also mentions filtering based on packet content, but the specific criteria are not provided. * The purpose of truncation and specific header matching is not fully explained in the context. * The command `tcpdump -i eth0 -s 36 -w test.pcap` suggests that the resulting pcap file will be named `test.pcap` and will contain only L2 and L3 headers.

正文

tcpdump: An Incomplete Guide

Published at 2018-11-28 | Last Update 2021-01-11

tcpdump is a network tool to dump traffic on the network. This post servers as a guide to some frequently used commands. For a complete guide, please refer to the man page, or man tcpdump on a linux machine.

1 Basic options

A help summary:

tcpdump [ -AbdDefhHIJKlLnNOpqStuUvxX# ] [ -B buffer_size ]
        [ -c count ]
        [ -C file_size ] [ -G rotate_seconds ] [ -F file ]
        [ -i interface ] [ -j tstamp_type ] [ -m module ] [ -M secret ]
        [ --number ] [ -Q in|out|inout ]
        [ -r file ] [ -V file ] [ -s snaplen ] [ -T type ] [ -w file ]
        [ -W filecount ]
        [ -E spi@ipaddr algo:secret,...  ]
        [ -y datalinktype ] [ -z postrotate-command ] [ -Z user ]
        [ --time-stamp-precision=tstamp_precision ]
        [ --immediate-mode ] [ --version ]
        [ expression ]

Running tcpdump needs root privilege, so prefix sudo before all commands in this post if you are not root user.

1.1 Capture options

The simplest way to capture traffic on a host is to specify a device with -i option, the output may look like this:

$ sudo tcpdump -i eth0 # use CTL-C to terminate it
18:10:14.578057 IP 192.168.1.3.ssh > 192.168.1.124.53519: Flags [P.], seq 2350:2350, ack 166, win 198, length 240
18:10:14.578775 IP 192.168.1.124.53519 > 192.168.1.3.ssh: Flags [.], ack 240, win 252, length 0
18:10:14.634826 ARP, Request who-has 192.168.1.68 tell 192.168.1.81, length 46
18:10:14.670785 ARP, Request who-has 192.168.1.146 tell 192.168.1.81, length 46
^C
4 packets captured
39 packets received by filter
0 packets dropped by kernel

One tcpdump process could only capture one device, to capture multiple devices, e.g. both eth0 and eth1, you have to launch two processes:

$ tcpdump -i eth0 [OPTIONS]

$ tcpdump -i eth1 [OPTIONS]

1.2 Output options

This section describes the options for displaying packets info on standard output.

Verbosity

  • -v verbose
  • -vv more verbose
  • -vvv even more verbose

IP, Protocol, Port

  • -n print IP instead of host name. This can be used to avoid DNS lookups
  • -nn print integer protocol/port numbers instead of protocl/port names, e.g. 22 vs ssh80 vs http

Examples:

$ tcpdump -i eth0
14:54:35.161548 IP ctn-1.example.com > ctn-2.example.com: ICMP echo request, id 29455, seq 0, length 64
14:54:35.161599 IP ctn-2.example.com > ctn-1.example.com: ICMP echo reply, id 29455, seq 0, length 64

$ tcpdump -n -i eth0
14:55:34.296206 IP 192.168.1.3 > 192.168.1.4: ICMP echo request, id 29711, seq 0, length 64
14:55:34.296259 IP 192.168.1.4 > 192.168.1.3: ICMP echo reply, id 29711, seq 0, length 64

MAC Address

  • -e also print MAC address
$ tcpdump -n -e -i eth0
15:05:12.225901 fa:16:3e:39:8c:fd > 00:22:0d:27:c2:45, ethertype IPv4 (0x0800), length 294: 192.168.1.3 > 192.168.1.124: Flags [P.], seq ...
15:05:12.226585 00:22:0d:27:c2:45 > fa:16:3e:39:8c:fd, ethertype IPv4 (0x0800), length 60: 192.168.1.124 > 192.168.1.3: Flags [.], ack ...

Packet Content

  • -x print the data of each packet (minus its link level header) in hex
  • -xx print the data of each packet, including its link level header, in hex.
  • -X print the data of each packet (minus its link level header) in hex and ASCII.
  • -XX print the data of each packet, including its link level header, in hex and ASCII.
$ tcpdump -i eth0 -x
19:33:33.724674 IP 192.168.1.3 > 192.168.1.4: ICMP echo request, id 10258, seq 0, length 64
        0x0000:  4500 0054 6e2b 4000 4001 4926 c0a8 0103
        0x0010:  c0a8 0104 0800 a20e 2812 0000 0f1c 1ec3
        0x0020:  0000 0000 0000 0000 0000 0000 0000 0000
        0x0030:  0000 0000 0000 0000 0000 0000 0000 0000
        0x0040:  0000 0000 0000 0000 0000 0000 0000 0000
        0x0050:  0000 0000

1.3 Save to file & read from file

  • -w outfile.pcap save packets to file
  • -G rotate the dump file, should be used with -w option
  • -r outfile.pcap read a captured file

Captured files usually suffixed with cap or .pcap, which means packet capture file. The captured files are totally different from those generated with > outfile, which only redirects the messages on standard output (text) to a file.

# save raw packets to file
$ tcpdump -i eth0 -w test.pcap

# redirect logs to text file
$ tcpdump -i eth0 > test.txt

Captured files could be open again later:

$ tcpdump -e -nn -r test.pcap # read captured file content, print ether header, and be more numeric
15:10:40.111214 fa:16:30:a1:33:27 (oui Unknown) > fa:16:3f:e2:16:17 (oui Unknown), ethertype 802.1Q (0x8100), length 78: [|vlan]
15:10:40.111275 fa:16:30:a1:33:27 (oui Unknown) > fa:16:3f:e2:16:17 (oui Unknown), ethertype 802.1Q (0x8100), length 78: [|vlan]

Or, those files could also be opened with more professional traffic analyzing tools, e.g. Wireshark.

Split captured file

  • -C <N> write pcap file every N MB.
  • -W <N> keep at most N copies, then start to rotate.

Example: read a large pcap file, split it into 10MB chunks:

$ tcpdump -r a.pcap -C 10 b.pcap
$ ls
b.pcap0 b.pcap1 b.pcap2 ...

1.4 Stop capturing

CTL-C will stop capturing.

Besides, -c <count> will auto exit after receiving <count> packets.

$ tcpdump -i eth0 -c 2
15:00:18.129859 IP 192.168.1.3.ssh > 192.168.1.4.53519: Flags [P.], seq ...
15:00:18.130500 IP 192.168.1.4.53519 > 192.168.1.3.ssh: Flags [.], ack ...
2 packets captured

2 Matching expressions

tcpdump supports filter expressions, this is where the real power comes to place. A complete guide of pcap-filter could be get from it’s man page, or through:

$ man 7 pcap-filter

If no filter expressions specified, tcpdump will capture all the packets on the device, which may be huge in mount. With filter expressions, it will only capture those that match the expressions.

$ tcpdump [OPTIONS] [expression]

2.1 Match host

  • host <hostname or IP> - capture packets sent from and to host
  • src host <hostname or IP> - capture packets sent from host
  • dst host <hostname or IP> - capture packets sent to host

Examples:

$ tcpdump -i eth0 host baidu.com   # traffic from or to baidu.com
$ tcpdump -i eth0 host 192.168.1.3 # traffic from or to 192.168.1.3

$ tcpdump -i eth0 src host 192.168.1.3
$ tcpdump -i eth0 dst host 192.168.1.3

2.2 Match MAC address & VLAN

  • ether host <MAC> - capture packets sent from and to <MAC>
  • ether src <MAC> - capture packets sent from <MAC>
  • ether dst <MAC> - capture packets sent to <MAC>
  • vlan <VLAN ID> - match VLAN ID

2.3 Match network (ip range)

  • net <NET> mask <MASK> - IPv4 only
  • net <NET>/<LEN> - IPv4/IPv6

May be qualified with src and dst.

Examples:

$ tcpdump -i eth0 net 192.168.1.0 mask 255.255.255.0
$ tcpdump -i eth0 net 192.168.1.0/24

2.4 Match port & port ranges

  • port <port> - packets from and to <port>
  • src port <port> - packets from <port>
  • dst port <port> - packets to <port>
  • portrange <port1>-<port2> - packets from and to <port1>-<port2>
  • src portrange <port1>-<port2> - packets from <port1>-<port2>
  • dst portrange <port1>-<port2> - packets to <port1>-<port2>

Examples:

$ tcpdump -i eth0 port 80
$ tcpdump -i eth0 dst port 80
$ tcpdump -i eth0 src portrange 8000-8080

2.5 Match protocol (L2-L4)

Match protocols in L3 header:

  • ip proto <PROTO> - PROTO: icmp, icmp6, igmp, igrp, pim, ah, esp, vrrp, udp, or tcp

Follow are abbreviations:

  • icmp = proto icmp
  • tcp = proto tcp
  • udp = proto udp

Match protocols in L2 header:

  • ether proto <PROTO> - PROTO: ip, ip6, arp, rarp, atalk, aarp, decnet, sca, lat, mopdl, moprc, iso, stp, ipx, or netbeui

Follow are abbreviations:

  • ip = ether proto ip
  • ip6 = ether proto ip6
  • arp = ether proto arp
  • rarp = ether proto rarp
$ tcpdump -i eth0 arp
$ tcpdump -i eth0 icmp

2.6 Match traffic direction (ingress/egress)

  • --direction=[in|out|inout] or -Q [in|out] - note that not all platform supports this

2.7 Match TCP flags

TCP flags (bits):

  1. tcp-syn
  2. tcp-ack
  3. tcp-fin
  4. tcp-rst
  5. tcp-push

Filter expressions based on the above flags:

  • 'tcp[tcpflags] & (tcp-syn) != 0' or tcp[13] & 2 != 0: capture syn packets
  • 'tcp[tcpflags] & (tcp-syn|tcp-ack) != 0': capture TCP handshake packets

2.8 Logical operators

With logical operators, we could combine simple expressions into a complex one.

  • and or &&
  • or or ||
  • not or !

Examples:

# capture traffic: 192.168.1.3<->192.168.1.4:80
$ tcpdump -i eth0 'host 192.168.1.3 and (host 192.168.1.4 and port 80)'

# capture traffic: 192.168.1.3->192.168.1.4:80
$ tcpdump -i eth0 'src host 192.168.1.3 and (dst host 192.168.1.4 and port 80)'

# capture traffic: 192.168.1.0/24->10.1.1.4
$ tcpdump -i eth0 'src net 192.168.1.0/24 and dst host 10.1.1.4 -w test.pcap'

3 Advanced matching (header fields/bits matching)

This part is borrowed from [4].

General format:

  1. proto[x:y]: starting from xth byte, extract y consecutive bytes, where x starts from 0.

    For example: ip[2:2] means extracting the 3rd and 4th bytes.

  2. The extracted result could further be processed: e.g. result & 0xF != 0

3.1 Match fields in IP header

IP header:

byte:       0               1               2               3

bit  0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |Version|  IHL  |Type of Service|          Total Length         |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |         Identification        |Flags|      Fragment Offset    |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |  Time to Live |    Protocol   |         Header Checksum       |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                       Source Address                          |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                    Destination Address                        |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                    Options                    |    Padding    | <-- optional
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    |                            DATA ...                           |
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  • ip[6] = 32: packets with MF=1 (More Fragments) in Flags field, which indicates this is a fragmented packet
  • ip[8] < 5: packets with TTL < 5
  • ip[2:2] > 600: packets with length > 600 bytes (Total Length field)

3.2 Match fields in ICMP header

3.3 Match fields in TCP header

byte:       0               1               2               3

bit  0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|          Source Port          |       Destination Port        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                        Sequence Number                        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Acknowledgment Number                      |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|  Data |       |C|E|U|A|P|R|S|F|                               |
| Offset|  Res. |W|C|R|C|S|S|Y|I|            Window             |
|       |       |R|E|G|K|H|T|N|N|                               |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|           Checksum            |         Urgent Pointer        |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                    Options                    |    Padding    |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|                             data                              |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  • tcp[0:2] > 1024 or tcp src portrange 1025-65535: packets with src port > 1024
  • tcp[13] = 2 or tcp[tcpflags] & tcp-syn != 0syn packets
  • tcp[13] = 18syn+ack packets
  • tcp[13] & 2 = 2syn or syn+ack packets
  • tcp[13] & 1 = 1fin packets
  • tcp[13] & 4 = 4rst packets

3.4 Match fields in UDP header

3.5 Match fields in HTTP header

  • tcpdump <options> | grep GET: HTTP GET requests

4 Misc

4.1 Truncate packet length

-s <LEN> truncate each packet to length LEN bytes. This could substantially reduce the resulted pcap file size.

For example, if want to capture only L2 and L3 headers, you could truncate each packet to 14 (ether header) + 2 (potential VLAN) + 20 (IP header basic part) = 36 bytes, thus:

$ tcpdump -i eth0 -s 36 -w test.pcap

Reference

  1. Man Page of tcpdump
  2. Wireshark
  3. Man Page of pcap-filter
  4. Tcpdump advanced filters

与[转帖]tcpdump: An Incomplete Guide相似的内容:

[转帖]tcpdump: An Incomplete Guide

tcpdump: An Incomplete Guide Published at 2018-11-28 | Last Update 2021-01-11 1 Basic options 1.1 Capture options 1.2 Output options Verbosity IP, Pro

[转帖]tcpdump/wireshark 抓包及分析(2019)

http://arthurchiao.art/blog/tcpdump-practice-zh/ 本文将展示如何使用 tcpdump 抓包,以及如何用 tcpdump 和 wireshark 分析网络流量。 文中的例子比较简单,适合作为入门参考。 1 基础环境准备 为方便大家跟着上手练习,本文将搭建

[转帖]tcpdump非常实用的抓包实例

https://www.jianshu.com/p/83cf0e64a654 参考资料:http://www.jianshu.com/p/3cca9a74927c <<亲测可用tcpdump查看HTTP流量查看>> 抓包HTTP GET请求: [root@hostname /]# sudo tcpd

[转帖]Tcpdump抓包命令

tcpdump和ethereal可以用来获取和分析网络通讯活动,他们都是使用libpcap库来捕获网络封包的。 ​在混杂模式下他们可以监控网络适配器的所有通讯活动并捕获网卡所接收的所有帧。 ​要想设置网络接口为混杂模式并执行这些命令来捕获所有的网络封包,需要具有超级用户的权限。 你可以使用这些工具来

[转帖][译]tcpdump 示例教程

https://colobu.com/2019/07/16/a-tcpdump-tutorial-with-examples/ 目录 [−] 基于IP查找流量 根据来源和目标进行筛选 根据网段进行查找 使用十六进制输出 显示特定端口的流量 显示特定协议的流量 只显示 ipv6 的流量 查看一个端口段

[转帖]019 Linux tcpdump 抓包案例入门可真简单啊?

https://my.oschina.net/u/3113381/blog/5477908 1 tcpdump 是什么? tcpdump 可以将网络中传送的数据包完全截获下来提供分析。它支持针对网络层、协议、主机、端口的过滤,并提供 and、or、not 等逻辑语句来帮助你过滤掉不关注的信息。 通常

[转帖]使用 nsenter、dig 和 tcpdump 调试 Kubernetes 网络问题

https://zhuanlan.zhihu.com/p/410217354 使用 nsenter、dig 和 tcpdump 调试 Kubernetes 网络问题 作为 Kubernetes 管理员,我经常发现自己需要调试应用程序和系统问题。我遇到的大多数问题都可以通过 Grafana 仪表板和

[转帖]重定向Kubernetes pod中的tcpdump输出

https://www.cnblogs.com/charlieroro/p/17109943.html 最新发现一个比较有意思的库ksniff,它是一个kubectl 插件,使用tcpdump来远程捕获Kubernetes集群中的pod流量并保存到文件或输出到wireshark中,发布网络问题定位。

[转帖]一份快速实用的 tcpdump 命令参考手册

http://team.jiunile.com/blog/2019/06/tcpdump.html tcpdump 简介 对于 tcpdump 的使用,大部分管理员会分成两类。有一类管理员,他们熟知 tcpdump 和其中的所有标记;另一类管理员,他们仅了解基本的使用方法,剩下事情都要借助参考手册才

[转帖]《Linux性能优化实战》笔记(20)—— 使用 tcpdump 和 Wireshark 分析网络流量

tcpdump 和 Wireshark 是最常用的网络抓包和分析工具,更是分析网络性能必不可少的利器。 tcpdump 仅支持命令行格式使用,常用在服务器中抓取和分析网络包。Wireshark 除了可以抓包,还提供了强大的图形界面和汇总分析工具,在分析复杂的网络情景时,尤为简单和实用。因而,在实际分