search
HomeOperation and MaintenanceLinux Operation and MaintenanceInstructions for using the packet capture tool tcpdump
Instructions for using the packet capture tool tcpdumpJun 23, 2017 am 11:13 AM
tcpdumptoolusageillustrate


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 helioshot or heliosace

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
解析JSP注释的使用方法和分类解析JSP注释的使用方法和分类Feb 01, 2024 am 08:01 AM

JSP注释的分类及用法解析JSP注释分为两种:单行注释:以结尾,只能注释单行代码。多行注释:以/*开头,以*/结尾,可以注释多行代码。单行注释示例多行注释示例/**这是一段多行注释*可以注释多行代码*/JSP注释的用法JSP注释可以用来注释JSP代码,使其更易于阅

如何正确使用C语言的exit函数如何正确使用C语言的exit函数Feb 18, 2024 pm 03:40 PM

c语言exit函数怎么用,需要具体代码示例在C语言中,我们常常需要在程序中提前终止程序的执行,或者在某个特定的条件下退出程序。C语言提供了exit()函数来实现这个功能。本文将介绍exit()函数的用法,并提供相应的代码示例。exit()函数是C语言中的标准库函数,它包含在头文件中。它的作用是终止程序的执行,并且可以带一个整型

Python函数介绍:isinstance函数的用法和示例Python函数介绍:isinstance函数的用法和示例Nov 04, 2023 pm 03:15 PM

Python函数介绍:isinstance函数的用法和示例Python是一门功能强大的编程语言,提供了许多内置函数,使得编程变得更加方便和高效。其中一个非常有用的内置函数是isinstance()函数。本文将介绍isinstance函数的用法和示例,并提供具体的代码示例。isinstance()函数用于判断一个对象是否是指定的类或类型的实例。该函数的语法如下

Python函数介绍:abs函数的用法和示例Python函数介绍:abs函数的用法和示例Nov 03, 2023 pm 12:05 PM

Python函数介绍:abs函数的用法和示例一、abs函数的用法介绍在Python中,abs函数是一个内置函数,用于计算给定数值的绝对值。它可以接受一个数字参数,并返回该数字的绝对值。abs函数的基本语法如下:abs(x)其中,x是要计算绝对值的数值参数,可以是整数或浮点数。二、abs函数的示例下面我们将通过一些具体的示例来展示abs函数的用法:示例1:计算

Tcpdump如何捕获和分析数据包Tcpdump如何捕获和分析数据包Mar 20, 2024 pm 07:36 PM

Tcpdump是一款功能强大的网络分析工具,主要用于Linux系统和macOS中的网络流量分析。网络管理员可以通过tcpdump进行网络流量的捕获和分析,从而进行网络嗅探和监控TCP/IP数据包。它依赖于一个名为"libpcap"的库来有效地捕获网络流量。除了帮助网络管理员识别网络问题和解决故障外,tcpdump还有助于定期监测网络活动并检查网络安全性。捕获到的数据会被存储在一个名为"pcap"文件中,然后可以使用TCP/IP数据包分析工具(例如Wireshark)或其他命令行工具来进一步分析这

使用苹果快捷指令的方法使用苹果快捷指令的方法Feb 18, 2024 pm 05:22 PM

苹果快捷指令怎么用随着科技的不断发展,手机已经成为了人们生活中不可或缺的一部分。而在众多手机品牌中,苹果手机凭借其稳定的系统和强大的功能一直备受用户的喜爱。其中,苹果快捷指令这一功能更是让用户们的手机使用体验更加便捷和高效。苹果快捷指令是苹果公司为其iOS12及更高版本推出的一项功能,通过创建和执行自定义指令,帮助用户简化手机操作流程,以达到更高效的工作和

教你使用Win10的快捷键教你使用Win10的快捷键Dec 30, 2023 am 11:32 AM

windows10常用快捷键可以为我们省去很多的时间,今天给大家介绍一些常用的快捷键用法,非常的方便快捷,下面一起来看看具体的使用方法吧。Win10快捷键用法介绍复制、粘贴和其他常规键盘快捷方式按此键执行此操作Ctrl+X剪切选定项Ctrl+C(或Ctrl+Insert)复制选定项Ctrl+V(或Shift+Insert)粘贴选定项Ctrl+Z撤消操作Alt+Tab在打开的应用之间切换Alt+F4关闭活动项,或者退出活动应用Windows徽标键+L锁定电脑Windows徽标键+D显示和隐藏桌面F

解析SQL中使用distinct关键字解析SQL中使用distinct关键字Feb 18, 2024 pm 09:21 PM

SQL中distinct用法详解在SQL数据库中,我们经常会遇到需要去除重复数据的情况。此时,我们可以使用distinct关键字,它能够帮助我们去除重复数据,使得查询结果更加清晰和准确。distinct的基本使用方法非常简单,只需要在select语句中使用distinct关键字即可。例如,以下是一个普通的select语句:SELECTcolumn_name

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft