计算机网络实用工具之tcpdump

简介

tcpdump是一个运行在命令行下的数据包分析器。能够获取到该计算机发送或接收的TCP/IP和其他数据包。

tcpdump 适用于大多数的类Unix操作系统,包括Linux、Solaris、BSD、Mac OS X、HP-UX和AIX 等等。在这些系统中,tcpdump 需要使用libpcap这个捕捉数据的库。其在Windows下的版本称为WinDump,它需要WinPcap驱动,相当于在Linux平台下的libpcap。

可以用于打印在网络接口上收发的数据包,过滤与特定表达式匹配的数据包;可以使用此工具来跟踪网络问题、检测攻击或监视网络活动。

官网

Home | TCPDUMP & LIBPCAP

安装

# tcpdump依赖于libpcap# ubuntu 22.04
# 查看libpcap
ubuntu~$ dpkg -l |grep libpcap
ii  libpcap0.8:amd64                       1.10.1-4build1                          amd64        system interface for user-level packet capture# 安装tcpdump
ubuntu~$ sudo apt install tcpdump# centos 7
# 查看libpcap
[root@centos7 ~]# rpm -qa libpcap
libpcap-1.5.3-13.el7_9.x86_64# 安装tcpdump
[root@centos7 ~]# sudo yum install tcpdump

使用帮助

[root@centos7 ~]# tcpdump -h
tcpdump version 4.99.1
libpcap version 1.10.1 (with TPACKET_V3)
OpenSSL 3.0.2 15 Mar 2022
Usage: tcpdump [-AbdDefhHIJKlLnNOpqStuUvxX#] [ -B size ] [ -c count ] [--count][ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ][ -i interface ] [ --immediate-mode ] [ -j tstamptype ][ -M secret ] [ --number ] [ --print ] [ -Q in|out|inout ][ -r file ] [ -s snaplen ] [ -T type ] [ --version ][ -V file ] [ -w file ] [ -W filecount ] [ -y datalinktype ][ --time-stamp-precision precision ] [ --micro ] [ --nano ][ -z postrotate-command ] [ -Z user ] [ expression ]

使用示例

运行 tcpdump 的通用格式是: tcpdump <options> <expression>

options

# 查看网卡tcpdump -D或tcpdump --list-interfaces
root@ubuntu:~# tcpdump -D
1.ens33 [Up, Running]
2.lo [Up, Running, Loopback]
3.any (Pseudo-device that captures on all interfaces) [Up, Running]
4.docker0 [Up]
5.bluetooth-monitor (Bluetooth Linux Monitor) [none]
6.nflog (Linux netfilter log (NFLOG) interface) [none]
7.nfqueue (Linux netfilter queue (NFQUEUE) interface) [none]
8.bluetooth0 (Bluetooth adapter number 0) [none]
root@ubuntu:~# 
root@ubuntu:~# tcpdump --list-interfaces
1.ens33 [Up, Running]
2.lo [Up, Running, Loopback]
3.any (Pseudo-device that captures on all interfaces) [Up, Running]
4.docker0 [Up]
5.bluetooth-monitor (Bluetooth Linux Monitor) [none]
6.nflog (Linux netfilter log (NFLOG) interface) [none]
7.nfqueue (Linux netfilter queue (NFQUEUE) interface) [none]
8.bluetooth0 (Bluetooth adapter number 0) [none]# 指定接口
# 所有接口
root@ubuntu:~# tcpdump -i any# ens33接口-i interface或--interface=interface
root@ubuntu:~# tcpdump -i ens33
root@ubuntu:~# tcpdump --interface=ens33# 将icmp数据包保存到icmp.pcap文件,参数-w,使用-w时,数据包不会显示在终端上。
root@ubuntu:~# tcpdump --interface=ens33 -w icmp.pcap icmp# 限制文件大小,-C参数,必须和-w参数一起使用
# 抓取的数据包保存到test.pcap文件中,大小达到2M时生成新的文件test.pcap1,数字递增
root@ubuntu:~# tcpdump -w test.pcap -C 2# 从文件中读取数据
root@ubuntu:/tmp# tcpdump -r test.pcap# -v参数,获取更多信息,-c 1意思是获取到1个数据包后停止,-n 不要将地址转换为名称
root@ubuntu:/tmp# tcpdump -n -c 1 icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
14:18:26.384013 IP 192.168.21.128 > 183.2.172.42: ICMP echo request, id 5, seq 1, length 64
1 packet captured
2 packets received by filter
0 packets dropped by kernel
root@ubuntu:/tmp# 
root@ubuntu:/tmp# tcpdump -v -n -c 1 icmp
tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
14:19:01.320676 IP (tos 0x0, ttl 64, id 21682, offset 0, flags [DF], proto ICMP (1), length 84)192.168.21.128 > 183.2.172.185: ICMP echo request, id 6, seq 1, length 64
1 packet captured
2 packets received by filter
0 packets dropped by kernel# 如果您还想以 ASCII 格式打印每个数据包的内容,则可以使用该 -A选项。例如,这将显示 HTTP 请求和响应的内容。
root@ubuntu:/tmp# tcpdump -A -n src port 80
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
14:24:47.351778 IP 183.2.172.42.80 > 192.168.21.128.44002: Flags [S.], seq 2034896310, ack 4106329319, win 64240, options [mss 1460], length 0
E..,
......X...*.....P..yJ......`.............
14:24:47.352217 IP 183.2.172.42.80 > 192.168.21.128.44002: Flags [.], ack 78, win 64240, length 0
E..(
......[...*.....P..yJ.....4P....c........
14:24:47.378366 IP 183.2.172.42.80 > 192.168.21.128.44002: Flags [.], seq 1:1461, ack 78, win 64240, length 1460: HTTP: HTTP/1.1 200 OK
E............*.....P..yJ.....4P.......HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 2381
Content-Type: text/html
Date: Wed, 22 Nov 2023 14:24:47 GMT
Etag: "588604dc-94d"
Last-Modified: Mon, 23 Jan 2017 13:27:56 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>...........................</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=............ class="bg s_btn"></span> 
14:24:47.378452 IP 183.2.172.42.80 > 192.168.21.128.44002: Flags [P.], seq 1461:2782, ack 78, win 64240, length 1321: HTTP
E..Q
!.....0...*.....P..yJ.k...4P...Q...</form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>......</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>......</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>......</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>......</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>......</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">......</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">............</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>............</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>.....................</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>............</a>&nbsp;...ICP...030173...&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>14:24:47.378907 IP 183.2.172.42.80 > 192.168.21.128.44002: Flags [.], ack 79, win 64239, length 0
E..(
".....X...*.....P..yJ.....5P.............
14:24:47.403420 IP 183.2.172.42.80 > 192.168.21.128.44002: Flags [FP.], seq 2782, ack 79, win 64239, length 0
E..(
#.....W...*.....P..yJ.....5P....}........# 显示数据链路层地址使用-e参数
root@ubuntu:/tmp# tcpdump -e -c 2 -n
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
14:26:54.254562 00:0c:29:2b:39:27 > 00:50:56:c0:00:08, ethertype IPv4 (0x0800), length 242: 192.168.21.128.22 > 192.168.21.1.49523: Flags [P.], seq 117054726:117054914, ack 2091745178, win 501, length 188
14:26:54.254829 00:0c:29:2b:39:27 > 00:50:56:c0:00:08, ethertype IPv4 (0x0800), length 298: 192.168.21.128.22 > 192.168.21.1.49523: Flags [P.], seq 188:432, ack 1, win 501, length 244
2 packets captured
3 packets received by filter
0 packets dropped by kernel# 禁用混杂模式-p或--no-promiscuous-mode

expression

# type
host for an individual host (e.g. host 192.168.1.1)
net for network (e.g. net 192.168.0.0/24),
port for a port number (e.g. port 22)
portrange for a ranges of ports (e.g. portrange 1000-2000)
If no type is specified, host is assumed.# dir
src - source only
dst - destination only
src or dst - either side of the connection may match
src and dst both source and destination should match the specified identifier
If no direction is specified, src or dst is assumed.# proto
This qualifier lets you target only a specific protocol. Protocols that you may wish to specify include:ether (Ethernet)
wlan (Wireless Lan)
ip (Internet Protocol v4)
ip6 (Internet Protocol v6)
arp (Address Resolution Protocol)
rarp (Reverse - ARP)
tcp (Transmission Control Protocol)
udp (User Datagram Protocol)# Combining filter expressions with and, or, not
not
Can also be written as ! - select only traffic which does not match the expression which follows.
tcpdump not tcp # match only traffic which doesn't use the TCP protocoland
Can also be written as && - select traffic which matches both the preceding expression and the following expression.
tcpdump icmp and host google.dnsor
Can also be written as || - select traffic which matches the preceding expression or the following expression.# 示例:
# 以下两句是等效的
tcpdump tcp dst port 80 or tcp dst port 443
tcpdump tcp dst port 80 or 443# 可以使用括号()进一步定义逻辑:
tcpdump host athena and (port 80 or 443)# 协议过滤
tcpdump ip and tcp# 基于IP地址的过滤
tcpdump src host 192.168.1.1 and dst host 192.168.1.2tcpdump net 192.168.1    # filter for all traffic from the 192.168.1.0/24 subnet(192.168对应于192.168.0.0/16)# 端口过滤器
tcpdump src portrange 10000-20000 and dst port 443# 数据包长度
tcpdump ip host 192.168.1.1 and less 100
tcpdump icmp greater 50

种草

本文为"计算机网络实用工具系列"的内容之一,会持续更新其它相关博文

我的博文内容主要针对“计算机网络”、“安全”、“运维”和“云计算”方向,感兴趣朋友的请关注我,我将不定期发布新的博文并不断改进已发布博文。

后期依据大家对博文的评论,点赞及关注情况,针对大家感兴趣的内容我也会录制视频并整理出成套的学习资料免费分享给大家,期待能和大家一起交流学习。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/162197.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Altium Designer学习笔记9

忽视了一个最大的问题&#xff0c;就是元器件的封装&#xff0c;不应该是根据AD系统的封装走&#xff0c;而应该是根据立创商城上的规格书&#xff0c;确认每个封装的大小&#xff0c;画出封装图&#xff0c;然后才是布局和走线。 1、确认电容的封装采用0805&#xff0c;贴片电…

【css】Google第三方登录按钮样式修改

文章目录 场景前置准备修改样式官方属性修改样式CSS修改样式按钮的高度height和border-radiusLogo和文字布局 场景 需要用到谷歌的第三方登录&#xff0c;登录按钮有自己的样式。根据官方文档&#xff1a;概览 | Authentication | Google for Developers&#xff0c;提供两种第…

局域网协议:地址解析协议(ARP,Address Resolution Protocol)

地址解析协议&#xff08;ARP&#xff0c;Address Resolution Protocol&#xff09;是一种用于在IP网络中将IP地址映射到物理MAC地址的协议。在IP网络中&#xff0c;IP是用于寻址&#xff0c;真正将数据包从一个设备发送到另外一个设备&#xff0c;用于通信的是物理MAC地址。 …

40、Flink 的Apache Kafka connector(kafka sink的介绍及使用示例)-2

Flink 系列文章 1、Flink 部署、概念介绍、source、transformation、sink使用示例、四大基石介绍和示例等系列综合文章链接 13、Flink 的table api与sql的基本概念、通用api介绍及入门示例 14、Flink 的table api与sql之数据类型: 内置数据类型以及它们的属性 15、Flink 的ta…

geemap学习笔记012:如何搜索Earth Engine Python脚本

前言 本节主要是介绍如何查询Earth Engine中已经集成好的Python脚本案例。 1 导入库 !pip install geemap #安装geemap库 import ee import geemap2 搜索Earth Engine Python脚本 很简单&#xff0c;只需要一行代码。 geemap.ee_search()使用方法 后记 大家如果有问题需…

vue截取URL中的参数

url&#xff1a; http://localhost:81/login?redirect%2Findex&access_tokeneyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvdUV4dGVybmFsSWQiOiI0OTI2MjYzMTIxMDU1NDAxMTM4IiwiYXVkIjpbImVudGVycHJpc2VfbW9iaWxlX3Jlc291cmNlIiwiYmZmX2FwaV9yZXN 截取参数&#xff1a; let…

如何提高图片转excel的效果?(软件选择篇)

在日常的工作中&#xff0c;我们常常会遇到一些财务报表类的图片需要转换成可编辑的excel&#xff0c;但是&#xff0c;受各种条件的限制&#xff0c;常常只能通过手工录入这种原始的方式来实现&#xff0c;随着人工智能、深度学习以及网络技术的发展&#xff0c;这种原始的录入…

SpringBoot集成七牛云OSS详细介绍

&#x1f4d1;前言 本文主要SpringBoot集成七牛云OSS详细介绍的文章&#xff0c;如果有什么需要改进的地方还请大佬指出⛺️ &#x1f3ac;作者简介&#xff1a;大家好&#xff0c;我是青衿&#x1f947; ☁️博客首页&#xff1a;CSDN主页放风讲故事 &#x1f304;每日一句&a…

【Java工具篇】Java反编译工具Bytecode Viewer

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

【C++高阶(四)】红黑树深度剖析--手撕红黑树!

&#x1f493;博主CSDN主页:杭电码农-NEO&#x1f493;   ⏩专栏分类:C从入门到精通⏪   &#x1f69a;代码仓库:NEO的学习日记&#x1f69a;   &#x1f339;关注我&#x1faf5;带你学习C   &#x1f51d;&#x1f51d; 红黑树 1. 前言2. 红黑树的概念以及性质3. 红黑…

计算机网络之数据链路层

一、概述 1.1概述 物理层发出去的信号需要通过数据链路层才知道是否到达目的地&#xff1b;才知道比特流的分界线 链路(Link)&#xff1a;从一个结点到相邻结点的一段物理线路&#xff0c;中间没有任何其他交换结点数据链路(Data Link)&#xff1a;把实现通信协议的硬件和软件…

电商API接口|电商数据接入|拼多多平台根据商品ID查商品详情SKU和商品价格参数

随着科技的不断进步&#xff0c;API开发领域也逐渐呈现出蓬勃发展的势头。今天我将向大家介绍API接口&#xff0c;电商API接口具备独特的特点&#xff0c;使得数据获取变得更加高效便捷。 快速获取API数据——优化数据访问速度 传统的数据获取方式可能需要经过多个中介环节&…

华大基因认知障碍基因检测服务,助力认知障碍疾病防控

认知障碍是一种严重的神经系统疾病&#xff0c;对人类的脑健康产生了重大影响。据报告显示&#xff0c;在我国65岁以上的人群中&#xff0c;存在轻度认知障碍的患者约为3,800万&#xff0c;而中重度痴呆患者则约为1,500万&#xff0c;患病人口数量庞大。这种疾病不仅会对患者的…

免费多域名SSL证书

顾名思义&#xff0c;免费多域名SSL证书就是一种能够为多个域名或子域提供HTTPS安全保护的证书。这意味着&#xff0c;如果您有三个域名——例如example.com、example.cn和company.com&#xff0c;您可以使用一个免费的多域名SSL证书为所有这些域名提供安全保障&#xff0c;而无…

TransFusionNet:JetsonTX2下肝肿瘤和血管分割的语义和空间特征融合框架

TransFusionNet: Semantic and Spatial Features Fusion Framework for Liver Tumor and Vessel Segmentation Under JetsonTX2 TransFusionNet&#xff1a;JetsonTX2下肝肿瘤和血管分割的语义和空间特征融合框架背景贡献实验方法Transformer-Based Semantic Feature Extractio…

pyhton接口猜用户登录和密码

import requests import base64 NUM 0 # 读取 URL 文件内容并生成 URL 列表 with open("urlall.txt", r) as file:urls [url.strip() for url in file.readlines() if url.strip()]# 读取密码文件内容并生成密码列表 with open("password.txt", r) as fil…

前端下载多个文件链接整合为压缩包

前端下载多个文件链接整合为压缩包 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document</ti…

AI写代码 可以代替人工吗?

近年AI技术非常火热&#xff0c;有人就说&#xff0c;用AI写代码程序员不就都得下岗吗&#xff1f;对此我的回答是否定的&#xff0c;因为AI虽然已经有了编写代码的能力&#xff0c;但它现在的水平大多还仅限于根据业务需求搭建框架&#xff0c;而具体的功能实现还尚且稚嫩&…

11.22 知识总结(cookie、 session相关知识点)

一、 Cookie和Session的发展史 一开始&#xff1a;只有一个页面&#xff0c;没有登录功能&#xff0c;大家看到东西都一样 新闻 时代发展&#xff0c;出现了需要登录注册的网站&#xff0c;要有一门技术存储我们的登录信息 京东、天猫 cookie 存储形式&#xff1a;k:v键值对 …

【愚公系列】保姆级教程带你实现HarmonyOS手语猜一猜元服务

&#x1f680;前言 最近HarmonyOS NEXT大火&#xff0c;这个纯血鸿蒙吸引力了大家的关注。虽然现在还没面向个人开发者开放&#xff0c;但我们可以基于最新的API9及开发工具来尝试开发鸿蒙新的应用形态——元服务。来体验下未来在HarmonyOS NEXT上实现的应用开发。 HarmonyOS…