zoukankan      html  css  js  c++  java
  • ubuntu下tcpdump使用

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

      安装好tcpdump之后,运行tcpdump:

      1. tcpdump -D 获取网络适配器列表,以下是在Ubuntu上获取到的结果:
      root@holmesian-laptop:~# tcpdump -D

    1.eth0
    2.eth1
    3.usbmon1 (USB bus number 1)
    4.eth2
    5.usbmon2 (USB bus number 2)
    6.any (Pseudo-device that captures on all interfaces)
    7.lo



      2. tcpdump -i <需要监控的网络适配器编号>,例如我想监控我的无线网卡eth0,则使用tcpdump -i 1。
      root@holmesian-laptop:~# tcpdump -i 2

    tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
    listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
    20:39:23.081636 ARP, Request who-has 192.168.4.10 tell 192.168.4.253, length 46
    20:39:23.082586 IP botnet-virtual-machine.local.65387 > dec3000.xjtu.edu.cn.domain: 44290+ PTR? 10.4.168.192.in-addr.arpa. (43)
    20:39:23.082965 IP botnet-virtual-machine.local.65387 > ns2.xjtu.edu.cn.domain: 44290+ PTR? 10.4.168.192.in-addr.arpa. (43)
    20:39:23.083228 IP botnet-virtual-machine.local.65387 > dec3000.xjtu.edu.cn.domain: 44290+ PTR? 10.4.168.192.in-addr.arpa. (43)
    20:39:23.083484 IP botnet-virtual-machine.local.65387 > ns2.xjtu.edu.cn.domain: 44290+ PTR? 10.4.168.192.in-addr.arpa. (43)
    20:39:23.083734 IP botnet-virtual-machine.local.65387 > dec3000.xjtu.edu.cn.domain: 44290+ PTR? 10.4.168.192.in-addr.arpa. (43)
    20:39:23.083988 IP dec3000.xjtu.edu.cn.domain > botnet-virtual-machine.local.65387: 44290 NXDomain* 0/1/0 (78)
    20:39:23.084231 IP dec3000.xjtu.edu.cn.domain > botnet-virtual-machine.local.65387: 44290 NXDomain* 0/1/0 (78)


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

      3. 使用无线网卡wlan0监控IP地址为172.16.86.111上443端口的tcp协议
      tcpdump -i 2 host 172.16.86.111 and tcp port 443


      4. 如果想要显示数据包的内容,需要使用-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 表示自动设置长度使其能够显示所有数据。

      5. 捕获的数据太多,不断刷屏,可能需要将数据内容记录到文件里,需要使用-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
      则只能看到最简单的数据传输交互过程,看不到数据包内容,查看时也需要使用相应的参数。

      6.总结
      总结一下,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 ]

  • 相关阅读:
    只能输入正整数 以及常用的正则表达式 (转载)
    SVN cleanup操作反复失败解决办法 (转载)
    关于${pageContext.request.contextPath}的理解 (转载)
    Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds
    一则Oracle EXP导出报错的解决办法(转载)
    js数组的sort排序详解(转载)
    JavaScript arguments对象(转载)
    转 :meta name的含义
    一些小问题
    005.JMS可靠性机制
  • 原文地址:https://www.cnblogs.com/tina-smile/p/3697254.html
Copyright © 2011-2022 走看看