跳转至

tcpdump


2013-04-18 by dongnan

功能

tcpdump 可以将网络中传送的数据包头完全截获下来提供分析的工具。 它支持针对网络层、协议、主机、网络或端口的过滤,并提供andornot等逻辑语句来帮助你去掉无用的信息,

tcpdump 对截获的数据并没有进行彻底解码,数据包内的大部分内容是使用十六进制的形式直接打印输出的。

举个栗子

tcpdump 抓取FTP交互通信数据。

下面结果每一行中间都有携带的标志:

  • S=SYN:发起连接标志
  • P=PUSH:传送数据标志
  • F=FIN:关闭连接标志
  • ack:表示确认包
  • RST= RESET:异常关闭连接
  • .:表示没有任何标志

FTP被动模式命令端口

tcpdump -i em1 tcp port 21 -vvvn

tcpdump: listening on em1, link-type EN10MB (Ethernet), capture size 262144 bytes
16:28:55.029712 IP (tos 0x0, ttl 53, id 59531, offset 0, flags [DF], proto TCP (6), length 60)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [S], cksum 0xc260 (correct), seq 2279740035, win 29200, options [mss 1460,sackOK,TS val 949352968 ecr 0,nop,wscale 7], length 0
#...省略
16:28:55.037574 IP (tos 0x0, ttl 64, id 55795, offset 0, flags [DF], proto TCP (6), length 72)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [P.], cksum 0x6ea0 (incorrect -> 0x9c90), seq 1:21, ack 1, win 227, options [nop,nop,TS val 4094336663 ecr 949352969], length 20: FTP, length: 20
    220 (vsFTPd 3.0.2)

登录服务器(明文):

16:28:57.223346 IP (tos 0x0, ttl 53, id 59534, offset 0, flags [DF], proto TCP (6), length 66)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [P.], cksum 0xd2f4 (correct), seq 1:15, ack 21, win 229, options [nop,nop,TS val 949353517 ecr 4094336663], length 14: FTP, length: 14
    USER support
#...省略
16:28:57.223590 IP (tos 0x0, ttl 64, id 55797, offset 0, flags [DF], proto TCP (6), length 86)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [P.], cksum 0x6eae (incorrect -> 0xa3a0), seq 21:55, ack 15, win 227, options [nop,nop,TS val 4094338849 ecr 949353517], length 34: FTP, length: 34
    331 Please specify the password.
#...省略
16:28:59.639400 IP (tos 0x0, ttl 53, id 59536, offset 0, flags [DF], proto TCP (6), length 66)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [P.], cksum 0x1e9a (correct), seq 15:29, ack 55, win 229, options [nop,nop,TS val 949354121 ecr 4094338849], length 14: FTP, length: 14
    PASS hw8LL50
16:28:59.678523 IP (tos 0x0, ttl 64, id 55798, offset 0, flags [DF], proto TCP (6), length 75)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [P.], cksum 0x6ea3 (incorrect -> 0xfd30), seq 55:78, ack 29, win 227, options [nop,nop,TS val 4094341304 ecr 949354121], length 23: FTP, length: 23
    230 Login successful.
#...省略
16:28:59.687279 IP (tos 0x0, ttl 53, id 59538, offset 0, flags [DF], proto TCP (6), length 58)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [P.], cksum 0x2ae0 (correct), seq 29:35, ack 78, win 229, options [nop,nop,TS val 949354131 ecr 4094341304], length 6: FTP, length: 6
    SYST
16:28:59.687488 IP (tos 0x0, ttl 64, id 55799, offset 0, flags [DF], proto TCP (6), length 71)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [P.], cksum 0x6e9f (incorrect -> 0x772d), seq 78:97, ack 35, win 227, options [nop,nop,TS val 4094341313 ecr 949354131], length 19: FTP, length: 19
    215 UNIX Type: L8

进入被动模式:

16:29:42.749849 IP (tos 0x0, ttl 53, id 59540, offset 0, flags [DF], proto TCP (6), length 58)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [P.], cksum 0x03c5 (correct), seq 35:41, ack 97, win 229, options [nop,nop,TS val 949364898 ecr 4094341313], length 6: FTP, length: 6
    PASV
16:29:42.750336 IP (tos 0x0, ttl 64, id 55800, offset 0, flags [DF], proto TCP (6), length 104)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [P.], cksum 0x6ec0 (incorrect -> 0x6349), seq 97:149, ack 41, win 227, options [nop,nop,TS val 4094384375 ecr 949364898], length 52: FTP, length: 52
    227 Entering Passive Mode (1,1,1,1,39,50).
16:29:42.753940 IP (tos 0x0, ttl 53, id 59541, offset 0, flags [DF], proto TCP (6), length 52)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [.], cksum 0x0c03 (correct), seq 41, ack 149, win 229, options [nop,nop,TS val 949364899 ecr 4094384375], length 0

使用dir命令:

16:29:42.758512 IP (tos 0x0, ttl 53, id 59542, offset 0, flags [DF], proto TCP (6), length 58)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [P.], cksum 0x5f4c (correct), seq 41:47, ack 149, win 229, options [nop,nop,TS val 949364900 ecr 4094384375], length 6: FTP, length: 6
    LIST
16:29:42.759745 IP (tos 0x0, ttl 64, id 55801, offset 0, flags [DF], proto TCP (6), length 91)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [P.], cksum 0x6eb3 (incorrect -> 0xf6a6), seq 149:188, ack 47, win 227, options [nop,nop,TS val 4094384385 ecr 949364900], length 39: FTP, length: 39
    150 Here comes the directory listing.
16:29:42.764371 IP (tos 0x0, ttl 64, id 55802, offset 0, flags [DF], proto TCP (6), length 76)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [P.], cksum 0x6ea4 (incorrect -> 0x462f), seq 188:212, ack 47, win 227, options [nop,nop,TS val 4094384389 ecr 949364900], length 24: FTP, length: 24
    226 Directory send OK.
16:29:42.767904 IP (tos 0x0, ttl 53, id 59543, offset 0, flags [DF], proto TCP (6), length 52)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [.], cksum 0x0bb0 (correct), seq 47, ack 212, win 229, options [nop,nop,TS val 949364903 ecr 4094384385], length 0

使用quit命令:

16:32:18.937611 IP (tos 0x0, ttl 53, id 55806, offset 0, flags [DF], proto TCP (6), length 52)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [P.], cksum 0x619a (correct), seq 2279740137:2279740143, ack 2458536190, win 229, options [nop,nop,TS val 949404148 ecr 4094476548], length 6: FTP, length: 6
    QUIT
16:32:19.747635 IP (tos 0x0, ttl 64, id 55809, offset 0, flags [DF], proto TCP (6), length 66)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [P.], cksum 0x6e9a (incorrect -> 0x1d30), seq 1:15, ack 6, win 227, options [nop,nop,TS val 4094541373 ecr 949404148], length 14: FTP, length: 14
    221 Goodbye.
16:32:19.747659 IP (tos 0x0, ttl 64, id 55810, offset 0, flags [DF], proto TCP (6), length 52)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [F.], cksum 0x6e8c (incorrect -> 0x0c0f), seq 15, ack 6, win 227, options [nop,nop,TS val 4094541373 ecr 949404148], length 0
16:32:19.752028 IP (tos 0x0, ttl 53, id 59554, offset 0, flags [DF], proto TCP (6), length 52)
    1.1.1.1.55312 > 2.2.2.2.ftp: Flags [F.], cksum 0x0c0b (correct), seq 6, ack 16, win 229, options [nop,nop,TS val 949404149 ecr 4094541373], length 0
16:32:19.752138 IP (tos 0x0, ttl 64, id 55811, offset 0, flags [DF], proto TCP (6), length 52)
    2.2.2.2.ftp > 1.1.1.1.55312: Flags [.], cksum 0x6e8c (incorrect -> 0x0c09), seq 16, ack 7, win 227, options [nop,nop,TS val 4094541377 ecr 949404149], length 0

FTP被动模式数据端口

tcpdump -i em1 tcp portrange 10030-10035 -vvvn

tcpdump: listening on em1, link-type EN10MB (Ethernet), capture size 262144 bytes
#...省略
16:41:53.060798 IP (tos 0x0, ttl 53, id 9655, offset 0, flags [DF], proto TCP (6), length 60)
    1.1.1.1.59744 > 2.2.2.2.10033: Flags [S], cksum 0x8329 (correct), seq 3573404771, win 29200, options [mss 1460,sackOK,TS val 949547476 ecr 0,nop,wscale 7], length 0
16:41:53.060912 IP (tos 0x0, ttl 64, id 0, offset 0, flags [DF], proto TCP (6), length 60)
    2.2.2.2.10033 > 1.1.1.1.59744: Flags [S.], cksum 0x6e94 (incorrect -> 0x18cd), seq 3572180090, ack 3573404772, win 28960, options [mss 1460,sackOK,TS val 4095114686 ecr 949547476,nop,wscale 7], length 0
16:41:53.064358 IP (tos 0x0, ttl 53, id 9656, offset 0, flags [DF], proto TCP (6), length 52)
    1.1.1.1.59744 > 2.2.2.2.10033: Flags [.], cksum 0xb7d3 (correct), seq 1, ack 1, win 229, options [nop,nop,TS val 949547477 ecr 4095114686], length 0
16:41:53.064958 IP (tos 0x8, ttl 64, id 22969, offset 0, flags [DF], proto TCP (6), length 190)
    2.2.2.2.10033 > 1.1.1.1.59744: Flags [P.], cksum 0x6f16 (incorrect -> 0x974b), seq 1:139, ack 1, win 227, options [nop,nop,TS val 4095114690 ecr 949547477], length 138
16:41:53.064990 IP (tos 0x8, ttl 64, id 22970, offset 0, flags [DF], proto TCP (6), length 52)
    2.2.2.2.10033 > 1.1.1.1.59744: Flags [F.], cksum 0x6e8c (incorrect -> 0xb746), seq 139, ack 1, win 227, options [nop,nop,TS val 4095114690 ecr 949547477], length 0
#...省略

命令参数

tcpdump -h
tcpdump version 4.2.1
libpcap version 1.1.1
Usage: tcpdump [-aAbdDefhHIKlLnNOpqRStuUvxX] [ -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 ]

 tcpdump的选项介绍
 -a: 将网络地址和广播地址转变成名字。
 -d: 将匹配信息包的代码以人们能够理解的汇编格式给出。
 -dd: 将匹配信息包的代码以c语言程序段的格式给出。
 -ddd: 将匹配信息包的代码以十进制的形式给出。
 -e: 在输出行打印出数据链路层的头部信息。
 -f: 将外部的Internet地址以数字的形式打印出来。
 -l: 使标准输出变为缓冲行形式。
 -n: 不把网络地址转换成名字。
 -t: 在输出的每一行不打印时间戳。
 -v: 输出一个稍微详细的信息,例如在ip包中可以包括ttl和服务类型的信息。
 -vv: 输出详细的报文信息。
 -c: 在收到指定的包的数目后,tcpdump就会停止。
 -F: 从指定的文件中读取表达式,忽略其它的表达式。
 -i: 指定监听的网络接口。
 -r: 从指定的文件中读取包(这些包一般通过-w选项产生)。
 -w: 直接将包写入文件中,并不分析和打印出来。
 -T: 将监听到的包直接解释为指定的类型的报文,常见的类型有`rpc`和`snmp`协议。

表达式

表达式是一个正则表达式,tcpdump利用它作为过滤报文的条件,如果一个报文满足表达式的条件, 则这个报文将会被捕获,如果没有给出任何条件,则网络上所有的信息包将会被截获。

在表达式中几种类型的关键字:

  • 类型的关键字,主要包括host,net,port,如果没有指定类型,缺省的类型是host
  • 确定传输方向的关键字,主要包括src , dst ,dst or src, dst and src,这些关键字指明了传输的方向,如果没有指明方向关键字,则缺省是src or dst关键字。
  • 协议的关键字,主要包括fddi,ip ,arp,rarp,tcp,udp等类型,如果没有指定任何协议,则tcpdump将会监听所有协议的信息包。
  • 其它重要的关键字,gateway, broadcast,less,greater
  • 还有三种逻辑运算,取非运算是 not!,与运算是and&&,或运算是or||

举个栗子

获取所有192.168.57.71的主机收到的和发出的所有的数据包:

tcpdump host 192.168.57.71

获取主机192.168.57.71和主机192.168.57.72192.168.57.73 的通信:

tcpdump host 192.168.57.71 and 192.168.57.72 or 192.168.57.73

获取主机192.168.57.71除了和主机192.168.57.72之外所有主机通信的ip包:

tcpdump ip host 192.168.57.71 and ! 192.168.57.72

获取主机192.168.57.71接收或发出的telnet数据包:

tcpdump tcp port 23 and host 192.168.57.71

获取发往主机的所有icmp包:

tcpdump icmp -vvn -i eth0

获取主机与192.168.4.35的所有icmp 包:

tcpdump -nnv icmp and host 192.168.4.35

指定一个端口范围:

tcpdump -i em1 tcp portrange 10030-10035 -vvvn

参考

http://baike.baidu.com/view/76504.htm

回到页面顶部