tcpdump抓包(转)

Linux 环境下,通常通过 tcpdump 来进行抓包和分析。它是几乎所有 Linux 发行版本预装的数据包抓取和分析工具。

tcpdump 工具的获取和安装可以参阅相应操作系统的官方文档,本文不再详述。

 

tcpdump 的用法如下:

tcpdump [ -AbdDefhHIJKlLnNOpqStuUvxX# ] [ -B buffer_size ] 
         [ -c count ] 
         [ -C file_size ] [ -G rotate_seconds ] [ -F file ] 
         [ -i interface ] [ -j tstamp_type ] [ -m module ] [ -M secret ] 
         [ --number ] [ -Q in|out|inout ] 
         [ -r file ] [ -V file ] [ -s snaplen ] [ -T type ] [ -w file ] 
         [ -W filecount ] 
         [ -E spi@ipaddr algo:secret,... ] 
         [ -y datalinktype ] [ -z postrotate-command ] [ -Z user ] 
         [ --time-stamp-precision=tstamp_precision ] 
         [ --immediate-mode ] [ --version ] 
         [ expression ]

 

常见参数说明(区分大小写):

  • -s 设置数据包抓取长度。如果置为0,则表示自动选择合适的长度来抓取数据包。
  • -w将抓包结果导出到文件,而不是在控制台进行分析和打印输出。
  • -i 指定需要监听的接口(网卡)。
  • -vvv 输出详细的数据交互信息。
  • expression 一个正则表达式,用作过滤报文的条件。主要包含如下几类:
    • 关于类型的关键字:包括 host(主机),net(网络),port(端口)。
    • 确定传输方向的关键字:包括 src(源),dst(目标),dst or src(源或目标),dst and src(源和目标)。
    • 使用协议的关键字:包括 icmp,ip,arp,rarp,tcp,udp等类型。

更多参数说明及用法,可以参阅 tcpdump 的 man 帮助。

 

常见用法和示例输出:

  1. 抓取指定网卡指定端口的交互数据
    • 操作指令
      tcpdump -s 0 -i eth0 port 22
    • 示例输出
      tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
      listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes
      20:24:59.414951 IP 172.16.2.226.ssh > 42.120.74.107.43414: Flags [P.], seq 442372:442536, ack 53, win 141, length 164
      20:24:59.415002 IP 172.16.2.226.ssh > 42.120.74.107.43414: Flags [P.], seq 442536:442700, ack 53, win 141, length 164
      20:24:59.415052 IP 172.16.2.226.ssh > 42.120.74.107.43414: Flags [P.], seq 442700:442864, ack 53, win 141, length 164
      20:24:59.415103 IP 172.16.2.226.ssh > 42.120.74.107.43414: Flags [P.], seq 442864:443028, ack 53, win 141, length 164
  2.  抓取指定网卡发送给指定 IP 上指定端口的交互数据,并在控制台输出详细交互信息 
    • 操作指令
      tcpdump -s 0 -i eth1 -vvv port 22
    • tcpdump -s 0 -i enp1s0 -vvv dst 172.16.2.226 and port 22
    • 示例输出
      tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
      20:24:20.991006 IP (tos 0x10, ttl 64, id 22747, offset 0, flags [DF], proto TCP (6), length 316)
          172.16.2.226.ssh > 42.120.74.107.43414: Flags [P.], cksum 0x2504 (incorrect -> 0x270d), seq 133624:133900, ack 1, win 141, length 276
      20:24:20.991033 IP (tos 0x0, ttl 53, id 2348, offset 0, flags [DF], proto TCP (6), length 92)
          42.120.74.107.43414 > 172.16.2.226.ssh: Flags [P.], cksum 0x4759 (correct), seq 1:53, ack 129036, win 15472, length 52
      20:24:20.991130 IP (tos 0x10, ttl 64, id 22748, offset 0, flags [DF], proto TCP (6), length 540)
          172.16.2.226.ssh > 42.120.74.107.43414: Flags [P.], cksum 0x25e4 (incorrect -> 0x5e78), seq 133900:134400, ack 53, win 141, length 500
      20:24:20.991162 IP (tos 0x0, ttl 53, id 2349, offset 0, flags [DF], proto TCP (6), length 40)
          42.120.74.107.43414 > 172.16.2.226.ssh: Flags [.], cksum 0xf39e (correct), seq 53, ack 129812, win 15278, length 0
  3. 抓取发送至指定 IP 的 ping 交互数据,并在控制台输出详细交互信息 
    • 操作指令
      tcpdump -s 0 -i eth1 -vvv dst 223.5.5.5 and icmp
    • 示例输出
      tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 65535 bytes
      20:26:00.368958 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84)
          172.16.2.226 > public1.alidns.com: ICMP echo request, id 55097, seq 341, length 64
      20:26:01.369996 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84)
          172.16.2.226 > public1.alidns.com: ICMP echo request, id 55097, seq 342, length 64
      20:26:02.371058 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84)
          172.16.2.226 > public1.alidns.com: ICMP echo request, id 55097, seq 343, length 64
      20:26:03.372181 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto ICMP (1), length 84)
          172.16.2.226 > public1.alidns.com: ICMP echo request, id 55097, seq 344, length 64
  4. 抓取系统内所有接口数据并保存到指定文件
    • 操作指令
      tcpdump -i any -s 0 -w test.cap
    • 示例输出
      tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 65535 bytes
posted @ 2018-05-17 16:03  行知散人  阅读(234)  评论(0编辑  收藏  举报