Home  >  Article  >  Operation and Maintenance  >  Instructions for using the packet capture tool tcpdump

Instructions for using the packet capture tool tcpdump

巴扎黑
巴扎黑Original
2017-06-23 11:13:002229browse

Directory of this article:

##1.1 tcpdump options

1.2 tcpdump expression

##1.3 tcpdump example

tcpdump uses the command line to filter and capture interface data packets, and its rich features are reflected in flexible expressions.

Tcpdump without any options will capture the first network interface by default, and packet capture will only stop when the tcpdump process is terminated.

For example:

shell> tcpdump -nn -i eth0 icmp
The following is the detailed tcpdump usage.


1.1 tcpdump options

Its command format is:

tcpdump [ -DenNqvX ] [ -c count ] [ -F  ] [ -i interface ] [ -r -s snaplen ] [ - 注意,是最终要获取这么多个包。例如,指定若未指定该选项,将从系统接口列表中搜寻编号最小的已配置好的接口(不包括loopback接口,要抓取loopback接口使用tcpdump -i lo),
            :一旦找到第一个符合条件的接口,搜寻马上结束。可以使用-n:对地址以数字方式显式,否则显式为主机名,也就是说--N:不打印出host的域名部分。例如tcpdump将会打印而不是-P:指定要抓取的包是流入还是流出的包。可以给定的值为、和,默认为-s len:设置tcpdump的数据包抓取长度为len,如果不设置默认将会是65535字节。对于要抓取的数据包较大时,长度设置不够可能会产生包截断,若出现包截断,
      :输出行中会出现-----vv:产生比---:将抓包数据输出到文件中而不是标准输出。可以同时配合选项使得输出文件每time秒就自动切换到另一个文件。可通过-r:从给定的数据包文件中读取数据。使用表示从标准输入中读取。
So commonly used options Just these:

    tcpdump -D
  • ##tcpdump -c num -i int -nn -XX -vvv

1.2 tcpdump expression

Expression is used to filter which types of data packets are output. If no expression is given, all All data packets will be output, otherwise only the packets whose expression is true will be output. It is recommended that shell metacharacters appearing in expressions be surrounded by single quotes.

The expression of tcpdump consists of one or more "units". Each unit generally contains the modifier of the ID and an ID (number or name). There are three modifiers:

(1).type: Specify the type of ID.

The values ​​that can be given are host/net/port/portrange. For example "host foo", "net 128.3", "port 20", "portrange 6000-6008". The default type is host.

(2).dir: Specify the direction of the ID.

The values ​​that can be given include src/dst/src or dst/src and dst, and the default is src or dst. For example, "src foo" represents a packet whose source host is foo, "dst net 128.3" represents a packet whose destination network is 128.3, and "src or dst port 22" represents a packet whose source or destination port is 22.

(3).proto: Limit the matching packet types through the given protocol.

Commonly used protocols include tcp/udp/arp/ip/ether/icmp, etc. If the protocol type is not given, all possible types will be matched. For example "tcp port 21", "udp portrange 7000-7009".

So,

A basic expression unit format is "proto dir type ID"

In addition to using expression units composed of modifiers and IDs, there are also keywords Expression units: gateway, broadcast, less, greater and arithmetic expressions.

Expression units can be connected using the operators "and / && / or / || / not / !"

to form complex conditional expressions. For example, "host foo and not port ftp and not port ftp-data", this means that the filtered data packets must meet the requirements of "packets with host foo and port not ftp (port 21) and ftp-data (port 20)", commonly used ports The corresponding relationship with the name can be found in the /etc/service file in the Linux system.

In addition, the same modifiers can be omitted. For example, "tcp dst port ftp or ftp-data or domain" has the same meaning as "tcp dst port ftp or tcp dst port ftp-data or tcp dst port domain". All indicate that the protocol of the packet is tcp and the destination port is ftp or ftp-data or domain (port 53).

Using brackets "()" can change the priority of an expression, but it should be noted that the brackets will be interpreted by the shell, so the backslash "\" should be used to escape them to "\(\)". When needed, it also needs to be enclosed in quotation marks.

1.3 tcpdump example

(1).Default startup

tcpdump
By default, starting tcpdump directly will monitor all circulating data packets on the first network interface (non-lo port). There will be a lot of results captured in this way, and the scrolling will be very fast.

(2). Monitor the data packets of the specified network interface

tcpdump -i eth1
If the network card is not specified, the default tcpdump only The first network interface, such as eth0, will be monitored.

(3). Monitor the data packets of the specified host, such as all data packets entering or leaving longshuai

tcpdump host longshuai
(4).Print the data packets communicated between helios<-->hot or helios<-->ace

tcpdump host helios and \( hot or ace \)
(5). Print the IP data packets communicated between ace and any other host, but not including the data packets between ace and helios

tcpdump ip host ace and not helios

(6).截获主机hostname发送的所有数据

tcpdump src host hostname

(7).监视所有发送到主机hostname的数据包

tcpdump dst host hostname

(8).监视指定主机和端口的数据包

tcpdump tcp port 22 and host hostname

(9).对本机的udp 123端口进行监视(123为ntp的服务端口)

tcpdump udp port 123

(10).监视指定网络的数据包,如本机与192.168网段通信的数据包,"-c 10"表示只抓取10个包

tcpdump -c 10 net 192.168

(11).打印所有通过网关snup的ftp数据包(注意,表达式被单引号括起来了,这可以防止shell对其中的括号进行错误解析)

shell> tcpdump 'gateway snup and (port ftp or ftp-data)'

(12).抓取ping包

[root@server2 ~]# tcpdump -c 5 -nn -i eth0 icmp 

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes12:11:23.273638 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16422, seq 10, length 6412:11:23.273666 IP 192.168.100.62 > 192.168.100.70: ICMP echo reply, id 16422, seq 10, length 6412:11:24.356915 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16422, seq 11, length 6412:11:24.356936 IP 192.168.100.62 > 192.168.100.70: ICMP echo reply, id 16422, seq 11, length 6412:11:25.440887 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16422, seq 12, length 645 packets captured6 packets received by filter0 packets dropped by kernel

如果明确要抓取主机为192.168.100.70对本机的ping,则使用and操作符。

[root@server2 ~]# tcpdump -c 5 -nn -i eth0 icmp and src 192.168.100.62tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes12:09:29.957132 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 1, length 6412:09:31.041035 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 2, length 6412:09:32.124562 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 3, length 6412:09:33.208514 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 4, length 6412:09:34.292222 IP 192.168.100.70 > 192.168.100.62: ICMP echo request, id 16166, seq 5, length 645 packets captured5 packets received by filter0 packets dropped by kernel

注意不能直接写icmp src 192.168.100.70,因为icmp协议不支持直接应用host这个type。

(13).抓取到本机22端口包

[root@server2 ~]# tcpdump -c 10 -nn -i eth0 tcp dst port 22  tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes12:06:57.574293 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 535528834, win 2053, length 012:06:57.629125 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 193, win 2052, length 012:06:57.684688 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 385, win 2051, length 012:06:57.738977 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 577, win 2050, length 012:06:57.794305 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 769, win 2050, length 012:06:57.848720 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 961, win 2049, length 012:06:57.904057 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 1153, win 2048, length 012:06:57.958477 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 1345, win 2047, length 012:06:58.014338 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 1537, win 2053, length 012:06:58.069361 IP 192.168.100.1.5788 > 192.168.100.62.22: Flags [.], ack 1729, win 2052, length 010 packets captured10 packets received by filter0 packets dropped by kernel

(14).解析包数据

[root@server2 ~]# tcpdump -c 2 -q -XX -vvv -nn -i eth0 tcp dst port 22tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes12:15:54.788812 IP (tos 0x0, ttl 64, id 19303, offset 0, flags [DF], proto TCP (6), length 40)192.168.100.1.5788 > 192.168.100.62.22: tcp 00x0000:  000c 2908 9234 0050 56c0 0008 0800 4500  ..)..4.PV.....E.0x0010:  0028 4b67 4000 4006 a5d8 c0a8 6401 c0a8  .(Kg@.@.....d...0x0020:  643e 169c 0016 2426 5fd6 1fec 2b62 5010  d>....$&_...+bP.0x0030:  0803 7844 0000 0000 0000 0000            ..xD........12:15:54.842641 IP (tos 0x0, ttl 64, id 19304, offset 0, flags [DF], proto TCP (6), length 40)192.168.100.1.5788 > 192.168.100.62.22: tcp 00x0000:  000c 2908 9234 0050 56c0 0008 0800 4500  ..)..4.PV.....E.0x0010:  0028 4b68 4000 4006 a5d7 c0a8 6401 c0a8  .(Kh@.@.....d...0x0020:  643e 169c 0016 2426 5fd6 1fec 2d62 5010  d>....$&_...-bP.0x0030:  0801 7646 0000 0000 0000 0000            ..vF........2 packets captured2 packets received by filter0 packets dropped by kernel

总的来说,tcpdump对基本的数据包抓取方法还是较简单的。只要掌握有限的几个选项(-nn -XX -vvv -i -c -q),再组合表达式即可。

 

姊妹篇:网络扫描工具nmap

 

回到系列文章大纲:

转载请注明出处:

The above is the detailed content of Instructions for using the packet capture tool tcpdump. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn