Ubuntu的抓包工具tcpdump

注意:本文最后更新于 2623 天前,有关的内容可能已经发生变化,请参考使用。

Ubuntu默认是安装好了tcpdump工具的,如果没有安装的话使用sudo apt-get install tcpdump即可安装。如果遇到tcpdump: no suitable device found的问题,检查一下是不是在用root权限运行tcpdump,tcpdump只能在root权限下工作

  • tcpdump -D

获取网络适配器列表,以下是在Ubuntu上获取到的结果:

    root@holmesian-laptop:~# tcpdump -D  
    1.eth0  
    2.wlan0  
    3.usbmon1 (USB bus number 1)  
    4.usbmon2 (USB bus number 2)  
    5.usbmon3 (USB bus number 3)  
    6.usbmon4 (USB bus number 4)  
    7.usbmon5 (USB bus number 5)  
    8.any (Pseudo-device that captures on all interfaces)  
    9.lo  
  • tcpdump -i

<需要监控的网络适配器编号>**,例如我想监控我的无线网卡wlan0,则使用tcpdump -i 2

    root@holmesian-laptop:~# tcpdump -i 2  
    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode  
    listening on wlan0, link-type EN10MB (Ethernet), capture size 96 bytes  
    21:24:14.578430 00:24:f9:05:78:00 (oui Unknown) Unknown SSAP 0x78 >
    00:1f:3a:18:fa:06 (oui Unknown) Unknown DSAP 0xd8 Information, send seq 0, rcv
    seq 16, Flags [Command], length 70  
    21:24:14.578447 00:24:f9:05:78:00 (oui Unknown) Unknown SSAP 0x78 >
    00:1f:3a:18:fa:06 (oui Unknown) Unknown DSAP 0xd8 Information, send seq 0, rcv
    seq 16, Flags [Command], length 223  
    21:24:14.995603 00:24:f9:05:78:00 (oui Unknown) Unknown SSAP 0x20 >
    78:dd:08:d1:b2:ca (oui Unknown) Unknown DSAP 0x76 Information, send seq 0, rcv
    seq 16, Flags [Command], length 70  
    21:24:15.019811 00:24:f9:05:78:00 (oui Unknown) Unknown SSAP 0xa6 >
    2c:81:58:ec:9c:54 (oui Unknown) Unknown DSAP 0x0a Information, send seq 0, rcv
    seq 16, Flags [Command], length 72  

如果不使用-i来定义监控适配器的话,默认使用列表中的第一个;

  • 实例

使用无线网卡wlan0监控IP地址为172.16.86.111上443端口的tcp协议:

    tcpdump -i 2 host 172.16.86.111 and tcp port 443  
  • 数据包内容

想要显示数据包的内容,需要使用-X参数,例如我想要显示捕获的https数据包http header的内容:

    tcpdump -X -i 2 host 172.16.86.111 and tcp port 443  
    
    21:27:53.662741 IP holmesian-laptop.local.44239 > 172.16.86.111.https: Flags
    [S], seq 24296623, win 5840, options [mss 1460,sackOK,TS val 153804 ecr
    0,nop,wscale 6], length 0  
    0x0000: 4500 003c e463 4000 4006 514a ac10 567e E..<.c@.@.QJ..V~  
    0x0010: ac10 566f accf 01bb 0172 bcaf 0000 0000 ..Vo.....r......  
    0x0020: a002 16d0 66a8 0000 0204 05b4 0402 080a ....f...........  
    0x0030: 0002 58cc 0000 0000 0103 0306 ..X.........  
    21:27:56.660488 IP holmesian-laptop.local.44239 > 172.16.86.111.https: Flags
    [S], seq 24296623, win 5840, options [mss 1460,sackOK,TS val 154554 ecr
    0,nop,wscale 6], length 0  
    0x0000: 4500 003c e464 4000 4006 5149 ac10 567e E..<.d@.@.QI..V~  
    0x0010: ac10 566f accf 01bb 0172 bcaf 0000 0000 ..Vo.....r......  
    0x0020: a002 16d0 63ba 0000 0204 05b4 0402 080a ....c...........  
    0x0030: 0002 5bba 0000 0000 0103 0306 ..[.........  
    .c  

可以看到该结果只显示了https头的一部分,没有显示全,是因为tcpdump默认将显示的数据长度截断了,可以使用-s后面加数据长度,来设置数据显示长度:

    tcpdump -X -s 0 -i 2 host 172.16.86.111 and tcp port 443  

以上的例子中,-s 0 表示自动设置长度使其能够显示所有数据。

  • 更多设置

捕获的数据太多,不断刷屏,可能需要将数据内容记录到文件里,需要使用-w参数:

    tcpdump -X -s 0 -w aaa host 192.9.200.59 and tcp port 8000  

则将之前显示在屏幕中的内容,写入tcpdump可执行文件同级目录下的aaa文件中。

文件查看方式如下,需要使用-r参数:

    tcpdump -X -s 0 -i 2 -r holmesian host 172.16.86.111 and tcp port 443  

如果这样写:

    tcpdump -r holmesian  

则只能看到最简单的数据传输交互过程,看不到数据包内容,查看时也需要使用相应的参数。

  • 总结

tcpdump的参数分两个部分,选项(Options)和表达式(expression):

    root@holmesian-laptop:~# tcpdump -h  
    tcpdump version 4.0.0  
    libpcap version 1.0.0  
    Usage: tcpdump [-aAdDefIKlLnNOpqRStuUvxX] [ -B size ] [ -c count ]  
    [ -C file_size ] [ -E algo:secret ] [ -F file ] [ -G seconds ]  
    [ -i interface ] [ -M secret ] [ -r file ]  
    [ -s snaplen ] [ -T type ] [ -w file ] [ -W filecount ]  
    [ -y datalinktype ] [ -z command ] [ -Z user ]  
    [ expression ]  

「倘若有所帮助,不妨酌情赞赏!」

Holmesian

感谢您的支持!

使用微信扫描二维码完成支付


相关文章

发表新评论