Tcpdump网络/命令行抓包工具详解
<p> </p><p><strong> 概述</strong></p><p>
用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者的定义对网络上的数据包进行截获的包分析工具。</p><p>
tcpdump可以将网络中传送的数据包的“头”完全截获下来提供分析。它支持针对网络层、协议、主机、网络或端口的过滤,并提供and、or、not等逻辑语句来帮助你去掉无用的信息。</p><p>
tcpdump基于底层libpcap库开发,运行需要root权限。</p><p><strong>一、tcpdump安装</strong></p><p><strong>1.环境 虚拟机:vmware 15.5.2 os: ubuntu 12.04</strong></p><p><strong>2.安装tcpdump</strong></p><ol class="dp-sql list-paddingleft-2"><li>sudo apt-get install tcpdump </li></ol><p>
<img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/c38867033406b413f8f3d74450b85181.jpg" width="auto"/></p><p><strong>3. 版本查看</strong></p><ol class="dp-sql list-paddingleft-2"><li>tcpdump <span class="comment">--h</span> </li></ol><p><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/8ff03dc0452d0bf141cf2c6e24e8591a.jpg" width="auto"/></p><p>
tcpdump version 4.0。</p><p>
libpcap version 1.1.1 表示libpcap的版本。</p><p><strong>二、tcpdump参数常用参数</strong></p><p>
选项说明:</p><p><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/52aced0de314b7c053dbfd4c5a36cb37.jpg" width="auto"/></p><p><strong>三、命令选项使用举例</strong></p><p><strong>1. 截获主机收到和发出的所有数据包。</strong></p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump </li></ol><p>
说明:</p><p>
tcpdump截取包默认显示数据包的头部。</p><p>
普通情况下,直接启动tcpdump将监视第一个网络接口上所有流过的数据包。</p><p>
基础格式:时间 数据包类型 源IP 端口/协议 > 目标IP 端口/协议 协议详细信息</p><center><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/0460489babb9a263920a811895e60738.jpg" width="auto"/></center><p>
按下Ctrl+C会终止tcpdump命令。且会在结尾处生成统计信息。</p><center><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/5cc13bad3dd58161ce506db379d5093f.jpg" width="auto"/></center><p>
终止tcpdump</p><p><strong>2. 指定抓包数量 -c</strong></p><p>
指定抓取2个数据包。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -c 2 </li></ol><p>
说明:</p><p>
最后会自动生成统计信息。</p><center><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/a2d6acae3a6a36885a73997dc8f4006a.jpg" width="auto"/></center><p>
【注意,已经切换到管理员了,虚拟机中要产生数据包,可以另外开一个窗口ping baidu.com后面不再提示】</p><p>
<img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/5506892ebe50b4163bf0c6ecc5ce307d.jpg" width="auto"/></p><p>
ping baidu.com</p><p><strong>3. 将抓包信息写入文件 -w</strong></p><p>
使用-w选项指定记录文件。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -c 10 -w tcpdump_test.log </li></ol><p>
说明:</p><p>
保存的文件不是文本格式,不能直接查看。tcpdump保存的文件的格式是几乎所有主流的抓包工具软件都可以读取。所以可以使用更易读的图形界面工具来查看记录文件。</p><center><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/411e023cc8e05b616b30587f422e776a.jpg" width="auto"/></center><p><strong>4. 读取记录文件 -r</strong></p><p>
使用-r选项读取文件。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -r tcpdump_test.log </li></ol><p>
![读取记录文件</p><center><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" src="https://zhuji.jb51.net/uploads/img/202305/1fe8224879b0542ca5cb899af6ca142c.jpg"/></center><p><strong>5. 打印出所有可工作的接口 -D</strong></p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -D </li></ol><p><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" src="https://zhuji.jb51.net/uploads/img/202305/194d57627ff30440ed02aa7b2161a19d.jpg"/></p><p>
其中网卡为eth0。</p><p><strong>6. 指定监控的网卡 -i</strong></p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -i eth0 </li></ol><p>
如果不指定网卡,默认tcpdump只会监视第一个网络接口,一般是eth0。</p><p><strong>7. 显示更详细的数据包信息 -v -vv</strong></p><p>
选项-v,-vv可以显示更详细的抓包信息。</p><center><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" src="https://zhuji.jb51.net/uploads/img/202305/0d129bcdd01f45ff621bae5225e9a69e.jpg"/></center><p>
tcpdump -v</p><center><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/f80736ce0dd5dcdf69eaeb7ad4679bbc.jpg" width="auto"/></center><p>
tcpdump -vv</p><p><strong>8. 不使用域名反解 -n</strong></p><p>
使用-n后,tcpdump会直接显示IP地址,不会显示域名(与netstat命令相似)。</p><p><strong>9. 增加抓包时间戳 -tttt选项</strong></p><p>
tcpdump的所有输出打印行中都会默认包含时间戳信息;时间戳信息的显示格式如下</p><ol class="dp-sql list-paddingleft-2"><li>hh:mm:ss.frac (nt: 小时:分钟:秒.) </li></ol><p>
此时间戳的精度与内核时间精度一致, 反映的是内核第一次看到对应数据包的时间;</p><p>
而数据包从物理线路传递到内核的时间, 以及内核花费在此包上的中断处理时间都没有算进来;</p><p>
使用-tttt选项,抓包结果中将包含抓包日期:</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -tttt </li></ol><p>
<img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/afbd036036517b058a4fc94a82a08217.jpg" width="auto"/></p><p>
增加抓包时间戳</p><p><strong>四、条件过滤</strong></p><p><strong>1. 过滤:指定需要抓取的协议</strong></p><p>
tcpdump可以只抓某种协议的包,支持指定以下协议:「ip,ip6,arp,tcp,udp,wlan」等。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump udp </li><li> </li><li>tcpdump icmp </li><li> </li><li>tcpdump tcp </li><li> </li><li>tcpdump arp </li></ol><p><strong>2. 过滤:指定协议的端口号</strong></p><p>
使用port参数,用于指定端口号。</p><p>
命令:tcpdump tcp port 80</p><p>
使用portrange参数,用于指定端口范围。</p><p>
命令:tcpdump tcp portrange 1-1024</p><p><strong>3. 过滤:指定源与目标</strong></p><p>
src 表示源。</p><p>
dst 表示目标。</p><p>
命令:</p><p>
tcpdump src port 8080</p><p>
tcpdump dst port 80</p><p><strong>4. 过滤:指定特定主机的消息包</strong></p><p>
使用host指定需要监听的主机。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump host 192.168.1.113 </li></ol><p>
注意:若使用了host参数使用了计算机名或域名。例tcpdump host shi-pc ,则无法再使用-n选项。</p><p><strong>5. 过滤:指定数据包大小</strong></p><p>
使用greater(大于)与less(小于)可以指定数据包大小的范围。</p><p>
「例:只抓取大于1000字节的数据包。」</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump greater 1000 </li></ol><p>
「例:只抓取小于10字节的数据包。」</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump less 10 </li></ol><p><strong>五、 逻辑表达式</strong></p><p>
使用基本逻辑组合拼装出更精细的过滤条件。</p><p><strong>1. 逻辑与</strong></p><p>
逻辑与关系,使用and。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump tcp <span class="op">and</span> host 192.168.1.112 </li><li> </li><li>tcpdump tcp <span class="op">and</span> src 192.168.1.112 <span class="op">and</span> port 8080 </li></ol><p><strong>2. 逻辑或</strong></p><p>
逻辑或关系,使用or。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump host 192.168.1.112 <span class="op">or</span> 192.168.1.113 </li></ol><p><strong>3. 逻辑非</strong></p><p>
逻辑非关系,使用not,也可以使用 ! 。</p><p>
若使用 ! 必须与其后面的字符隔开一个空格。</p><p>
例:当通过ssh协议远程使用tcpdump时,为了避免ssh的数据包的输出,所以一般需要禁止ssh数据包的输出。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump <span class="op">not</span> tcp port 22 </li><li> </li><li>tcpdump ! tcp port 22 </li></ol><p><strong>4. 括号</strong></p><p>
括号需要使用在引号内,或转意使用。否则会报错。</p><p>
例:抓取非22端口,且主机为192.168.1.112 和 192.168.1.113的TCP数据包。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump <span class="op">not</span> tcp port 22 <span class="op">and</span> host 192.168.1.112or192.168.1.113 </li><li> </li><li>tcpdump <span class="string">"not tcp port 22 and host (192.168.1.112 or 192.168.1.113)"</span> </li><li> </li><li>tcpdump <span class="op">not</span> tcp port 22 <span class="op">and</span> host <span class="string">"(192.168.1.112 or 192.168.1.113)"</span> </li></ol><p><strong>六、其他实例</strong></p><p><strong>1. 打印所有进入或离开sundown的数据包</strong></p><ol class="dp-sql list-paddingleft-2"><li>tcpdump host sundown </li></ol><p><strong>2. 截获主机210.27.48.1 和主机210.27.48.2 或210.27.48.3的通信</strong></p><ol class="dp-sql list-paddingleft-2"><li>tcpdump host 210.27.48.1 <span class="op">and</span> (210.27.48.2 <span class="op">or</span> 210.27.48.3 ) </li></ol><p><strong>3. 如果想要获取主机210.27.48.1除了和主机210.27.48.2之外所有主机通信的ip包,使用命令:</strong></p><ol class="dp-sql list-paddingleft-2"><li>tcpdump ip host 210.27.48.1 <span class="op">and</span> ! 210.27.48.2 </li></ol><p><strong>4. 监视所有送到主机hostname的数据包</strong></p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -i eth0 dst host hostname </li></ol><p><strong>5. 获取主机210.27.48.1接收或发出的telnet包</strong></p><p>
23为telnet的端口</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump tcp port 23 <span class="op">and</span> host 210.27.48.1 </li></ol><p><strong>6. 监视本机的udp 123 端口</strong></p><p>
123 为ntp的服务端口</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump udp port 123 </li></ol><p><strong>7. 使用tcpdump抓取HTTP包</strong></p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -XvvennSs 0 -i eth0 tcp=0x4745 <span class="op">or</span> tcp=0x4854 </li></ol><ul class=" list-paddingleft-2"><li>
0x4745 为"GET"前两个字母"GE",</li><li>
0x4854 为"HTTP"前两个字母"HT"。</li></ul><p>
tcpdump 对截获的数据并没有进行彻底解码,数据包内的大部分内容是使用十六进制的形式直接打印输出的。</p><p>
显然这不利于分析网络故障,通常的解决办法是先使用带**-w参数的tcpdump 截获数据并保存到文件中**,然后再使用其他程序(如Wireshark)进行解码分析。当然也应该定义过滤规则,以避免捕获的数据包填满整个硬盘。</p><p><strong>六、查看数据包完整内容</strong></p><p>
tcpdump默认不显示数据包的详细内容。</p><p><strong>方法一:</strong></p><p>
使用-A参数能以ASCII码显示数据包。</p><p>
例:只抓取1个数据包,并显示其内容。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -c 1 -A </li></ol><p>
<img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/09630c2292fa1c3c2d423505abb6cd71.jpg" width="auto"/></p><p><strong>方法二:</strong></p><p>
使用-X参数能16进制数与ASCII码共同显示数据包。</p><p>
例:只抓取1个数据包,并显示其内容。</p><p>
命令:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump -c 1 -X </li></ol><p>
<img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" src="https://zhuji.jb51.net/uploads/img/202305/c41106479a5d4d8b2f55f94dd8871caf.jpg"/></p><p><strong>七、tcpdump 与wiresharkWireshark</strong></p><p>
(以前是ethereal)是Windows下非常简单易用的抓包工具,现在也有Linux版本。</p><p>
通过Tcpdump抓取的数据包分析比较麻烦,要想很方便的分析数据包, 我们可以用Tcpdump + Wireshark 的完美组合实现:在 Linux 里抓包,然后在Windows 里分析包。</p><p>
保存数据包为wireshark能识别的文件:</p><ol class="dp-sql list-paddingleft-2"><li>tcpdump tcp -i eth1 -t -s 0 -c 100 <span class="op">and</span> dst port ! 22 <span class="op">and</span> src net 192.168.1.0/24 -w ./target.cap </li></ol><table><thead><tr class="firstRow"><th>
参数</th><th>
含义</th></tr></thead><tbody><tr><td>
tcp</td><td>
ip icmp arp rarp 和 tcp、udp、icmp这些选项等都要放到第一个参数的位置,用来过滤数据报的类型</td></tr><tr><td>
-i eth1</td><td>
只抓经过接口eth1的包</td></tr><tr><td>
-t</td><td>
不显示时间戳</td></tr><tr><td>
-s 0</td><td>
抓取数据包时默认抓取长度为68字节。加上-S 0 后可以抓到完整的数据包</td></tr><tr><td>
-c 100</td><td>
只抓取100个数据包</td></tr><tr><td>
dst port ! 22</td><td>
不抓取目标端口是22的数据包</td></tr><tr><td>
src net 192.168.1.0/24</td><td>
数据包的源网络地址为192.168.1.0/24</td></tr><tr><td>
-w ./target.cap</td><td>
保存成cap文件,方便用ethereal(即wireshark)分析</td></tr></tbody></table><p><br/><img style="max-width:100%!important;height:auto!important;" title="网络/命令行抓包工具Tcpdump详解" alt="网络/命令行抓包工具Tcpdump详解" border="0" height="auto" src="https://zhuji.jb51.net/uploads/img/202305/2c7d5874498d34ee6d9abebc54c13870.jpg" width="auto"/></p><p>
原文地址:https://mp.weixin.qq.com/s/n6b4zCM_Dcuuc2ugFSUY_w</p><p>
</p>
頁:
[1]